func (s *TraceSuite) TestTraceSimple(c *C) { handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { w.Header().Set("Content-Length", "5") w.Write([]byte("hello")) }) buf := &bytes.Buffer{} l := utils.NewFileLogger(buf, utils.INFO) trace := &bytes.Buffer{} t, err := New(handler, trace, Logger(l)) c.Assert(err, IsNil) srv := httptest.NewServer(t) defer srv.Close() re, _, err := testutils.MakeRequest(srv.URL+"/hello", testutils.Method("POST"), testutils.Body("123456")) c.Assert(err, IsNil) c.Assert(re.StatusCode, Equals, http.StatusOK) var r *Record c.Assert(json.Unmarshal(trace.Bytes(), &r), IsNil) c.Assert(r.Request.Method, Equals, "POST") c.Assert(r.Request.URL, Equals, "/hello") c.Assert(r.Response.Code, Equals, http.StatusOK) c.Assert(r.Request.BodyBytes, Equals, int64(6)) c.Assert(r.Response.Roundtrip, Not(Equals), float64(0)) c.Assert(r.Response.BodyBytes, Equals, int64(5)) }
func (s *STSuite) TestRequestLimitReached(c *C) { srv := testutils.NewHandler(func(w http.ResponseWriter, req *http.Request) { w.Write([]byte("hello")) }) defer srv.Close() // forwarder will proxy the request to whatever destination fwd, err := forward.New() c.Assert(err, IsNil) // this is our redirect to server rdr := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { req.URL = testutils.ParseURI(srv.URL) fwd.ServeHTTP(w, req) }) // stream handler will forward requests to redirect st, err := New(rdr, Logger(utils.NewFileLogger(os.Stdout, utils.INFO)), MaxRequestBodyBytes(4)) c.Assert(err, IsNil) proxy := httptest.NewServer(st) defer proxy.Close() re, _, err := testutils.Get(proxy.URL, testutils.Body("this request is too long")) c.Assert(err, IsNil) c.Assert(re.StatusCode, Equals, http.StatusRequestEntityTooLarge) }
func (s *ServerSuite) TestFrontendOptionsCRUD(c *C) { e := testutils.NewResponder("Hi, I'm endpoint 1") defer e.Close() c.Assert(s.mux.Start(), IsNil) b := MakeBatch(Batch{ Addr: "localhost:31000", Route: `Path("/")`, URL: e.URL, }) c.Assert(s.mux.UpsertServer(b.BK, b.S), IsNil) c.Assert(s.mux.UpsertFrontend(b.F), IsNil) c.Assert(s.mux.UpsertListener(b.L), IsNil) body := "Hello, this request is longer than 8 bytes" response, bodyBytes, err := testutils.MakeRequest(MakeURL(b.L, "/"), testutils.Body(body)) c.Assert(err, IsNil) c.Assert(string(bodyBytes), Equals, "Hi, I'm endpoint 1") c.Assert(response.StatusCode, Equals, http.StatusOK) settings := engine.HTTPFrontendSettings{ Limits: engine.HTTPFrontendLimits{ MaxBodyBytes: 8, }, FailoverPredicate: "IsNetworkError()", } b.F.Settings = settings c.Assert(s.mux.UpsertFrontend(b.F), IsNil) response, _, err = testutils.MakeRequest(MakeURL(b.L, "/"), testutils.Body(body)) c.Assert(err, IsNil) c.Assert(response.StatusCode, Equals, http.StatusRequestEntityTooLarge) }
func (s *RTSuite) TestRetryOnError(c *C) { srv := testutils.NewHandler(func(w http.ResponseWriter, req *http.Request) { w.Write([]byte("hello")) }) defer srv.Close() lb, rt := new(c, `IsNetworkError() && Attempts() <= 2`) proxy := httptest.NewServer(rt) defer proxy.Close() lb.UpsertServer(testutils.ParseURI("http://localhost:64321")) lb.UpsertServer(testutils.ParseURI(srv.URL)) re, body, err := testutils.Get(proxy.URL, testutils.Body("some request parameters")) c.Assert(err, IsNil) c.Assert(re.StatusCode, Equals, http.StatusOK) c.Assert(string(body), Equals, "hello") }
func (s *VESuite) TestFrontendUpdateLimits(c *C) { var headers http.Header server := testutils.NewHandler(func(w http.ResponseWriter, r *http.Request) { headers = r.Header w.Write([]byte("Hello, I'm totally fine")) }) defer server.Close() b, srv, url := "bk1", "srv1", server.URL _, err := s.client.Set(s.path("backends", b, "backend"), `{"Type": "http"}`, 0) c.Assert(err, IsNil) _, err = s.client.Set(s.path("backends", b, "servers", srv), fmt.Sprintf(`{"URL": "%s"}`, url), 0) c.Assert(err, IsNil) // Add frontend fId := "fr1" _, err = s.client.Set(s.path("frontends", fId, "frontend"), `{"Type": "http", "BackendId": "bk1", "Route": "Path(\"/path\")"}`, 0) c.Assert(err, IsNil) time.Sleep(time.Second) response, _, err := testutils.Get(fmt.Sprintf("%s%s", s.serviceUrl, "/path")) c.Assert(err, IsNil) c.Assert(response.StatusCode, Equals, http.StatusOK) c.Assert(response.Header.Get("X-Forwarded-For"), Not(Equals), "hello") _, err = s.client.Set( s.path("frontends", fId, "frontend"), `{"Type": "http", "BackendId": "bk1", "Route": "Path(\"/path\")", "Settings": {"Limits": {"MaxMemBodyBytes":2, "MaxBodyBytes":4}}}`, 0) c.Assert(err, IsNil) time.Sleep(time.Second) response, _, err = testutils.Get(fmt.Sprintf("%s%s", s.serviceUrl, "/path"), testutils.Body("This is longer than allowed 4 bytes")) c.Assert(err, IsNil) c.Assert(response.StatusCode, Equals, http.StatusRequestEntityTooLarge) }