-
Notifications
You must be signed in to change notification settings - Fork 0
/
mooxy_test.go
149 lines (133 loc) · 4.26 KB
/
mooxy_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package mooxy
import (
"testing"
"io"
"net/http"
"net/http/httptest"
)
type Controller struct {
content string
}
func (c Controller) ServeHTTP(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, c.content)
}
type TestOutput struct {
Body string
StatusCode int
Handler Controller
}
func NewTestOutput(body string, statusCode int) (TestOutput) {
var handler = Controller{body}
return TestOutput{body, statusCode, handler}
}
type TestRequest struct {
path string
method string
}
func NewTestRequest(path string, method string) (TestRequest) {
return TestRequest{path, method}
}
type TestCase struct {
route *Route
request TestRequest
output TestOutput
}
var testCases = []TestCase {
{
NewRoute("/foo").Methods(http.MethodGet),
NewTestRequest("/foo", http.MethodGet),
NewTestOutput("simple route match", 200),
},
{
NewRoute("/bar").Methods(http.MethodGet),
NewTestRequest("/bar", http.MethodGet),
NewTestOutput("second simple route match", 200),
},
{
NewRoute("/post/list").Methods(http.MethodGet),
NewTestRequest("/post/list", http.MethodGet),
NewTestOutput("third simple route match", 200),
},
{
NewRoute("/foo/bar").Methods(http.MethodGet),
NewTestRequest("/foo/bar", http.MethodGet),
NewTestOutput("simple route with root already defined", 200),
},
{
NewRoute("/long/path/to/url/long").Methods(http.MethodGet),
NewTestRequest("/long/path/to/url/long", http.MethodGet),
NewTestOutput("long path to url", 200),
},
{
NewRoute("/trailing/slash").Methods(http.MethodGet),
NewTestRequest("/trailing/slash/", http.MethodGet),
NewTestOutput("should match a request with trailing slash", 200),
},
{
nil,
NewTestRequest("/missing-route", http.MethodGet),
NewTestOutput("Page not found.\n", 404),
},
{
NewRoute("/foo/method/post").Methods(http.MethodPost, http.MethodConnect),
NewTestRequest("/foo/method/post", http.MethodGet),
NewTestOutput("Method not available\n", 405),
},
{
NewRoute("/foo/method/put").Methods(http.MethodPut),
NewTestRequest("/foo/method/put", http.MethodPost),
NewTestOutput("Method not available\n", 405),
},
{
NewRoute("/foo/bar/{id}").Methods(http.MethodGet),
NewTestRequest("/foo/bar/1", http.MethodGet),
NewTestOutput("Route with param", 200),
},
{
NewRoute("/post/{slug}").Methods(http.MethodGet),
NewTestRequest("/post/hello-world", http.MethodGet),
NewTestOutput("Route with string param", 200),
},
{
NewRoute("/post/{slug}/comments/{userId}").Methods(http.MethodGet),
NewTestRequest("/post/hello-world/comments/1", http.MethodGet),
NewTestOutput("Route with interleaved params", 200),
},
{
NewRoute("/img/{*}").Methods(http.MethodGet),
NewTestRequest("/img/2022/01/01/test.jpg", http.MethodGet),
NewTestOutput("Route that matches all subsequent paths", 200),
},
{
nil,
NewTestRequest("/img", http.MethodGet),
NewTestOutput("Page not found.\n", 404),
},
{
NewRoute("/foo").Methods(http.MethodPost),
NewTestRequest("/foo", http.MethodPost),
NewTestOutput("Add post to a route that has get", 200),
},
}
func TestRouter(t *testing.T) {
var router = NewRouter()
for _, test := range testCases{
if test.route != nil {
router.Handle(test.route, test.output.Handler)
}
}
for _, test := range testCases{
req := httptest.NewRequest(test.request.method, test.request.path, nil)
w := httptest.NewRecorder()
// make the call
router.ServeHTTP(w, req)
resp := w.Result()
body, _ := io.ReadAll(resp.Body)
if (resp.StatusCode != test.output.StatusCode) {
t.Errorf("Status code should be: %d got: %d for request %s", test.output.StatusCode, resp.StatusCode, test.request.path)
}
if (string(body) != test.output.Body) {
t.Errorf("Response for request %s should be: |%s| got: |%s|", test.request.path, test.output.Body, string(body))
}
}
}