forked from jackwhelpton/fasthttp-routing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_test.go
129 lines (112 loc) · 3.62 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
package routing
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/valyala/fasthttp"
)
func TestRouterNotFound(t *testing.T) {
r := New()
h := func(c *Context) error {
fmt.Fprint(c.RequestCtx, "ok")
return nil
}
r.Get("/users", h)
r.Post("/users", h)
r.NotFound(MethodNotAllowedHandler, NotFoundHandler)
var ctx fasthttp.RequestCtx
ctx.Request.Header.SetMethod("GET")
ctx.Request.SetRequestURI("/users")
r.HandleRequest(&ctx)
assert.Equal(t, "ok", string(ctx.Response.Body()), "response body")
assert.Equal(t, fasthttp.StatusOK, ctx.Response.StatusCode(), "HTTP status code")
ctx = fasthttp.RequestCtx{}
ctx.Request.Header.SetMethod("PUT")
ctx.Request.SetRequestURI("/users")
r.HandleRequest(&ctx)
assert.Equal(t, "GET, OPTIONS, POST", string(ctx.Response.Header.Peek("Allow")), "Allow header")
assert.Equal(t, fasthttp.StatusMethodNotAllowed, ctx.Response.StatusCode(), "HTTP status code")
ctx = fasthttp.RequestCtx{}
ctx.Request.Header.SetMethod("OPTIONS")
ctx.Request.SetRequestURI("/users")
r.HandleRequest(&ctx)
assert.Equal(t, "GET, OPTIONS, POST", string(ctx.Response.Header.Peek("Allow")), "Allow header")
assert.Equal(t, fasthttp.StatusOK, ctx.Response.StatusCode(), "HTTP status code")
ctx = fasthttp.RequestCtx{}
ctx.Request.Header.SetMethod("GET")
ctx.Request.SetRequestURI("/users/")
r.HandleRequest(&ctx)
assert.Equal(t, "", string(ctx.Response.Header.Peek("Allow")), "Allow header")
assert.Equal(t, fasthttp.StatusNotFound, ctx.Response.StatusCode(), "HTTP status code")
r.IgnoreTrailingSlash = true
ctx = fasthttp.RequestCtx{}
ctx.Request.Header.SetMethod("GET")
ctx.Request.SetRequestURI("/users/")
r.HandleRequest(&ctx)
assert.Equal(t, "ok", string(ctx.Response.Body()), "response body")
assert.Equal(t, fasthttp.StatusOK, ctx.Response.StatusCode(), "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()
c := NewContext(&fasthttp.RequestCtx{})
r.handleError(c, errors.New("abc"))
assert.Equal(t, fasthttp.StatusInternalServerError, c.Response.StatusCode())
c = NewContext(&fasthttp.RequestCtx{})
r.handleError(c, NewHTTPError(fasthttp.StatusNotFound))
assert.Equal(t, fasthttp.StatusNotFound, c.Response.StatusCode())
}
func TestRequestHandler(t *testing.T) {
var ctx fasthttp.RequestCtx
ctx.Request.Header.SetMethod("GET")
ctx.Request.SetRequestURI("/users/")
c := NewContext(&ctx)
h := RequestHandlerFunc(func(c *fasthttp.RequestCtx) { c.NotFound() })
assert.Nil(t, h(c))
assert.Equal(t, fasthttp.StatusNotFound, c.Response.StatusCode())
}