com.github.tomakehurst.wiremock.matching.MatchResult Java Examples

The following examples show how to use com.github.tomakehurst.wiremock.matching.MatchResult. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpSenderCompatibilityKit.java    From micrometer with Apache License 2.0 6 votes vote down vote up
@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"))
            )));
}
 
Example #2
Source File: HttpSenderCompatibilityKit.java    From micrometer with Apache License 2.0 6 votes vote down vote up
@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")));
}
 
Example #3
Source File: HttpSenderCompatibilityKit.java    From micrometer with Apache License 2.0 6 votes vote down vote up
@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")));
}
 
Example #4
Source File: HttpSenderCompatibilityKit.java    From micrometer with Apache License 2.0 6 votes vote down vote up
@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")));
}
 
Example #5
Source File: SimilarHtmlPattern.java    From ogham with Apache License 2.0 6 votes vote down vote up
@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();
		}
	};
}
 
Example #6
Source File: RequestVerifierFilter.java    From spring-cloud-netflix with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #7
Source File: WireMockRestServiceServer.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
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());
		}
	};
}
 
Example #8
Source File: WireMockVerifyHelper.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
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);
	}
}
 
Example #9
Source File: ProtoMatchers.java    From firebase-android-sdk with Apache License 2.0 4 votes vote down vote up
@Override
public MatchResult match(T value) {
  return MatchResult.of(predicate.test(converter.apply(value)));
}