@Test
  public void shouldUseCustomWriter() throws IOException {
    final RawHttpRequest request = MockRawHttpRequest.create();

    logbook.write(request);

    verify(writer).isActive(request);
  }
Beispiel #2
0
/** Verifies that {@link LogbookFilter} delegates to {@link HttpLogWriter} correctly. */
public final class WritingTest {

  private final HttpLogFormatter formatter =
      spy(new ForwardingHttpLogFormatter(new DefaultHttpLogFormatter()));
  private final HttpLogWriter writer = mock(HttpLogWriter.class);

  private final MockMvc mvc =
      MockMvcBuilders.standaloneSetup(new ExampleController())
          .addFilter(
              new LogbookFilter(Logbook.builder().formatter(formatter).writer(writer).build()))
          .build();

  @Before
  public void setUp() throws IOException {
    reset(formatter, writer);

    when(writer.isActive(any())).thenReturn(true);
  }

  @Test
  public void shouldLogRequest() throws Exception {
    mvc.perform(
        get("/api/sync")
            .with(protocol("HTTP/1.1"))
            .accept(MediaType.APPLICATION_JSON)
            .header("Host", "localhost")
            .contentType(MediaType.TEXT_PLAIN)
            .content("Hello, world!"));

    @SuppressWarnings("unchecked")
    final ArgumentCaptor<Precorrelation<String>> captor =
        ArgumentCaptor.forClass(Precorrelation.class);
    verify(writer).writeRequest(captor.capture());
    final Precorrelation<String> precorrelation = captor.getValue();

    assertThat(precorrelation.getRequest(), startsWith("Incoming Request:"));
    assertThat(
        precorrelation.getRequest(),
        endsWith(
            "GET http://localhost/api/sync HTTP/1.1\n"
                + "Accept: application/json\n"
                + "Host: localhost\n"
                + "Content-Type: text/plain\n"
                + "\n"
                + "Hello, world!"));
  }

  @Test
  public void shouldLogResponse() throws Exception {
    mvc.perform(get("/api/sync").with(protocol("HTTP/1.1")));

    @SuppressWarnings("unchecked")
    final ArgumentCaptor<Correlation<String, String>> captor =
        ArgumentCaptor.forClass(Correlation.class);
    verify(writer).writeResponse(captor.capture());
    final Correlation<String, String> correlation = captor.getValue();

    assertThat(correlation.getResponse(), startsWith("Outgoing Response:"));
    assertThat(
        correlation.getResponse(),
        endsWith(
            "HTTP/1.1 200 OK\n"
                + "Content-Type: application/json;charset=UTF-8\n"
                + "\n"
                + "{\"value\":\"Hello, world!\"}"));
  }

  private RequestPostProcessor protocol(final String protocol) {
    return request -> {
      request.setProtocol(protocol);
      return request;
    };
  }
}