CurlHttpLogFormatter implements HttpLogFormatter { @Override public String format(final Precorrelation precorrelation, final HttpRequest request) throws IOException { final List<String> command = new ArrayList<>(); command.add(precorrelation.getId()); command.add("curl"); command.add("-v"); command.add("-X"); command.add(request.getMethod()); command.add(quote(request.getRequestUri())); request.getHeaders().forEach((header, values) -> { values.forEach(value -> { command.add("-H"); command.add(quote(header + ": " + value)); }); }); final String body = request.getBodyAsString(); if (!body.isEmpty()) { command.add("--data-binary"); command.add(quote(body)); } return String.join(" ", command); } CurlHttpLogFormatter();  CurlHttpLogFormatter(final HttpLogFormatter fallback); @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 HttpLogFormatter unit = new CurlHttpLogFormatter(); final String curl = unit.format(new SimplePrecorrelation(correlationId, systemUTC()), request); assertThat(curl, is("c9408eaa-677d-11e5-9457-10ddb1ee7671 " + "curl -v -X GET 'http: }
@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 HttpLogFormatter unit = new CurlHttpLogFormatter(); final String curl = unit.format(new SimplePrecorrelation(correlationId, systemUTC()), request); assertThat(curl, is("0eae9f6c-6824-11e5-8b0a-10ddb1ee7671 " + "curl -v -X GET 'http: }
@Test void shouldEscape() 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("char='") .withHeaders(HttpHeaders.of("Foo'Bar", "Baz")) .withBodyAsString("{\"message\":\"Hello, 'world'!\"}"); final HttpLogFormatter unit = new CurlHttpLogFormatter(); final String curl = unit.format(new SimplePrecorrelation(correlationId, systemUTC()), request); assertThat(curl, is("c9408eaa-677d-11e5-9457-10ddb1ee7671 " + "curl -v -X GET 'http: }
@Test void shouldDelegateLogResponse() throws IOException { final HttpLogFormatter fallback = mock(HttpLogFormatter.class); final HttpLogFormatter unit = new CurlHttpLogFormatter(fallback); final MockHttpResponse response = MockHttpResponse.create(); final Correlation correlation = new SimpleCorrelation("3881ae92-6824-11e5-921b-10ddb1ee7671", MIN, MIN); unit.format(correlation, response); verify(fallback).format(correlation, response); }