-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmuxtagconfig_test.go
206 lines (146 loc) · 4.48 KB
/
muxtagconfig_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
package muxtagconfig
import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/Experticity/tagconfig"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
)
type HackerReferences struct {
Handle string `mux.url:"handle" mux.form:"true"`
SuperComputer string `mux.url:"super.computer" mux.form:"true"`
Rival string `mux.url:"rival" mux.param:"true"`
Display string `mux.url:"type" mux.path:"true"`
}
type SaltyMeats struct {
Bacon []string `mux.url:"bacon" mux.param:"true"`
}
func mockRequest(path string) *http.Request {
req, _ := http.NewRequest("GET", path, nil)
req.Form = url.Values{}
req.PostForm = url.Values{}
return req
}
// This is just to test the wrapper. The rest of the testing is on what it wraps.
func TestParseMuxRequestToStruct(t *testing.T) {
req := mockRequest("/root/period")
handle := "lord.nikon"
req.Form.Add("handle", handle)
hr := &HackerReferences{}
err := ParseMuxRequestToStruct(req, hr)
assert.NoError(t, err)
assert.Equal(t, handle, hr.Handle)
}
func TestMuxURLFormGet(t *testing.T) {
req := mockRequest("/root/period")
handle := "lord.nikon"
req.Form.Add("handle", handle)
hr := &HackerReferences{}
mug := &MuxURLGetter{req}
err := tagconfig.Process(mug, hr)
assert.NoError(t, err)
assert.Equal(t, handle, hr.Handle)
}
func TestMuxURLPostFormGet(t *testing.T) {
req := mockRequest("/root/period")
handle := "lord.nikon"
req.PostForm.Add("handle", handle)
hr := &HackerReferences{}
mug := &MuxURLGetter{req}
err := tagconfig.Process(mug, hr)
assert.NoError(t, err)
assert.Equal(t, handle, hr.Handle)
}
func TestMuxURLParamGet(t *testing.T) {
rival := "acid.burn"
req := mockRequest("/root/period/?rival=" + rival)
hr := &HackerReferences{}
mug := &MuxURLGetter{req}
err := tagconfig.Process(mug, hr)
assert.NoError(t, err)
assert.Equal(t, rival, hr.Rival)
}
func TestMuxMultipleCSVURLParamToSliceGet(t *testing.T) {
bacon := "slice"
moreBacon := "bits"
req := mockRequest("/salty/meats/?bacon=" + bacon + "," + moreBacon)
hr := &SaltyMeats{}
mug := &MuxURLGetter{req}
err := tagconfig.Process(mug, hr)
assert.NoError(t, err)
assert.Equal(t, []string{bacon, moreBacon}, hr.Bacon)
}
func TestMuxMultipleURLParamToSliceGet(t *testing.T) {
bacon := "slice"
moreBacon := "bits"
req := mockRequest("/salty/meats/?bacon=" + bacon + "&bacon=" + moreBacon)
hr := &SaltyMeats{}
mug := &MuxURLGetter{Request: req}
err := tagconfig.Process(mug, hr)
assert.NoError(t, err)
assert.Equal(t, []string{bacon, moreBacon}, hr.Bacon)
}
func TestMuxURLPathVariableGet(t *testing.T) {
r := mux.NewRouter()
r.HandleFunc("/display/{type}", handler(t))
s := httptest.NewServer(r)
defer s.Close()
client := &http.Client{}
displayType := "activeMartix"
res, err := client.Do(mockRequest(s.URL + "/display/" + displayType))
assert.NoError(t, err)
defer res.Body.Close()
assert.NoError(t, err)
var hrRes HackerReferences
err = json.NewDecoder(res.Body).Decode(&hrRes)
assert.NoError(t, err)
assert.Equal(t, displayType, hrRes.Display)
}
func TestMuxURLGetterAll(t *testing.T) {
// Expectations
displayType := "activeMartix"
rival := "crash.override"
handle := "the.plague"
scomp := "gibson"
fv := url.Values{"handle": {handle}}
pfv := url.Values{"super.computer": {scomp}}
r := mux.NewRouter()
r.HandleFunc("/display/{type}/", handlerWithForm(t, fv, pfv))
s := httptest.NewServer(r)
defer s.Close()
u := s.URL + "/display/" + displayType + "/?rival=" + rival
req := mockRequest(u)
client := &http.Client{}
res, err := client.Do(req)
assert.NoError(t, err)
var hrRes HackerReferences
err = json.NewDecoder(res.Body).Decode(&hrRes)
assert.NoError(t, err)
assert.Equal(t, rival, hrRes.Rival)
assert.Equal(t, displayType, hrRes.Display)
}
func handlerWithForm(t *testing.T, formValues, postFormValues url.Values) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
req.Form = formValues
req.PostForm = postFormValues
hr := &HackerReferences{}
mug := &MuxURLGetter{req}
err := tagconfig.Process(mug, hr)
assert.NoError(t, err)
err = json.NewEncoder(w).Encode(hr)
assert.NoError(t, err)
}
}
func handler(t *testing.T) func(w http.ResponseWriter, req *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
hr := &HackerReferences{}
mug := &MuxURLGetter{req}
err := tagconfig.Process(mug, hr)
assert.NoError(t, err)
err = json.NewEncoder(w).Encode(hr)
assert.NoError(t, err)
}
}