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

The following examples show how to use com.github.tomakehurst.wiremock.matching.RequestPattern. 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: WireMockRestServiceServer.java    From spring-cloud-contract with Apache License 2.0 6 votes vote down vote up
private void bodyPatterns(ResponseActions expect, RequestPattern request) {
	if (request.getBodyPatterns() == null) {
		return;
	}
	for (final ContentPattern<?> pattern : request.getBodyPatterns()) {
		if (pattern instanceof MatchesJsonPathPattern) {
			expect.andExpect(MockRestRequestMatchers
					.jsonPath(((MatchesJsonPathPattern) pattern).getMatchesJsonPath())
					.exists());
		}
		else if (pattern instanceof MatchesXPathPattern) {
			expect.andExpect(xpath((MatchesXPathPattern) pattern));
		}
		expect.andExpect(matchContents(pattern));
	}
}
 
Example #2
Source File: WireMockRestServiceServer.java    From spring-cloud-contract with Apache License 2.0 6 votes vote down vote up
private Matcher<String> requestMatcher(RequestPattern request) {
	return new TypeSafeMatcher<String>() {
		@Override
		protected boolean matchesSafely(String item) {
			if (request.getUrlPathPattern() != null) {
				return Pattern.compile(request.getUrlPathPattern())
						.matcher(withoutBaseUrl(item)).matches();
			}
			else if (request.getUrlMatcher() != null) {
				return request.getUrlMatcher().match(item).isExactMatch();
			}
			else if (request.getUrlPattern() != null) {
				return Pattern.compile(request.getUrlPattern()).matcher(item)
						.matches();
			}
			return false;
		}

		@Override
		public void describeTo(Description description) {

		}
	};
}
 
Example #3
Source File: WireMockRestServiceServer.java    From spring-cloud-contract with Apache License 2.0 6 votes vote down vote up
private void requestHeaders(ResponseActions expect, RequestPattern request) {
	if (request.getHeaders() != null) {
		for (final String header : request.getHeaders().keySet()) {
			final MultiValuePattern pattern = request.getHeaders().get(header);
			expect.andExpect(header(header, new BaseMatcher<String>() {

				@Override
				public boolean matches(Object item) {
					return pattern.match(
							new MultiValue(header, Arrays.asList((String) item)))
							.isExactMatch();
				}

				@Override
				public void describeTo(Description description) {
					description
							.appendText("should match header: " + header + " with ")
							.appendText(pattern.getExpected());
				}
			}));
		}
	}
}
 
Example #4
Source File: BasicMappingBuilder.java    From spring-cloud-contract with Apache License 2.0 6 votes vote down vote up
@Override
public StubMapping build() {
	if (this.scenarioName == null && (this.requiredScenarioState != null
			|| this.newScenarioState != null)) {
		throw new IllegalStateException(
				"Scenario name must be specified to require or set a new scenario state");
	}
	RequestPattern requestPattern = this.requestPatternBuilder.build();
	ResponseDefinition response = (this.responseDefBuilder != null
			? this.responseDefBuilder : aResponse()).build();
	StubMapping mapping = new StubMapping(requestPattern, response);
	mapping.setPriority(this.priority);
	mapping.setScenarioName(this.scenarioName);
	mapping.setRequiredScenarioState(this.requiredScenarioState);
	mapping.setNewScenarioState(this.newScenarioState);
	mapping.setUuid(this.id);
	mapping.setName(this.name);
	mapping.setPersistent(this.isPersistent);
	mapping.setPostServeActions(
			this.postServeActions.isEmpty() ? null : this.postServeActions);
	mapping.setMetadata(this.metadata);
	return mapping;
}
 
