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

The following examples show how to use com.github.tomakehurst.wiremock.matching.UrlPattern. 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: RedirectTest.java    From gradle-download-task with Apache License 2.0 6 votes vote down vote up
/**
 * Make sure the plugin fails with too many redirects
 * @throws Exception if anything goes wrong
 */
@Test(expected = TaskExecutionException.class)
public void tooManyRedirects() throws Exception {
    UrlPattern up1 = urlPathEqualTo("/" + REDIRECT);
    redirectWireMockRule.stubFor(get(up1)
            .withQueryParam("r", matching("[0-9]+"))
            .willReturn(aResponse()
                    .withStatus(HttpServletResponse.SC_FOUND)
                    .withTransformer("redirect", "redirects", 51)));

    Download t = makeProjectAndTask();
    t.src(redirectWireMockRule.url(REDIRECT) + "?r=52");
    File dst = folder.newFile();
    t.dest(dst);
    t.execute();
}
 
Example #2
Source File: RedirectTest.java    From gradle-download-task with Apache License 2.0 6 votes vote down vote up
/**
 * Tests if the plugin can handle one redirect
 * @throws Exception if anything goes wrong
 */
@Test
public void oneRedirect() throws Exception {
    UrlPattern up1 = urlEqualTo("/" + REDIRECT);
    wireMockRule.stubFor(get(up1)
            .willReturn(aResponse()
                    .withStatus(HttpServletResponse.SC_FOUND)
                    .withHeader("Location", wireMockRule.url(TEST_FILE_NAME))));

    UrlPattern up2 = urlEqualTo("/" + TEST_FILE_NAME);
    wireMockRule.stubFor(get(up2)
            .willReturn(aResponse()
                    .withBody(CONTENTS)));

    Download t = makeProjectAndTask();
    t.src(wireMockRule.url(REDIRECT));
    File dst = folder.newFile();
    t.dest(dst);
    t.execute();

    String dstContents = FileUtils.readFileToString(dst);
    assertEquals(CONTENTS, dstContents);

    wireMockRule.verify(1, getRequestedFor(up1));
    wireMockRule.verify(1, getRequestedFor(up2));
}
 
Example #3
Source File: WireMockTestUtils.java    From junit-servers with MIT License 6 votes vote down vote up
private static void stubRequest(String method, String endpoint, int status, Collection<Pair> headers, String body) {
	UrlPattern urlPattern = urlEqualTo(endpoint);
	MappingBuilder request = request(method, urlPattern);

	ResponseDefinitionBuilder response = aResponse().withStatus(status);

	HttpHeaders httpHeaders = new HttpHeaders();

	for (Pair header : headers) {
		String name = header.getO1();
		List<String> values = header.getO2();
		HttpHeader h = new HttpHeader(name, values);
		httpHeaders = httpHeaders.plus(h);
	}

	response.withHeaders(httpHeaders);

	if (body != null) {
		response.withBody(body);
	}

	stubFor(request.willReturn(response));
}
 
Example #4
Source File: WireMockTestUtils.java    From junit-servers with MIT License 5 votes vote down vote up
/**
 * 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);
}
 
Example #5
Source File: RedirectTest.java    From gradle-download-task with Apache License 2.0 5 votes vote down vote up
/**
 * Tests if the plugin can handle circular redirects
 * @throws Exception if anything goes wrong
 */
@Test(expected = TaskExecutionException.class)
public void circularRedirect() throws Exception {
    UrlPattern up1 = urlPathEqualTo("/" + REDIRECT);
    wireMockRule.stubFor(get(up1)
            .willReturn(aResponse()
                    .withStatus(HttpServletResponse.SC_FOUND)
                    .withHeader("Location", "/" + REDIRECT)));

    Download t = makeProjectAndTask();
    t.src(wireMockRule.url(REDIRECT));
    File dst = folder.newFile();
    t.dest(dst);
    t.execute();
}
 
Example #6
Source File: RedirectTest.java    From gradle-download-task with Apache License 2.0 5 votes vote down vote up
/**
 * Tests if the plugin can handle ten redirects
 * @throws Exception if anything goes wrong
 */
@Test
public void tenRedirect() throws Exception {
    UrlPattern up1 = urlPathEqualTo("/" + REDIRECT);
    redirectWireMockRule.stubFor(get(up1)
            .withQueryParam("r", matching("[0-9]+"))
            .willReturn(aResponse()
                    .withStatus(HttpServletResponse.SC_FOUND)
                    .withTransformer("redirect", "redirects", 10)));

    UrlPattern up2 = urlEqualTo("/" + TEST_FILE_NAME);
    redirectWireMockRule.stubFor(get(up2)
            .willReturn(aResponse()
                    .withBody(CONTENTS)));

    Download t = makeProjectAndTask();
    t.src(redirectWireMockRule.url(REDIRECT) + "?r=10");
    File dst = folder.newFile();
    t.dest(dst);
    t.execute();

    String dstContents = FileUtils.readFileToString(dst);
    assertEquals(CONTENTS, dstContents);

    verify(10, getRequestedFor(up1));
    verify(1, getRequestedFor(up2));
}
 
