-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples_test.go
290 lines (265 loc) · 8.09 KB
/
examples_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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package request
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"github.com/gorilla/mux"
)
func ExampleDecode() {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var req struct {
Active bool `query:"active"`
State string `json:"state"`
Delay int `header:"X-DELAY"`
}
err := Decode(r, &req)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Println(err.Error())
}
fmt.Printf("%+v\n", req)
}))
defer ts.Close()
body := `{"state":"idle"}`
req, _ := http.NewRequest(http.MethodPost, ts.URL+"?active=true", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-DELAY", "60")
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println("request failed")
}
if resp.StatusCode == http.StatusBadRequest {
fmt.Println("decode failed")
}
// Output:
// {Active:true State:idle Delay:60}
}
func ExampleDecode_mux() {
r := mux.NewRouter()
r.Handle("/users/{user}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var req struct {
User string `path:"user"`
Active bool `query:"active"`
State string `json:"state"`
Delay int `header:"X-DELAY"`
}
err := Decode(r, &req)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Println(err.Error())
}
fmt.Printf("%+v\n", req)
}))
body := `{"state":"idle"}`
req, _ := http.NewRequest(http.MethodPost, "http://www.example.com/users/adam?active=true", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-DELAY", "60")
rec := httptest.NewRecorder()
r.ServeHTTP(rec, req)
if rec.Code == http.StatusBadRequest {
fmt.Println("decode failed")
}
// Output:
// {User:adam Active:true State:idle Delay:60}
}
func ExampleDecode_body() {
r := mux.NewRouter()
r.Handle("/users/{user}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var req struct {
Request struct {
State string `json:"state"`
} `body:"application/json"`
Active bool `query:"active"`
Delay int `header:"X-DELAY"`
}
err := Decode(r, &req)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Println(err.Error())
}
fmt.Printf("%+v\n", req)
}))
body := `{"state":"idle"}`
req, _ := http.NewRequest(http.MethodPost, "http://www.example.com/users/adam?active=true", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-DELAY", "60")
rec := httptest.NewRecorder()
r.ServeHTTP(rec, req)
if rec.Code == http.StatusBadRequest {
fmt.Println("decode failed")
}
// Output:
// {Request:{State:idle} Active:true Delay:60}
}
func ExampleDecode_slice() {
r := mux.NewRouter()
r.Handle("/users", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var req struct {
IDs []string `query:"id,explode"`
Triggers []bool `query:"triggers"`
Single []string `query:"single,explode"`
Solitaire []string `query:"solitaire"`
Delays []int `header:"X-DELAY"`
Request []struct {
State string `json:"state"`
} `body:"application/json"`
}
err := Decode(r, &req)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Println(err.Error())
}
fmt.Printf("%+v\n", req)
}))
body := `[{"state":"idle"},{"state":"active"}]`
req, _ := http.NewRequest(http.MethodPost, "http://www.example.com/users?id=adam&id=eve&triggers=true,false,true,false&single=first&solitaire=second", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header["X-Delay"] = []string{"60", "120", "240"}
rec := httptest.NewRecorder()
r.ServeHTTP(rec, req)
if rec.Code == http.StatusBadRequest {
fmt.Println("decode failed")
}
// Output:
// {IDs:[adam eve] Triggers:[true false true false] Single:[first] Solitaire:[second] Delays:[60 120 240] Request:[{State:idle} {State:active}]}
}
func ExampleDecode_multiple() {
r := mux.NewRouter()
r.Handle("/users", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var req struct {
Value string `query:"value" header:"value" json:"value"`
}
err := Decode(r, &req)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Println(err.Error())
}
fmt.Printf("%+v\n", req)
}))
// Override Body
body := `{"value":"body"}`
req, _ := http.NewRequest(http.MethodPost, "http://www.example.com/users?value=query", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("value", "header")
rec := httptest.NewRecorder()
r.ServeHTTP(rec, req)
if rec.Code == http.StatusBadRequest {
fmt.Println("decode failed")
}
// Fallback Header
body = `{}`
req, _ = http.NewRequest(http.MethodPost, "http://www.example.com/users?value=query", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("value", "header")
rec = httptest.NewRecorder()
r.ServeHTTP(rec, req)
if rec.Code == http.StatusBadRequest {
fmt.Println("decode failed")
}
// Fallback Query
body = `{}`
req, _ = http.NewRequest(http.MethodPost, "http://www.example.com/users?value=query", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
rec = httptest.NewRecorder()
r.ServeHTTP(rec, req)
if rec.Code == http.StatusBadRequest {
fmt.Println("decode failed")
}
// Fallback Empty
body = `{}`
req, _ = http.NewRequest(http.MethodPost, "http://www.example.com/users", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
rec = httptest.NewRecorder()
r.ServeHTTP(rec, req)
if rec.Code == http.StatusBadRequest {
fmt.Println("decode failed")
}
// Output:
// {Value:body}
// {Value:header}
// {Value:query}
// {Value:}
}
func ExampleDecode_embedded() {
r := mux.NewRouter()
r.Handle("/users", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var req struct {
Request struct {
Active bool `query:"active"`
State string `json:"state"`
Delay int `header:"X-DELAY"`
} `body:"application/json"`
}
err := Decode(r, &req)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Println(err.Error())
}
fmt.Printf("%+v\n", req)
}))
body := `{"state":"idle"}`
req, _ := http.NewRequest(http.MethodPost, "http://www.example.com/users?active=true", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Delay", "60")
rec := httptest.NewRecorder()
r.ServeHTTP(rec, req)
if rec.Code == http.StatusBadRequest {
fmt.Println("decode failed")
}
// Output:
// {Request:{Active:true State:idle Delay:60}}
}
func ExampleDecode_pointers() {
r := mux.NewRouter()
r.Handle("/users", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var req struct {
Request struct {
Active *bool `query:"active"`
NilActive *bool `query:"nonactive"`
State *string `json:"state"`
NilState *string `json:"nilstate"`
Delay *int `header:"X-DELAY"`
NilDelay *int `header:"X-NIL-DELAY"`
} `body:"application/json"`
}
err := Decode(r, &req)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Println(err.Error())
}
printPtr := func(ptr any) string {
switch p := ptr.(type) {
case *string:
if p == nil {
return "nil"
}
return *p
case *bool:
if p == nil {
return "nil"
}
return fmt.Sprintf("%t", *p)
case *int:
if p == nil {
return "nil"
}
return fmt.Sprintf("%d", *p)
}
return ""
}
fmt.Printf("{Request:{Active:%s NilActive:%s State:%s NilState:%s Delay:%s NilDelay:%s}}\n",
printPtr(req.Request.Active), printPtr(req.Request.NilActive), printPtr(req.Request.State), printPtr(req.Request.NilState), printPtr(req.Request.Delay), printPtr(req.Request.NilDelay))
}))
body := `{"state":"idle"}`
req, _ := http.NewRequest(http.MethodPost, "http://www.example.com/users?active=true", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Delay", "60")
rec := httptest.NewRecorder()
r.ServeHTTP(rec, req)
if rec.Code == http.StatusBadRequest {
fmt.Println("decode failed")
}
// Output:
// {Request:{Active:true NilActive:nil State:idle NilState:nil Delay:60 NilDelay:nil}}
}