Exemplo n.º 1
0
func (s *ConfSuite) SetUpTest(c *C) {
	s.router = hostroute.NewHostRouter()
	proxy, err := vulcan.NewProxy(s.router)
	if err != nil {
		c.Fatal(err)
	}
	s.conf = NewConfigurator(proxy)
}
Exemplo n.º 2
0
func (s *Service) createProxy() error {
	s.router = hostroute.NewHostRouter()
	proxy, err := vulcan.NewProxy(s.router)
	if err != nil {
		return err
	}
	s.proxy = proxy
	return nil
}
Exemplo n.º 3
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")
}