"bodyPatterns": [{
"equalToJsonTemplate": {
"aStringField": "",
"aIntField": 1,
"aNullField": null,
"anObject": {
"anotherField": 1
},
"aList": [1, "2", 3 ],
},
"ignoreExtraElements": true}]
the following would match
{
"aStringField": "something",
"aIntField": 123,
"aNullField": {},
"anObject": {
"anotherField": 123
},
"aList": [123, "456", 789],
}null would be like a wildcard, anything matches but the field needs to be present. Else leaving the field out and using ignoreExtraElement would also act like a wildcard and make the field optional.Is there a similar feature ? Or can I suggest the feature ? There is plenty of room for suggestions like allowing fields to specify constraints on the values, but then that would start to look like the use of matchesJsonPath and using sub expressions to match the whole tree.Cheers.
What do you think of the idea of using JSON Schema instead of an example?
stubFor(WireMock.post(urlEqualTo(url)).withRequestBody(equalToJson(readFile(requestFile)))
.willReturn(aResponse().withStatus(HttpStatus.SC_OK)
.withBody(replaceValues(readFile(responseFile), responseParams))));
Custom match
==============
stubFor(post(urlPathMatching(url)).withRequestBody(equalToJson(readFile(requestFile)))
.andMatching("myMatcher", Parameters.one("stub-path", requestFile))
.willReturn(aResponse().withStatus(HttpStatus.SC_OK).withBody(replaceValues(readFile(responseFile), responseParams))));
url is same for both the stubs above.
WiremockRule
============
@Rule
public WireMockRule wireMockRule = new WireMockRule( wireMockConfig().port(WIREMOCK_PORT).extensions(DynamoMatcher.class).notifier(new ConsoleNotifier(false)));
Am I doing anything wrong?
@Override
public String getName() {
return "myMatcher";
}
@Override
public MatchResult match(Request request, Parameters parameters) {
String stubbedRequestFilePath = parameters.get("stub-path").toString();
try {
String stubbedRequest = Stubber.readFile(stubbedRequestFilePath);
String requestBody = new String(request.getBody());
assertJsonEquals(stubbedRequest, requestBody);
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return null;
}
so when I debug, the stubbedRequest is same as what I am stubbing for a custom matcher. But the requestBody contains the request of first basic stub. I am assuming that, since the url is same for both the stubs it is blindly getting into custom matcher. or custom stub overriding the basic stub.
Is it possible to specify the wiremock rule for a single stub?
String requestStub = replaceValues(readFile(requestFile), requestParams);
stubFor(post(urlPathMatching(url)).withRequestBody(equalToJson(requestStub))
.andMatching("dynamoMatcher", Parameters.one("request-stub", requestStub))
.willReturn(aResponse().withStatus(HttpStatus.SC_OK).withBody(readFile(responseFile))));
Matcher
========
@Override
public MatchResult match(Request request, Parameters parameters) {
String stubbedRequest = parameters.get("request-stub").toString();
String requestBody = new String(request.getBody());
assertJsonEquals(stubbedRequest, requestBody);
return null;
}
@Rule
public WireMockRule wireMockRule = new WireMockRule( wireMockConfig().port(WIREMOCK_PORT).extensions(MyMaytcher.class).notifier(new ConsoleNotifier(false)));
@Before
public void setup() {
wireMock = WireMock.create().port(wireMockRule.port()).build();
}
String firstRequestStub = replaceValues(readFile(requestFile), requestParams);
stubFor(WireMock.post(urlEqualTo(url)).withRequestBody(equalToJson(firstRequestStub))
.willReturn(aResponse().withStatus(HttpStatus.SC_OK).withBody(readFile(firstResponseFile))));
String secondRequestStub = replaceValues(readFile(requestFile), requestParams);
stubFor(post(urlPathMatching(url)).withRequestBody(equalToJson(secondRequestStub))
.andMatching("dynamoMatcher", Parameters.one("request-stub", secondRequestStub))
.willReturn(aResponse().withStatus(HttpStatus.SC_OK).withBody(readFile(secondResponseFile))));
I am using the above two stubs for two external calls in a single test which have same endpoint and different requests and responses. For some reason, for the first external call, it steps into the custom matcher of second stub and it fails..
Please assist me on this.