-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_test.go
113 lines (102 loc) · 2.38 KB
/
node_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
package hodor
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
var defaultHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%s", r.URL.Path)
})
func TestRouter(t *testing.T) {
router := NewRouter()
routes := []struct {
method Method
pattern string
}{
{GET, "/fuck/you"},
{GET, "/fuck/me"},
{POST, "/fuck/you/again"},
{GET, "/"},
{GET, "/shit"},
}
for _, r := range routes {
router.AddRoute(r.method, r.pattern, defaultHandler)
}
for _, r := range routes {
w := httptest.NewRecorder()
req := httptest.NewRequest(r.method.String(), r.pattern, nil)
router.ServeHTTP(w, req)
if w.Code != 200 {
t.Errorf("%s %s not match", r.method, r.pattern)
}
if got, exp := w.Body.String(), r.pattern; got != exp {
t.Errorf("%s %s response not match. exp: %s, got: %s ",
r.method, r.pattern, exp, got)
}
}
cases := []struct {
method Method
pattern string
code int
}{
{POST, "/fuck/you", 405},
{GET, "/fuck/her", 404},
{POST, "/fuck/her", 404},
}
for _, c := range cases {
w := httptest.NewRecorder()
req := httptest.NewRequest(c.method.String(), c.pattern, nil)
router.ServeHTTP(w, req)
if got, exp := w.Code, c.code; got != exp {
t.Errorf("%s %s code not match. exp: %d, got: %d ",
c.method, c.pattern, exp, got)
}
}
}
func TestNamedNode(t *testing.T) {
router := NewRouter()
routes := []struct {
method Method
pattern string
}{
{GET, "/fuck/you"},
{POST, "/fuck/you"},
{DELETE, "/fuck/you"},
{GET, "/fuck/me"},
{POST, "/fuck/:name/again"},
{GET, "/"},
{GET, "/shit"},
{GET, "/:name"},
{POST, "/:name"},
}
for _, r := range routes {
router.AddRoute(r.method, r.pattern, defaultHandler)
}
cases := []struct {
method Method
pattern string
code int
}{
{POST, "/fuck/you", 200},
{DELETE, "/fuck/you", 200},
{HEAD, "/fuck/you", 405},
{GET, "/fuck/her", 404},
{GET, "/fuck/me", 200},
{POST, "/fuck/her", 404},
{POST, "/fuck/her/again", 200},
{POST, "/fuck/you/again", 200},
{POST, "/fuck/you/once", 404},
{GET, "/alice", 200},
{POST, "/alice", 200},
}
for _, c := range cases {
w := httptest.NewRecorder()
req := httptest.NewRequest(c.method.String(), c.pattern, nil)
router.ServeHTTP(w, req)
if got, exp := w.Code, c.code; got != exp {
t.Errorf("%s %s code not match. exp: %d, got: %d ",
c.method, c.pattern, exp, got)
}
}
}