-
-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathprinter_test.go
More file actions
192 lines (170 loc) · 3.71 KB
/
Copy pathprinter_test.go
File metadata and controls
192 lines (170 loc) · 3.71 KB
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
package httpexpect
import (
"errors"
"io"
"net/http"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
type failingReader struct{}
func (failingReader) Read(_ []byte) (n int, err error) {
return 0, errors.New("error")
}
func (failingReader) Close() error {
return errors.New("error")
}
func httpRequest(body io.Reader) *http.Request {
req, _ := http.NewRequest("GET", "http://example.com", body)
return req
}
func httpResponse(body io.Reader) *http.Response {
resp := &http.Response{}
if body != nil {
resp.Body = io.NopCloser(body)
}
return resp
}
func TestPrinter_Compact(t *testing.T) {
cases := []struct {
name string
request *http.Request
response *http.Response
}{
{
name: "nil request and response",
request: nil,
response: nil,
},
{
name: "has body",
request: httpRequest(strings.NewReader("test")),
response: httpResponse(strings.NewReader("test")),
},
{
name: "no body",
request: httpRequest(nil),
response: httpResponse(nil),
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
printer := NewCompactPrinter(t)
printer.Request(tc.request)
printer.Response(tc.response, 0)
})
}
}
func TestPrinter_Curl(t *testing.T) {
cases := []struct {
name string
request *http.Request
response *http.Response
}{
{
name: "nil request and response",
request: nil,
response: nil,
},
{
name: "has body",
request: httpRequest(strings.NewReader("test")),
response: httpResponse(strings.NewReader("test")),
},
{
name: "no body",
request: httpRequest(nil),
response: httpResponse(nil),
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
printer := NewCurlPrinter(t)
printer.Request(tc.request)
printer.Response(tc.response, 0)
})
}
}
func TestPrinter_Debug(t *testing.T) {
cases := []struct {
name string
printBody bool
request *http.Request
response *http.Response
}{
{
name: "nil request and response",
printBody: true,
request: nil,
response: nil,
},
{
name: "has body, print body",
printBody: true,
request: httpRequest(strings.NewReader("test")),
response: httpResponse(strings.NewReader("test")),
},
{
name: "has body, don't print body",
printBody: false,
request: httpRequest(strings.NewReader("test")),
response: httpResponse(strings.NewReader("test")),
},
{
name: "no body, print body",
printBody: true,
request: httpRequest(nil),
response: httpResponse(nil),
},
{
name: "no body, don't print body",
printBody: false,
request: httpRequest(nil),
response: httpResponse(nil),
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
printer := NewDebugPrinter(t, tc.printBody)
printer.Request(tc.request)
printer.Response(tc.response, 0)
})
}
}
func TestPrinter_Panics(t *testing.T) {
t.Run("CompactPrinter", func(t *testing.T) {
printer := NewCompactPrinter(t)
assert.NotPanics(t, func() {
printer.Request(&http.Request{})
})
assert.NotPanics(t, func() {
printer.Response(&http.Response{
Body: failingReader{},
}, 0)
})
})
t.Run("CurlPrinter", func(t *testing.T) {
printer := NewCurlPrinter(t)
assert.Panics(t, func() {
printer.Request(&http.Request{})
})
assert.NotPanics(t, func() {
printer.Response(&http.Response{
Body: failingReader{},
}, 0)
})
})
t.Run("DebugPrinter", func(t *testing.T) {
printer := NewDebugPrinter(t, true)
assert.Panics(t, func() {
printer.Request(&http.Request{
Body: failingReader{},
})
})
assert.Panics(t, func() {
printer.Response(&http.Response{
Body: failingReader{},
}, 0)
})
})
}