func (s *BackendSuite) TestFrontendBadParams(c *C) { // Bad route _, err := NewHTTPFrontend(route.NewMux(), "f1", "b1", "/home -- afawf \\~", HTTPFrontendSettings{}) c.Assert(err, NotNil) // Empty params _, err = NewHTTPFrontend(route.NewMux(), "", "", "", HTTPFrontendSettings{}) c.Assert(err, NotNil) }
func TestNotFound(t *testing.T) { combinedRouter := NewMux(Routers{ RouterEntry{ Name: "vulcanrouter", Router: route.NewMux(), }, }) if err := combinedRouter.Handle("Path(\"/\")", &testHandler{respCode: 1}); err != nil { t.Logf("Error when adding path: %v", err) } if err := combinedRouter.Handle("Path(\"/foo\")", &testHandler{respCode: 2}); err != nil { t.Logf("Error when adding path: %v", err) } if err := combinedRouter.Handle("Path(\"/bar\")", &testHandler{respCode: 3}); err != nil { t.Logf("Error when adding path: %v", err) } { req, _ := http.NewRequest("GET", "http://localhost/obviously bad route def", strings.NewReader("")) rec := httptest.NewRecorder() combinedRouter.ServeHTTP(rec, req) assert.Equal(t, http.StatusNotFound, rec.Code, "Test failed") } }
func (r *vulcandRouter) SetCName(cname, name string) error { usedName, err := router.Retrieve(name) if err != nil { return err } if !router.ValidCName(cname, r.domain) { return router.ErrCNameNotAllowed } frontendName := r.frontendName(cname) if found, _ := r.client.GetFrontend(engine.FrontendKey{Id: frontendName}); found != nil { return router.ErrCNameExists } frontend, err := engine.NewHTTPFrontend( route.NewMux(), frontendName, r.backendName(usedName), fmt.Sprintf(`Host(%q)`, cname), engine.HTTPFrontendSettings{}, ) if err != nil { return &router.RouterError{Err: err, Op: "set-cname"} } err = r.client.UpsertFrontend(*frontend, engine.NoTTL) if err != nil { return &router.RouterError{Err: err, Op: "set-cname"} } return nil }
func (s *BackendSuite) TestFrontendDefaults(c *C) { f, err := NewHTTPFrontend(route.NewMux(), "f1", "b1", `Path("/home")`, HTTPFrontendSettings{}) c.Assert(err, IsNil) c.Assert(f.GetId(), Equals, "f1") c.Assert(f.String(), Not(Equals), "") c.Assert(f.Route, Equals, `Path("/home")`) }
func (v *vulcan) NewFrontend(rsc *kubernetes.Resource) (loadbalancer.Frontend, error) { s := engine.HTTPFrontendSettings{} if val, ok := rsc.GetAnnotation(loadbalancer.PassHostHeaderKey); ok { b, _ := strconv.ParseBool(val) s.PassHostHeader = b } if val, ok := rsc.GetAnnotation(loadbalancer.TrustForwardHeadersKey); ok { b, _ := strconv.ParseBool(val) s.TrustForwardHeader = b } if val, ok := rsc.GetAnnotation(loadbalancer.FailoverExpressionKey); ok { s.FailoverPredicate = val } if val, ok := rsc.GetAnnotation(loadbalancer.FrontendSettingsKey); ok { if er := json.Unmarshal([]byte(val), &s); er != nil { logger.Warnf("Failed to parse settings for frontend %q: %v", rsc.ID, er) } } f, er := engine.NewHTTPFrontend(vroute.NewMux(), rsc.ID(), rsc.ID(), NewRoute(rsc.Route).String(), s) if er != nil { return nil, er } return newFrontend(f), nil }
func MakeFrontend(route string, backendId string) engine.Frontend { f, err := engine.NewHTTPFrontend(routelib.NewMux(), UID("frontend"), backendId, route, engine.HTTPFrontendSettings{}) if err != nil { panic(err) } return *f }
func (s *BackendSuite) TestFrontendsFromJSON(c *C) { f, err := NewHTTPFrontend(route.NewMux(), "f1", "b1", `Path("/path")`, HTTPFrontendSettings{}) c.Assert(err, IsNil) bytes, err := json.Marshal(f) fs := []Frontend{*f} bytes, err = json.Marshal(map[string]interface{}{"Frontends": fs}) r := plugin.NewRegistry() c.Assert(r.AddSpec(connlimit.GetSpec()), IsNil) out, err := FrontendsFromJSON(route.NewMux(), bytes) c.Assert(err, IsNil) c.Assert(out, NotNil) c.Assert(out, DeepEquals, fs) }
func scopedHandler(scope string, proxy http.Handler) (http.Handler, error) { if scope == "" { return proxy, nil } mux := route.NewMux() mux.SetNotFound(&DefaultNotFound{}) if err := mux.Handle(scope, proxy); err != nil { return nil, err } return mux, nil }
func (s *BackendSuite) TestFrontendBadOptions(c *C) { settings := []HTTPFrontendSettings{ HTTPFrontendSettings{ FailoverPredicate: "bad predicate", }, } for _, s := range settings { f, err := NewHTTPFrontend(route.NewMux(), "f1", "b", `Path("/home")`, s) c.Assert(err, NotNil) c.Assert(f, IsNil) } }
func setDefaults(o Options) Options { if o.MetricsClient == nil { o.MetricsClient = metrics.NewNop() } if o.TimeProvider == nil { o.TimeProvider = &timetools.RealTime{} } if o.Router == nil { o.Router = route.NewMux() } return o }
func setDefaults(o Options) Options { if o.MetricsClient == nil { o.MetricsClient = metrics.NewNop() } if o.TimeProvider == nil { o.TimeProvider = &timetools.RealTime{} } if o.Router == nil { o.Router = route.NewMux() } if o.IncomingConnectionTracker == nil { o.IncomingConnectionTracker = newDefaultConnTracker() } return o }
func (cmd *Command) upsertFrontendAction(c *cli.Context) error { settings, err := getFrontendSettings(c) if err != nil { return err } f, err := engine.NewHTTPFrontend(route.NewMux(), c.String("id"), c.String("b"), c.String("route"), settings) if err != nil { return err } if err := cmd.client.UpsertFrontend(*f, c.Duration("ttl")); err != nil { return err } cmd.printOk("frontend upserted") return nil }
func TestRejectedRoute(t *testing.T) { combinedRouter := NewMux(Routers{ RouterEntry{ Name: "vulcanrouter", Router: route.NewMux(), }, RouterEntry{ Name: "jsrouter", Router: jsroute.NewMux(), }, }) if err := combinedRouter.Handle("Path(\"\"); && Host(\"\")", &testHandler{respCode: 1}); err != nil { t.Logf("Error when adding path: %v", err) } else { assert.Fail(t, "This path should have produced an error, but was accepted.") } }
func TestNoRouter(t *testing.T) { combinedRouter := NewMux(Routers{ RouterEntry{ Name: "vulcanrouter", Router: route.NewMux(), }, }) if err := combinedRouter.Handle("Path(\"/\")", &testHandler{respCode: 1}); err != nil { t.Logf("Error when adding path: %v", err) } if err := combinedRouter.Handle("Path(\"/foo\")", &testHandler{respCode: 2}); err != nil { t.Logf("Error when adding path: %v", err) } if err := combinedRouter.Handle("Path(\"/bar\")", &testHandler{respCode: 3}); err != nil { t.Logf("Error when adding path: %v", err) } if err := combinedRouter.Handle("Obviously bad route def", &testHandler{respCode: 3}); err == nil { t.Logf("No error when adding path: Obviously bad route def. An error was expected") } }
func (r *vulcandRouter) AddBackend(name string) error { backendName := r.backendName(name) frontendName := r.frontendName(r.frontendHostname(name)) backendKey := engine.BackendKey{Id: backendName} frontendKey := engine.FrontendKey{Id: frontendName} if found, _ := r.client.GetBackend(backendKey); found != nil { return router.ErrBackendExists } if found, _ := r.client.GetFrontend(frontendKey); found != nil { return router.ErrBackendExists } backend, err := engine.NewHTTPBackend( backendName, engine.HTTPBackendSettings{}, ) if err != nil { return &router.RouterError{Err: err, Op: "add-backend"} } err = r.client.UpsertBackend(*backend) if err != nil { return err } frontend, err := engine.NewHTTPFrontend( route.NewMux(), frontendName, backend.Id, fmt.Sprintf(`Host(%q)`, r.frontendHostname(name)), engine.HTTPFrontendSettings{}, ) if err != nil { return &router.RouterError{Err: err, Op: "add-backend"} } err = r.client.UpsertFrontend(*frontend, engine.NoTTL) if err != nil { r.client.DeleteBackend(backendKey) return &router.RouterError{Err: err, Op: "add-backend"} } return router.Store(name, name, routerName) }
func (s *BackendSuite) TestNewFrontendWithOptions(c *C) { settings := HTTPFrontendSettings{ Limits: HTTPFrontendLimits{ MaxMemBodyBytes: 12, MaxBodyBytes: 400, }, FailoverPredicate: "IsNetworkError() && Attempts() <= 1", Hostname: "host1", TrustForwardHeader: true, } f, err := NewHTTPFrontend(route.NewMux(), "f1", "b1", `Path("/home")`, settings) c.Assert(err, IsNil) c.Assert(f.Id, Equals, "f1") o := f.HTTPSettings() c.Assert(o.Limits.MaxMemBodyBytes, Equals, int64(12)) c.Assert(o.Limits.MaxBodyBytes, Equals, int64(400)) c.Assert(o.FailoverPredicate, NotNil) c.Assert(o.TrustForwardHeader, Equals, true) c.Assert(o.Hostname, Equals, "host1") }
package main import ( "strings" router "github.com/vulcand/route" "github.com/vulcand/vulcand/engine" ) var validateRoute = router.NewMux().IsValid type vulcandEngine interface { basePath(ID string) string engineType() string path(ID string) string } type backend struct { engine.Backend } func newBackend(ID string) *backend { return &backend{engine.Backend{Id: ID, Type: backendType}} } func (b *backend) engineType() string { return "backend" } func (b *backend) basePath(ID string) string { return vulcandPath("backends", ID)
func NewRegistry() *Registry { return &Registry{ specs: []*MiddlewareSpec{}, router: route.NewMux(), } }
func TestMultipleRouters(t *testing.T) { combinedRouter := NewMux(Routers{ RouterEntry{ Name: "vulcanrouter", Router: route.NewMux(), }, RouterEntry{ Name: "jsrouter", Router: jsroute.NewMux(), }, RouterEntry{ Name: "testrouter", Router: &testRouter{routedef: "Obviously bad routedef"}, }, }) if err := combinedRouter.Handle("Path(\"/\")", &testHandler{respCode: 1}); err != nil { t.Logf("Error when adding path: %v", err) } if err := combinedRouter.Handle("Path(\"/foo\")", &testHandler{respCode: 2}); err != nil { t.Logf("Error when adding path: %v", err) } if err := combinedRouter.Handle("Path(\"/bar\")", &testHandler{respCode: 3}); err != nil { t.Logf("Error when adding path: %v", err) } if err := combinedRouter.Handle("Obviously bad routedef", &testHandler{respCode: 4}); err != nil { t.Logf("Error when adding path: %v", err) } if err := combinedRouter.Handle("Request.Header[\"User-Agent\"] == \"fakeagent\"", &testHandler{respCode: 5}); err != nil { t.Logf("Error when adding path: %v", err) } { req, _ := http.NewRequest("GET", "http://localhost/", strings.NewReader("")) rec := httptest.NewRecorder() combinedRouter.ServeHTTP(rec, req) assert.Equal(t, 1, rec.Code, "Test failed") } { req, _ := http.NewRequest("GET", "http://localhost/foo", strings.NewReader("")) rec := httptest.NewRecorder() combinedRouter.ServeHTTP(rec, req) assert.Equal(t, 2, rec.Code, "Test failed") } { req, _ := http.NewRequest("GET", "http://localhost/bar", strings.NewReader("")) rec := httptest.NewRecorder() combinedRouter.ServeHTTP(rec, req) assert.Equal(t, 3, rec.Code, "Test failed") } { req, _ := http.NewRequest("GET", "http://localhost/obviously bad route def", strings.NewReader("")) rec := httptest.NewRecorder() combinedRouter.ServeHTTP(rec, req) assert.Equal(t, 4, rec.Code, "Test failed") } { req, _ := http.NewRequest("GET", "http://localhost/obviously bad route def", strings.NewReader("")) req.Header.Add("User-Agent", "fakeagent") rec := httptest.NewRecorder() combinedRouter.ServeHTTP(rec, req) assert.Equal(t, 5, rec.Code, "Test failed") } }