github.com/jxgolibs/go-oauth2-server@v1.0.1/util/routes/routes_test.go (about) 1 package routes_test 2 3 import ( 4 "fmt" 5 "net/http" 6 "net/http/httptest" 7 "testing" 8 9 "github.com/RichardKnop/go-oauth2-server/util/routes" 10 "github.com/gorilla/mux" 11 "github.com/stretchr/testify/assert" 12 "github.com/urfave/negroni" 13 ) 14 15 // helloWorldMiddleware is a test middleware that writes "hello world" to the 16 // response so we can check the middleware is registered 17 type helloWorldMiddleware struct{} 18 19 // ServeHTTP as per the negroni.Handler interface 20 func (m *helloWorldMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { 21 vars := mux.Vars(r) 22 id := vars["id"] 23 w.Write([]byte(fmt.Sprintf("hello world %s", id))) 24 next(w, r) 25 } 26 27 func TestAddRoutes(t *testing.T) { 28 var ( 29 router = mux.NewRouter() 30 r *http.Request 31 err error 32 match *mux.RouteMatch 33 w *httptest.ResponseRecorder 34 ) 35 36 // Add a test GET route without a middleware 37 routes.AddRoutes([]routes.Route{ 38 { 39 Name: "foobar_route", 40 Method: "GET", 41 Pattern: "/bar", 42 HandlerFunc: func(w http.ResponseWriter, r *http.Request) {}, 43 }, 44 }, router.PathPrefix("/foo").Subrouter()) 45 46 // Add a test PUT route with a middleware and a named parameter 47 routes.AddRoutes([]routes.Route{ 48 { 49 Name: "helloworld_route", 50 Method: "PUT", 51 Pattern: "/world/{id:[0-9]+}", 52 HandlerFunc: func(w http.ResponseWriter, r *http.Request) {}, 53 Middlewares: []negroni.Handler{ 54 new(helloWorldMiddleware), 55 }, 56 }, 57 }, router.PathPrefix("/hello").Subrouter()) 58 59 // Test the foobar_route 60 r, err = http.NewRequest("GET", "http://1.2.3.4/foo/bar", nil) 61 assert.NoError(t, err, "Request setup should not get an error") 62 63 // Test the route matches expected name 64 match = new(mux.RouteMatch) 65 router.Match(r, match) 66 assert.Equal(t, "foobar_route", match.Route.GetName()) 67 68 // Test no middleware has been registered 69 w = httptest.NewRecorder() 70 router.ServeHTTP(w, r) 71 assert.Equal(t, "", w.Body.String()) 72 73 // Test the helloworld_route 74 r, err = http.NewRequest("PUT", "http://1.2.3.4/hello/world/1", nil) 75 assert.NoError(t, err, "Request setup should not get an error") 76 77 // Test the route matches expected name 78 match = new(mux.RouteMatch) 79 router.Match(r, match) 80 assert.Equal(t, "helloworld_route", match.Route.GetName()) 81 82 // Test the helloWorldMiddleware has been registered 83 w = httptest.NewRecorder() 84 router.ServeHTTP(w, r) 85 assert.Equal(t, "hello world 1", w.Body.String()) 86 } 87 88 func TestRecoveryMiddlewareHandlesPanic(t *testing.T) { 89 var ( 90 router = mux.NewRouter() 91 app = negroni.Classic() 92 r *http.Request 93 err error 94 match *mux.RouteMatch 95 ) 96 97 // Add a test GET route without a middleware 98 routes.AddRoutes([]routes.Route{ 99 { 100 Name: "panic_route", 101 Method: "GET", 102 Pattern: "/panic", 103 HandlerFunc: func(w http.ResponseWriter, r *http.Request) { 104 panic("oh no") 105 }, 106 }, 107 }, router.PathPrefix("/foo").Subrouter()) 108 109 // Test the foobar_route 110 r, err = http.NewRequest("GET", "http://1.2.3.4/foo/panic", nil) 111 assert.NoError(t, err, "Request setup should not get an error") 112 113 // Test the route matches expected name 114 match = new(mux.RouteMatch) 115 router.Match(r, match) 116 assert.Equal(t, "panic_route", match.Route.GetName()) 117 118 // Test that panic does not crash the app 119 app.UseHandler(router) 120 app.ServeHTTP(httptest.NewRecorder(), r) 121 }