Search in sources :

Example 71 with WireMockResponse

use of com.github.tomakehurst.wiremock.testsupport.WireMockResponse in project wiremock by wiremock.

the class StubbingAcceptanceTest method mappingWithUrlContainingQueryParameters.

@Test
public void mappingWithUrlContainingQueryParameters() {
    stubFor(get(urlEqualTo("/search?name=John&postcode=N44LL")).willReturn(aResponse().withHeader("Location", "/nowhere").withStatus(302)));
    WireMockResponse response = testClient.get("/search?name=John&postcode=N44LL");
    assertThat(response.statusCode(), is(302));
}
Also used : WireMockResponse(com.github.tomakehurst.wiremock.testsupport.WireMockResponse) Test(org.junit.jupiter.api.Test)

Example 72 with WireMockResponse

use of com.github.tomakehurst.wiremock.testsupport.WireMockResponse in project wiremock by wiremock.

the class StubbingAcceptanceTest method matchingOnRequestBodyWithAdvancedXPath.

@Test
public void matchingOnRequestBodyWithAdvancedXPath() {
    stubFor(post("/xpath/advanced").withRequestBody(matchingXPath("//counter/text()", equalTo("123"))).willReturn(ok()));
    WireMockResponse response = testClient.postXml("/xpath/advanced", "<counter>6666</counter>");
    assertThat(response.statusCode(), is(HTTP_NOT_FOUND));
    response = testClient.postXml("/xpath/advanced", "<counter>123</counter>");
    assertThat(response.statusCode(), is(HTTP_OK));
}
Also used : WireMockResponse(com.github.tomakehurst.wiremock.testsupport.WireMockResponse) Test(org.junit.jupiter.api.Test)

Example 73 with WireMockResponse

use of com.github.tomakehurst.wiremock.testsupport.WireMockResponse in project wiremock by wiremock.

the class StubbingAcceptanceTest method matchingOnRequestBodyWithAContainsAndANegativeRegex.

@Test
public void matchingOnRequestBodyWithAContainsAndANegativeRegex() {
    stubFor(put(urlEqualTo("/match/this/body/too")).withRequestBody(containing("Blah")).withRequestBody(notMatching(".*[0-9]+.*")).willReturn(aResponse().withStatus(HTTP_OK).withBodyFile("plain-example.txt")));
    WireMockResponse response = testClient.putWithBody("/match/this/body/too", "Blah12345", "text/plain");
    assertThat(response.statusCode(), is(HTTP_NOT_FOUND));
    response = testClient.putWithBody("/match/this/body/too", "BlahBlahBlah", "text/plain");
    assertThat(response.statusCode(), is(HTTP_OK));
}
Also used : WireMockResponse(com.github.tomakehurst.wiremock.testsupport.WireMockResponse) Test(org.junit.jupiter.api.Test)

Example 74 with WireMockResponse

use of com.github.tomakehurst.wiremock.testsupport.WireMockResponse in project wiremock by wiremock.

the class StubbingAcceptanceTest method matchingOnMultipartRequestBodyWithTwoRegexes.

@Test
public void matchingOnMultipartRequestBodyWithTwoRegexes() {
    stubFor(post(urlEqualTo("/match/this/part")).withMultipartRequestBody(aMultipart().withBody(matching(".*Blah.*"))).withMultipartRequestBody(aMultipart().withBody(matching(".*@[0-9]{5}@.*"))).willReturn(aResponse().withStatus(HTTP_OK).withBodyFile("plain-example.txt")));
    WireMockResponse response = testClient.postWithMultiparts("/match/this/part", singletonList(part("part-1", "Blah...but not the rest", TEXT_PLAIN)));
    assertThat(response.statusCode(), is(HTTP_NOT_FOUND));
    response = testClient.postWithMultiparts("/match/this/part", singletonList(part("part-1", "@12345@...but not the rest", TEXT_PLAIN)));
    assertThat(response.statusCode(), is(HTTP_NOT_FOUND));
    response = testClient.postWithMultiparts("/match/this/part", singletonList(part("good-part", "BlahBlah@56565@Blah", TEXT_PLAIN)));
    assertThat(response.statusCode(), is(HTTP_OK));
}
Also used : WireMockResponse(com.github.tomakehurst.wiremock.testsupport.WireMockResponse) Test(org.junit.jupiter.api.Test)

Example 75 with WireMockResponse

use of com.github.tomakehurst.wiremock.testsupport.WireMockResponse in project wiremock by wiremock.

the class StubbingAcceptanceTest method getAndAssertUnderlyingExceptionInstanceClass.

private void getAndAssertUnderlyingExceptionInstanceClass(String url, Class<?> expectedClass) {
    boolean thrown = false;
    try {
        WireMockResponse response = testClient.get(url);
        response.content();
    } catch (Exception e) {
        assertThat(e.getCause(), instanceOf(expectedClass));
        thrown = true;
    }
    assertTrue(thrown, "No exception was thrown");
}
Also used : WireMockResponse(com.github.tomakehurst.wiremock.testsupport.WireMockResponse) IOException(java.io.IOException) MalformedChunkCodingException(org.apache.hc.core5.http.MalformedChunkCodingException) NoHttpResponseException(org.apache.hc.core5.http.NoHttpResponseException)

Aggregations

WireMockResponse (com.github.tomakehurst.wiremock.testsupport.WireMockResponse)172 Test (org.junit.jupiter.api.Test)168 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 Errors (com.github.tomakehurst.wiremock.common.Errors)11 WireMockTestClient (com.github.tomakehurst.wiremock.testsupport.WireMockTestClient)9 StubMapping (com.github.tomakehurst.wiremock.stubbing.StubMapping)7 UUID (java.util.UUID)5 Matchers.containsString (org.hamcrest.Matchers.containsString)4 Stopwatch (com.google.common.base.Stopwatch)3 ByteArrayEntity (org.apache.hc.core5.http.io.entity.ByteArrayEntity)3 StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)3 ConsoleNotifier (com.github.tomakehurst.wiremock.common.ConsoleNotifier)2 Request (com.github.tomakehurst.wiremock.http.Request)2 JsonVerifiable (com.toomuchcoding.jsonassert.JsonVerifiable)2 IOException (java.io.IOException)2 WireMockServer (com.github.tomakehurst.wiremock.WireMockServer)1 WireMock (com.github.tomakehurst.wiremock.client.WireMock)1 FileSource (com.github.tomakehurst.wiremock.common.FileSource)1 SingleRootFileSource (com.github.tomakehurst.wiremock.common.SingleRootFileSource)1 Admin (com.github.tomakehurst.wiremock.core.Admin)1