Example #5
Source File: GoogleHttpClientEdgeGridInterceptorIntegrationTest.java    From AkamaiOPEN-edgegrid-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testInterceptor() throws URISyntaxException, IOException, RequestSigningException {

    wireMockServer.stubFor(get(urlPathEqualTo("/billing-usage/v1/reportSources"))
            .withHeader("Authorization", matching(".*"))
            .withHeader("Host", equalTo(SERVICE_MOCK))
            .willReturn(aResponse()
                    .withStatus(302)
                    .withHeader("Location", "/billing-usage/v1/reportSources/alternative")));

    wireMockServer.stubFor(get(urlPathEqualTo("/billing-usage/v1/reportSources/alternative"))
            .withHeader("Authorization", matching(".*"))
            .withHeader("Host", equalTo(SERVICE_MOCK))
            .willReturn(aResponse()
                    .withStatus(200)
                    .withHeader("Content-Type", "text/xml")
                    .withBody("<response>Some content</response>")));

    HttpRequestFactory requestFactory = createSigningRequestFactory();

    URI uri = URI.create("https://endpoint.net/billing-usage/v1/reportSources");
    HttpRequest request = requestFactory.buildGetRequest(new GenericUrl(uri));
    // Mimic what the library does to process the interceptor.
    request.setFollowRedirects(true).execute();

    List<LoggedRequest> loggedRequests = wireMockServer.findRequestsMatching(RequestPattern
            .everything()).getRequests();
    MatcherAssert.assertThat(loggedRequests.get(0).getHeader("Authorization"),
            Matchers.not(CoreMatchers.equalTo(loggedRequests.get(1).getHeader("Authorization"))));

}
 
Example #6
Source File: ApacheHttpClientEdgeGridInterceptorIntegrationTest.java    From AkamaiOPEN-edgegrid-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testInterceptor() throws URISyntaxException, IOException, RequestSigningException {
    wireMockServer.stubFor(get(urlPathEqualTo("/billing-usage/v1/reportSources"))
            .withHeader("Authorization", matching(".*"))
            .withHeader("Host", equalTo(getHost()))
            .willReturn(aResponse()
                    .withStatus(302)
                    .withHeader("Location", "/billing-usage/v1/reportSources/alternative")));

    wireMockServer.stubFor(get(urlPathEqualTo("/billing-usage/v1/reportSources/alternative"))
            .withHeader("Authorization", matching(".*"))
            .withHeader("Host", equalTo(getHost()))
            .willReturn(aResponse()
                    .withStatus(200)
                    .withHeader("Content-Type", "text/xml")
                    .withBody("<response>Some content</response>")));

    HttpGet request = new HttpGet("http://endpoint.net/billing-usage/v1/reportSources");

    HttpClient client = HttpClientSetup.getHttpClientWithRelaxedSsl()
            .addInterceptorFirst(new ApacheHttpClientEdgeGridInterceptor(credential))
            .setRoutePlanner(new ApacheHttpClientEdgeGridRoutePlanner(credential))
            .build();

    client.execute(request);

    List<LoggedRequest> loggedRequests = wireMockServer.findRequestsMatching(RequestPattern
            .everything()).getRequests();

    MatcherAssert.assertThat(loggedRequests.get(0).getHeader("Authorization"),
            Matchers.not(CoreMatchers.equalTo(loggedRequests.get(1).getHeader("Authorization"))));
}
 
Example #7
Source File: RestAssuredIntegrationTest.java    From AkamaiOPEN-edgegrid-java with Apache License 2.0 5 votes vote down vote up
@Test
public void signAgainFollowedRedirects() throws URISyntaxException, IOException {

    wireMockServer.stubFor(get(urlPathEqualTo("/billing-usage/v1/reportSources"))
            .withHeader("Authorization", matching(".*"))
            .withHeader("Host", equalTo(SERVICE_MOCK))
            .willReturn(aResponse()
                    .withStatus(302)
                    .withHeader("Location", "/billing-usage/v1/reportSources/alternative")));

    wireMockServer.stubFor(get(urlPathEqualTo("/billing-usage/v1/reportSources/alternative"))
            .withHeader("Authorization", matching(".*"))
            .withHeader("Host", equalTo(SERVICE_MOCK))
            .willReturn(aResponse()
                    .withStatus(200)
                    .withHeader("Content-Type", "text/xml")
                    .withBody("<response>Some content</response>")));

    getBaseRequestSpecification()
            .get("/billing-usage/v1/reportSources")
            .then().statusCode(200);

    List<LoggedRequest> loggedRequests = wireMockServer.findRequestsMatching(RequestPattern
            .everything()).getRequests();
    MatcherAssert.assertThat(loggedRequests.get(0).getHeader("Authorization"),
            Matchers.not(CoreMatchers.equalTo(loggedRequests.get(1).getHeader("Authorization"))));
}
 
