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("")));
}
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));
}
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));
}
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));
}
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));
}
Aggregations