/** * Static builder to create a not found response. */ public static HttpResponse notFoundResponse() { return new HttpResponse().withStatusCode(NOT_FOUND_404.code()).withReasonPhrase(NOT_FOUND_404.reasonPhrase()); }
assertEquals(HttpStatusCode.OK_200.code(), response.getStatusLine().getStatusCode()); assertThat(response.getHeaders("Set-Cookie").length, is(2)); assertThat(response.getHeaders("Set-Cookie")[0].getValue(), is(setCookieOneHeader.getValue()));
assertEquals(HttpStatusCode.OK_200.code(), response.getStatusLine().getStatusCode()); assertEquals("an_example_body", new String(EntityUtils.toByteArray(response.getEntity()), StandardCharsets.UTF_8));
/** * Static builder to create a response with a 200 status code and the string response body. * * @param body a string */ public static HttpResponse response(String body) { return new HttpResponse().withStatusCode(OK_200.code()).withReasonPhrase(OK_200.reasonPhrase()).withBody(body); }
@Test public void shouldReturnResponseByMatchingStringBody() throws IOException, URISyntaxException { // when getMockServerClient() .when( request() .withBody( exact("some_random_body") ), Times.exactly(2) ) .respond( response() .withBody("some_string_body_response") ); // then HttpClient httpClient = createHttpClient(); HttpPost request = new HttpPost( new URIBuilder() .setScheme("http") .setHost("localhost") .setPort(getServerPort()) .setPath(addContextToPath("some_path")) .build() ); request.setEntity(new StringEntity("some_random_body")); HttpResponse response = httpClient.execute(request); assertThat(new String(EntityUtils.toByteArray(response.getEntity()), UTF_8), is("some_string_body_response")); assertThat(response.getStatusLine().getStatusCode(), is(OK_200.code())); }
.withStatusCode(HttpStatusCode.ACCEPTED_202.code()) .withReasonPhrase(HttpStatusCode.ACCEPTED_202.reasonPhrase()) .withBody("some_body_response") ); .withStatusCode(HttpStatusCode.NOT_FOUND_404.code()) .withReasonPhrase(HttpStatusCode.NOT_FOUND_404.reasonPhrase()), makeRequest( request() .withStatusCode(HttpStatusCode.NOT_FOUND_404.code()) .withReasonPhrase(HttpStatusCode.NOT_FOUND_404.reasonPhrase()), makeRequest( request()
HttpResponse response = httpClient.execute(request); assertThat(new String(EntityUtils.toByteArray(response.getEntity()), UTF_8), is("some_body")); assertThat(response.getStatusLine().getStatusCode(), is(OK_200.code()));
.withStatusCode(HttpStatusCode.ACCEPTED_202.code()) .withReasonPhrase(HttpStatusCode.ACCEPTED_202.reasonPhrase()) .withBody("some_body_response") ); .withStatusCode(HttpStatusCode.ACCEPTED_202.code()) .withReasonPhrase(HttpStatusCode.ACCEPTED_202.reasonPhrase()) .withBody("some_body_response"), makeRequest( .withStatusCode(HttpStatusCode.ACCEPTED_202.code()) .withReasonPhrase(HttpStatusCode.ACCEPTED_202.reasonPhrase()) .withBody("some_body_response"), makeRequest(
@Test public void shouldForwardRequestsUsingHttpClient() throws Exception { // given HttpClient httpClient = createHttpClient(); // when HttpPost request = new HttpPost( new URIBuilder() .setScheme("http") .setHost("localhost") .setPort(getServerPort()) .setPath(addContextToPath("test_headers_and_body")) .build() ); request.setEntity(new StringEntity("an_example_body")); HttpResponse response = httpClient.execute(request); // then assertEquals(HttpStatusCode.OK_200.code(), response.getStatusLine().getStatusCode()); assertEquals("an_example_body", new String(EntityUtils.toByteArray(response.getEntity()), UTF_8)); // and getMockServerClient().verify( request() .withMethod("POST") .withPath("/test_headers_and_body") .withBody("an_example_body"), exactly(1) ); }
.withStatusCode(HttpStatusCode.ACCEPTED_202.code()) .withReasonPhrase(HttpStatusCode.ACCEPTED_202.reasonPhrase()) .withBody("some_body_response") ); .withStatusCode(HttpStatusCode.NOT_FOUND_404.code()) .withReasonPhrase(HttpStatusCode.NOT_FOUND_404.reasonPhrase()), makeRequest( request() .withStatusCode(HttpStatusCode.NOT_FOUND_404.code()) .withReasonPhrase(HttpStatusCode.NOT_FOUND_404.reasonPhrase()), makeRequest( request()
HttpResponse response = httpClient.execute(request); assertThat(new String(EntityUtils.toByteArray(response.getEntity()), UTF_8), is("some_body")); assertThat(response.getStatusLine().getStatusCode(), is(OK_200.code()));
.withStatusCode(HttpStatusCode.ACCEPTED_202.code()) .withReasonPhrase(HttpStatusCode.ACCEPTED_202.reasonPhrase()) .withBody("some_body_response") ); .withStatusCode(HttpStatusCode.ACCEPTED_202.code()) .withReasonPhrase(HttpStatusCode.ACCEPTED_202.reasonPhrase()) .withBody("some_body_response"), makeRequest( .withStatusCode(HttpStatusCode.ACCEPTED_202.code()) .withReasonPhrase(HttpStatusCode.ACCEPTED_202.reasonPhrase()) .withBody("some_body_response"), makeRequest(
/** * Returns whether server MockServer is running, by polling the MockServer a configurable amount of times */ public boolean isRunning(int attempts, long timeout, TimeUnit timeUnit) { try { HttpResponse httpResponse = sendRequest(request().withMethod("PUT").withPath(calculatePath("status"))); if (httpResponse.getStatusCode() == HttpStatusCode.OK_200.code()) { return true; } else if (attempts == 0) { return false; } else { try { timeUnit.sleep(timeout); } catch (InterruptedException e) { // ignore interrupted exception } return isRunning(attempts - 1, timeout, timeUnit); } } catch (SocketConnectionException sce) { return false; } }
@Test public void shouldReturnResponseWithOnlyBody() { // when mockServerClient.when(request()).respond(response().withBody("some_body")); // then // - in http assertEquals( response() .withStatusCode(OK_200.code()) .withReasonPhrase(OK_200.reasonPhrase()) .withBody("some_body"), makeRequest( request() .withPath(calculatePath("")), headersToIgnore) ); // - in https assertEquals( response() .withStatusCode(OK_200.code()) .withReasonPhrase(OK_200.reasonPhrase()) .withBody("some_body"), makeRequest( request() .withSecure(true) .withPath(calculatePath("")), headersToIgnore) ); }
@Test public void shouldReturnResponseForRequestNotInSsl() { // when mockServerClient.when(request().withSecure(false)).respond(response().withBody("some_body")); // then // - in http assertEquals( response() .withStatusCode(OK_200.code()) .withReasonPhrase(OK_200.reasonPhrase()) .withBody("some_body"), makeRequest( request() .withPath(calculatePath("")), headersToIgnore) ); // - in https assertEquals( response() .withStatusCode(HttpStatusCode.NOT_FOUND_404.code()) .withReasonPhrase(HttpStatusCode.NOT_FOUND_404.reasonPhrase()), makeRequest( request() .withSecure(true) .withPath(calculatePath("")), headersToIgnore) ); }
@Test public void shouldReturnResponseForRequestInSsl() { // when mockServerClient.when(request().withSecure(true)).respond(response().withBody("some_body")); // then // - in http assertEquals( response() .withStatusCode(HttpStatusCode.NOT_FOUND_404.code()) .withReasonPhrase(HttpStatusCode.NOT_FOUND_404.reasonPhrase()), makeRequest( request() .withPath(calculatePath("")), headersToIgnore) ); // - in https assertEquals( response() .withStatusCode(OK_200.code()) .withReasonPhrase(OK_200.reasonPhrase()) .withBody("some_body"), makeRequest( request() .withSecure(true) .withPath(calculatePath("")), headersToIgnore) ); }
.withStatusCode(OK_200.code()) .withReasonPhrase(OK_200.reasonPhrase()) .withBody("some_body"), makeRequest( .withStatusCode(OK_200.code()) .withReasonPhrase(OK_200.reasonPhrase()) .withBody("some_body"), makeRequest( .withStatusCode(HttpStatusCode.NOT_FOUND_404.code()) .withReasonPhrase(HttpStatusCode.NOT_FOUND_404.reasonPhrase()), makeRequest( request() .withStatusCode(HttpStatusCode.NOT_FOUND_404.code()) .withReasonPhrase(HttpStatusCode.NOT_FOUND_404.reasonPhrase()), makeRequest( request()
.withStatusCode(OK_200.code()) .withReasonPhrase(OK_200.reasonPhrase()) .withBody("some_body"), makeRequest( .withStatusCode(HttpStatusCode.NOT_FOUND_404.code()) .withReasonPhrase(HttpStatusCode.NOT_FOUND_404.reasonPhrase()), makeRequest( request() .withStatusCode(HttpStatusCode.NOT_FOUND_404.code()) .withReasonPhrase(HttpStatusCode.NOT_FOUND_404.reasonPhrase()), makeRequest( request()