Debian Codegraph @

helpers/tdhttp/request_test.go

raw
1 // Copyright (c) 2019-2023, Maxime Soulé
2 // All rights reserved.
3 //
4 // This source code is licensed under the BSD-style license found in the
5 // LICENSE file in the root directory of this source tree.
6
7 package tdhttp_test
8
9 import (
10 "errors"
11 "io"
12 "net/http"
13 "net/url"
14 "testing"
15
16 "github.com/maxatome/go-testdeep/helpers/tdhttp"
17 "github.com/maxatome/go-testdeep/td"
18 )
19
20 func TestBasicAuthHeader(t *testing.T) {
21 td.Cmp(t,
22 tdhttp.BasicAuthHeader("max", "5ecr3T"),
23 http.Header{"Authorization": {"Basic bWF4OjVlY3IzVA=="}})
24 }
25
26 func TestNewRequest(tt *testing.T) {
27 t := td.NewT(tt)
28
29 t.Run("NewRequest", func(t *td.T) {
30 req := tdhttp.NewRequest("GET", "/path", nil,
31 "Foo", "Bar",
32 "Zip", "Test")
33
34 t.Cmp(req.Header, http.Header{
35 "Foo": {"Bar"},
36 "Zip": {"Test"},
37 })
38 })
39
40 t.Run("NewRequest last header value less", func(t *td.T) {
41 req := tdhttp.NewRequest("GET", "/path", nil,
42 "Foo", "Bar",
43 "Zip")
44
45 t.Cmp(req.Header, http.Header{
46 "Foo": {"Bar"},
47 "Zip": {""},
48 })
49 })
50
51 t.Run("NewRequest header http.Header", func(t *td.T) {
52 req := tdhttp.NewRequest("GET", "/path", nil,
53 http.Header{
54 "Foo": {"Bar"},
55 "Zip": {"Test"},
56 })
57
58 t.Cmp(req.Header, http.Header{
59 "Foo": {"Bar"},
60 "Zip": {"Test"},
61 })
62 })
63
64 t.Run("NewRequest header http.Cookie", func(t *td.T) {
65 req := tdhttp.NewRequest("GET", "/path", nil,
66 &http.Cookie{Name: "cook1", Value: "val1"},
67 http.Cookie{Name: "cook2", Value: "val2"},
68 []*http.Cookie{
69 {Name: "cook3", Value: "val3"},
70 {Name: "cook4", Value: "val4"},
71 },
72 )
73
74 t.Cmp(req.Header, http.Header{
75 "Cookie": {"cook1=val1; cook2=val2; cook3=val3; cook4=val4"},
76 })
77 })
78
79 t.Run("NewRequest header flattened", func(t *td.T) {
80 req := tdhttp.NewRequest("GET", "/path", nil,
81 td.Flatten([]string{
82 "Foo", "Bar",
83 "Zip", "Test",
84 }),
85 td.Flatten(map[string]string{
86 "Pipo": "Bingo",
87 "Hey": "Yo",
88 }),
89 )
90
91 t.Cmp(req.Header, http.Header{
92 "Foo": {"Bar"},
93 "Zip": {"Test"},
94 "Pipo": {"Bingo"},
95 "Hey": {"Yo"},
96 })
97 })
98
99 t.Run("NewRequest header combined", func(t *td.T) {
100 req := tdhttp.NewRequest("GET", "/path", nil,
101 "H1", "V1",
102 http.Header{
103 "H1": {"V2"},
104 "H2": {"V1", "V2"},
105 },
106 "H2", "V3",
107 td.Flatten([]string{
108 "H2", "V4",
109 "H3", "V1",
110 "H3", "V2",
111 }),
112 td.Flatten(map[string]string{
113 "H2": "V5",
114 "H3": "V3",
115 }),
116 )
117
118 t.Cmp(req.Header, http.Header{
119 "H1": {"V1", "V2"},
120 "H2": {"V1", "V2", "V3", "V4", "V5"},
121 "H3": {"V1", "V2", "V3"},
122 })
123 })
124
125 t.Run("NewRequest and query params", func(t *td.T) {
126 req := tdhttp.NewRequest("GET", "/path", nil,
127 url.Values{"p1": {"a", "b"}},
128 url.Values{"p2": {"a", "b"}},
129 tdhttp.Q{"p1": "c", "p2": []string{"c", "d"}},
130 tdhttp.Q{"p1": 123, "p3": true},
131 )
132
133 t.Cmp(req.URL.String(), "/path?p1=a&p1=b&p1=c&p1=123&p2=a&p2=b&p2=c&p2=d&p3=true")
134
135 // Query param already set in path
136 req = tdhttp.NewRequest("GET", "/path?already=true", nil,
137 tdhttp.Q{"p1": 123, "p3": true},
138 )
139 t.Cmp(req.URL.String(), "/path?already=true&p1=123&p3=true")
140 })
141
142 t.Run("NewRequest host", func(t *td.T) {
143 req := tdhttp.NewRequest("GET", "https://pipo.com:123/path", nil)
144 td.Cmp(t, req.Host, "pipo.com:123")
145 td.Cmp(t, req.URL, td.String("/path"))
146
147 req = tdhttp.NewRequest("GET", "/path", nil, "Host", "pipo.com:456")
148 td.Cmp(t, req.Host, "pipo.com:456")
149 td.Cmp(t, req.URL, td.String("/path"))
150
151 req = tdhttp.NewRequest(
152 "GET", "https://pipo.com:123/path", nil,
153 "Host", "bingo.com:456")
154 td.Cmp(t, req.Host, "pipo.com:123", "URL wins")
155 td.Cmp(t, req.URL, td.String("/path"))
156 })
157
158 t.Run("NewRequest and hooks", func(t *td.T) {
159 req := tdhttp.NewRequest("GET", "/path", nil,
160 func(req *http.Request) error {
161 req.Header.Set("H1", "V1")
162 return nil
163 },
164 func(req *http.Request) error {
165 req.Header.Set("H2", "V2")
166 return nil
167 })
168
169 t.Cmp(req.Header, http.Header{
170 "H1": {"V1"},
171 "H2": {"V2"},
172 })
173 })
174
175 t.Run("NewRequest panics", func(t *td.T) {
176 t.CmpPanic(
177 func() { tdhttp.NewRequest("GET", "/path", nil, "H", "V", true) },
178 td.HasPrefix("newRequestParams... can only contains string, http.Header, ([]*|*|)http.Cookie, url.Values, tdhttp.Q and func(*http.Request) error, not bool (@ newRequestParams[2])"))
179
180 t.CmpPanic(
181 func() { tdhttp.NewRequest("GET", "/path", nil, "H1", true) },
182 td.HasPrefix(`header "H1" should have a string value, not a bool (@ newRequestParams[1])`))
183
184 // Hook error
185 t.CmpPanic(
186 func() {
187 tdhttp.NewRequest("GET", "/path", nil, func(*http.Request) error {
188 return errors.New("hook error")
189 })
190 },
191 td.String("hook failed: hook error"))
192
193 t.CmpPanic(
194 func() { tdhttp.Get("/path", true) },
195 td.HasPrefix("newRequestParams... can only contains string, http.Header, ([]*|*|)http.Cookie, url.Values, tdhttp.Q and func(*http.Request) error, not bool (@ newRequestParams[0])"))
196
197 t.CmpPanic(
198 func() { tdhttp.Head("/path", true) },
199 td.HasPrefix("newRequestParams... can only contains string, http.Header, ([]*|*|)http.Cookie, url.Values, tdhttp.Q and func(*http.Request) error, not bool (@ newRequestParams[0])"))
200
201 t.CmpPanic(
202 func() { tdhttp.Options("/path", nil, true) },
203 td.HasPrefix("newRequestParams... can only contains string, http.Header, ([]*|*|)http.Cookie, url.Values, tdhttp.Q and func(*http.Request) error, not bool (@ newRequestParams[0])"))
204
205 t.CmpPanic(
206 func() { tdhttp.Post("/path", nil, true) },
207 td.HasPrefix("newRequestParams... can only contains string, http.Header, ([]*|*|)http.Cookie, url.Values, tdhttp.Q and func(*http.Request) error, not bool (@ newRequestParams[0])"))
208
209 t.CmpPanic(
210 func() { tdhttp.PostForm("/path", nil, true) },
211 td.HasPrefix("newRequestParams... can only contains string, http.Header, ([]*|*|)http.Cookie, url.Values, tdhttp.Q and func(*http.Request) error, not bool (@ newRequestParams[0])"))
212
213 t.CmpPanic(
214 func() { tdhttp.PostMultipartFormData("/path", &tdhttp.MultipartBody{}, true) },
215 td.HasPrefix("newRequestParams... can only contains string, http.Header, ([]*|*|)http.Cookie, url.Values, tdhttp.Q and func(*http.Request) error, not bool (@ newRequestParams[0])"))
216
217 t.CmpPanic(
218 func() { tdhttp.Patch("/path", nil, true) },
219 td.HasPrefix("newRequestParams... can only contains string, http.Header, ([]*|*|)http.Cookie, url.Values, tdhttp.Q and func(*http.Request) error, not bool (@ newRequestParams[0])"))
220
221 t.CmpPanic(
222 func() { tdhttp.Put("/path", nil, true) },
223 td.HasPrefix("newRequestParams... can only contains string, http.Header, ([]*|*|)http.Cookie, url.Values, tdhttp.Q and func(*http.Request) error, not bool (@ newRequestParams[0])"))
224
225 t.CmpPanic(
226 func() { tdhttp.Delete("/path", nil, true) },
227 td.HasPrefix("newRequestParams... can only contains string, http.Header, ([]*|*|)http.Cookie, url.Values, tdhttp.Q and func(*http.Request) error, not bool (@ newRequestParams[0])"))
228
229 // Bad target
230 t.CmpPanic(
231 func() { tdhttp.NewRequest("GET", ":/badpath", nil) },
232 td.HasPrefix(`target is not a valid path: `))
233
234 // Q error
235 t.CmpPanic(
236 func() { tdhttp.Get("/", tdhttp.Q{"bad": map[string]bool{}}) },
237 td.HasPrefix(`newRequestParams... tdhttp.Q bad parameter: don't know how to add type map[string]bool (map) to param "bad" (@ newRequestParams[0])`))
238 })
239
240 // Get
241 t.Cmp(tdhttp.Get("/path", "Foo", "Bar"),
242 td.Struct(
243 &http.Request{
244 Method: "GET",
245 Header: http.Header{"Foo": {"Bar"}},
246 },
247 td.StructFields{
248 "URL": td.String("/path"),
249 }))
250
251 // Head
252 t.Cmp(tdhttp.Head("/path", "Foo", "Bar"),
253 td.Struct(
254 &http.Request{
255 Method: "HEAD",
256 Header: http.Header{"Foo": {"Bar"}},
257 },
258 td.StructFields{
259 "URL": td.String("/path"),
260 }))
261
262 // Options
263 t.Cmp(tdhttp.Options("/path", nil, "Foo", "Bar"),
264 td.Struct(
265 &http.Request{
266 Method: "OPTIONS",
267 Header: http.Header{"Foo": {"Bar"}},
268 },
269 td.StructFields{
270 "URL": td.String("/path"),
271 }))
272
273 // Post
274 t.Cmp(tdhttp.Post("/path", nil, "Foo", "Bar"),
275 td.Struct(
276 &http.Request{
277 Method: "POST",
278 Header: http.Header{"Foo": {"Bar"}},
279 },
280 td.StructFields{
281 "URL": td.String("/path"),
282 }))
283
284 // PostForm - url.Values
285 t.Cmp(
286 tdhttp.PostForm("/path",
287 url.Values{
288 "param1": {"val1", "val2"},
289 "param2": {"zip"},
290 },
291 "Foo", "Bar"),
292 td.Struct(
293 &http.Request{
294 Method: "POST",
295 Header: http.Header{
296 "Content-Type": {"application/x-www-form-urlencoded"},
297 "Foo": {"Bar"},
298 },
299 },
300 td.StructFields{
301 "URL": td.String("/path"),
302 "Body": td.Smuggle(
303 io.ReadAll,
304 []byte("param1=val1&param1=val2&param2=zip"),
305 ),
306 }))
307
308 // PostForm - tdhttp.Q
309 t.Cmp(
310 tdhttp.PostForm("/path",
311 tdhttp.Q{
312 "param1": "val1",
313 "param2": "val2",
314 },
315 "Foo", "Bar"),
316 td.Struct(
317 &http.Request{
318 Method: "POST",
319 Header: http.Header{
320 "Content-Type": {"application/x-www-form-urlencoded"},
321 "Foo": {"Bar"},
322 },
323 },
324 td.StructFields{
325 "URL": td.String("/path"),
326 "Body": td.Smuggle(
327 io.ReadAll,
328 []byte("param1=val1&param2=val2"),
329 ),
330 }))
331
332 // PostForm - nil data
333 t.Cmp(
334 tdhttp.PostForm("/path", nil, "Foo", "Bar"),
335 td.Struct(
336 &http.Request{
337 Method: "POST",
338 Header: http.Header{
339 "Content-Type": {"application/x-www-form-urlencoded"},
340 "Foo": {"Bar"},
341 },
342 },
343 td.StructFields{
344 "URL": td.String("/path"),
345 "Body": td.Smuggle(
346 io.ReadAll,
347 []byte{},
348 ),
349 }))
350
351 // PostMultipartFormData
352 req := tdhttp.PostMultipartFormData("/path",
353 &tdhttp.MultipartBody{
354 Boundary: "BoUnDaRy",
355 Parts: []*tdhttp.MultipartPart{
356 tdhttp.NewMultipartPartString("p1", "body1!"),
357 tdhttp.NewMultipartPartString("p2", "body2!"),
358 },
359 },
360 "Foo", "Bar")
361 t.Cmp(req,
362 td.Struct(
363 &http.Request{
364 Method: "POST",
365 Header: http.Header{
366 "Content-Type": {`multipart/form-data; boundary="BoUnDaRy"`},
367 "Foo": {"Bar"},
368 },
369 },
370 td.StructFields{
371 "URL": td.String("/path"),
372 }))
373 if t.CmpNoError(req.ParseMultipartForm(10000)) {
374 t.Cmp(req.PostFormValue("p1"), "body1!")
375 t.Cmp(req.PostFormValue("p2"), "body2!")
376 }
377
378 // Put
379 t.Cmp(tdhttp.Put("/path", nil, "Foo", "Bar"),
380 td.Struct(
381 &http.Request{
382 Method: "PUT",
383 Header: http.Header{"Foo": {"Bar"}},
384 },
385 td.StructFields{
386 "URL": td.String("/path"),
387 }))
388
389 // Patch
390 t.Cmp(tdhttp.Patch("/path", nil, "Foo", "Bar"),
391 td.Struct(
392 &http.Request{
393 Method: "PATCH",
394 Header: http.Header{"Foo": {"Bar"}},
395 },
396 td.StructFields{
397 "URL": td.String("/path"),
398 }))
399
400 // Delete
401 t.Cmp(tdhttp.Delete("/path", nil, "Foo", "Bar"),
402 td.Struct(
403 &http.Request{
404 Method: "DELETE",
405 Header: http.Header{"Foo": {"Bar"}},
406 },
407 td.StructFields{
408 "URL": td.String("/path"),
409 }))
410 }
411
412 type TestStruct struct {
413 Name string `json:"name" xml:"name"`
414 }
415
416 func TestNewJSONRequest(tt *testing.T) {
417 t := td.NewT(tt)
418
419 t.Run("NewJSONRequest", func(t *td.T) {
420 req := tdhttp.NewJSONRequest("GET", "/path",
421 TestStruct{
422 Name: "Bob",
423 },
424 "Foo", "Bar",
425 "Zip", "Test")
426
427 t.String(req.Header.Get("Content-Type"), "application/json")
428 t.String(req.Header.Get("Foo"), "Bar")
429 t.String(req.Header.Get("Zip"), "Test")
430
431 body, err := io.ReadAll(req.Body)
432 if t.CmpNoError(err, "read request body") {
433 t.String(string(body), `{"name":"Bob"}`)
434 }
435 })
436
437 t.Run("NewJSONRequest panic", func(t *td.T) {
438 t.CmpPanic(
439 func() { tdhttp.NewJSONRequest("GET", "/path", func() {}) },
440 td.Contains("json: unsupported type: func()"))
441
442 t.CmpPanic(
443 func() { tdhttp.PostJSON("/path", func() {}) },
444 td.Contains("json: unsupported type: func()"))
445
446 t.CmpPanic(
447 func() { tdhttp.PutJSON("/path", func() {}) },
448 td.Contains("json: unsupported type: func()"))
449
450 t.CmpPanic(
451 func() { tdhttp.PatchJSON("/path", func() {}) },
452 td.Contains("json: unsupported type: func()"))
453
454 t.CmpPanic(
455 func() { tdhttp.DeleteJSON("/path", func() {}) },
456 td.Contains("json: unsupported type: func()"))
457
458 t.CmpPanic(
459 func() { tdhttp.NewJSONRequest("GET", "/path", td.JSONPointer("/a", 0)) },
460 td.Contains("JSON encoding failed: json: error calling MarshalJSON for type *td.tdJSONPointer: JSONPointer TestDeep operator cannot be json.Marshal'led"))
461
462 // Common user mistake
463 t.CmpPanic(
464 func() { tdhttp.NewJSONRequest("GET", "/path", td.JSON(`{}`)) },
465 td.Contains(`JSON encoding failed: json: error calling MarshalJSON for type *td.tdJSON: JSON TestDeep operator cannot be json.Marshal'led, use json.RawMessage() instead`))
466 })
467
468 // Post
469 t.Cmp(tdhttp.PostJSON("/path", 42, "Foo", "Bar"),
470 td.Struct(
471 &http.Request{
472 Method: "POST",
473 Header: http.Header{
474 "Foo": {"Bar"},
475 "Content-Type": {"application/json"},
476 },
477 },
478 td.StructFields{
479 "URL": td.String("/path"),
480 }))
481
482 // Put
483 t.Cmp(tdhttp.PutJSON("/path", 42, "Foo", "Bar"),
484 td.Struct(
485 &http.Request{
486 Method: "PUT",
487 Header: http.Header{
488 "Foo": {"Bar"},
489 "Content-Type": {"application/json"},
490 },
491 },
492 td.StructFields{
493 "URL": td.String("/path"),
494 }))
495
496 // Patch
497 t.Cmp(tdhttp.PatchJSON("/path", 42, "Foo", "Bar"),
498 td.Struct(
499 &http.Request{
500 Method: "PATCH",
501 Header: http.Header{
502 "Foo": {"Bar"},
503 "Content-Type": {"application/json"},
504 },
505 },
506 td.StructFields{
507 "URL": td.String("/path"),
508 }))
509
510 // Delete
511 t.Cmp(tdhttp.DeleteJSON("/path", 42, "Foo", "Bar"),
512 td.Struct(
513 &http.Request{
514 Method: "DELETE",
515 Header: http.Header{
516 "Foo": {"Bar"},
517 "Content-Type": {"application/json"},
518 },
519 },
520 td.StructFields{
521 "URL": td.String("/path"),
522 }))
523 }
524
525 func TestNewXMLRequest(tt *testing.T) {
526 t := td.NewT(tt)
527
528 t.Run("NewXMLRequest", func(t *td.T) {
529 req := tdhttp.NewXMLRequest("GET", "/path",
530 TestStruct{
531 Name: "Bob",
532 },
533 "Foo", "Bar",
534 "Zip", "Test")
535
536 t.String(req.Header.Get("Content-Type"), "application/xml")
537 t.String(req.Header.Get("Foo"), "Bar")
538 t.String(req.Header.Get("Zip"), "Test")
539
540 body, err := io.ReadAll(req.Body)
541 if t.CmpNoError(err, "read request body") {
542 t.String(string(body), `<TestStruct><name>Bob</name></TestStruct>`)
543 }
544 })
545
546 t.Run("NewXMLRequest panic", func(t *td.T) {
547 t.CmpPanic(
548 func() { tdhttp.NewXMLRequest("GET", "/path", func() {}) },
549 td.Contains("XML encoding failed"))
550
551 t.CmpPanic(
552 func() { tdhttp.PostXML("/path", func() {}) },
553 td.Contains("XML encoding failed"))
554
555 t.CmpPanic(
556 func() { tdhttp.PutXML("/path", func() {}) },
557 td.Contains("XML encoding failed"))
558
559 t.CmpPanic(
560 func() { tdhttp.PatchXML("/path", func() {}) },
561 td.Contains("XML encoding failed"))
562
563 t.CmpPanic(
564 func() { tdhttp.DeleteXML("/path", func() {}) },
565 td.Contains("XML encoding failed"))
566 })
567
568 // Post
569 t.Cmp(tdhttp.PostXML("/path", 42, "Foo", "Bar"),
570 td.Struct(
571 &http.Request{
572 Method: "POST",
573 Header: http.Header{
574 "Foo": {"Bar"},
575 "Content-Type": {"application/xml"},
576 },
577 },
578 td.StructFields{
579 "URL": td.String("/path"),
580 }))
581
582 // Put
583 t.Cmp(tdhttp.PutXML("/path", 42, "Foo", "Bar"),
584 td.Struct(
585 &http.Request{
586 Method: "PUT",
587 Header: http.Header{
588 "Foo": {"Bar"},
589 "Content-Type": {"application/xml"},
590 },
591 },
592 td.StructFields{
593 "URL": td.String("/path"),
594 }))
595
596 // Patch
597 t.Cmp(tdhttp.PatchXML("/path", 42, "Foo", "Bar"),
598 td.Struct(
599 &http.Request{
600 Method: "PATCH",
601 Header: http.Header{
602 "Foo": {"Bar"},
603 "Content-Type": {"application/xml"},
604 },
605 },
606 td.StructFields{
607 "URL": td.String("/path"),
608 }))
609
610 // Delete
611 t.Cmp(tdhttp.DeleteXML("/path", 42, "Foo", "Bar"),
612 td.Struct(
613 &http.Request{
614 Method: "DELETE",
615 Header: http.Header{
616 "Foo": {"Bar"},
617 "Content-Type": {"application/xml"},
618 },
619 },
620 td.StructFields{
621 "URL": td.String("/path"),
622 }))
623 }