RequestLogger { public void logRequest() { requestRecord.stop(); addRequestHandlingParameters(); generateLog(); } RequestLogger(RequestRecord requestRecord, HttpServletRequest httpRequest,
			HttpServletResponse httpResponse); static final boolean isRequestLoggingEnabled(); void logRequest();  }
@Test public void stopsRequestRecord() throws Exception { createLoggerWithoutResponse(httpResponse).logRequest(); verify(requestRecord).stop(); }
@Test public void addsHttpStatusAsValue() throws Exception { when(httpResponse.getStatus()).thenReturn(123); createLoggerWithoutResponse(httpResponse).logRequest(); verify(requestRecord).addValue(eq(Fields.RESPONSE_STATUS), valueCaptor.capture()); assertThat(valueCaptor.getValue().asLong(), is(123L)); }
@Test public void addsResponseContentTypeAsTag() throws Exception { mockGetHeader(HttpHeaders.CONTENT_TYPE, "application/vnd.test"); createLoggerWithoutResponse(httpResponse).logRequest(); verify(requestRecord).addTag(Fields.RESPONSE_CONTENT_TYPE, "application/vnd.test"); }
@Test public void addsRequestContentLengthAsValue() throws Exception { when(httpRequest.getContentLength()).thenReturn(12345); createLoggerWithoutResponse(httpResponse).logRequest(); verify(requestRecord).addValue(eq(Fields.REQUEST_SIZE_B), valueCaptor.capture()); assertThat(valueCaptor.getValue().asLong(), is(12345L)); }
@Test public void addsResponseContentLengthAsValueFromHeaderIfAvailable() throws Exception { mockGetHeader(HttpHeaders.CONTENT_LENGTH, "1234"); createLoggerWithoutResponse(httpResponse).logRequest(); verify(requestRecord).addValue(eq(Fields.RESPONSE_SIZE_B), valueCaptor.capture()); verifyZeroInteractions(responseWrapper); assertThat(valueCaptor.getValue().asLong(), is(1234L)); }
@Test public void addsResponseContentLengthAsValueFromWrapperAsFAllback() throws Exception { when(responseWrapper.getContentLength()).thenReturn(1234L); createLoggerWithoutResponse(responseWrapper).logRequest(); verify(requestRecord).addValue(eq(Fields.RESPONSE_SIZE_B), valueCaptor.capture()); assertThat(valueCaptor.getValue().asLong(), is(1234L)); }
@Test public void writesRequestLogWithMDCEntries() throws Exception { Map<String, String> mdcAttributes = new HashMap<>(); mdcAttributes.put("this-key", "this-value"); mdcAttributes.put("that-key", "that-value"); when(httpRequest.getAttribute(MDC.class.getName())).thenReturn(mdcAttributes); createLoggerWithoutResponse(httpResponse).logRequest(); assertThat(systemOut.toString(), both(containsString("\"this-key\":\"this-value\"")) .and(containsString("\"that-key\":\"that-value\""))); }