-
Notifications
You must be signed in to change notification settings - Fork 51
/
router_test.go
132 lines (115 loc) · 3.47 KB
/
router_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package routing
import (
"errors"
"fmt"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)
func TestRouterNotFound(t *testing.T) {
r := New()
h := func(c *Context) error {
fmt.Fprint(c.Response, "ok")
return nil
}
r.Get("/users", h)
r.Post("/users", h)
r.NotFound(MethodNotAllowedHandler, NotFoundHandler)
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/users", nil)
r.ServeHTTP(res, req)
assert.Equal(t, "ok", res.Body.String(), "response body")
assert.Equal(t, http.StatusOK, res.Code, "HTTP status code")
res = httptest.NewRecorder()
req, _ = http.NewRequest("PUT", "/users", nil)
r.ServeHTTP(res, req)
assert.Equal(t, "GET, OPTIONS, POST", res.Header().Get("Allow"), "Allow header")
assert.Equal(t, http.StatusMethodNotAllowed, res.Code, "HTTP status code")
res = httptest.NewRecorder()
req, _ = http.NewRequest("OPTIONS", "/users", nil)
r.ServeHTTP(res, req)
assert.Equal(t, "GET, OPTIONS, POST", res.Header().Get("Allow"), "Allow header")
assert.Equal(t, http.StatusOK, res.Code, "HTTP status code")
res = httptest.NewRecorder()
req, _ = http.NewRequest("GET", "/users/", nil)
r.ServeHTTP(res, req)
assert.Equal(t, "", res.Header().Get("Allow"), "Allow header")
assert.Equal(t, http.StatusNotFound, res.Code, "HTTP status code")
r.IgnoreTrailingSlash = true
res = httptest.NewRecorder()
req, _ = http.NewRequest("GET", "/users/", nil)
r.ServeHTTP(res, req)
assert.Equal(t, "ok", res.Body.String(), "response body")
assert.Equal(t, http.StatusOK, res.Code, "HTTP status code")
}
func TestRouterUse(t *testing.T) {
r := New()
assert.Equal(t, 2, len(r.notFoundHandlers))
r.Use(NotFoundHandler)
assert.Equal(t, 3, len(r.notFoundHandlers))
}
func TestRouterRoute(t *testing.T) {
r := New()
r.Get("/users").Name("users")
assert.NotNil(t, r.Route("users"))
assert.Nil(t, r.Route("users2"))
}
func TestRouterAdd(t *testing.T) {
r := New()
assert.Equal(t, 0, r.maxParams)
r.add("GET", "/users/<id>", nil)
assert.Equal(t, 1, r.maxParams)
}
func TestRouterFind(t *testing.T) {
r := New()
r.add("GET", "/users/<id>", []Handler{NotFoundHandler})
handlers, params := r.Find("GET", "/users/1")
assert.Equal(t, 1, len(handlers))
if assert.Equal(t, 1, len(params)) {
assert.Equal(t, "1", params["id"])
}
}
func TestRouterNormalizeRequestPath(t *testing.T) {
tests := []struct {
path string
expected string
}{
{"/", "/"},
{"/users", "/users"},
{"/users/", "/users"},
{"/users//", "/users"},
{"///", "/"},
}
r := New()
r.IgnoreTrailingSlash = true
for _, test := range tests {
result := r.normalizeRequestPath(test.path)
assert.Equal(t, test.expected, result)
}
}
func TestRouterHandleError(t *testing.T) {
r := New()
res := httptest.NewRecorder()
c := &Context{Response: res}
r.handleError(c, errors.New("abc"))
assert.Equal(t, http.StatusInternalServerError, res.Code)
res = httptest.NewRecorder()
c = &Context{Response: res}
r.handleError(c, NewHTTPError(http.StatusNotFound))
assert.Equal(t, http.StatusNotFound, res.Code)
}
func TestHTTPHandler(t *testing.T) {
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/users/", nil)
c := NewContext(res, req)
h1 := HTTPHandlerFunc(http.NotFound)
assert.Nil(t, h1(c))
assert.Equal(t, http.StatusNotFound, res.Code)
res = httptest.NewRecorder()
req, _ = http.NewRequest("GET", "/users/", nil)
c = NewContext(res, req)
h2 := HTTPHandler(http.NotFoundHandler())
assert.Nil(t, h2(c))
assert.Equal(t, http.StatusNotFound, res.Code)
}