Search in sources :

Example 31 with HttpURI

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);
}
Also used : InMemoryEventClient(io.airlift.event.client.InMemoryEventClient) Request(org.eclipse.jetty.server.Request) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) HttpURI(org.eclipse.jetty.http.HttpURI) TraceTokenManager(io.airlift.tracetoken.TraceTokenManager) Response(org.eclipse.jetty.server.Response) Principal(java.security.Principal) Test(org.testng.annotations.Test)

Aggregations

HttpURI (org.eclipse.jetty.http.HttpURI)31 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 MetaData (org.eclipse.jetty.http.MetaData)10 Request (org.eclipse.jetty.server.Request)10 HttpFields (org.eclipse.jetty.http.HttpFields)9 Stream (org.eclipse.jetty.http2.api.Stream)8 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)8 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 Test (org.junit.Test)7 InetSocketAddress (java.net.InetSocketAddress)6 Session (org.eclipse.jetty.http2.api.Session)6 FuturePromise (org.eclipse.jetty.util.FuturePromise)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 Promise (org.eclipse.jetty.util.Promise)5 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)4 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)4 ServerConfigService (com.thoughtworks.go.server.service.ServerConfigService)3 HttpChannel (org.eclipse.jetty.server.HttpChannel)3 HttpInput (org.eclipse.jetty.server.HttpInput)3 IOException (java.io.IOException)2