github.com/seeker-insurance/kit@v0.0.13/web/routing_test.go (about) 1 package web 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/seeker-insurance/kit/pretty" 8 "github.com/labstack/echo" 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestRoute_Handle(t *testing.T) { 13 const ( 14 m = "f" 15 path = "http://localhost.com" 16 name = "foo" 17 ) 18 19 noOpMiddleWare := func(hf echo.HandlerFunc) echo.HandlerFunc { return hf } 20 alwaysNilMiddleWare := func(hf echo.HandlerFunc) echo.HandlerFunc { return func(echo.Context) error { return nil } } 21 hf := func(ApiContext) error { return nil } 22 wantHF := wrapApiRoute(hf) 23 rt := Route{Path: path, Name: name} 24 25 wantMiddleware := []echo.MiddlewareFunc{noOpMiddleWare, alwaysNilMiddleWare} 26 wantHandler := MethodHandler{m, wantHF, wantMiddleware} 27 wantHandlers := []*MethodHandler{&wantHandler} 28 29 got := rt.Handle(m, hf, noOpMiddleWare, alwaysNilMiddleWare) 30 want := Route{Path: path, Name: name, Handlers: wantHandlers} 31 assert.Empty(t, pretty.Diff(want, *got), 0, fmt.Sprint(pretty.Diff(got, want))) 32 } 33 34 func TestRoute_SetName(t *testing.T) { 35 rt := new(Route) 36 rt.SetName("foo") 37 assert.Equal(t, rt.Name, "foo") 38 } 39 40 func TestRouteConfig_AddRoute(t *testing.T) { 41 const path = "http://somepath.com" 42 cfg := new(RouteConfig) 43 rt := cfg.AddRoute(path) 44 assert.Equal(t, *rt, Route{Path: path, Handlers: []*MethodHandler{}}) 45 assert.Equal(t, cfg.Routes, []*Route{rt}) 46 } 47 48 func Test_routeByName(t *testing.T) { 49 const name = "foo" 50 cfg := new(RouteConfig) 51 wantRoute := Route{ 52 Name: name, 53 } 54 55 cfg.Routes = []*Route{&Route{}, &wantRoute, &Route{Name: "bar"}} 56 got, err := routeByName(cfg, name) 57 assert.Equal(t, got, &wantRoute) 58 assert.NoError(t, err) 59 60 cfg = &RouteConfig{Routes: []*Route{&Route{}, &Route{Name: "bar"}}} 61 _, err = routeByName(cfg, name) 62 assert.Error(t, err) 63 64 } 65 66 func Test_wrapApiRoute(t *testing.T) { 67 68 } 69 70 func Test_initRoutes(t *testing.T) { 71 const ( 72 path = "http://localpath.com/foo" 73 name = "foo" 74 method = "m" 75 ) 76 hf := func(ApiContext) error { return nil } 77 e := echo.New() 78 cfg := new(RouteConfig) 79 rt := cfg.AddRoute(path).SetName(name).Handle(method, hf) 80 initRoutes(e, cfg) 81 82 wantRoutes := []*Route{rt} 83 assert.Equal(t, cfg.Routes, wantRoutes) 84 assert.Equal(t, cfg.Routes[0].ERoute.Name, rt.Name) 85 86 wantEchoRoute := echo.Route{Method: method, Path: path, Name: name} 87 assert.Len(t, e.Routes(), 1) 88 assert.Equal(t, wantEchoRoute, *e.Routes()[0]) 89 90 }