Debian Codegraph @

helpers/tdhttp/internal/response_test.go

raw
1 // Copyright (c) 2021, 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 internal_test
8
9 import (
10 "bytes"
11 "io"
12 "net/http"
13 "testing"
14
15 "github.com/maxatome/go-testdeep/helpers/tdhttp/internal"
16 "github.com/maxatome/go-testdeep/internal/test"
17 "github.com/maxatome/go-testdeep/td"
18 )
19
20 func newResponse(body string) *http.Response {
21 return &http.Response{
22 Status: "200 OK",
23 StatusCode: 200,
24 Proto: "HTTP/1.0",
25 ProtoMajor: 1,
26 ProtoMinor: 0,
27 Header: http.Header{
28 "A": []string{"foo"},
29 "B": []string{"bar"},
30 },
31 Body: io.NopCloser(bytes.NewBufferString(body)),
32 }
33 }
34
35 func inBQ(s string) string {
36 return "`" + s + "`"
37 }
38
39 func TestDumpResponse(t *testing.T) {
40 tb := test.NewTestingTB("TestDumpResponse")
41 internal.DumpResponse(tb, newResponse("one-line"))
42 td.Cmp(t, tb.LastMessage(),
43 `Received response:
44 `+inBQ(`HTTP/1.0 200 OK
45 A: foo
46 B: bar
47
48 one-line`))
49
50 tb.ResetMessages()
51 internal.DumpResponse(tb, newResponse("multi\r\nlines\r\nand\ttabs héhé"))
52 td.Cmp(t, tb.LastMessage(),
53 `Received response:
54 `+inBQ(`HTTP/1.0 200 OK
55 A: foo
56 B: bar
57
58 multi
59 lines
60 `+"and\ttabs héhé"))
61
62 tb.ResetMessages()
63 internal.DumpResponse(tb, newResponse("multi\nlines\nand\ttabs héhé"))
64 td.Cmp(t, tb.LastMessage(),
65 `Received response:
66 `+inBQ(`HTTP/1.0 200 OK
67 A: foo
68 B: bar
69
70 multi
71 lines
72 `+"and\ttabs héhé"))
73
74 // one \r more in body
75 tb.ResetMessages()
76 internal.DumpResponse(tb, newResponse("multi\r\nline\r"))
77 td.Cmp(t, tb.LastMessage(),
78 `Received response:
79 "HTTP/1.0 200 OK\r\nA: foo\r\nB: bar\r\n\r\nmulti\r\nline\r"`)
80
81 // BOM
82 tb.ResetMessages()
83 internal.DumpResponse(tb, newResponse("\ufeff"))
84 td.Cmp(t, tb.LastMessage(),
85 `Received response:
86 "HTTP/1.0 200 OK\r\nA: foo\r\nB: bar\r\n\r\n\ufeff"`)
87
88 // Rune error
89 tb.ResetMessages()
90 internal.DumpResponse(tb, newResponse("\xf4\x9f\xbf\xbf"))
91 td.Cmp(t, tb.LastMessage(),
92 `Received response:
93 "HTTP/1.0 200 OK\r\nA: foo\r\nB: bar\r\n\r\n\xf4\x9f\xbf\xbf"`)
94
95 // `
96 tb.ResetMessages()
97 internal.DumpResponse(tb, newResponse("he`o"))
98 td.Cmp(t, tb.LastMessage(),
99 `Received response:
100 "HTTP/1.0 200 OK\r\nA: foo\r\nB: bar\r\n\r\nhe`+"`"+`o"`)
101
102 // 0x7f
103 tb.ResetMessages()
104 internal.DumpResponse(tb, newResponse("\x7f"))
105 td.Cmp(t, tb.LastMessage(),
106 td.Re(`Received response:
107 "HTTP/1.0 200 OK\\r\\nA: foo\\r\\nB: bar\\r\\n\\r\\n(\\u007f|\\x7f)"`))
108 }