Example #8
Source File: RestAssuredIntegrationTest.java    From AkamaiOPEN-edgegrid-java with Apache License 2.0 5 votes vote down vote up
@Test
public void replaceProvidedHostHeaderOnlyInApacheClient() throws URISyntaxException, IOException,
        RequestSigningException {

    wireMockServer.stubFor(get(urlPathEqualTo("/billing-usage/v1/reportSources"))
            .withHeader("Authorization", matching(".*"))
            .willReturn(aResponse()
                    .withStatus(200)));

    getBaseRequestSpecification()
            .header("Host", "ignored-hostname.com")
            .filter(new Filter() {
                @Override
                public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
                    MatcherAssert.assertThat(requestSpec.getHeaders().getList("Host").size(),
                            CoreMatchers.equalTo(1));
                    MatcherAssert.assertThat(requestSpec.getHeaders().get("Host").getValue(),
                            CoreMatchers.equalTo("ignored-hostname.com"));
                    return ctx.next(requestSpec, responseSpec);
                }
            })
            .get("/billing-usage/v1/reportSources");


    List<LoggedRequest> loggedRequests = wireMockServer.findRequestsMatching(RequestPattern
            .everything()).getRequests();
    MatcherAssert.assertThat(loggedRequests.get(0).getHeader("Host"),
            CoreMatchers.equalTo(SERVICE_MOCK));

}
 
Example #9
Source File: RestAssuredEdgeGridFilterTest.java    From AkamaiOPEN-edgegrid-java with Apache License 2.0 5 votes vote down vote up
@Test
// Due to limitations of REST-assured we cannot sign again followed redirects
// https://github.com/akamai-open/AkamaiOPEN-edgegrid-java/issues/21
public void cannotSignAgainFollowedRedirects() throws URISyntaxException, IOException {

    wireMockServer.stubFor(get(urlPathEqualTo("/billing-usage/v1/reportSources"))
            .withHeader("Authorization", matching(".*"))
            .withHeader("Host", equalTo(SERVICE_MOCK))
            .willReturn(aResponse()
                    .withStatus(302)
                    .withHeader("Location", "/billing-usage/v1/reportSources/alternative")));

    wireMockServer.stubFor(get(urlPathEqualTo("/billing-usage/v1/reportSources/alternative"))
            .withHeader("Authorization", matching(".*"))
            .withHeader("Host", equalTo(SERVICE_MOCK))
            .willReturn(aResponse()
                    .withStatus(200)
                    .withHeader("Content-Type", "text/xml")
                    .withBody("<response>Some content</response>")));

    RestAssured.given()
            .relaxedHTTPSValidation()
            .filter(new RestAssuredEdgeGridFilter(credential))
            .get("/billing-usage/v1/reportSources")
            .then().statusCode(200);

    List<LoggedRequest> loggedRequests = wireMockServer.findRequestsMatching(RequestPattern
            .everything()).getRequests();
    MatcherAssert.assertThat(loggedRequests.get(0).getHeader("Authorization"),
            CoreMatchers.equalTo(loggedRequests.get(1).getHeader("Authorization")));
}
 
Example #10
Source File: RestAssuredEdgeGridFilterIntegrationTest.java    From AkamaiOPEN-edgegrid-java with Apache License 2.0 5 votes vote down vote up
@Test
// Due to limitations of REST-assured we cannot sign again followed redirects
// https://github.com/akamai-open/AkamaiOPEN-edgegrid-java/issues/21
public void cannotSignAgainFollowedRedirects() throws URISyntaxException, IOException {

    wireMockServer.stubFor(get(urlPathEqualTo("/billing-usage/v1/reportSources"))
            .withHeader("Authorization", matching(".*"))
            .withHeader("Host", equalTo(SERVICE_MOCK))
            .willReturn(aResponse()
                    .withStatus(302)
                    .withHeader("Location", "/billing-usage/v1/reportSources/alternative")));

    wireMockServer.stubFor(get(urlPathEqualTo("/billing-usage/v1/reportSources/alternative"))
            .withHeader("Authorization", matching(".*"))
            .withHeader("Host", equalTo(SERVICE_MOCK))
            .willReturn(aResponse()
                    .withStatus(200)
                    .withHeader("Content-Type", "text/xml")
                    .withBody("<response>Some content</response>")));

    RestAssured.given()
            .relaxedHTTPSValidation()
            .filter(new RestAssuredEdgeGridFilter(credential))
            .get("/billing-usage/v1/reportSources")
            .then().statusCode(200);

    List<LoggedRequest> loggedRequests = wireMockServer.findRequestsMatching(RequestPattern
            .everything()).getRequests();
    MatcherAssert.assertThat(loggedRequests.get(0).getHeader("Authorization"),
            CoreMatchers.equalTo(loggedRequests.get(1).getHeader("Authorization")));
}