DefaultHttpLogFormatter implements HttpLogFormatter { @Override public String format(final Precorrelation precorrelation, final HttpRequest request) throws IOException { final String correlationId = precorrelation.getId(); final String body = request.getBodyAsString(); final StringBuilder result = new StringBuilder(body.length() + 2048); result.append(direction(request)); result.append(" Request: "); result.append(correlationId); result.append('\n'); result.append("Remote: "); result.append(request.getRemote()); result.append('\n'); result.append(request.getMethod()); result.append(' '); RequestURI.reconstruct(request, result); result.append(' '); result.append(request.getProtocolVersion()); result.append('\n'); writeHeaders(request.getHeaders(), result); writeBody(body, result); return result.toString(); }  @Override String format(final Precorrelation precorrelation, final HttpRequest request); @Override String format(final Correlation correlation, final HttpResponse response);  }
@Test void shouldLogRequest() throws IOException { final String correlationId = "c9408eaa-677d-11e5-9457-10ddb1ee7671"; final HttpRequest request = MockHttpRequest.create() .withProtocolVersion("HTTP/1.0") .withOrigin(Origin.REMOTE) .withPath("/test") .withQuery("limit=1") .withHeaders(HttpHeaders.empty() .update("Accept", "application/json") .update("Content-Type", "text/plain")) .withBodyAsString("Hello, world!"); final String http = unit.format(new SimplePrecorrelation(correlationId, systemUTC()), request); assertThat(http, is("Incoming Request: c9408eaa-677d-11e5-9457-10ddb1ee7671\n" + "Remote: 127.0.0.1\n" + "GET http: "Accept: application/json\n" + "Content-Type: text/plain\n" + "\n" + "Hello, world!")); }
@Test void shouldLogRequestWithoutQueryParameters() throws IOException { final String correlationId = "2bd05240-6827-11e5-bbee-10ddb1ee7671"; final HttpRequest request = MockHttpRequest.create() .withOrigin(Origin.LOCAL) .withPath("/test") .withHeaders(HttpHeaders.empty() .update("Accept", "application/json") .update("Content-Type", "text/plain")) .withBodyAsString("Hello, world!"); final String http = unit.format(new SimplePrecorrelation(correlationId, systemUTC()), request); assertThat(http, is("Outgoing Request: 2bd05240-6827-11e5-bbee-10ddb1ee7671\n" + "Remote: 127.0.0.1\n" + "GET http: "Accept: application/json\n" + "Content-Type: text/plain\n" + "\n" + "Hello, world!")); }
@Test void shouldLogRequestWithoutBody() throws IOException { final String correlationId = "0eae9f6c-6824-11e5-8b0a-10ddb1ee7671"; final HttpRequest request = MockHttpRequest.create() .withPath("/test") .withHeaders(HttpHeaders.of("Accept", "application/json")); final String http = unit.format(new SimplePrecorrelation(correlationId, systemUTC()), request); assertThat(http, is("Incoming Request: 0eae9f6c-6824-11e5-8b0a-10ddb1ee7671\n" + "Remote: 127.0.0.1\n" + "GET http: "Accept: application/json")); }
@Test void shouldLogResponse() throws IOException { final String correlationId = "2d51bc02-677e-11e5-8b9b-10ddb1ee7671"; final HttpResponse response = MockHttpResponse.create() .withProtocolVersion("HTTP/1.0") .withOrigin(Origin.REMOTE) .withStatus(201) .withHeaders(HttpHeaders.of("Content-Type", "application/json")) .withBodyAsString("{\"success\":true}"); final String http = unit.format(new SimpleCorrelation( correlationId, Instant.MIN, Instant.MIN.plusMillis(125)), response); assertThat(http, is("Incoming Response: 2d51bc02-677e-11e5-8b9b-10ddb1ee7671\n" + "Duration: 125 ms\n" + "HTTP/1.0 201 Created\n" + "Content-Type: application/json\n" + "\n" + "{\"success\":true}")); }
@Test void shouldLogResponseWithoutBody() throws IOException { final String correlationId = "3881ae92-6824-11e5-921b-10ddb1ee7671"; final HttpResponse response = MockHttpResponse.create() .withOrigin(Origin.LOCAL) .withStatus(400) .withHeaders(HttpHeaders.of("Content-Type", "application/json")); final String http = unit.format(new SimpleCorrelation(correlationId, Instant.MIN, Instant.MIN.plusMillis(100)), response); assertThat(http, is("Outgoing Response: 3881ae92-6824-11e5-921b-10ddb1ee7671\n" + "Duration: 100 ms\n" + "HTTP/1.1 400 Bad Request\n" + "Content-Type: application/json")); }
@Test void shouldLogResponseForUnknownStatusCode() throws IOException { final String correlationId = "2d51bc02-677e-11e5-8b9b-10ddb1ee7671"; final HttpResponse response = MockHttpResponse.create() .withProtocolVersion("HTTP/1.0") .withOrigin(Origin.REMOTE) .withStatus(1000) .withHeaders(HttpHeaders.of("Content-Type", "application/json")) .withBodyAsString("{\"success\":true}"); final String http = unit.format(new SimpleCorrelation(correlationId, Instant.MIN, Instant.MIN.plusMillis(125)), response); assertThat(http, is("Incoming Response: 2d51bc02-677e-11e5-8b9b-10ddb1ee7671\n" + "Duration: 125 ms\n" + "HTTP/1.0 1000\n" + "Content-Type: application/json\n" + "\n" + "{\"success\":true}")); }
@Test void shouldLogResponseForEmptyHeader() throws IOException { final String correlationId = "2d51bc02-677e-11e5-8b9b-10ddb1ee7671"; final HttpResponse response = MockHttpResponse.create() .withProtocolVersion("HTTP/1.0") .withOrigin(Origin.REMOTE) .withStatus(201) .withHeaders(HttpHeaders.empty() .update("Content-Type", "application/json") .update("X-Empty-Header", Collections.emptyList())) .withBodyAsString("{\"success\":true}"); final String http = unit.format(new SimpleCorrelation(correlationId, Instant.MIN, Instant.MIN.plusMillis(125)), response); assertThat(http, is("Incoming Response: 2d51bc02-677e-11e5-8b9b-10ddb1ee7671\n" + "Duration: 125 ms\n" + "HTTP/1.0 201 Created\n" + "Content-Type: application/json\n" + "X-Empty-Header: \n" + "\n" + "{\"success\":true}")); }