Search in sources :

Example 61 with WireMockResponse

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

the class ProxyAcceptanceTest method clientLibrariesTendToAddTheTrailingSlashWhenTheContextPathIsEmpty.

/**
 * NOTE: {@link org.apache.hc.core5.http.client.HttpClient} always has a / when the context path
 * is empty. This is also the behaviour of curl (see e.g. <a
 * href="https://curl.haxx.se/mail/archive-2016-08/0027.html">here</a>)
 */
@Test
public void clientLibrariesTendToAddTheTrailingSlashWhenTheContextPathIsEmpty() {
    initWithDefaultConfig();
    target.register(get("/").willReturn(ok()));
    proxy.register(any(anyUrl()).willReturn(aResponse().proxiedFrom(targetServiceBaseUrl)));
    WireMockResponse responseToRequestWithoutSlash = testClient.getViaProxy("http://localhost:" + proxyingService.port());
    assertThat(responseToRequestWithoutSlash.statusCode(), is(200));
    WireMockResponse responseToRequestWithSlash = testClient.getViaProxy("http://localhost:" + proxyingService.port() + "/");
    assertThat(responseToRequestWithSlash.statusCode(), is(200));
    target.verifyThat(2, getRequestedFor(urlEqualTo("/")));
    target.verifyThat(0, getRequestedFor(urlEqualTo("")));
}
Also used : WireMockResponse(com.github.tomakehurst.wiremock.testsupport.WireMockResponse) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 62 with WireMockResponse

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

the class ProxyAcceptanceTest method successfullyGetsResponseBinaryResponses.

@Test
public void successfullyGetsResponseBinaryResponses() throws IOException {
    initWithDefaultConfig();
    final byte[] bytes = new byte[] { 0x10, 0x49, 0x6e, (byte) 0xb7, 0x46, (byte) 0xe6, 0x52, (byte) 0x95, (byte) 0x95, 0x42 };
    HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
    server.createContext("/binary", new HttpHandler() {

        @Override
        public void handle(HttpExchange exchange) throws IOException {
            InputStream request = exchange.getRequestBody();
            byte[] buffy = new byte[10];
            request.read(buffy);
            if (Arrays.equals(buffy, bytes)) {
                exchange.sendResponseHeaders(200, bytes.length);
                OutputStream out = exchange.getResponseBody();
                out.write(bytes);
                out.close();
            } else {
                exchange.sendResponseHeaders(500, 0);
                exchange.close();
            }
        }
    });
    server.start();
    proxy.register(post(urlEqualTo("/binary")).willReturn(aResponse().proxiedFrom("http://localhost:" + server.getAddress().getPort()).withBody(bytes)));
    WireMockResponse post = testClient.post("/binary", new ByteArrayEntity(bytes, ContentType.DEFAULT_BINARY));
    assertThat(post.statusCode(), is(200));
    assertThat(post.binaryContent(), Matchers.equalTo(bytes));
}
Also used : HttpHandler(com.sun.net.httpserver.HttpHandler) WireMockResponse(com.github.tomakehurst.wiremock.testsupport.WireMockResponse) ByteArrayEntity(org.apache.hc.core5.http.io.entity.ByteArrayEntity) InetSocketAddress(java.net.InetSocketAddress) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) HttpServer(com.sun.net.httpserver.HttpServer) HttpExchange(com.sun.net.httpserver.HttpExchange) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 63 with WireMockResponse

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

the class ProxyAcceptanceTest method chunkedDribbleDelayIsAddedToProxiedResponse.

@Test
public void chunkedDribbleDelayIsAddedToProxiedResponse() {
    initWithDefaultConfig();
    target.register(get("/chunk-delayed").willReturn(ok()));
    proxy.register(any(anyUrl()).willReturn(aResponse().proxiedFrom(targetServiceBaseUrl).withChunkedDribbleDelay(10, 300)));
    Stopwatch stopwatch = Stopwatch.createStarted();
    WireMockResponse response = testClient.getViaProxy("http://localhost:" + proxyingService.port() + "/chunk-delayed");
    stopwatch.stop();
    assertThat(response.statusCode(), is(200));
    assertThat(stopwatch.elapsed(MILLISECONDS), greaterThanOrEqualTo(300L));
}
Also used : WireMockResponse(com.github.tomakehurst.wiremock.testsupport.WireMockResponse) Stopwatch(com.google.common.base.Stopwatch) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 64 with WireMockResponse

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

the class RecordApiAcceptanceTest method returnsErrorWhenAttemptingToStopRecordingWhenNotStarted.

@Test
public void returnsErrorWhenAttemptingToStopRecordingWhenNotStarted() {
    proxyServerStartWithEmptyFileRoot();
    WireMockResponse response = proxyingTestClient.postWithBody("/__admin/recordings/stop", "", "text/plain", "utf-8");
    assertThat(response.content(), equalToJson(NOT_RECORDING_ERROR));
}
Also used : WireMockResponse(com.github.tomakehurst.wiremock.testsupport.WireMockResponse) Test(org.junit.jupiter.api.Test)

Example 65 with WireMockResponse

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

the class StubbingAcceptanceTest method matchingOnRequestBodyWithTwoRegexes.

@Test
public void matchingOnRequestBodyWithTwoRegexes() {
    stubFor(put(urlEqualTo("/match/this/body")).withRequestBody(matching(".*Blah.*")).withRequestBody(matching(".*@[0-9]{5}@.*")).willReturn(aResponse().withStatus(HTTP_OK).withBodyFile("plain-example.txt")));
    WireMockResponse response = testClient.putWithBody("/match/this/body", "Blah...but not the rest", "text/plain");
    assertThat(response.statusCode(), is(HTTP_NOT_FOUND));
    response = testClient.putWithBody("/match/this/body", "@12345@...but not the rest", "text/plain");
    assertThat(response.statusCode(), is(HTTP_NOT_FOUND));
    response = testClient.putWithBody("/match/this/body", "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)

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