Codebase list golang-github-go-kit-kit / 5a8ac03
don't encode json response with http.StatusNoContent 204 status cannot contain a message body: https://tools.ietf.org/html/rfc7231#page-53 Closes #435 Victor Vrantchan 7 years ago
2 changed file(s) with 31 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
150150 code = sc.StatusCode()
151151 }
152152 w.WriteHeader(code)
153 if code == http.StatusNoContent {
154 return nil
155 }
153156 return json.NewEncoder(w).Encode(response)
154157 }
155158
153153 buf, _ := ioutil.ReadAll(resp.Body)
154154 if want, have := `{"foo":"bar"}`, strings.TrimSpace(string(buf)); want != have {
155155 t.Errorf("Body: want %s, have %s", want, have)
156 }
157 }
158
159 type noContentResponse struct{}
160
161 func (e noContentResponse) StatusCode() int { return http.StatusNoContent }
162
163 func TestEncodeNoContent(t *testing.T) {
164 handler := httptransport.NewServer(
165 context.Background(),
166 func(context.Context, interface{}) (interface{}, error) { return noContentResponse{}, nil },
167 func(context.Context, *http.Request) (interface{}, error) { return struct{}{}, nil },
168 httptransport.EncodeJSONResponse,
169 )
170
171 server := httptest.NewServer(handler)
172 defer server.Close()
173
174 resp, err := http.Get(server.URL)
175 if err != nil {
176 t.Fatal(err)
177 }
178 if want, have := http.StatusNoContent, resp.StatusCode; want != have {
179 t.Errorf("StatusCode: want %d, have %d", want, have)
180 }
181 buf, _ := ioutil.ReadAll(resp.Body)
182 if want, have := 0, len(buf); want != have {
183 t.Errorf("Body: want no content, have %d bytes", have)
156184 }
157185 }
158186