-
Notifications
You must be signed in to change notification settings - Fork 14
/
matchers_test.go
188 lines (178 loc) · 4.27 KB
/
matchers_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
// Copyright 2012 The Gorilla Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package reverse
import (
"net/http"
"net/url"
"testing"
)
func equalStringSlice(s1, s2 []string) bool {
if len(s1) != len(s2) {
return false
}
for k, v := range s1 {
if s2[k] != v {
return false
}
}
return true
}
func equalValues(u1, u2 url.Values) bool {
if len(u1) != len(u2) {
return false
}
for k, v := range u1 {
if !equalStringSlice(v, u2[k]) {
return false
}
}
return true
}
func testMatcher(t *testing.T, name string, m Matcher, r *http.Request, expect bool) {
result := m.Match(r)
if result != expect {
t.Errorf("%s: got %v, expected %v", name, result, expect)
}
}
func TestHost(t *testing.T) {
const name = "Host"
type test struct {
host string
rURL string
expect bool
}
tests := []test{
{"domain.com", "http://domain.com", true},
{"domain.com", "http://other.com", false},
}
for _, v := range tests {
r, err := http.NewRequest("GET", v.rURL, nil)
if err != nil {
t.Fatal(err)
}
testMatcher(t, name, NewHost(v.host), r, v.expect)
}
}
func TestMethod(t *testing.T) {
const name = "Method"
type test struct {
methods []string
rMethod string
expect bool
}
tests := []test{
{[]string{"GET", "POST"}, "GET", true},
{[]string{"GET", "POST"}, "POST", true},
{[]string{"get", "post"}, "GET", true},
{[]string{"get", "post"}, "POST", true},
{[]string{"POST", "PUT"}, "GET", false},
}
for _, v := range tests {
r, err := http.NewRequest(v.rMethod, "http://domain.com", nil)
if err != nil {
t.Fatal(err)
}
testMatcher(t, name, NewMethod(v.methods), r, v.expect)
}
}
func TestRegexpHost(t *testing.T) {
const name = "RegexpHost"
type test struct {
host string
rURL string
expect bool
values url.Values
}
tests := []test{
{`(?P<subdomain>[a-z]+)\.domain\.com`, "http://sub.domain.com", true, url.Values{"subdomain": {"sub"}}},
{`(?P<subdomain>[a-z]+)\.domain\.com`, "http://123.domain.com", false, nil},
}
for _, v := range tests {
r, err := http.NewRequest("GET", v.rURL, nil)
if err != nil {
t.Fatal(err)
}
matcher, err := NewRegexpHost(v.host)
if err != nil {
t.Fatal(err)
}
testMatcher(t, name, matcher, r, v.expect)
result := Result{}
matcher.Extract(&result, r)
if v.expect {
if !equalValues(v.values, result.Values) {
t.Errorf("%s: expected %v, got %v", name, v.values, result.Values)
}
u := url.URL{}
if err := matcher.Build(&u, result.Values); err != nil {
t.Errorf("%s: error building URL", name)
} else {
u2, _ := url.Parse(v.rURL)
if u.Host != u2.Host {
t.Errorf("%s: expected %q, got %q", name, u2.Host, u.Host)
}
}
}
}
}
func TestRegexpPath(t *testing.T) {
const name = "RegexpPath"
type test struct {
path string
rURL string
expect bool
values url.Values
}
tests := []test{
{`/(?P<abc>[a-z]+)/(?P<ghi>[a-z]+)`, "http://domain.com/def/jkl", true, url.Values{"abc": {"def"}, "ghi": {"jkl"}}},
{`/(?P<abc>[a-z]+)/(?P<ghi>[a-z]+)`, "http://domain.com/123/456", false, nil},
}
for _, v := range tests {
r, err := http.NewRequest("GET", v.rURL, nil)
if err != nil {
t.Fatal(err)
}
matcher, err := NewRegexpPath(v.path)
if err != nil {
t.Fatal(err)
}
testMatcher(t, name, matcher, r, v.expect)
result := Result{}
matcher.Extract(&result, r)
if v.expect {
if !equalValues(v.values, result.Values) {
t.Errorf("%s: expected %v, got %v", name, v.values, result.Values)
}
u := url.URL{}
if err := matcher.Build(&u, result.Values); err != nil {
t.Errorf("%s: error building URL", name)
} else {
u2, _ := url.Parse(v.rURL)
if u.Path != u2.Path {
t.Errorf("%s: expected %q, got %q", name, u2.Path, u.Path)
}
}
}
}
}
func TestScheme(t *testing.T) {
const name = "Scheme"
type test struct {
schemes []string
rURL string
expect bool
}
tests := []test{
{[]string{"http", "https"}, "http://domain.com", true},
{[]string{"http", "https"}, "https://domain.com", true},
{[]string{"https"}, "http://domain.com", false},
}
for _, v := range tests {
r, err := http.NewRequest("GET", v.rURL, nil)
if err != nil {
t.Fatal(err)
}
testMatcher(t, name, NewScheme(v.schemes), r, v.expect)
}
}