-
-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathhttp_v3.go
More file actions
319 lines (249 loc) · 9.94 KB
/
Copy pathhttp_v3.go
File metadata and controls
319 lines (249 loc) · 9.94 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package consumer
import (
"log"
"testing"
"github.com/pact-foundation/pact-go/v2/matchers"
"github.com/pact-foundation/pact-go/v2/models"
"github.com/pact-foundation/pact-go/v2/utils"
)
// V3HTTPMockProvider is the entrypoint for V3 http consumer tests
// This object is not thread safe
type V3HTTPMockProvider struct {
*httpMockProvider
}
// NewV3Pact configures a new V3 HTTP Mock Provider for consumer tests
func NewV3Pact(config MockHTTPProviderConfig) (*V3HTTPMockProvider, error) {
provider := &V3HTTPMockProvider{
httpMockProvider: &httpMockProvider{
config: config,
specificationVersion: models.V3,
},
}
err := provider.configure()
if err != nil {
return nil, err
}
return provider, err
}
// AddInteraction to the pact
func (p *V3HTTPMockProvider) AddInteraction() *V3UnconfiguredInteraction {
log.Println("[DEBUG] pact add V3 interaction")
interaction := p.mockserver.NewInteraction("")
i := &V3UnconfiguredInteraction{
interaction: &Interaction{
specificationVersion: models.V3,
interaction: interaction,
},
provider: p,
}
return i
}
type V3UnconfiguredInteraction struct {
interaction *Interaction
provider *V3HTTPMockProvider
}
// Given specifies a provider state, may be called multiple times. Optional.
func (i *V3UnconfiguredInteraction) Given(state string) *V3UnconfiguredInteraction {
i.interaction.interaction.Given(state)
return i
}
// GivenWithParameter specifies a provider state with parameters, may be called multiple times. Optional.
func (i *V3UnconfiguredInteraction) GivenWithParameter(state models.ProviderState) *V3UnconfiguredInteraction {
if len(state.Parameters) > 0 {
i.interaction.interaction.GivenWithParameter(state.Name, state.Parameters)
} else {
i.interaction.interaction.Given(state.Name)
}
return i
}
type V3InteractionWithRequest struct {
interaction *Interaction
provider *V3HTTPMockProvider
}
type V3RequestBuilderFunc func(*V3RequestBuilder)
type V3RequestBuilder struct {
interaction *Interaction
provider *V3HTTPMockProvider
}
// UponReceiving specifies the name of the test case. This becomes the name of
// the consumer/provider pair in the Pact file. Mandatory.
func (i *V3UnconfiguredInteraction) UponReceiving(description string) *V3UnconfiguredInteraction {
i.interaction.interaction.UponReceiving(description)
return i
}
// WithRequest provides a builder for the expected request
func (i *V3UnconfiguredInteraction) WithCompleteRequest(request Request) *V3InteractionWithCompleteRequest {
i.interaction.WithCompleteRequest(request)
return &V3InteractionWithCompleteRequest{
interaction: i.interaction,
provider: i.provider,
}
}
type V3InteractionWithCompleteRequest struct {
interaction *Interaction
provider *V3HTTPMockProvider
}
// WithRequest provides a builder for the expected request
func (i *V3InteractionWithCompleteRequest) WithCompleteResponse(response Response) *V3InteractionWithResponse {
i.interaction.WithCompleteResponse(response)
return &V3InteractionWithResponse{
interaction: i.interaction,
provider: i.provider,
}
}
// WithRequest provides a builder for the expected request
func (i *V3UnconfiguredInteraction) WithRequest(method Method, path string, builders ...V3RequestBuilderFunc) *V3InteractionWithRequest {
return i.WithRequestPathMatcher(method, matchers.String(path), builders...)
}
// WithRequestPathMatcher allows a matcher in the expected request path
func (i *V3UnconfiguredInteraction) WithRequestPathMatcher(method Method, path matchers.Matcher, builders ...V3RequestBuilderFunc) *V3InteractionWithRequest {
i.interaction.interaction.WithRequest(string(method), path)
for _, builder := range builders {
builder(&V3RequestBuilder{
interaction: i.interaction,
provider: i.provider,
})
}
return &V3InteractionWithRequest{
interaction: i.interaction,
provider: i.provider,
}
}
// Query specifies any query string on the expect request
func (i *V3RequestBuilder) Query(key string, values ...matchers.Matcher) *V3RequestBuilder {
i.interaction.interaction.WithQuery(keyValuesToMapStringArrayInterface(key, values...))
return i
}
// Header adds a header to the expected request
func (i *V3RequestBuilder) Header(key string, values ...matchers.Matcher) *V3RequestBuilder {
i.interaction.interaction.WithRequestHeaders(keyValuesToMapStringArrayInterface(key, values...))
return i
}
// Headers sets the headers on the expected request
func (i *V3RequestBuilder) Headers(headers matchers.HeadersMatcher) *V3RequestBuilder {
i.interaction.interaction.WithRequestHeaders(headersMatcherToNativeHeaders(headers))
return i
}
// JSONBody adds a JSON body to the expected request
func (i *V3RequestBuilder) JSONBody(body interface{}) *V3RequestBuilder {
// TODO: Don't like panic, but not sure if there is a better builder experience?
if err := validateMatchers(i.interaction.specificationVersion, body); err != nil {
panic(err)
}
if s, ok := body.(string); ok {
// Check if someone tried to add an object as a string representation
// as per original allowed implementation, e.g.
// { "foo": "bar", "baz": like("bat") }
if utils.IsJSONFormattedObject(string(s)) {
log.Println("[WARN] request body appears to be a JSON formatted object, " +
"no matching will occur. Support for structured strings has been" +
"deprecated as of 0.13.0. Please use the JSON() method instead")
}
}
i.interaction.interaction.WithJSONRequestBody(body)
return i
}
// BinaryBody adds a binary body to the expected request
func (i *V3RequestBuilder) BinaryBody(body []byte) *V3RequestBuilder {
i.interaction.interaction.WithBinaryRequestBody(body)
return i
}
// MultipartBody adds a multipart body to the expected request
func (i *V3RequestBuilder) MultipartBody(contentType string, filename string, mimePartName string) *V3RequestBuilder {
i.interaction.interaction.WithRequestMultipartFile(contentType, filename, mimePartName)
return i
}
// Body adds general body to the expected request
func (i *V3RequestBuilder) Body(contentType string, body []byte) *V3RequestBuilder {
// Check if someone tried to add an object as a string representation
// as per original allowed implementation, e.g.
// { "foo": "bar", "baz": like("bat") }
if utils.IsJSONFormattedObject(string(body)) {
log.Println("[WARN] request body appears to be a JSON formatted object, " +
"no matching will occur. Support for structured strings has been" +
"deprecated as of 0.13.0. Please use the JSON() method instead")
}
i.interaction.interaction.WithRequestBody(contentType, body)
return i
}
// BodyMatch uses struct tags to automatically determine matchers from the given struct
func (i *V3RequestBuilder) BodyMatch(body interface{}) *V3RequestBuilder {
i.interaction.interaction.WithJSONRequestBody(matchers.MatchV2(body))
return i
}
// WillRespondWith sets the expected status and provides a response builder
func (i *V3InteractionWithRequest) WillRespondWith(status int, builders ...V3ResponseBuilderFunc) *V3InteractionWithResponse {
i.interaction.interaction.WithStatus(status)
for _, builder := range builders {
builder(&V3ResponseBuilder{
interaction: i.interaction,
provider: i.provider,
})
}
return &V3InteractionWithResponse{
interaction: i.interaction,
provider: i.provider,
}
}
type V3ResponseBuilderFunc func(*V3ResponseBuilder)
type V3ResponseBuilder struct {
interaction *Interaction
provider *V3HTTPMockProvider
}
type V3InteractionWithResponse struct {
interaction *Interaction
provider *V3HTTPMockProvider
}
// Header adds a header to the expected response
func (i *V3ResponseBuilder) Header(key string, values ...matchers.Matcher) *V3ResponseBuilder {
i.interaction.interaction.WithResponseHeaders(keyValuesToMapStringArrayInterface(key, values...))
return i
}
// Headers sets the headers on the expected response
func (i *V3ResponseBuilder) Headers(headers matchers.HeadersMatcher) *V3ResponseBuilder {
i.interaction.interaction.WithResponseHeaders(headersMatcherToNativeHeaders(headers))
return i
}
// JSONBody adds a JSON body to the expected response
func (i *V3ResponseBuilder) JSONBody(body interface{}) *V3ResponseBuilder {
// TODO: Don't like panic, how to build a better builder here - nil return + log?
if err := validateMatchers(i.interaction.specificationVersion, body); err != nil {
panic(err)
}
if s, ok := body.(string); ok {
// Check if someone tried to add an object as a string representation
// as per original allowed implementation, e.g.
// { "foo": "bar", "baz": like("bat") }
if utils.IsJSONFormattedObject(string(s)) {
log.Println("[WARN] response body appears to be a JSON formatted object, " +
"no matching will occur. Support for structured strings has been" +
"deprecated as of 0.13.0. Please use the JSON() method instead")
}
}
i.interaction.interaction.WithJSONResponseBody(body)
return i
}
// BinaryBody adds a binary body to the expected response
func (i *V3ResponseBuilder) BinaryBody(body []byte) *V3ResponseBuilder {
i.interaction.interaction.WithBinaryResponseBody(body)
return i
}
// MultipartBody adds a multipart body to the expected response
func (i *V3ResponseBuilder) MultipartBody(contentType string, filename string, mimePartName string) *V3ResponseBuilder {
i.interaction.interaction.WithResponseMultipartFile(contentType, filename, mimePartName)
return i
}
// Body adds general body to the expected request
func (i *V3ResponseBuilder) Body(contentType string, body []byte) *V3ResponseBuilder {
i.interaction.interaction.WithResponseBody(contentType, body)
return i
}
// BodyMatch uses struct tags to automatically determine matchers from the given struct
func (i *V3ResponseBuilder) BodyMatch(body interface{}) *V3ResponseBuilder {
i.interaction.interaction.WithJSONResponseBody(matchers.MatchV2(body))
return i
}
// ExecuteTest runs the current test case against a Mock Service.
func (m *V3InteractionWithResponse) ExecuteTest(t *testing.T, integrationTest func(MockServerConfig) error) error {
return m.provider.ExecuteTest(t, integrationTest)
}