-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
debughttp_test.go
212 lines (199 loc) · 6.6 KB
/
debughttp_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
package debughttp
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Check transport implements the interface
var _ http.RoundTripper = (*Transport)(nil)
// returns the "%p" representation of the thing passed in
func ptr(p interface{}) string {
return fmt.Sprintf("%p", p)
}
func TestSetDefaults(t *testing.T) {
old := http.DefaultTransport.(*http.Transport)
newT := new(http.Transport)
setDefaults(newT, old)
// Can't use assert.Equal or reflect.DeepEqual for this as it has functions in
// Check functions by comparing the "%p" representations of them
assert.Equal(t, ptr(old.Proxy), ptr(newT.Proxy), "when checking .Proxy")
assert.Equal(t, ptr(old.DialContext), ptr(newT.DialContext), "when checking .DialContext")
// Check the other public fields
assert.Equal(t, ptr(old.Dial), ptr(newT.Dial), "when checking .Dial")
assert.Equal(t, ptr(old.DialTLS), ptr(newT.DialTLS), "when checking .DialTLS")
assert.Equal(t, old.TLSClientConfig, newT.TLSClientConfig, "when checking .TLSClientConfig")
assert.Equal(t, old.TLSHandshakeTimeout, newT.TLSHandshakeTimeout, "when checking .TLSHandshakeTimeout")
assert.Equal(t, old.DisableKeepAlives, newT.DisableKeepAlives, "when checking .DisableKeepAlives")
assert.Equal(t, old.DisableCompression, newT.DisableCompression, "when checking .DisableCompression")
assert.Equal(t, old.MaxIdleConns, newT.MaxIdleConns, "when checking .MaxIdleConns")
assert.Equal(t, old.MaxIdleConnsPerHost, newT.MaxIdleConnsPerHost, "when checking .MaxIdleConnsPerHost")
assert.Equal(t, old.IdleConnTimeout, newT.IdleConnTimeout, "when checking .IdleConnTimeout")
assert.Equal(t, old.ResponseHeaderTimeout, newT.ResponseHeaderTimeout, "when checking .ResponseHeaderTimeout")
assert.Equal(t, old.ExpectContinueTimeout, newT.ExpectContinueTimeout, "when checking .ExpectContinueTimeout")
assert.Equal(t, old.TLSNextProto, newT.TLSNextProto, "when checking .TLSNextProto")
assert.Equal(t, old.MaxResponseHeaderBytes, newT.MaxResponseHeaderBytes, "when checking .MaxResponseHeaderBytes")
}
func TestCleanAuth(t *testing.T) {
for _, test := range []struct {
in string
want string
}{
{"", ""},
{"floo", "floo"},
{"Authorization: ", "Authorization: "},
{"Authorization: \n", "Authorization: \n"},
{"Authorization: A", "Authorization: X"},
{"Authorization: A\n", "Authorization: X\n"},
{"Authorization: AAAA", "Authorization: XXXX"},
{"Authorization: AAAA\n", "Authorization: XXXX\n"},
{"Authorization: AAAAA", "Authorization: XXXX"},
{"Authorization: AAAAA\n", "Authorization: XXXX\n"},
{"Authorization: AAAA\n", "Authorization: XXXX\n"},
{"Authorization: AAAAAAAAA\nPotato: Help\n", "Authorization: XXXX\nPotato: Help\n"},
{"Sausage: 1\nAuthorization: AAAAAAAAA\nPotato: Help\n", "Sausage: 1\nAuthorization: XXXX\nPotato: Help\n"},
} {
got := string(cleanAuth([]byte(test.in), Auth[0]))
assert.Equal(t, test.want, got, test.in)
}
}
func TestCleanAuths(t *testing.T) {
transport := NewDefault(nil)
for _, test := range []struct {
in string
want string
}{
{"", ""},
{"floo", "floo"},
{"Authorization: AAAAAAAAA\nPotato: Help\n", "Authorization: XXXX\nPotato: Help\n"},
{"X-Auth-Token: AAAAAAAAA\nPotato: Help\n", "X-Auth-Token: XXXX\nPotato: Help\n"},
{"X-Auth-Token: AAAAAAAAA\nAuthorization: AAAAAAAAA\nPotato: Help\n", "X-Auth-Token: XXXX\nAuthorization: XXXX\nPotato: Help\n"},
} {
got := string(transport.cleanAuths([]byte(test.in)))
assert.Equal(t, test.want, got, test.in)
}
}
func TestTransport(t *testing.T) {
const (
requestBody = "Request text"
responseBody = "Response body"
)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, responseBody)
if r.Body != nil {
buf, err := ioutil.ReadAll(r.Body)
require.NoError(t, err)
if len(buf) > 0 {
fmt.Fprintln(w, strings.ToUpper(string(buf)))
}
}
}))
defer ts.Close()
var lines []string
logf := func(format string, v ...interface{}) {
line := fmt.Sprintf(format, v...)
line = strings.Replace(line, "\r", "", -1)
lines = append(lines, line)
}
for _, test := range []struct {
name string
flags DumpFlags
wantHeaders bool
wantReqBody bool
wantRespBody bool
wantAuth bool
}{
{
name: "NoDump",
flags: 0,
},
{
name: "DumpHeaders",
flags: DumpHeaders,
wantHeaders: true,
},
{
name: "DumpBodies",
flags: DumpBodies,
wantHeaders: true,
wantReqBody: true,
wantRespBody: true,
},
{
name: "DumpRequests",
flags: DumpRequests,
wantHeaders: true,
wantReqBody: true,
wantRespBody: false,
},
{
name: "DumpResponses",
flags: DumpResponses,
wantHeaders: true,
wantReqBody: false,
wantRespBody: true,
},
{
name: "DumpResponsesWithAuth",
flags: DumpResponses | DumpAuth,
wantHeaders: true,
wantReqBody: false,
wantRespBody: true,
wantAuth: true,
},
} {
t.Run(test.name, func(t *testing.T) {
client := NewClient(&Options{
Flags: test.flags,
Logf: logf,
})
lines = nil
// Do the test request
req, err := http.NewRequest("PUT", ts.URL, bytes.NewBufferString(requestBody))
require.NoError(t, err)
req.Header.Set("Authorization", "POTATO")
resp, err := client.Do(req)
assert.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
body, err := ioutil.ReadAll(resp.Body)
assert.NoError(t, err)
assert.NoError(t, resp.Body.Close())
expectedResponse := responseBody + "\n" + strings.ToUpper(requestBody) + "\n"
assert.Equal(t, expectedResponse, string(body))
if !test.wantHeaders {
assert.Equal(t, 0, len(lines))
return
}
// Check what we expect was logged
require.Equal(t, 8, len(lines))
assert.Equal(t, SeparatorReq, lines[0])
assert.Contains(t, lines[1], "HTTP REQUEST")
assert.Contains(t, lines[2], "PUT / HTTP")
if test.wantAuth {
assert.Contains(t, lines[2], "\nAuthorization: POTATO\n")
} else {
assert.Contains(t, lines[2], "\nAuthorization: XXXX\n")
}
if test.wantReqBody {
assert.Contains(t, lines[2], requestBody)
} else {
assert.NotContains(t, lines[2], requestBody)
}
assert.Equal(t, SeparatorReq, lines[3])
assert.Equal(t, SeparatorResp, lines[4])
assert.Contains(t, lines[5], "HTTP RESPONSE")
assert.Contains(t, lines[6], "200 OK\n")
if test.wantRespBody {
assert.Contains(t, lines[6], expectedResponse)
} else {
assert.NotContains(t, lines[6], expectedResponse)
}
assert.Equal(t, SeparatorResp, lines[7])
})
}
}