-
-
Notifications
You must be signed in to change notification settings - Fork 472
/
PactDslResponse.java
430 lines (385 loc) · 16.4 KB
/
PactDslResponse.java
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package au.com.dius.pact.consumer.dsl;
import au.com.dius.pact.consumer.ConsumerPactBuilder;
import au.com.dius.pact.consumer.xml.PactXmlBuilder;
import au.com.dius.pact.core.model.OptionalBody;
import au.com.dius.pact.core.model.ProviderState;
import au.com.dius.pact.core.model.Request;
import au.com.dius.pact.core.model.RequestResponseInteraction;
import au.com.dius.pact.core.model.RequestResponsePact;
import au.com.dius.pact.core.model.Response;
import au.com.dius.pact.core.model.generators.Category;
import au.com.dius.pact.core.model.generators.Generators;
import au.com.dius.pact.core.model.generators.ProviderStateGenerator;
import au.com.dius.pact.core.model.matchingrules.ContentTypeMatcher;
import au.com.dius.pact.core.model.matchingrules.MatchingRules;
import au.com.dius.pact.core.model.matchingrules.MatchingRulesImpl;
import au.com.dius.pact.core.model.matchingrules.RegexMatcher;
import au.com.dius.pact.core.model.matchingrules.RuleLogic;
import au.com.dius.pact.core.support.expressions.DataType;
import com.mifmif.common.regex.Generex;
import org.apache.http.entity.ContentType;
import org.json.JSONObject;
import org.w3c.dom.Document;
import javax.xml.transform.TransformerException;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import java.util.regex.Pattern;
import static au.com.dius.pact.consumer.Headers.MULTIPART_HEADER_REGEX;
import static com.google.common.collect.Lists.newArrayList;
public class PactDslResponse {
private static final String CONTENT_TYPE = "Content-Type";
static final String DEFAULT_JSON_CONTENT_TYPE_REGEX = "application/json(;\\s?charset=[\\w\\-]+)?";
private final ConsumerPactBuilder consumerPactBuilder;
private PactDslRequestWithPath request;
private final PactDslRequestWithoutPath defaultRequestValues;
private final PactDslResponse defaultResponseValues;
private int responseStatus = 200;
private Map<String, List<String>> responseHeaders = new HashMap<>();
private OptionalBody responseBody = OptionalBody.missing();
private MatchingRules responseMatchers = new MatchingRulesImpl();
private Generators responseGenerators = new Generators();
public PactDslResponse(ConsumerPactBuilder consumerPactBuilder, PactDslRequestWithPath request,
PactDslRequestWithoutPath defaultRequestValues,
PactDslResponse defaultResponseValues) {
this.consumerPactBuilder = consumerPactBuilder;
this.request = request;
this.defaultRequestValues = defaultRequestValues;
this.defaultResponseValues = defaultResponseValues;
setupDefaultValues();
}
private void setupDefaultValues() {
if (defaultResponseValues != null) {
responseStatus = defaultResponseValues.responseStatus;
responseHeaders.putAll(defaultResponseValues.responseHeaders);
responseBody = defaultResponseValues.responseBody;
responseMatchers = ((MatchingRulesImpl) defaultResponseValues.responseMatchers).copy();
responseGenerators = new Generators(defaultResponseValues.responseGenerators.getCategories());
}
}
/**
* Response status code
*
* @param status HTTP status code
*/
public PactDslResponse status(int status) {
this.responseStatus = status;
return this;
}
/**
* Response headers to return
*
* Provide the headers you want to validate, other headers will be ignored.
*
* @param headers key-value pairs of headers
*/
public PactDslResponse headers(Map<String, String> headers) {
for (Map.Entry<String, String> entry: headers.entrySet()) {
this.responseHeaders.put(entry.getKey(), Collections.singletonList(entry.getValue()));
}
return this;
}
/**
* Response body to return
*
* @param body Response body in string form
*/
public PactDslResponse body(String body) {
this.responseBody = OptionalBody.body(body.getBytes());
return this;
}
/**
* Response body to return
*
* @param body body in string form
* @param contentType the Content-Type response header value
*/
public PactDslResponse body(String body, String contentType) {
return body(body, ContentType.parse(contentType));
}
/**
* Response body to return
*
* @param body body in string form
* @param contentType the Content-Type response header value
*/
public PactDslResponse body(String body, ContentType contentType) {
Charset charset = contentType.getCharset() == null ? Charset.defaultCharset() : contentType.getCharset();
responseBody = OptionalBody.body(body.getBytes(charset), new au.com.dius.pact.core.model.ContentType(contentType.toString()));
responseHeaders.put(CONTENT_TYPE, Collections.singletonList(contentType.toString()));
return this;
}
/**
* The body of the request
*
* @param body Response body in Java Functional Interface Supplier that must return a string
*/
public PactDslResponse body(Supplier<String> body) {
responseBody = OptionalBody.body(body.get().getBytes());
return this;
}
/**
* The body of the request
*
* @param body Response body in Java Functional Interface Supplier that must return a string
* @param contentType the Content-Type response header value
*/
public PactDslResponse body(Supplier<String> body, String contentType) {
return body(body, contentType);
}
/**
* The body of the request
*
* @param body Response body in Java Functional Interface Supplier that must return a string
* @param contentType the Content-Type response header value
*/
public PactDslResponse body(Supplier<String> body, ContentType contentType) {
Charset charset = contentType.getCharset() == null ? Charset.defaultCharset() : contentType.getCharset();
responseBody = OptionalBody.body(body.get().getBytes(charset), new au.com.dius.pact.core.model.ContentType(contentType.toString()));
responseHeaders.put(CONTENT_TYPE, Collections.singletonList(contentType.toString()));
return this;
}
/**
* The body of the request with possible single quotes as delimiters
* and using {@link QuoteUtil} to convert single quotes to double quotes if required.
*
* @param body Request body in string form
*/
public PactDslResponse bodyWithSingleQuotes(String body) {
if (body != null) {
body = QuoteUtil.convert(body);
}
return body(body);
}
/**
* The body of the request with possible single quotes as delimiters
* and using {@link QuoteUtil} to convert single quotes to double quotes if required.
*
* @param body Request body in string form
* @param contentType the Content-Type response header value
*/
public PactDslResponse bodyWithSingleQuotes(String body, String contentType) {
if (body != null) {
body = QuoteUtil.convert(body);
}
return body(body, contentType);
}
/**
* The body of the request with possible single quotes as delimiters
* and using {@link QuoteUtil} to convert single quotes to double quotes if required.
*
* @param body Request body in string form
* @param contentType the Content-Type response header value
*/
public PactDslResponse bodyWithSingleQuotes(String body, ContentType contentType) {
if (body != null) {
body = QuoteUtil.convert(body);
}
return body(body, contentType);
}
/**
* Response body to return
*
* @param body Response body in JSON form
*/
public PactDslResponse body(JSONObject body) {
if (isContentTypeHeaderNotSet()) {
matchHeader(CONTENT_TYPE, DEFAULT_JSON_CONTENT_TYPE_REGEX, ContentType.APPLICATION_JSON.toString());
this.responseBody = OptionalBody.body(body.toString().getBytes());
} else {
String contentType = getContentTypeHeader();
ContentType ct = ContentType.parse(contentType);
Charset charset = ct.getCharset() != null ? ct.getCharset() : Charset.defaultCharset();
this.responseBody = OptionalBody.body(body.toString().getBytes(charset),
new au.com.dius.pact.core.model.ContentType(contentType));
}
return this;
}
/**
* Response body to return
*
* @param body Response body built using the Pact body DSL
*/
public PactDslResponse body(DslPart body) {
DslPart parent = body.close();
if (parent instanceof PactDslJsonRootValue) {
((PactDslJsonRootValue)parent).setEncodeJson(true);
}
responseMatchers.addCategory(parent.getMatchers());
responseGenerators.addGenerators(parent.generators);
Charset charset = Charset.defaultCharset();
String contentType = ContentType.APPLICATION_JSON.toString();
if (isContentTypeHeaderNotSet()) {
matchHeader(CONTENT_TYPE, DEFAULT_JSON_CONTENT_TYPE_REGEX, contentType);
} else {
contentType = getContentTypeHeader();
ContentType ct = ContentType.parse(contentType);
charset = ct.getCharset() != null ? ct.getCharset() : Charset.defaultCharset();
}
if (parent.getBody() != null) {
responseBody = OptionalBody.body(parent.getBody().toString().getBytes(charset),
new au.com.dius.pact.core.model.ContentType(contentType));
} else {
responseBody = OptionalBody.nullBody();
}
return this;
}
/**
* Response body to return
*
* @param body Response body as an XML Document
*/
public PactDslResponse body(Document body) throws TransformerException {
if (isContentTypeHeaderNotSet()) {
responseHeaders.put(CONTENT_TYPE, Collections.singletonList(ContentType.APPLICATION_XML.toString()));
responseBody = OptionalBody.body(ConsumerPactBuilder.xmlToString(body).getBytes());
} else {
String contentType = getContentTypeHeader();
ContentType ct = ContentType.parse(contentType);
Charset charset = ct.getCharset() != null ? ct.getCharset() : Charset.defaultCharset();
responseBody = OptionalBody.body(ConsumerPactBuilder.xmlToString(body).getBytes(charset),
new au.com.dius.pact.core.model.ContentType(contentType));
}
return this;
}
/**
* Response body as a binary data. It will match any expected bodies against the content type.
* @param example Example contents to use in the consumer test
* @param contentType Content type of the data
*/
public PactDslResponse withBinaryData(byte[] example, String contentType) {
responseBody = OptionalBody.body(example, au.com.dius.pact.core.model.ContentType.fromString(contentType));
responseHeaders.put(CONTENT_TYPE, Collections.singletonList(contentType));
responseMatchers.addCategory("body").addRule("$", new ContentTypeMatcher(contentType));
return this;
}
/**
* Match a response header. A random example header value will be generated from the provided regular expression.
*
* @param header Header to match
* @param regexp Regular expression to match
*/
public PactDslResponse matchHeader(String header, String regexp) {
return matchHeader(header, regexp, new Generex(regexp).random());
}
/**
* Match a response header.
*
* @param header Header to match
* @param regexp Regular expression to match
* @param headerExample Example value to use
*/
public PactDslResponse matchHeader(String header, String regexp, String headerExample) {
responseMatchers.addCategory("header").setRule(header, new RegexMatcher(regexp));
responseHeaders.put(header, Collections.singletonList(headerExample));
return this;
}
private void addInteraction() {
consumerPactBuilder.getInteractions().add(new RequestResponseInteraction(
request.description,
request.state,
new Request(request.requestMethod, request.path, request.query,
request.requestHeaders, request.requestBody, request.requestMatchers, request.requestGenerators),
new Response(responseStatus, responseHeaders, responseBody, responseMatchers, responseGenerators),
null
));
}
/**
* Terminates the DSL and builds a pact to represent the interactions
*/
public RequestResponsePact toPact() {
addInteraction();
return new RequestResponsePact(request.provider, request.consumer, consumerPactBuilder.getInteractions());
}
/**
* Description of the request that is expected to be received
*
* @param description request description
*/
public PactDslRequestWithPath uponReceiving(String description) {
addInteraction();
return new PactDslRequestWithPath(consumerPactBuilder, request, description, defaultRequestValues,
defaultResponseValues);
}
/**
* Adds a provider state to this interaction
* @param state Description of the state
*/
public PactDslWithState given(String state) {
addInteraction();
return new PactDslWithState(consumerPactBuilder, request.consumer.getName(), request.provider.getName(),
new ProviderState(state), defaultRequestValues, defaultResponseValues);
}
/**
* Adds a provider state to this interaction
* @param state Description of the state
* @param params Data parameters for this state
*/
public PactDslWithState given(String state, Map<String, Object> params) {
addInteraction();
return new PactDslWithState(consumerPactBuilder, request.consumer.getName(), request.provider.getName(),
new ProviderState(state, params), defaultRequestValues, defaultResponseValues);
}
/**
* Adds a header that will have it's value injected from the provider state
* @param name Header Name
* @param expression Expression to be evaluated from the provider state
* @param example Example value to use in the consumer test
*/
public PactDslResponse headerFromProviderState(String name, String expression, String example) {
responseGenerators.addGenerator(Category.HEADER, name, new ProviderStateGenerator(expression, DataType.STRING));
responseHeaders.put(name, Collections.singletonList(example));
return this;
}
/**
* Match a set cookie header
* @param cookie Cookie name to match
* @param regex Regex to match the cookie value with
* @param example Example value
*/
public PactDslResponse matchSetCookie(String cookie, String regex, String example) {
au.com.dius.pact.core.model.matchingrules.Category header = responseMatchers.addCategory("header");
if (header.numRules("set-cookie") > 0) {
header.addRule("set-cookie", new RegexMatcher(Pattern.quote(cookie + "=") + regex));
} else {
header.setRule("set-cookie", new RegexMatcher(Pattern.quote(cookie + "=") + regex), RuleLogic.OR);
}
if (responseHeaders.containsKey("set-cookie")) {
responseHeaders.get("set-cookie").add(cookie + "=" + example);
} else {
responseHeaders.put("set-cookie", newArrayList(cookie + "=" + example));
}
return this;
}
/**
* XML Response body to return
*
* @param xmlBuilder XML Builder used to construct the XML document
*/
public PactDslResponse body(PactXmlBuilder xmlBuilder) {
responseMatchers.addCategory(xmlBuilder.getMatchingRules());
responseGenerators.addGenerators(xmlBuilder.getGenerators());
if (isContentTypeHeaderNotSet()) {
responseHeaders.put(CONTENT_TYPE, Collections.singletonList(ContentType.APPLICATION_XML.toString()));
responseBody = OptionalBody.body(xmlBuilder.asBytes());
} else {
String contentType = getContentTypeHeader();
ContentType ct = ContentType.parse(contentType);
Charset charset = ct.getCharset() != null ? ct.getCharset() : Charset.defaultCharset();
responseBody = OptionalBody.body(xmlBuilder.asBytes(charset),
new au.com.dius.pact.core.model.ContentType(contentType));
}
return this;
}
protected boolean isContentTypeHeaderNotSet() {
return responseHeaders.keySet().stream().noneMatch(key -> key.equalsIgnoreCase(CONTENT_TYPE));
}
protected String getContentTypeHeader() {
return responseHeaders.entrySet().stream().filter(entry -> entry.getKey().equalsIgnoreCase(CONTENT_TYPE))
.findFirst()
.map(entry -> entry.getValue().get(0)).orElse("");
}
}