Esempio n. 1
0
func ParseUrl(in string) (*HttpEndpoint, error) {
	url, err := netutils.ParseUrl(in)
	if err != nil {
		return nil, err
	}
	return &HttpEndpoint{url: url, id: fmt.Sprintf("%s://%s", url.Scheme, url.Host)}, nil
}
Esempio n. 2
0
func NewRedirectFallback(r Redirect) (*RedirectFallback, error) {
	u, err := netutils.ParseUrl(r.URL)
	if err != nil {
		return nil, err
	}
	return &RedirectFallback{u: u}, nil
}
Esempio n. 3
0
func (r *RoundRobin) FindEndpointByUrl(url string) *WeightedEndpoint {
	out, err := netutils.ParseUrl(url)
	if err != nil {
		return nil
	}
	found, _ := r.findEndpointByUrl(out)
	return found
}
Esempio n. 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")
}
Esempio n. 5
0
func NewWebhookSideEffect(w Webhook) (*WebhookSideEffect, error) {
	if w.Method == "" {
		return nil, fmt.Errorf("Supply method")
	}
	_, err := netutils.ParseUrl(w.URL)
	if err != nil {
		return nil, err
	}

	return &WebhookSideEffect{w: w}, nil
}