Search in sources :

Example 1 with InMemoryEventClient

use of io.airlift.event.client.InMemoryEventClient in project airlift by airlift.

the class TestDelimitedRequestLog method testTraceTokenHeader.

@Test
public void testTraceTokenHeader() throws Exception {
    Request request = mock(Request.class);
    Response response = mock(Response.class);
    TraceTokenManager tokenManager = new TraceTokenManager();
    InMemoryEventClient eventClient = new InMemoryEventClient();
    DelimitedRequestLog logger = new DelimitedRequestLog(file.getAbsolutePath(), 1, 256, Long.MAX_VALUE, tokenManager, eventClient, new SystemCurrentTimeMillisProvider(), false);
    String token = "test-trace-token";
    when(request.getHttpVersion()).thenReturn(HTTP_2);
    when(request.getHeader(TRACETOKEN_HEADER)).thenReturn(token);
    // log a request without a token set by tokenManager
    logger.log(request, response, 0, 0, 0, new DoubleSummaryStats(new DoubleSummaryStatistics()));
    // create and set a new token with tokenManager
    tokenManager.createAndRegisterNewRequestToken();
    logger.log(request, response, 0, 0, 0, new DoubleSummaryStats(new DoubleSummaryStatistics()));
    // clear the token HTTP header
    when(request.getHeader(TRACETOKEN_HEADER)).thenReturn(null);
    logger.log(request, response, 0, 0, 0, new DoubleSummaryStats(new DoubleSummaryStatistics()));
    logger.stop();
    List<Object> events = eventClient.getEvents();
    assertEquals(events.size(), 3);
    // first two events should have the token set from the header
    for (int i = 0; i < 2; i++) {
        assertEquals(((HttpRequestEvent) events.get(i)).getTraceToken(), token);
    }
    // last event should have the token set by the tokenManager
    assertEquals(((HttpRequestEvent) events.get(2)).getTraceToken(), tokenManager.getCurrentRequestToken());
}
Also used : Response(org.eclipse.jetty.server.Response) InMemoryEventClient(io.airlift.event.client.InMemoryEventClient) Request(org.eclipse.jetty.server.Request) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) TraceTokenManager(io.airlift.tracetoken.TraceTokenManager) Test(org.testng.annotations.Test)

Example 2 with InMemoryEventClient

use of io.airlift.event.client.InMemoryEventClient in project airlift by airlift.

the class TestDelimitedRequestLog method testXForwardedForSkipPrivateAddresses.

@Test
public void testXForwardedForSkipPrivateAddresses() throws Exception {
    Request request = mock(Request.class);
    Response response = mock(Response.class);
    String clientIp = "1.1.1.1";
    when(request.getRemoteAddr()).thenReturn("9.9.9.9");
    when(request.getHeaders("X-FORWARDED-FOR")).thenReturn(Collections.enumeration(ImmutableList.of(clientIp, "192.168.1.2, 172.16.0.1", "169.254.1.2, 127.1.2.3", "10.1.2.3")));
    when(request.getHttpVersion()).thenReturn(HTTP_2);
    InMemoryEventClient eventClient = new InMemoryEventClient();
    DelimitedRequestLog logger = new DelimitedRequestLog(file.getAbsolutePath(), 1, 256, Long.MAX_VALUE, null, eventClient, false);
    logger.log(request, response, 0, 0, 0, new DoubleSummaryStats(new DoubleSummaryStatistics()));
    logger.stop();
    List<Object> events = eventClient.getEvents();
    assertEquals(events.size(), 1);
    HttpRequestEvent event = (HttpRequestEvent) events.get(0);
    assertEquals(event.getClientAddress(), clientIp);
}
Also used : Response(org.eclipse.jetty.server.Response) InMemoryEventClient(io.airlift.event.client.InMemoryEventClient) Request(org.eclipse.jetty.server.Request) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) Test(org.testng.annotations.Test)

Example 3 with InMemoryEventClient

use of io.airlift.event.client.InMemoryEventClient in project airlift by airlift.

the class TestDelimitedRequestLog method testNoXForwardedFor.

@Test
public void testNoXForwardedFor() throws Exception {
    Request request = mock(Request.class);
    Response response = mock(Response.class);
    String clientIp = "1.1.1.1";
    when(request.getRemoteAddr()).thenReturn(clientIp);
    when(request.getHttpVersion()).thenReturn(HTTP_2);
    InMemoryEventClient eventClient = new InMemoryEventClient();
    DelimitedRequestLog logger = new DelimitedRequestLog(file.getAbsolutePath(), 1, 256, Long.MAX_VALUE, null, eventClient, false);
    logger.log(request, response, 0, 0, 0, new DoubleSummaryStats(new DoubleSummaryStatistics()));
    logger.stop();
    List<Object> events = eventClient.getEvents();
    assertEquals(events.size(), 1);
    HttpRequestEvent event = (HttpRequestEvent) events.get(0);
    assertEquals(event.getClientAddress(), clientIp);
}
Also used : Response(org.eclipse.jetty.server.Response) InMemoryEventClient(io.airlift.event.client.InMemoryEventClient) Request(org.eclipse.jetty.server.Request) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) Test(org.testng.annotations.Test)

Example 4 with InMemoryEventClient

use of io.airlift.event.client.InMemoryEventClient in project airlift by airlift.

the class TestPersonStore method testPut.

@Test
public void testPut() {
    InMemoryEventClient eventClient = new InMemoryEventClient();
    PersonStore store = new PersonStore(new StoreConfig(), eventClient);
    store.put("foo", new Person("foo@example.com", "Mr Foo"));
    assertEquals(new Person("foo@example.com", "Mr Foo"), store.get("foo"));
    assertEquals(store.getAll().size(), 1);
    assertEquals(eventClient.getEvents(), ImmutableList.of(personAdded("foo", new Person("foo@example.com", "Mr Foo"))));
}
Also used : InMemoryEventClient(io.airlift.event.client.InMemoryEventClient) Test(org.testng.annotations.Test)

Example 5 with InMemoryEventClient

use of io.airlift.event.client.InMemoryEventClient in project airlift by airlift.

the class TestPersonStore method testGetAll.

@Test
public void testGetAll() {
    PersonStore store = new PersonStore(new StoreConfig(), new InMemoryEventClient());
    store.put("foo", new Person("foo@example.com", "Mr Foo"));
    store.put("bar", new Person("bar@example.com", "Mr Bar"));
    assertEquals(store.getAll().size(), 2);
    assertEquals(store.getAll(), asList(new Person("foo@example.com", "Mr Foo"), new Person("bar@example.com", "Mr Bar")));
}
Also used : InMemoryEventClient(io.airlift.event.client.InMemoryEventClient) Test(org.testng.annotations.Test)

Aggregations

InMemoryEventClient (io.airlift.event.client.InMemoryEventClient)14 Test (org.testng.annotations.Test)13 DoubleSummaryStatistics (java.util.DoubleSummaryStatistics)6 Request (org.eclipse.jetty.server.Request)6 Response (org.eclipse.jetty.server.Response)6 TraceTokenManager (io.airlift.tracetoken.TraceTokenManager)2 Duration (io.airlift.units.Duration)1 Principal (java.security.Principal)1 HttpURI (org.eclipse.jetty.http.HttpURI)1 BeforeMethod (org.testng.annotations.BeforeMethod)1