*http.Request) { w.Write([]byte("sample")) w.WriteHeader(http.StatusTeapot) } func Test(t *testing.T) { req := httptest.NewRequest(http.MethodGet, "/", nil) res := httptest.NewRecorder() h := &MyHandler{} h.Sample(res, req) if res.Code != http.StatusTeapot { t.Errorf("status code got %d, should be %d", res.Code, http.StatusTeapot) } } === RUN Test main_test.go:40: status code got 200, should be 418 --- FAIL: Test (0.00s)
*http.Request) { w.Write([]byte("sample")) w.WriteHeader(http.StatusTeapot) } func Test(t *testing.T) { req := httptest.NewRequest(http.MethodGet, "/", nil) res := httptest.NewRecorder() h := &MyHandler{} h.Sample(res, req) if res.Code != http.StatusTeapot { t.Errorf("status code got %d, should be %d", res.Code, http.StatusTeapot) } } === RUN Test main_test.go:40: status code got 200, should be 418 --- FAIL: Test (0.00s)
in buf is written to // rw.Body, if not nil. func (rw *ResponseRecorder) Write(buf []byte) (int, error) { rw.writeHeader(buf, "") if rw.Body != nil { rw.Body.Write(buf) } return len(buf), nil }