-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmux_test.go
156 lines (124 loc) · 3.51 KB
/
mux_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
package muxie
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
func expect(t *testing.T, method, url string, testieOptions ...func(*http.Request)) *testie {
req, err := http.NewRequest(method, url, nil)
if err != nil {
t.Fatal(err)
}
for _, opt := range testieOptions {
opt(req)
}
return testReq(t, req)
}
func withHeader(key string, value string) func(*http.Request) {
return func(r *http.Request) {
r.Header.Add(key, value)
}
}
func withURLParam(key string, value string) func(*http.Request) {
return func(r *http.Request) {
r.URL.Query().Add(key, value)
}
}
func withFormField(key string, value string) func(*http.Request) {
return func(r *http.Request) {
if r.Form == nil {
r.Form = make(url.Values)
}
r.Form.Add(key, value)
enc := strings.NewReader(r.Form.Encode())
r.Body = ioutil.NopCloser(enc)
r.ContentLength = int64(enc.Len())
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
}
func expectWithBody(t *testing.T, method, url string, body string, headers http.Header) *testie {
req, err := http.NewRequest(method, url, bytes.NewBufferString(body))
if err != nil {
t.Fatal(err)
}
if len(headers) > 0 {
req.Header = http.Header{}
for k, v := range headers {
req.Header[k] = v
}
}
return testReq(t, req)
}
func testReq(t *testing.T, req *http.Request) *testie {
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
resp.Request = req
return &testie{t: t, resp: resp}
}
func testHandler(t *testing.T, handler http.Handler, method, url string) *testie {
w := httptest.NewRecorder()
req := httptest.NewRequest(method, url, nil)
handler.ServeHTTP(w, req)
resp := w.Result()
resp.Request = req
return &testie{t: t, resp: resp}
}
type testie struct {
t *testing.T
resp *http.Response
}
func (te *testie) statusCode(expected int) *testie {
if got := te.resp.StatusCode; expected != got {
te.t.Fatalf("%s: expected status code: %d but got %d", te.resp.Request.URL, expected, got)
}
return te
}
func (te *testie) bodyEq(expected string) *testie {
b, err := ioutil.ReadAll(te.resp.Body)
te.resp.Body.Close()
if err != nil {
te.t.Fatal(err)
}
if got := string(b); expected != got {
te.t.Fatalf("%s: expected to receive '%s' but got '%s'", te.resp.Request.URL, expected, got)
}
return te
}
func (te *testie) headerEq(key, expected string) *testie {
if got := te.resp.Header.Get(key); expected != got {
te.t.Fatalf("%s: expected header value of %s to be: '%s' but got '%s'", te.resp.Request.URL, key, expected, got)
}
return te
}
func TestMuxPathCorrection(t *testing.T) {
mux := NewMux()
mux.PathCorrection = true
mux.HandleFunc("/hello/here", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello %s", r.URL.Query().Get("name"))
})
srv := httptest.NewServer(mux)
defer srv.Close()
expect(t, http.MethodGet, srv.URL+"/hello//here/?name=kataras").bodyEq("Hello kataras")
}
func TestMuxOf(t *testing.T) {
printPathHandler := func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Handler of %s", r.URL.Path)
}
mux := NewMux()
mux.HandleFunc("/", printPathHandler)
v1 := mux.Of("/v1") // or "/v1/" or even "v1"
v1.HandleFunc("/", printPathHandler)
v1.HandleFunc("/hello", printPathHandler)
srv := httptest.NewServer(mux)
defer srv.Close()
expect(t, http.MethodGet, srv.URL).bodyEq("Handler of /")
expect(t, http.MethodGet, srv.URL+"/v1").bodyEq("Handler of /v1")
expect(t, http.MethodGet, srv.URL+"/v1/hello").bodyEq("Handler of /v1/hello")
}