Пример #1
0
func testHandler(t *testing.T, h http.Handler, path, expRespBody string, expRespCode int) {
	req, err := http.NewRequest("GET", "http://example.com"+path, nil)
	if err != nil {
		t.Fatal(err)
	}

	hooked := false
	buf := new(bytes.Buffer)
	hook := func(frw *FlexibleResponseWriter) {
		hooked = true
		if frw.Code != expRespCode {
			t.Errorf("Expected response code %d for %s but received %d", expRespCode, path, frw.Code)
		}
		frw.BodyWriter = utils.NopCloser(buf)
	}

	resp := NewFlexibleResponseWriter(hook)
	h.ServeHTTP(resp, req)

	if !hooked {
		t.Errorf("The hook function did not execute")
	}
	if buf.String() != expRespBody {
		t.Errorf("Expected response body %s for %s but received %s", expRespBody, path, buf.String())
	}
}
Пример #2
0
func (h *Headers) wrapResponseWriter(w http.ResponseWriter) http.ResponseWriter {
	var newW = httputils.NewFlexibleResponseWriter(func(frw *httputils.FlexibleResponseWriter) {
		httputils.CopyHeaders(frw.Header(), w.Header())
		h.response.rewrite(w.Header())
		frw.BodyWriter = utils.NopCloser(w)
		w.WriteHeader(frw.Code)
	})
	httputils.CopyHeaders(w.Header(), newW.Header())
	return newW
}
Пример #3
0
func toResponseWriteCloser(rw http.ResponseWriter) responseWriteCloser {
	if rwc, ok := rw.(responseWriteCloser); ok {
		return rwc
	}
	return struct {
		http.ResponseWriter
		io.Closer
	}{
		rw,
		utils.NopCloser(nil),
	}
}