use of org.eclipse.jetty.http.HttpURI in project airlift by airlift.
the class TestDelimitedRequestLog method testWriteLog.
@Test
public void testWriteLog() throws Exception {
Request request = mock(Request.class);
Response response = mock(Response.class);
Principal principal = mock(Principal.class);
long timeToFirstByte = 456;
long timeToLastByte = 3453;
long now = System.currentTimeMillis();
long timestamp = now - timeToLastByte;
String user = "martin";
String agent = "HttpClient 4.0";
String referrer = "http://www.google.com";
String ip = "4.4.4.4";
String protocol = "protocol";
String method = "GET";
long requestSize = 5432;
String requestContentType = "request/type";
long responseSize = 32311;
int responseCode = 200;
String responseContentType = "response/type";
HttpURI uri = new HttpURI("http://www.example.com/aaa+bbb/ccc?param=hello%20there&other=true");
long beginToDispatchMillis = 333;
long firstToLastContentTimeInMillis = 444;
long beginToEndMillis = 555;
DoubleSummaryStatistics stats = new DoubleSummaryStatistics();
stats.accept(1);
stats.accept(3);
DoubleSummaryStats responseContentInterarrivalStats = new DoubleSummaryStats(stats);
TraceTokenManager tokenManager = new TraceTokenManager();
InMemoryEventClient eventClient = new InMemoryEventClient();
MockCurrentTimeMillisProvider currentTimeMillisProvider = new MockCurrentTimeMillisProvider(timestamp + timeToLastByte);
DelimitedRequestLog logger = new DelimitedRequestLog(file.getAbsolutePath(), 1, 256, Long.MAX_VALUE, tokenManager, eventClient, currentTimeMillisProvider, false);
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.getRequestURI()).thenReturn(uri.toString());
when(request.getUserPrincipal()).thenReturn(principal);
when(request.getMethod()).thenReturn(method);
when(request.getContentRead()).thenReturn(requestSize);
when(request.getHttpVersion()).thenReturn(HTTP_2);
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();
logger.log(request, response, beginToDispatchMillis, beginToEndMillis, firstToLastContentTimeInMillis, responseContentInterarrivalStats);
logger.stop();
List<Object> events = eventClient.getEvents();
assertEquals(events.size(), 1);
HttpRequestEvent event = (HttpRequestEvent) events.get(0);
assertEquals(event.getTimeStamp().toEpochMilli(), timestamp);
assertEquals(event.getClientAddress(), ip);
assertEquals(event.getProtocol(), protocol);
assertEquals(event.getMethod(), method);
assertEquals(event.getRequestUri(), uri.toString());
assertEquals(event.getUser(), user);
assertEquals(event.getAgent(), agent);
assertEquals(event.getReferrer(), referrer);
assertEquals(event.getRequestSize(), requestSize);
assertEquals(event.getRequestContentType(), requestContentType);
assertEquals(event.getResponseSize(), responseSize);
assertEquals(event.getResponseCode(), responseCode);
assertEquals(event.getResponseContentType(), responseContentType);
assertEquals(event.getTimeToFirstByte(), (Long) timeToFirstByte);
assertEquals(event.getTimeToLastByte(), timeToLastByte);
assertEquals(event.getTraceToken(), tokenManager.getCurrentRequestToken());
assertEquals(event.getBeginToDispatchMillis(), beginToDispatchMillis);
assertEquals(event.getFirstToLastContentTimeInMillis(), firstToLastContentTimeInMillis);
assertEquals(event.getResponseContentInterarrivalStats(), responseContentInterarrivalStats);
String actual = asCharSource(file, UTF_8).read();
String expected = String.format("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", ISO_FORMATTER.format(Instant.ofEpochMilli(timestamp)), ip, method, uri, user, agent, responseCode, requestSize, responseSize, event.getTimeToLastByte(), tokenManager.getCurrentRequestToken(), HTTP_2.toString(), beginToDispatchMillis, beginToEndMillis, firstToLastContentTimeInMillis, format("%.2f, %.2f, %.2f, %d", stats.getMin(), stats.getAverage(), stats.getMax(), stats.getCount()));
assertEquals(actual, expected);
}
Aggregations