Example #7
Source File: WireMockTestUtils.java    From junit-servers with MIT License 5 votes vote down vote up
/**
 * Verify that a given request has been triggered.
 *
 * @param endpoint Request endpoint.
 * @param method Request method.
 * @param cookies Cookies sent in HTTP request.
 */
static void assertRequestWithCookies(String endpoint, HttpMethod method, Iterable<Pair> cookies) {
	UrlPattern urlPattern = urlEqualTo(endpoint);
	RequestMethod rqMethod = new RequestMethod(method.name());
	RequestPatternBuilder rq = new RequestPatternBuilder(rqMethod, urlPattern);

	for (Pair cookie : cookies) {
		String cookieName = cookie.getO1();
		String cookieValue = cookie.getO2().get(0);
		rq.withCookie(cookieName, equalTo(cookieValue));
	}

	WireMock.verify(1, rq);
}
 
Example #8
Source File: WireMockTestUtils.java    From junit-servers with MIT License 5 votes vote down vote up
/**
 * Verify that a given request has been triggered.
 *
 * @param endpoint Request endpoint.
 * @param method Request method.
 * @param body Request body.
 */
static void assertRequestWithBody(String endpoint, HttpMethod method, String body) {
	UrlPattern urlPattern = urlEqualTo(endpoint);
	RequestMethod rqMethod = new RequestMethod(method.name());
	RequestPatternBuilder rq = new RequestPatternBuilder(rqMethod, urlPattern);
	rq.withRequestBody(equalTo(body));
	WireMock.verify(1, rq);
}
 
Example #9
Source File: WireMockTestUtils.java    From junit-servers with MIT License 5 votes vote down vote up
/**
 * 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);
}
 
Example #10
Source File: HttpConnectorEnv.java    From sputnik with Apache License 2.0 5 votes vote down vote up
public void stubPost(UrlPattern url, String responseFile) throws Exception {
    wireMockServer.stubFor(post(url)
            .withHeader("Authorization", equalTo("Basic dXNlcjpwYXNz"))
            .willReturn(aResponse()
                    .withStatus(HttpStatus.SC_OK)
                    .withHeader("Content-Type", "application/json")
                    .withBody(IOUtils.toString(getClass().getResourceAsStream(responseFile)))));
}
 
Example #11
Source File: HttpConnectorEnv.java    From sputnik with Apache License 2.0 5 votes vote down vote up
public void stubGet(UrlPattern url, String responseFile) throws Exception {
    wireMockServer.stubFor(get(url)
            .willReturn(aResponse()
                    .withStatus(HttpStatus.SC_OK)
                    .withHeader("Content-Type", "application/json")
                    .withBody(IOUtils.toString(getClass().getResourceAsStream(responseFile)))));
}
 
Example #12
Source File: SchemaRegistryMock.java    From fluent-kafka-streams-tests with MIT License 4 votes vote down vote up
private static UrlPattern getSchemaPattern(final Integer id) {
    return WireMock.urlPathEqualTo(SCHEMA_BY_ID_PATTERN + id);
}
 
Example #13
Source File: HttpConnectorEnv.java    From sputnik with Apache License 2.0 4 votes vote down vote up
public void stubGet(UrlPattern url, ResponseDefinitionBuilder responseDefinitionBuilder) {
    wireMockServer.stubFor(get(url)
            .withHeader("Authorization", equalTo("Basic dXNlcjpwYXNz"))
            .willReturn(responseDefinitionBuilder));
}
 
Example #14
Source File: BasicMappingBuilder.java    From spring-cloud-contract with Apache License 2.0 4 votes vote down vote up
BasicMappingBuilder(RequestMethod method, UrlPattern urlPattern) {
	this.requestPatternBuilder = new RequestPatternBuilder(method, urlPattern);
}
 
Example #15
Source File: WireMockSnippet.java    From spring-cloud-contract with Apache License 2.0 4 votes vote down vote up
private UrlPattern requestPattern(Operation operation) {
	return urlPathEqualTo(operation.getRequest().getUri().getPath());
}
 
Example #16
Source File: SchemaRegistryMock.java    From fluent-kafka-streams-tests with MIT License 4 votes vote down vote up
private static UrlPattern getSubjectVersionsPattern(final String subject) {
    return WireMock.urlEqualTo(ALL_SUBJECT_PATTERN + "/" + subject + "/versions");
}
 
Example #17
Source File: SchemaRegistryMock.java    From fluent-kafka-streams-tests with MIT License 4 votes vote down vote up
private static UrlPattern getSubjectPattern(final String subject) {
    return WireMock.urlEqualTo(ALL_SUBJECT_PATTERN + "/" + subject);
}
 
Example #18
Source File: WireMockTestUtils.java    From junit-servers with MIT License 3 votes vote down vote up
/**
 * Verify that a given request has been triggered.
 *
 * @param endpoint Request endpoint.
 * @param method Request method.
 * @param headerName Header name.
 * @param headerValue Header value.
 */
static void assertRequestWithHeader(String endpoint, HttpMethod method, String headerName, String headerValue) {
	UrlPattern urlPattern = urlEqualTo(endpoint);
	RequestMethod rqMethod = new RequestMethod(method.name());
	RequestPatternBuilder rq = new RequestPatternBuilder(rqMethod, urlPattern);
	rq.withHeader(headerName, equalTo(headerValue));
	WireMock.verify(1, rq);
}