-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes_test.go
206 lines (181 loc) · 5.86 KB
/
routes_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright 2013 Justin Wilson. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package dingo
import (
"bytes"
"fmt"
"net/http"
"net/url"
"testing"
)
/*-------------------------Helpers-------------------------*/
func wc(ctx Context, s string) {
ctx.Response.Write([]byte(s))
}
func dummyHandler(ctx Context) {}
func dummyHandler_0(ctx Context) { wc(ctx, ctx.Request.URL.Path) }
func dummyHandler_1(ctx Context, a string) { wc(ctx, fmt.Sprintf("/%s/", a)) }
func dummyHandler_2(ctx Context, a, b string) { wc(ctx, fmt.Sprintf("/%s/%s/", a, b)) }
func dummyHandler_3(ctx Context, a, b, c string) { wc(ctx, fmt.Sprintf("/%s/%s/%s/", a, b, c)) }
type rePath struct {
ReExp, Path string
}
type sPath struct {
RawPath, Path string
}
func sroutes(m map[sPath]Handler) map[string]Route {
r := make(map[string]Route)
for k, v := range m {
r[k.RawPath] = NewSRoute(k.Path, v)
}
return r
}
func reroutes(m map[rePath]Handler) map[string]Route {
r := make(map[string]Route)
for k, v := range m {
r[k.Path] = NewReRoute(k.ReExp, v)
}
return r
}
func rroutes(m map[rePath]interface{}) map[string]Route {
r := make(map[string]Route)
for k, v := range m {
r[k.Path] = NewRRoute(k.ReExp, v)
}
return r
}
func dummyRequest(path string) *http.Request {
req := &http.Request{Method: "GET"}
req.URL, _ = url.Parse("http://www.juzt.in" + path)
return req
}
type dummyWriter struct {
buf *bytes.Buffer
}
func (w dummyWriter) Header() http.Header {
return nil
}
func (w dummyWriter) Write(data []byte) (int, error) {
return w.buf.Write(data)
}
func (w dummyWriter) WriteHeader(status int) {}
func (w dummyWriter) String() string {
return w.buf.String()
}
/*-------------------------Data-------------------------*/
var srMatchCol = map[sPath]Handler{
sPath{"/", "/"}: dummyHandler_0,
sPath{"/blog/", "/blog/"}: dummyHandler_0,
sPath{"/a/b/c/", "/a/b/c/"}: dummyHandler_0,
sPath{"/some-thing-cool/", "/some-thing-cool/"}: dummyHandler_0,
}
var srNoMatchCol = map[sPath]Handler{
sPath{"/no/", "/"}: dummyHandler_0,
sPath{"/blog", "/blog/"}: dummyHandler_0,
sPath{"/a/b/c", "/a/b/c/"}: dummyHandler_0,
sPath{"/some-thing-cool", "/some-thing-cool/"}: dummyHandler_0,
}
var reMatchCol = map[rePath]Handler{
rePath{"^/$", "/"}: dummyHandler_0,
rePath{"^/blog/$", "/blog/"}: dummyHandler_0,
rePath{"^/a/b/c/$", "/a/b/c/"}: dummyHandler_0,
rePath{"^/some-thing-cool/$", "/some-thing-cool/"}: dummyHandler_0,
}
var reNoMatchCol = map[rePath]Handler{
rePath{"^/$", ""}: dummyHandler_0,
rePath{"^/blog$", "/blog/"}: dummyHandler_0,
rePath{"^/a/b/c$", "/a/b/c/"}: dummyHandler_0,
rePath{"^/some-thing-cool$", "/some-thing-cool/"}: dummyHandler_0,
}
var rrMatchCol = map[rePath]interface{}{
rePath{"^/$", "/"}: dummyHandler_0,
rePath{"^/blog/$", "/blog/"}: dummyHandler_0,
rePath{"^/a/b/c/$", "/a/b/c/"}: dummyHandler_0,
rePath{"^/some-thing-cool/$", "/some-thing-cool/"}: dummyHandler_0,
}
var rrNoMatchCol = map[rePath]interface{}{
rePath{"^/$", ""}: dummyHandler_0,
rePath{"^/blog$", "/blog/"}: dummyHandler_0,
rePath{"^/a/b/c$", "/a/b/c/"}: dummyHandler_0,
rePath{"^/some-thing-cool$", "/some-thing-cool/"}: dummyHandler_0,
}
var rrArgCol = map[rePath]interface{}{
rePath{"^/$", "/"}: dummyHandler_0,
rePath{"^/(.*)/$", "/a/"}: dummyHandler_1,
rePath{"^/(.*)/(.*)/$", "/a/b/"}: dummyHandler_2,
rePath{"^/(.*)/(.*)/(.*)/$", "/a/b/c/"}: dummyHandler_3,
}
/*-------------------------Tests-------------------------*/
func TestSRouteMatches(t *testing.T) {
sr := sroutes(srMatchCol)
for path, r := range sr {
if !r.Matches(path) {
t.Errorf("Route doesn't match it's path: (%s) != (%s)", r.Path(), path)
}
}
}
func TestSRouteNoMatches(t *testing.T) {
sr := sroutes(srNoMatchCol)
for path, r := range sr {
if r.Matches(path) {
t.Errorf("Route doesn't match it's path: (%s) != (%s)", r.Path(), path)
}
}
}
func TestSRouteExecute(t *testing.T) {
sr := sroutes(srMatchCol)
for path, r := range sr {
ctx := *new(Context)
ctx.Response = dummyWriter{new(bytes.Buffer)}
ctx.Request = dummyRequest(path)
r.Execute(ctx)
if ctx.Response.(dummyWriter).String() != r.Path() {
t.Errorf("Handler recieved non-matching args: %v != %s", ctx.Response, r.Path())
}
}
}
func TestReRouteMatches(t *testing.T) {
re := reroutes(reMatchCol)
for path, r := range re {
if !r.Matches(path) {
t.Errorf("Route doesn't match it's path: (%s) != (%s)", r.Path(), path)
}
}
}
func TestReRouteNoMatches(t *testing.T) {
re := reroutes(reNoMatchCol)
for path, r := range re {
if r.Matches(path) {
t.Errorf("Route matches invalid path: (%s) == (%s)", r.Path(), path)
}
}
}
func TestRRouteMatches(t *testing.T) {
rr := rroutes(rrMatchCol)
for path, r := range rr {
if !r.Matches(path) {
t.Errorf("Route doesn't match it's path: (%s) != (%s)", r.Path(), path)
}
}
}
func TestRRouteNoMatches(t *testing.T) {
rr := rroutes(rrNoMatchCol)
for path, r := range rr {
if r.Matches(path) {
t.Errorf("Route matches invalid path: (%s) == (%s)", r.Path(), path)
}
}
}
func TestRRRouteExecute(t *testing.T) {
rr := rroutes(rrArgCol)
for path, r := range rr {
ctx := *new(Context)
ctx.Response = dummyWriter{new(bytes.Buffer)}
ctx.Request = dummyRequest(path)
r.Execute(ctx)
if ctx.Response.(dummyWriter).String() != path {
t.Errorf("Handler recieved non-matching args: %v != %s", ctx.Response, path)
}
}
}