Java源码示例:com.github.tomakehurst.wiremock.matching.MatchResult
示例1
@ParameterizedTest
@DisplayName("successfully send a request with NO body and receive a response with NO body")
@EnumSource(HttpSender.Method.class)
void successfulRequestSentWithNoBody(HttpSender.Method method, @WiremockResolver.Wiremock WireMockServer server) throws Throwable {
server.stubFor(any(urlEqualTo("/metrics")));
HttpSender.Response response = httpSender.newRequest(server.baseUrl() + "/metrics")
.withMethod(method)
.send();
assertThat(response.code()).isEqualTo(200);
assertThat(response.body()).isEqualTo(HttpSender.Response.NO_RESPONSE_BODY);
server.verify(WireMock.requestMadeFor(request ->
MatchResult.aggregate(
MatchResult.of(request.getMethod().getName().equals(method.name())),
MatchResult.of(request.getUrl().equals("/metrics"))
)));
}
示例2
@ParameterizedTest
@DisplayName("successfully send a request with a body and receive a response with a body")
@EnumSource(value = HttpSender.Method.class, names = {"POST", "PUT"})
void successfulRequestSentWithBody(HttpSender.Method method, @WiremockResolver.Wiremock WireMockServer server) throws Throwable {
server.stubFor(any(urlEqualTo("/metrics"))
.willReturn(ok("a body")));
HttpSender.Response response = httpSender.newRequest(server.baseUrl() + "/metrics")
.withMethod(method)
.accept("customAccept")
.withContent("custom/type", "this is a line")
.send();
assertThat(response.code()).isEqualTo(200);
assertThat(response.body()).isEqualTo("a body");
server.verify(WireMock.requestMadeFor(request ->
MatchResult.aggregate(
MatchResult.of(request.getMethod().getName().equals(method.name())),
MatchResult.of(request.getUrl().equals("/metrics"))
))
.withHeader("Accept", equalTo("customAccept"))
.withHeader("Content-Type", containing("custom/type")) // charset may be added to the type
.withRequestBody(equalTo("this is a line")));
}
示例3
@ParameterizedTest
@EnumSource(HttpSender.Method.class)
void basicAuth(HttpSender.Method method, @WiremockResolver.Wiremock WireMockServer server) throws Throwable {
server.stubFor(any(urlEqualTo("/metrics"))
.willReturn(unauthorized()));
HttpSender.Response response = httpSender.newRequest(server.baseUrl() + "/metrics")
.withMethod(method)
.withBasicAuthentication("superuser", "superpassword")
.send();
assertThat(response.code()).isEqualTo(401);
server.verify(WireMock.requestMadeFor(request ->
MatchResult.aggregate(
MatchResult.of(request.getMethod().getName().equals(method.name())),
MatchResult.of(request.getUrl().equals("/metrics"))
))
.withBasicAuth(new BasicCredentials("superuser", "superpassword")));
}
示例4
@ParameterizedTest
@EnumSource(HttpSender.Method.class)
void customHeader(HttpSender.Method method, @WiremockResolver.Wiremock WireMockServer server) throws Throwable {
server.stubFor(any(urlEqualTo("/metrics"))
.willReturn(unauthorized()));
HttpSender.Response response = httpSender.newRequest(server.baseUrl() + "/metrics")
.withMethod(method)
.withHeader("customHeader", "customHeaderValue")
.send();
assertThat(response.code()).isEqualTo(401);
server.verify(WireMock.requestMadeFor(request ->
MatchResult.aggregate(
MatchResult.of(request.getMethod().getName().equals(method.name())),
MatchResult.of(request.getUrl().equals("/metrics"))
))
.withHeader("customHeader", equalTo("customHeaderValue")));
}
示例5
@Override
public MatchResult match(String value) {
DetailedDiff diff = HtmlUtils.compare(expectedValue, value);
return new MatchResult() {
@Override
public boolean isExactMatch() {
return diff.similar();
}
@Override
public double getDistance() {
return diff.getAllDifferences().size();
}
};
}
示例6
@Override
public Response filter(FilterableRequestSpecification requestSpec,
FilterableResponseSpecification responseSpec, FilterContext context) {
Map<String, Object> configuration = getConfiguration(requestSpec, context);
configuration.put("contract.jsonPaths", this.jsonPaths.keySet());
Response response = context.next(requestSpec, responseSpec);
if (requestSpec.getBody() != null && !this.jsonPaths.isEmpty()) {
String actual = new String((byte[]) requestSpec.getBody());
for (JsonPath jsonPath : this.jsonPaths.values()) {
new JsonPathValue(jsonPath, actual).assertHasValue(Object.class,
"an object");
}
}
if (this.builder != null) {
this.builder.willReturn(getResponseDefinition(response));
StubMapping stubMapping = this.builder.build();
MatchResult match = stubMapping.getRequest()
.match(new WireMockRestAssuredRequestAdapter(requestSpec));
assertThat(match.isExactMatch()).as("wiremock did not match request")
.isTrue();
configuration.put("contract.stubMapping", stubMapping);
}
return response;
}
示例7
private RequestMatcher matchContents(
@SuppressWarnings("rawtypes") final ContentPattern pattern) {
return new RequestMatcher() {
@Override
public void match(ClientHttpRequest request)
throws IOException, AssertionError {
MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
@SuppressWarnings("unchecked")
MatchResult result = pattern.match(mockRequest.getBodyAsString());
MatcherAssert.assertThat(
"Request as string [" + mockRequest.getBodyAsString() + "]",
result.isExactMatch());
}
};
}
示例8
public void configure(T result) {
Map<String, Object> configuration = getConfiguration(result);
byte[] requestBodyContent = getRequestBodyContent(result);
if (requestBodyContent != null) {
String actual = new String(requestBodyContent, Charset.forName("UTF-8"));
for (JsonPath jsonPath : this.jsonPaths.values()) {
new JsonPathValue(jsonPath, actual).assertHasValue(Object.class,
"an object");
}
}
configuration.put("contract.jsonPaths", this.jsonPaths.keySet());
if (this.contentType != null) {
configuration.put("contract.contentType", this.contentType);
MediaType resultType = getContentType(result);
assertThat(resultType).isNotNull().as("no content type");
assertThat(this.contentType.includes(resultType)).isTrue()
.as("content type did not match");
}
if (this.builder != null) {
this.builder.willReturn(getResponseDefinition(result));
StubMapping stubMapping = this.builder.build();
Request request = getWireMockRequest(result);
MatchResult match = stubMapping.getRequest().match(request);
assertThat(match.isExactMatch()).as("wiremock did not match request")
.isTrue();
configuration.put("contract.stubMapping", stubMapping);
}
}
示例9
@Override
public MatchResult match(T value) {
return MatchResult.of(predicate.test(converter.apply(value)));
}