| 100 | } |
| 101 | |
| 102 | func TestHTTPClientBufferedStream(t *testing.T) { |
| 103 | // bodysize has a size big enought to make the resopnse.Body not an instant read |
| 104 | // so if the response is cancelled it wount be all readed and the test would fail |
| 105 | // The 6000 has not a particular meaning, it big enough to fulfill the usecase. |
| 106 | const bodysize = 6000 |
| 107 | var ( |
| 108 | testbody = string(make([]byte, bodysize)) |
| 109 | encode = func(context.Context, *http.Request, interface{}) error { return nil } |
| 110 | decode = func(_ context.Context, r *http.Response) (interface{}, error) { |
| 111 | return TestResponse{r.Body, ""}, nil |
| 112 | } |
| 113 | ) |
| 114 | |
| 115 | server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 116 | w.WriteHeader(http.StatusOK) |
| 117 | w.Write([]byte(testbody)) |
| 118 | })) |
| 119 | |
| 120 | client := httptransport.NewClient( |
| 121 | "GET", |
| 122 | mustParse(server.URL), |
| 123 | encode, |
| 124 | decode, |
| 125 | httptransport.BufferedStream(true), |
| 126 | ) |
| 127 | |
| 128 | res, err := client.Endpoint()(context.Background(), struct{}{}) |
| 129 | if err != nil { |
| 130 | t.Fatal(err) |
| 131 | } |
| 132 | |
| 133 | // Check that the response was successfully decoded |
| 134 | response, ok := res.(TestResponse) |
| 135 | if !ok { |
| 136 | t.Fatal("response should be TestResponse") |
| 137 | } |
| 138 | defer response.Body.Close() |
| 139 | // Faking work |
| 140 | time.Sleep(time.Second * 1) |
| 141 | |
| 142 | // Check that response body was NOT closed |
| 143 | b := make([]byte, len(testbody)) |
| 144 | _, err = response.Body.Read(b) |
| 145 | if want, have := io.EOF, err; have != want { |
| 146 | t.Fatalf("want %q, have %q", want, have) |
| 147 | } |
| 148 | if want, have := testbody, string(b); want != have { |
| 149 | t.Errorf("want %q, have %q", want, have) |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | func TestClientFinalizer(t *testing.T) { |
| 154 | var ( |