-
Notifications
You must be signed in to change notification settings - Fork 5
/
middleware_test.go
50 lines (42 loc) · 1.36 KB
/
middleware_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package shift
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func TestRouteContextMiddleware(t *testing.T) {
r := New()
r.Use(RouteContext())
r.GET("/foo/:name", func(w http.ResponseWriter, r *http.Request, _ Route) error {
route := RouteOf(r)
assert(t, route.Path == "/foo/:name", fmt.Sprintf("path > expected: /foo/:name, got: %s", route.Path))
name := route.Params.Get("name")
assert(t, name == "bar", fmt.Sprintf("param > expected: bar, got: %s", name))
return nil
})
srv := r.Serve()
rw := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/foo/bar", nil)
srv.ServeHTTP(rw, req)
assert(t, rw.Code == http.StatusOK, fmt.Sprintf("http status code > expected: 200, got: %d", rw.Code))
}
func BenchmarkRouteContextMiddleware(b *testing.B) {
r := New()
r.Use(RouteContext())
r.GET("/movies/genres/:name", HTTPHandlerFunc(fakeHttpHandler))
srv := r.Serve()
rr := httptest.NewRecorder()
requests := make([]*http.Request, 0, 10)
for _, genre := range []string{"drama", "western", "sci-fi", "thriller", "animation", "adventure", "noir", "fantasy", "crime", "comedy"} {
req, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("/movies/genres/%s", genre), nil)
requests = append(requests, req)
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for _, req := range requests {
srv.ServeHTTP(rr, req)
}
}
}