Beispiel #1
0
func (s *LocSuite) TestTransportOperations(c *C) {
	backend := NewTestServer(func(w http.ResponseWriter, r *http.Request) {
		time.Sleep(10 * time.Millisecond)
		w.Write([]byte("Hi, I'm endpoint 1"))
	})
	defer backend.Close()

	rr := s.newRoundRobin(backend.URL)

	t := NewTransport(TransportOptions{Timeouts: Timeouts{Read: 1 * time.Millisecond}})

	loc, err := NewLocationWithOptions("loc1", rr, Options{Transport: t})
	c.Assert(err, IsNil)

	proxy, err := vulcan.NewProxy(&ConstRouter{
		Location: loc,
	})
	c.Assert(err, IsNil)

	srv := httptest.NewServer(proxy)
	defer srv.Close()

	response, _, err := MakeRequest(srv.URL, Opts{})
	c.Assert(err, IsNil)
	c.Assert(response.StatusCode, Equals, http.StatusRequestTimeout)

	tn := NewTransport(TransportOptions{Timeouts: Timeouts{Read: 20 * time.Millisecond}})
	loc.SetTransport(tn)

	response, _, err = MakeRequest(srv.URL, Opts{})
	c.Assert(err, IsNil)
	c.Assert(response.StatusCode, Equals, http.StatusOK)
}
Beispiel #2
0
func (s *LocSuite) newProxyWithParams(
	l LoadBalancer,
	readTimeout time.Duration,
	dialTimeout time.Duration,
	maxMemBytes int64,
	maxBodyBytes int64) (*HttpLocation, *httptest.Server) {

	location, err := NewLocationWithOptions("dummy", l, Options{
		TrustForwardHeader: true,
		Limits: Limits{
			MaxMemBodyBytes: maxMemBytes,
			MaxBodyBytes:    maxBodyBytes,
		},
	})
	if err != nil {
		panic(err)
	}
	proxy, err := vulcan.NewProxy(&ConstRouter{
		Location: location,
	})
	if err != nil {
		panic(err)
	}
	return location, httptest.NewServer(proxy)
}
Beispiel #3
0
// Test that X-Forwarded-For and X-Forwarded-Proto are passed through
func (s *LocSuite) TestForwardedProtoHTTPS(c *C) {
	called := false
	server := NewTestServer(func(w http.ResponseWriter, r *http.Request) {
		called = true
		c.Assert(r.Header.Get(headers.XForwardedProto), Equals, "https")
	})
	defer server.Close()

	lb := s.newRoundRobin(server.URL)

	location, err := NewLocation("dummy", lb)
	c.Assert(err, IsNil)

	proxy, err := vulcan.NewProxy(&ConstRouter{
		Location: location,
	})
	c.Assert(err, IsNil)

	srv := httptest.NewUnstartedServer(proxy)
	srv.StartTLS()
	defer srv.Close()

	_, _, err = MakeRequest(srv.URL, Opts{})
	c.Assert(err, IsNil)
	c.Assert(called, Equals, true)
}
Beispiel #4
0
// Test scenario when middleware redirects the request
func (s *LocSuite) TestMiddlewareRedirectsRequest(c *C) {
	server1 := NewTestServer(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hi, I'm endpoint 1"))
	})
	defer server1.Close()

	server2 := NewTestServer(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hi, I'm endpoint 2"))
	})
	defer server2.Close()

	rr1, rr2 := s.newRoundRobin(server1.URL), s.newRoundRobin(server2.URL)

	loc1, err := NewLocationWithOptions("loc1", rr1, Options{})
	c.Assert(err, IsNil)

	loc2, err := NewLocationWithOptions("loc2", rr2, Options{})
	c.Assert(err, IsNil)

	m := hostroute.NewHostRouter()
	route1 := exproute.NewExpRouter()
	c.Assert(route1.AddLocation(`TrieRoute("/loc1")`, loc1), IsNil)

	route2 := exproute.NewExpRouter()
	c.Assert(route2.AddLocation(`TrieRoute("/loc2")`, loc2), IsNil)

	m.SetRouter("localhost1", route1)
	m.SetRouter("localhost2", route2)

	p, err := vulcan.NewProxy(m)
	c.Assert(err, IsNil)

	proxy := httptest.NewServer(p)
	defer proxy.Close()

	redirect := &MiddlewareWrapper{
		OnRequest: func(r Request) (*http.Response, error) {
			u, err := netutils.ParseUrl("http://localhost2/loc2")
			if err != nil {
				return nil, err
			}
			return nil, &errors.RedirectError{URL: u}
		},
		OnResponse: func(r Request, a Attempt) {
		},
	}

	loc1.GetMiddlewareChain().Add("redir", 0, redirect)

	response, bodyBytes, err := MakeRequest(proxy.URL+"/loc1", Opts{Host: "localhost1"})
	c.Assert(err, IsNil)
	c.Assert(response.StatusCode, Equals, http.StatusOK)
	c.Assert(string(bodyBytes), Equals, "Hi, I'm endpoint 2")
}