@Test
public void testWriteLog()
throws Exception
{
final Request request = mock(Request.class);
final Response response = mock(Response.class);
final Principal principal = mock(Principal.class);
final long timeToFirstByte = 456;
final long timeToLastByte = 3453;
final long now = System.currentTimeMillis();
final long timestamp = now - timeToLastByte;
final String user = "martin";
final String agent = "HttpClient 4.0";
final String referrer = "http://www.google.com";
final String ip = "4.4.4.4";
final String protocol = "protocol";
final String method = "GET";
final long requestSize = 5432;
final String requestContentType = "request/type";
final long responseSize = 32311;
final int responseCode = 200;
final String responseContentType = "response/type";
final HttpURI uri = new HttpURI("http://www.example.com/aaa+bbb/ccc?param=hello%20there&other=true");
final TraceTokenManager tokenManager = new TraceTokenManager();
MockCurrentTimeMillisProvider currentTimeMillisProvider = new MockCurrentTimeMillisProvider(timestamp + timeToLastByte);
DelimitedRequestLog logger = new DelimitedRequestLog(file.getAbsolutePath(), 1, 1_000_000_000, tokenManager, currentTimeMillisProvider);
when(principal.getName()).thenReturn(user);
when(request.getTimeStamp()).thenReturn(timestamp);
when(request.getHeader("User-Agent")).thenReturn(agent);
when(request.getHeader("Referer")).thenReturn(referrer);
when(request.getRemoteAddr()).thenReturn("9.9.9.9");
when(request.getHeaders("X-FORWARDED-FOR")).thenReturn(Collections.enumeration(ImmutableList.of("1.1.1.1, 2.2.2.2", "3.3.3.3, " + ip)));
when(request.getProtocol()).thenReturn("unknown");
when(request.getHeader("X-FORWARDED-PROTO")).thenReturn(protocol);
when(request.getAttribute(TimingFilter.FIRST_BYTE_TIME)).thenReturn(timestamp + timeToFirstByte);
when(request.getUri()).thenReturn(uri);
when(request.getUserPrincipal()).thenReturn(principal);
when(request.getMethod()).thenReturn(method);
when(request.getContentRead()).thenReturn(requestSize);
when(request.getHeader("Content-Type")).thenReturn(requestContentType);
when(response.getStatus()).thenReturn(responseCode);
when(response.getContentCount()).thenReturn(responseSize);
when(response.getHeader("Content-Type")).thenReturn(responseContentType);
tokenManager.createAndRegisterNewRequestToken();
long currentTime = currentTimeMillisProvider.getCurrentTimeMillis();
logger.log(request, response);
logger.stop();