use of com.github.tomakehurst.wiremock.matching.RequestPatternBuilder in project junit-servers by mjeanroy.
the class WireMockTestUtils method assertUploadRequest.
/**
* Verify that a given request has been triggered.
*
* @param endpoint Request endpoint.
* @param method Request method.
*/
static void assertUploadRequest(String endpoint, HttpMethod method, File file) {
UrlPattern urlPattern = urlEqualTo(endpoint);
RequestMethod rqMethod = new RequestMethod(method.name());
RequestPatternBuilder rq = new RequestPatternBuilder(rqMethod, urlPattern).withAllRequestBodyParts(new MultipartValuePatternBuilder().withName(file.getName()).withBody(new BinaryEqualToPattern(TestUtils.readFile(file))));
WireMock.verify(1, rq);
}
use of com.github.tomakehurst.wiremock.matching.RequestPatternBuilder in project junit-servers by mjeanroy.
the class WireMockTestUtils method assertRequest.
/**
* Verify that a given request has been triggered.
*
* @param endpoint Request endpoint.
* @param method Request method.
*/
static void assertRequest(String endpoint, HttpMethod method) {
UrlPattern urlPattern = urlEqualTo(endpoint);
RequestMethod rqMethod = new RequestMethod(method.name());
RequestPatternBuilder rq = new RequestPatternBuilder(rqMethod, urlPattern);
WireMock.verify(1, rq);
}
use of com.github.tomakehurst.wiremock.matching.RequestPatternBuilder in project spring-cloud-contract by spring-cloud.
the class WireMockRequestStubStrategy method doAppendBody.
private void doAppendBody(RequestPatternBuilder requestPattern) {
if (request.getBody() == null) {
return;
}
boolean bodyHasMatchingStrategy = request.getBody().getClientValue() instanceof MatchingStrategy;
MatchingStrategy matchingStrategy = getMatchingStrategyFromBody(request.getBody());
if (contentType == ContentType.JSON) {
Object clientSideBody = MapConverter.transformToClientValues(request.getBody());
Object originalBody = Optional.ofNullable(matchingStrategy).map(DslProperty::getClientValue).orElse(null);
if (bodyHasMatchingStrategy) {
requestPattern.withRequestBody(convertToValuePattern(matchingStrategy));
} else if (clientSideBody instanceof Pattern || clientSideBody instanceof RegexProperty) {
requestPattern.withRequestBody(convertToValuePattern(appendBodyRegexpMatchPattern(request.getBody(), contentType)));
} else {
Object body = JsonToJsonPathsConverter.removeMatchingJsonPaths(originalBody, request.getBodyMatchers());
JsonPaths values = JsonToJsonPathsConverter.transformToJsonPathWithStubsSideValuesAndNoArraySizeCheck(body);
if ((values.isEmpty() && request.getBodyMatchers() != null && !request.getBodyMatchers().hasMatchers()) || onlySizeAssertionsArePresent(values)) {
try {
requestPattern.withRequestBody(WireMock.equalToJson(new ObjectMapper().writeValueAsString(getMatchingStrategy(request.getBody().getClientValue()).getClientValue()), false, false));
} catch (JsonProcessingException e) {
throw new IllegalArgumentException("The MatchingStrategy could not be serialized", e);
}
} else {
values.stream().filter(v -> !v.assertsSize()).forEach(it -> requestPattern.withRequestBody(WireMock.matchingJsonPath(it.jsonPath().replace("\\\\", "\\"))));
}
}
Optional.ofNullable(request.getBodyMatchers()).map(BodyMatchers::matchers).ifPresent(bodyMatchers -> bodyMatchers.forEach(bodyMatcher -> {
String newPath = JsonToJsonPathsConverter.convertJsonPathAndRegexToAJsonPath(bodyMatcher, originalBody);
requestPattern.withRequestBody(WireMock.matchingJsonPath(newPath.replace("\\\\", "\\")));
}));
} else if (contentType == ContentType.XML) {
Object originalBody = Optional.ofNullable(matchingStrategy).map(DslProperty::getClientValue).orElse(null);
if (bodyHasMatchingStrategy) {
requestPattern.withRequestBody(convertToValuePattern(matchingStrategy));
} else {
Object body = XmlToXPathsConverter.removeMatchingXPaths(originalBody, request.getBodyMatchers());
List<BodyMatcher> byEqualityMatchersFromXml = XmlToXPathsConverter.mapToMatchers(body);
byEqualityMatchersFromXml.forEach(bodyMatcher -> addWireMockStubMatchingSection(bodyMatcher, requestPattern, originalBody));
}
Optional.ofNullable(request.getBodyMatchers()).map(BodyMatchers::matchers).ifPresent(bodyMatchers -> bodyMatchers.forEach(bodyMatcher -> addWireMockStubMatchingSection(bodyMatcher, requestPattern, originalBody)));
} else if (containsPattern(request.getBody())) {
requestPattern.withRequestBody(convertToValuePattern(appendBodyRegexpMatchPattern(request.getBody())));
} else {
requestBodyGuessedFromMatchingStrategy(requestPattern);
}
}
use of com.github.tomakehurst.wiremock.matching.RequestPatternBuilder in project furms by unity-idm.
the class UnityClientTest method shouldSend_POST_toUnityServer.
@Test
void shouldSend_POST_toUnityServer() {
// given
RequestPattern request = new RequestPatternBuilder(POST, new UrlPattern(new EqualToPattern("/path/to/unity/test"), false)).build();
server.addStubMapping(new StubMapping(request, ResponseDefinitionBuilder.responseDefinition().withHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE).withBody("{\"field\": \"string\"}").withStatus(SC_OK).build()));
// when
unityClient.post("/path/to/unity/test", new SampleDto("string"));
// then
VerificationResult verificationResult = server.countRequestsMatching(request);
assertThat(verificationResult.getCount()).isEqualTo(1);
}
use of com.github.tomakehurst.wiremock.matching.RequestPatternBuilder in project furms by unity-idm.
the class UnityClientTest method shouldSend_PUT_toUnityServer.
@Test
void shouldSend_PUT_toUnityServer() {
// given
RequestPattern request = new RequestPatternBuilder(PUT, new UrlPattern(new EqualToPattern("/path/to/unity/test"), false)).build();
server.addStubMapping(new StubMapping(request, ResponseDefinitionBuilder.responseDefinition().withHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE).withBody("{\"field\": \"string\"}").withStatus(SC_OK).build()));
// when
unityClient.put("/path/to/unity/test", Map.of("param", "test"));
// then
VerificationResult verificationResult = server.countRequestsMatching(request);
assertThat(verificationResult.getCount()).isEqualTo(1);
}
Aggregations