func (r inProcessRoundTripper) RoundTrip(req *http.Request) (response *http.Response, err error) { var match mux.RouteMatch switch handler, _ := r.mux.Handler(req); handler.(type) { default: err = fmt.Errorf("handler for request '%v' was not of type %v", req.RequestURI, reflect.TypeOf((*mux.Router)(nil))) return case *mux.Router: if !handler.(*mux.Router).Match(req, &match) { err = fmt.Errorf("no match found for path '%v'", req.URL) return } } responseWriter := mockHttp.TestResponseWriter{} match.Handler.(http.HandlerFunc)(&responseWriter, req) // TODO check that it's a HandlerFunc to avoid a panic var httpBuffer bytes.Buffer fmt.Fprintf(&httpBuffer, "HTTP/1.1 %v\r\n", statusLine(responseWriter.WrittenHeaderInt)) responseWriter.Header().Write(&httpBuffer) fmt.Fprintf(&httpBuffer, "\r\n%v", responseWriter.Output) response, err = http.ReadResponse(bufio.NewReader(&httpBuffer), req) return }
func TestRedirectTo(t *testing.T) { r := new(testhttp.TestResponseWriter) RedirectTo(r, "http://www.stretchr.com", "test") assert.Equal(t, "http://www.stretchr.com/test", r.Header().Get("Location")) assert.Equal(t, 0, r.StatusCode) }
func TestRedirectPerm(t *testing.T) { r := new(testhttp.TestResponseWriter) RedirectPerm(r, "http://www.stretchr.com", "test") assert.Equal(t, "http://www.stretchr.com/test", r.Header().Get("Location")) assert.Equal(t, http.StatusMovedPermanently, r.WrittenHeaderInt) }
func TestRedirectTemp(t *testing.T) { r := new(testhttp.TestResponseWriter) RedirectTemp(r, "http://www.stretchr.com", "test") assert.Equal(t, "http://www.stretchr.com/test", r.Header().Get("Location")) assert.Equal(t, http.StatusTemporaryRedirect, r.WrittenHeaderInt) }
func TestSetHttpResponseWriter(t *testing.T) { responseWriter := new(http_test.TestResponseWriter) testRequest, _ := http.NewRequest("GET", "http://goweb.org/people/123", nil) codecService := codecsservices.NewWebCodecService() c := NewWebContext(responseWriter, testRequest, codecService) responseWriter2 := new(http_test.TestResponseWriter) responseWriter2.Header().Set("Something", "true") testRequest2, _ := http.NewRequest("PUT", "http://goweb.org/people/123", nil) c.SetHttpRequest(testRequest2) c.SetHttpResponseWriter(responseWriter2) req := c.HttpRequest() res := c.HttpResponseWriter() assert.Equal(t, &testRequest2, &req) assert.Equal(t, responseWriter2, res) }