func assertRouter(router *Router, method string, requestPath string, body string, params ...string) { res := httptest.NewRecorder() req := build.Request().Path(requestPath).Method(method).Request router.ServeHTTP(res, req) Expect(res.Code).To.Equal(200) Expect(string(res.Body.Bytes())).To.Equal(body) }
func Test_Cache(t *testing.T) { route := &Route{Cache: &RouteCache{TTL: time.Minute}} ct := &CacheTests{ request: NewRequest(build.Request().Request, route, nil), } ct.request.params = params.New(0) Expectify(ct, t) }
func (r *RuntimeHelper) Get(path string) (*garnish.Runtime, *http.Request) { //snapshotting resets the stats //this gives each test empty stats to start with for _, route := range r.runtime.Routes { route.Stats.Snapshot() } return r.runtime, build.Request().Path(path).Request }
func Benchmark_Router(b *testing.B) { router := New(Configure()) router.Get("/users", testHandler("get-users")) router.Post("/users/:id", testHandler("create-user")) router.Get("/users/:userId/likes/:id", testHandler("get-favoriate")) requests := []*http.Request{ build.Request().Path("/404").Request, build.Request().Path("/users").Request, build.Request().Path("/users/499/likes/001a").Request, build.Request().Method("Post").Path("/users/943").Request, } b.ResetTimer() res := httptest.NewRecorder() for i := 0; i < b.N; i++ { router.ServeHTTP(res, requests[i%len(requests)]) } }
func (_ RouterTests) ExposesQueryParameters() { id := "" router := New(Configure()) router.Get("/v1/users", func(out http.ResponseWriter, req *Request) { id = req.Query("id") }) router.ServeHTTP(httptest.NewRecorder(), build.Request().URLString("/v1/users?id=goku").Request) Expect(id).To.Equal("goku") }
func assertNotFound(routePath, method string, requestPath string) { router := New(Configure()) router.Get(routePath, func(out http.ResponseWriter, req *Request) { out.WriteHeader(200) }) res := httptest.NewRecorder() router.ServeHTTP(res, build.Request().Method(method).Path(requestPath).Request) Expect(res.Code).To.Equal(404) }
func (_ RouterTests) NotFound() { router := New(Configure()) for _, path := range []string{"", "/", "it's", "/over/9000"} { res := httptest.NewRecorder() router.ServeHTTP(res, build.Request().Path(path).Request) Expect(res.Code).To.Equal(404).Message("path: %s", path) Expect(res.Body.Len()).To.Equal(0) } }
func (_ RouterTests) RouteWithNoParams() { var called bool router := New(Configure()) router.Delete("/harkonen", func(out http.ResponseWriter, req *Request) { Expect(req.Param("friends")).To.Equal("") called = true }) res := httptest.NewRecorder() router.ServeHTTP(res, build.Request().Method("DELETE").Path("/harkonen").Request) Expect(called).To.Equal(true) }
func (_ RouterTests) NotFoundWithCustomHandler() { router := New(Configure()) router.NotFound(func(out http.ResponseWriter, req *Request) { out.WriteHeader(4004) out.Write([]byte("not found")) }) res := httptest.NewRecorder() router.ServeHTTP(res, build.Request().Request) Expect(res.Code).To.Equal(4004) Expect(res.Body.Bytes()).To.Equal([]byte("not found")) }
func assertRouterNotFound(router *Router, method string, requestPath string) { res := httptest.NewRecorder() router.ServeHTTP(res, build.Request().Method(method).Path(requestPath).Request) Expect(res.Code).To.Equal(404) }