-
Notifications
You must be signed in to change notification settings - Fork 15
/
pcb_wb_test.go
207 lines (194 loc) · 4.41 KB
/
pcb_wb_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
package govcr
import (
"crypto/tls"
"crypto/x509"
"math/big"
"mime/multipart"
"net/http"
"net/textproto"
"testing"
"github.com/stretchr/testify/require"
"github.com/seborama/govcr/v15/cassette"
"github.com/seborama/govcr/v15/cassette/track"
)
func TestPrintedCircuitBoard_trackMatches(t *testing.T) {
// This test is in addition toTestRoundTrip_RequestMatcherDoesNotMutateState
pcb := &PrintedCircuitBoard{}
pcb.SetRequestMatchers(
func(httpRequest, trackRequest *track.Request) bool {
for k := range httpRequest.Header {
httpRequest.Header.Set(k, "nil")
}
for i := range httpRequest.Body {
httpRequest.Body[i] = 'X'
}
httpRequest.ContentLength = -1
for k := range httpRequest.MultipartForm.File {
for k2 := range httpRequest.MultipartForm.File[k] {
httpRequest.MultipartForm.File[k][k2].Filename = "nil"
for k3 := range httpRequest.MultipartForm.File[k][k2].Header {
httpRequest.MultipartForm.File[k][k2].Header.Set(k3, "nil")
}
}
}
for k := range trackRequest.Header {
trackRequest.Header.Set(k, "nil")
}
for i := range trackRequest.Body {
trackRequest.Body[i] = 'X'
}
trackRequest.ContentLength = -1
for k := range trackRequest.MultipartForm.File {
for k2 := range trackRequest.MultipartForm.File[k] {
trackRequest.MultipartForm.File[k][k2].Filename = "nil"
for k3 := range trackRequest.MultipartForm.File[k][k2].Header {
trackRequest.MultipartForm.File[k][k2].Header.Set(k3, "nil")
}
}
}
httpRequest = nil
trackRequest = nil
return true
},
)
k7 := &cassette.Cassette{
Tracks: []track.Track{
{
Request: track.Request{
Header: http.Header{
"req header": {"req header value"},
},
Body: []byte("req body"),
ContentLength: 456,
MultipartForm: &multipart.Form{
File: map[string][]*multipart.FileHeader{
"req multipartpostform file": {
{
Filename: "req multipartpostform file filename",
Header: textproto.MIMEHeader{
"req form": []string{"req form value"},
},
},
},
},
},
},
Response: &track.Response{
Status: "resp status",
Header: http.Header{
"resp header": {"resp header value"},
},
Body: []byte("resp body"),
TLS: &tls.ConnectionState{
PeerCertificates: []*x509.Certificate{
{
SerialNumber: big.NewInt(1234),
},
},
},
},
ErrType: strPtr("err type"),
ErrMsg: strPtr("err message"),
UUID: "test uuid",
},
},
}
httpRequest := &track.Request{
Header: map[string][]string{
"hreq header": {"hreq header value"},
},
Body: []byte("hreq body"),
ContentLength: 890,
MultipartForm: &multipart.Form{
File: map[string][]*multipart.FileHeader{
"hreq multipartpostform file": {
{
Filename: "hreq multipartpostform file filename",
Header: textproto.MIMEHeader{
"hreq form": []string{"hreq form value"},
},
},
},
},
},
}
res := pcb.trackMatches(k7, 0, httpRequest)
require.True(t, res)
// track validation
require.Equal(
t,
http.Header{
"req header": {"req header value"},
},
k7.Tracks[0].Request.Header,
)
require.Equal(
t,
[]byte("req body"),
k7.Tracks[0].Request.Body,
)
require.Equal(
t,
int64(456),
k7.Tracks[0].Request.ContentLength,
)
require.Len(
t,
k7.Tracks,
1,
)
_, ok := k7.Tracks[0].Request.MultipartForm.File["req multipartpostform file"]
require.True(
t,
ok,
)
require.Len(
t,
k7.Tracks[0].Request.MultipartForm.File["req multipartpostform file"],
1,
)
require.Equal(
t,
"req multipartpostform file filename",
k7.Tracks[0].Request.MultipartForm.File["req multipartpostform file"][0].Filename,
)
// httpRequest validation
require.Equal(
t,
http.Header{
"hreq header": {"hreq header value"},
},
httpRequest.Header,
)
require.Equal(
t,
[]byte("hreq body"),
httpRequest.Body,
)
require.Equal(
t,
int64(890),
httpRequest.ContentLength,
)
require.Len(
t,
k7.Tracks,
1,
)
_, ok2 := httpRequest.MultipartForm.File["hreq multipartpostform file"]
require.True(
t,
ok2,
)
require.Len(
t,
httpRequest.MultipartForm.File["hreq multipartpostform file"],
1,
)
require.Equal(
t,
"hreq multipartpostform file filename",
httpRequest.MultipartForm.File["hreq multipartpostform file"][0].Filename,
)
}
func strPtr(s string) *string { return &s }