private void setBody(HttpResponse httpResponse, FullHttpResponse fullHttpResponse) { if (fullHttpResponse.content().readableBytes() > 0) { byte[] bodyBytes = new byte[fullHttpResponse.content().readableBytes()]; fullHttpResponse.content().readBytes(bodyBytes); if (bodyBytes.length > 0) { if (ContentTypeMapper.isBinary(fullHttpResponse.headers().get(CONTENT_TYPE))) { httpResponse.withBody(new BinaryBody(bodyBytes)); } else { Charset requestCharset = ContentTypeMapper.getCharsetFromContentTypeHeader(fullHttpResponse.headers().get(CONTENT_TYPE)); httpResponse.withBody(new String(bodyBytes, requestCharset)); } } } } }
@Override public void writeResponse(HttpRequest request, HttpResponseStatus responseStatus, String body, String contentType) { HttpResponse response = response() .withStatusCode(responseStatus.code()) .withBody(body); if (body != null && !body.isEmpty()) { response.replaceHeader(header(CONTENT_TYPE.toString(), contentType + "; charset=utf-8")); } writeResponse(request, response, true); }
@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())); }
@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); } }
@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 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)); } }
@Test public void testHeartbeatReplicationWithResponseBody() throws Exception { InstanceInfo remoteInfo = new InstanceInfo(this.instanceInfo); remoteInfo.setStatus(InstanceStatus.DOWN); byte[] responseBody = toGzippedJson(remoteInfo); serverMockClient.when( request() .withMethod("PUT") .withHeader(header(PeerEurekaNode.HEADER_REPLICATION, "true")) .withPath("/eureka/v2/apps/" + this.instanceInfo.getAppName() + '/' + this.instanceInfo.getId()) ).respond( response() .withStatusCode(Status.CONFLICT.getStatusCode()) .withHeader(header("Content-Type", MediaType.APPLICATION_JSON)) .withHeader(header("Content-Encoding", "gzip")) .withBody(responseBody) ); EurekaHttpResponse<InstanceInfo> response = replicationClient.sendHeartBeat(this.instanceInfo.getAppName(), this.instanceInfo.getId(), this.instanceInfo, null); assertThat(response.getStatusCode(), is(equalTo(Status.CONFLICT.getStatusCode()))); assertThat(response.getEntity(), is(notNullValue())); }
@Test public void testHeartbeatReplicationWithResponseBody() throws Exception { InstanceInfo remoteInfo = new InstanceInfo(this.instanceInfo); remoteInfo.setStatus(InstanceStatus.DOWN); byte[] responseBody = toGzippedJson(remoteInfo); serverMockClient.when( request() .withMethod("PUT") .withHeader(header(PeerEurekaNode.HEADER_REPLICATION, "true")) .withPath("/eureka/v2/apps/" + this.instanceInfo.getAppName() + '/' + this.instanceInfo.getId()) ).respond( response() .withStatusCode(Status.CONFLICT.getStatusCode()) .withHeader(header("Content-Type", MediaType.APPLICATION_JSON)) .withHeader(header("Content-Encoding", "gzip")) .withBody(responseBody) ); EurekaHttpResponse<InstanceInfo> response = replicationClient.sendHeartBeat(this.instanceInfo.getAppName(), this.instanceInfo.getId(), this.instanceInfo, null); assertThat(response.getStatusCode(), is(equalTo(Status.CONFLICT.getStatusCode()))); assertThat(response.getEntity(), is(notNullValue())); }
@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 shouldVerifySequenceOfRequestsReceived() { // when mockServerClient.when(request().withPath(calculatePath("some_path.*")), exactly(6)).respond(response().withBody("some_body")); // then assertEquals( response("some_body"), makeRequest( request().withPath(calculatePath("some_path_one")), headersToIgnore) ); assertEquals( response("some_body"), makeRequest( request().withPath(calculatePath("some_path_two")), headersToIgnore) ); assertEquals( response("some_body"), makeRequest( request().withPath(calculatePath("some_path_three")), headersToIgnore) ); mockServerClient.verify(request(calculatePath("some_path_one")), request(calculatePath("some_path_three"))); mockServerClient.verify(request(calculatePath("some_path_one")), request(calculatePath("some_path_two"))); mockServerClient.verify(request(calculatePath("some_path_one")), request(calculatePath("some_path_two")), request(calculatePath("some_path_three"))); }
@Test public void shouldVerifySequenceOfRequestsReceivedIncludingThoseNotMatchingAnException() { // when mockServerClient.when(request().withPath(calculatePath("some_path.*")), exactly(4)).respond(response().withBody("some_body")); // then assertEquals( response("some_body"), makeRequest( request().withPath(calculatePath("some_path_one")), headersToIgnore) ); assertEquals( notFoundResponse(), makeRequest( request().withPath(calculatePath("not_found")), headersToIgnore) ); assertEquals( response("some_body"), makeRequest( request().withPath(calculatePath("some_path_three")), headersToIgnore) ); mockServerClient.verify(request(calculatePath("some_path_one")), request(calculatePath("some_path_three"))); mockServerClient.verify(request(calculatePath("some_path_one")), request(calculatePath("not_found"))); mockServerClient.verify(request(calculatePath("some_path_one")), request(calculatePath("not_found")), request(calculatePath("some_path_three"))); mockServerClient.verify(request(calculatePath("some_path_one")), request(calculatePath("not_found")), request(calculatePath("some_path_three"))); }