private void setStatusCode(HttpResponse httpResponse, FullHttpResponse fullHttpResponse) { HttpResponseStatus status = fullHttpResponse.status(); httpResponse.withStatusCode(status.code()); httpResponse.withReasonPhrase(status.reasonPhrase()); }
/** * 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()); }
@Override public void writeResponse(HttpRequest request, HttpResponseStatus responseStatus, String body, String contentType) { HttpResponse response = response() .withStatusCode(responseStatus.code()) .withReasonPhrase(responseStatus.reasonPhrase()) .withBody(body); if (body != null && !body.isEmpty()) { response.replaceHeader(header(CONTENT_TYPE.toString(), contentType + "; charset=utf-8")); } writeResponse(request, response, true); }
/** * 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); }
public HttpResponse buildObject() { return new HttpResponse() .withStatusCode(statusCode) .withReasonPhrase(reasonPhrase) .withBody(body != null ? body.buildObject() : null) .withHeaders(headers) .withCookies(cookies) .withDelay((delay != null ? delay.buildObject() : null)) .withConnectionOptions((connectionOptions != null ? connectionOptions.buildObject() : null)); }
public HttpResponse clone() { return response() .withStatusCode(statusCode) .withReasonPhrase(reasonPhrase) .withBody(body) .withHeaders(headers.clone()) .withCookies(cookies.clone()) .withDelay(getDelay()) .withConnectionOptions(connectionOptions); } }
response() .withStatusCode(HttpStatusCode.ACCEPTED_202.code()) .withReasonPhrase(HttpStatusCode.ACCEPTED_202.reasonPhrase()) .withBody("some_body_response") ); response() .withStatusCode(HttpStatusCode.NOT_FOUND_404.code()) .withReasonPhrase(HttpStatusCode.NOT_FOUND_404.reasonPhrase()), makeRequest( request() response() .withStatusCode(HttpStatusCode.NOT_FOUND_404.code()) .withReasonPhrase(HttpStatusCode.NOT_FOUND_404.reasonPhrase()), makeRequest( request()
@Test public void shouldNotVerifyNoRequestsReceived() { // when mockServerClient.when(request().withPath(calculatePath("some_path")), exactly(2)).respond(response().withBody("some_body")); // then assertEquals( response() .withStatusCode(OK_200.code()) .withReasonPhrase(OK_200.reasonPhrase()) .withBody("some_body"), makeRequest( request() .withPath(calculatePath("some_path")), headersToIgnore) ); try { mockServerClient.verifyZeroInteractions(); fail(); } catch (AssertionError ae) { assertThat(ae.getMessage(), startsWith("Request not found exactly 0 times, expected:<{ }> but was:<{" + NEW_LINE + " \"method\" : \"GET\"," + NEW_LINE + " \"path\" : \"" + calculatePath("some_path") + "\"," + NEW_LINE)); } }
@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) ); }
@Test public void shouldVerifyNotEnoughRequestsReceived() { // when mockServerClient.when(request().withPath(calculatePath("some_path")), exactly(2)).respond(response().withBody("some_body")); // then assertEquals( response() .withStatusCode(OK_200.code()) .withReasonPhrase(OK_200.reasonPhrase()) .withBody("some_body"), makeRequest( request() .withPath(calculatePath("some_path")), headersToIgnore) ); try { mockServerClient.verify(request() .withPath(calculatePath("some_path")), VerificationTimes.atLeast(2)); fail(); } catch (AssertionError ae) { assertThat(ae.getMessage(), startsWith("Request not found at least 2 times, expected:<{" + NEW_LINE + " \"path\" : \"" + calculatePath("some_path") + "\"" + NEW_LINE + "}> but was:<{" + NEW_LINE + " \"method\" : \"GET\"," + NEW_LINE + " \"path\" : \"" + calculatePath("some_path") + "\"," + NEW_LINE)); } }
@Test public void shouldVerifyNoMatchingRequestsReceived() { // when mockServerClient.when(request().withPath(calculatePath("some_path")), exactly(2)).respond(response().withBody("some_body")); // then assertEquals( response() .withStatusCode(OK_200.code()) .withReasonPhrase(OK_200.reasonPhrase()) .withBody("some_body"), makeRequest( request() .withPath(calculatePath("some_path")), headersToIgnore) ); try { mockServerClient.verify(request() .withPath(calculatePath("some_other_path")), VerificationTimes.exactly(2)); fail(); } catch (AssertionError ae) { assertThat(ae.getMessage(), startsWith("Request not found exactly 2 times, expected:<{" + NEW_LINE + " \"path\" : \"" + calculatePath("some_other_path") + "\"" + NEW_LINE + "}> but was:<{" + NEW_LINE + " \"method\" : \"GET\"," + NEW_LINE + " \"path\" : \"" + calculatePath("some_path") + "\"," + NEW_LINE)); } }
@Test public void shouldVerifyTooManyRequestsReceived() { // when mockServerClient.when(request().withPath(calculatePath("some_path")), exactly(2)).respond(response().withBody("some_body")); // then assertEquals( response() .withStatusCode(OK_200.code()) .withReasonPhrase(OK_200.reasonPhrase()) .withBody("some_body"), makeRequest( request() .withPath(calculatePath("some_path")), headersToIgnore) ); try { mockServerClient.verify(request() .withPath(calculatePath("some_path")), VerificationTimes.exactly(0)); fail(); } catch (AssertionError ae) { assertThat(ae.getMessage(), startsWith("Request not found exactly 0 times, expected:<{" + NEW_LINE + " \"path\" : \"" + calculatePath("some_path") + "\"" + NEW_LINE + "}> but was:<{" + NEW_LINE + " \"method\" : \"GET\"," + NEW_LINE + " \"path\" : \"" + calculatePath("some_path") + "\"," + NEW_LINE)); } }
response() .withStatusCode(HttpStatusCode.ACCEPTED_202.code()) .withReasonPhrase(HttpStatusCode.ACCEPTED_202.reasonPhrase()) .withBody("some_body_response") ); response() .withStatusCode(HttpStatusCode.ACCEPTED_202.code()) .withReasonPhrase(HttpStatusCode.ACCEPTED_202.reasonPhrase()) .withBody("some_body_response"), makeRequest(
response() .withStatusCode(OK_200.code()) .withReasonPhrase(OK_200.reasonPhrase()) .withBody("some_body2"), makeRequest(
.withHeader(CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(StandardCharsets.UTF_16).toString()) .withStatusCode(OK_200.code()) .withReasonPhrase(OK_200.reasonPhrase()) .withBody(body), makeRequest( .withHeader(CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(StandardCharsets.UTF_16).toString()) .withStatusCode(OK_200.code()) .withReasonPhrase(OK_200.reasonPhrase()) .withBody(body), makeRequest(
.withHeader(CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(StandardCharsets.UTF_8).toString()) .withStatusCode(OK_200.code()) .withReasonPhrase(OK_200.reasonPhrase()) .withBody(body), makeRequest( .withHeader(CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(StandardCharsets.UTF_8).toString()) .withStatusCode(OK_200.code()) .withReasonPhrase(OK_200.reasonPhrase()) .withBody(body), makeRequest(
@Test public void shouldVerifyReceivedRequestsWithNoBody() { // when mockServerClient.when(request().withPath(calculatePath("some_path")), exactly(2)).respond(response()); // then // - in http assertEquals( response() .withStatusCode(OK_200.code()) .withReasonPhrase(OK_200.reasonPhrase()), makeRequest( request() .withPath(calculatePath("some_path")), headersToIgnore) ); mockServerClient.verify(request() .withPath(calculatePath("some_path"))); mockServerClient.verify(request() .withPath(calculatePath("some_path")), VerificationTimes.exactly(1)); }
.withHeader(CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(StandardCharsets.UTF_8).toString()) .withStatusCode(OK_200.code()) .withReasonPhrase(OK_200.reasonPhrase()) .withBody(body), makeRequest( .withHeader(CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(StandardCharsets.UTF_8).toString()) .withStatusCode(OK_200.code()) .withReasonPhrase(OK_200.reasonPhrase()) .withBody(body), makeRequest(