Search in sources :

Example 26 with WireMockResponse

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

the class CookieMatchingAcceptanceTest method doesNotMatchWhenRequiredAbsentCookieIsPresent.

@Test
public void doesNotMatchWhenRequiredAbsentCookieIsPresent() {
    stubFor(get(urlEqualTo("/absent/cookie")).withCookie("my_cookie", absent()).willReturn(aResponse().withStatus(200)));
    WireMockResponse response = testClient.get("/absent/cookie", withHeader(COOKIE, "my_cookie=xxx-mycookievalue-xxx; my_other_cookie=exact-other-value; irrelevant_cookie=whatever"));
    assertThat(response.statusCode(), is(404));
}
Also used : WireMockResponse(com.github.tomakehurst.wiremock.testsupport.WireMockResponse) Test(org.junit.jupiter.api.Test)

Example 27 with WireMockResponse

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

the class AdminApiTest method resetScenariosViaPOST.

@Test
public void resetScenariosViaPOST() {
    dsl.stubFor(get(urlEqualTo("/stateful")).inScenario("changing-states").whenScenarioStateIs(STARTED).willSetStateTo("final").willReturn(aResponse().withBody("Initial")));
    dsl.stubFor(get(urlEqualTo("/stateful")).inScenario("changing-states").whenScenarioStateIs("final").willReturn(aResponse().withBody("Final")));
    assertThat(testClient.get("/stateful").content(), is("Initial"));
    assertThat(testClient.get("/stateful").content(), is("Final"));
    WireMockResponse response = testClient.post("/__admin/scenarios/reset", new StringEntity("", TEXT_PLAIN));
    assertThat(response.content(), is("{}"));
    assertThat(response.firstHeader("Content-Type"), is("application/json"));
    assertThat(testClient.get("/stateful").content(), is("Initial"));
}
Also used : WireMockResponse(com.github.tomakehurst.wiremock.testsupport.WireMockResponse) StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) Test(org.junit.jupiter.api.Test)

Example 28 with WireMockResponse

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

the class AlternativeServletContainerTest method supportsAlternativeHttpServerForFaultInjection.

@Test
public void supportsAlternativeHttpServerForFaultInjection() {
    stubFor(get(urlEqualTo("/alt-server")).willReturn(aResponse().withFault(Fault.EMPTY_RESPONSE)));
    WireMockResponse response = client.get("/alt-server");
    assertThat(response.statusCode(), is(418));
    assertThat(response.content(), is("No fault injector is configured!"));
}
Also used : WireMockResponse(com.github.tomakehurst.wiremock.testsupport.WireMockResponse) Test(org.junit.jupiter.api.Test)

Example 29 with WireMockResponse

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

the class Examples method toDoListScenario.

@Test
public void toDoListScenario() {
    stubFor(get(urlEqualTo("/todo/items")).inScenario("To do list").whenScenarioStateIs(STARTED).willReturn(aResponse().withBody("<items>" + "   <item>Buy milk</item>" + "</items>")));
    stubFor(post(urlEqualTo("/todo/items")).inScenario("To do list").whenScenarioStateIs(STARTED).withRequestBody(containing("Cancel newspaper subscription")).willReturn(aResponse().withStatus(201)).willSetStateTo("Cancel newspaper item added"));
    stubFor(get(urlEqualTo("/todo/items")).inScenario("To do list").whenScenarioStateIs("Cancel newspaper item added").willReturn(aResponse().withBody("<items>" + "   <item>Buy milk</item>" + "   <item>Cancel newspaper subscription</item>" + "</items>")));
    WireMockResponse response = testClient.get("/todo/items");
    assertThat(response.content(), containsString("Buy milk"));
    assertThat(response.content(), not(containsString("Cancel newspaper subscription")));
    response = testClient.postWithBody("/todo/items", "Cancel newspaper subscription", "text/plain", "UTF-8");
    assertThat(response.statusCode(), is(201));
    response = testClient.get("/todo/items");
    assertThat(response.content(), containsString("Buy milk"));
    assertThat(response.content(), containsString("Cancel newspaper subscription"));
}
Also used : WireMockResponse(com.github.tomakehurst.wiremock.testsupport.WireMockResponse) Test(org.junit.jupiter.api.Test)

Example 30 with WireMockResponse

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

the class SavingMappingsAcceptanceTest method doesNotDuplicateMappingsAlreadyPersistedToFileSystem.

@Test
public void doesNotDuplicateMappingsAlreadyPersistedToFileSystem() {
    // Check the mapping we're about to add isn't already there
    WireMockResponse response = testClient.get("/some/url");
    assertThat(response.statusCode(), is(404));
    // Add a mapping and save it
    stubFor(get(urlEqualTo("/some/url")).willReturn(aResponse().withBody("Response to /some/url")));
    saveAllMappings();
    // Save a second time
    saveAllMappings();
    // Check only one file has been written
    assertThat(MAPPINGS_DIRECTORY.listFiles().length, is(1));
}
Also used : WireMockResponse(com.github.tomakehurst.wiremock.testsupport.WireMockResponse) Test(org.junit.jupiter.api.Test)

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