-
Notifications
You must be signed in to change notification settings - Fork 34
/
router_middleware_test.go
66 lines (51 loc) · 1.89 KB
/
router_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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package traffic
import (
"fmt"
assert "github.com/pilu/miniassert"
"net/http"
"net/http/httptest"
"testing"
)
type fakeNotFoundHandlerContainer struct {
callsCount int
}
func (f *fakeNotFoundHandlerContainer) Handler(w ResponseWriter, r *Request) {
f.callsCount++
}
func newTestRequest(method, path string) (ResponseWriter, *httptest.ResponseRecorder, *Request) {
r, _ := http.NewRequest(method, path, nil)
request := newRequest(r)
recorder := httptest.NewRecorder()
env := make(map[string]interface{})
responseWriter := newResponseWriter(recorder, &env)
return responseWriter, recorder, request
}
func newTestRouterMiddleware() *RouterMiddleware {
router := &Router{}
router.routes = make(map[HttpMethod][]*Route)
router.beforeFilters = make([]HttpHandleFunc, 0)
router.middlewares = make([]Middleware, 0)
routerMiddleware := &RouterMiddleware{router}
return routerMiddleware
}
func TestRouterMiddleware_NotFound(t *testing.T) {
routerMiddleware := newTestRouterMiddleware()
fakeNotFound := new(fakeNotFoundHandlerContainer)
routerMiddleware.router.NotFoundHandler = fakeNotFound.Handler
responseWriter, recorder, request := newTestRequest("GET", "/")
routerMiddleware.ServeHTTP(responseWriter, request, func() Middleware { return nil })
// checks that the router middleware calls router.handleNotFound
assert.Equal(t, 1, fakeNotFound.callsCount)
assert.Equal(t, "", string(recorder.Body.Bytes()))
}
func TestRouterMiddleware_Found(t *testing.T) {
routerMiddleware := newTestRouterMiddleware()
responseWriter, recorder, request := newTestRequest("GET", "/")
testRootHandler := func(w ResponseWriter, r *Request) {
fmt.Fprint(w, "Hello World")
}
routerMiddleware.router.Get("/", testRootHandler)
routerMiddleware.ServeHTTP(responseWriter, request, func() Middleware { return nil })
assert.Equal(t, 200, recorder.Code)
assert.Equal(t, "Hello World", string(recorder.Body.Bytes()))
}