Search in sources :

Example 11 with JettyHttpClient

use of io.airlift.http.client.jetty.JettyHttpClient in project airlift by airlift.

the class TestOverrideMethodFilterInHttpServer method setup.

@BeforeClass
public void setup() throws Exception {
    resource = new TestResource();
    server = createServer(resource);
    client = new JettyHttpClient();
    server.start();
}
Also used : JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) BeforeClass(org.testng.annotations.BeforeClass)

Example 12 with JettyHttpClient

use of io.airlift.http.client.jetty.JettyHttpClient in project airlift by airlift.

the class TestHttpServerModule method testHttpRequestEvent.

@Test
public void testHttpRequestEvent() throws Exception {
    Map<String, String> properties = new ImmutableMap.Builder<String, String>().put("http-server.http.port", "0").put("http-server.log.path", new File(tempDir, "http-request.log").getAbsolutePath()).build();
    SingleUseEventClient eventClient = new SingleUseEventClient();
    Bootstrap app = new Bootstrap(new HttpServerModule(), new TestingNodeModule(), new InMemoryEventModule(), new TraceTokenModule(), binder -> newSetBinder(binder, EventClient.class).addBinding().toInstance(eventClient), binder -> binder.bind(Servlet.class).annotatedWith(TheServlet.class).to(EchoServlet.class).in(Scopes.SINGLETON));
    Injector injector = app.setRequiredConfigurationProperties(properties).doNotInitializeLogging().initialize();
    HttpServerInfo httpServerInfo = injector.getInstance(HttpServerInfo.class);
    EchoServlet echoServlet = (EchoServlet) injector.getInstance(Key.get(Servlet.class, TheServlet.class));
    HttpServer server = injector.getInstance(HttpServer.class);
    server.start();
    URI requestUri = httpServerInfo.getHttpUri().resolve("/my/path");
    String userAgent = "my-user-agent";
    String referrer = "http://www.google.com";
    String token = "this is a trace token";
    String requestBody = Joiner.on(" ").join(nCopies(50, "request"));
    String requestContentType = "request/type";
    int responseCode = 555;
    String responseBody = Joiner.on(" ").join(nCopies(100, "response"));
    String responseContentType = "response/type";
    echoServlet.responseBody = responseBody;
    echoServlet.responseStatusCode = responseCode;
    echoServlet.responseHeaders.put("Content-Type", responseContentType);
    long beforeRequest = System.currentTimeMillis();
    long afterRequest;
    HttpRequestEvent event;
    try (JettyHttpClient client = new JettyHttpClient()) {
        // test servlet bound correctly
        StringResponse response = client.execute(preparePost().setUri(requestUri).addHeader(USER_AGENT, userAgent).addHeader(CONTENT_TYPE, requestContentType).addHeader(REFERER, referrer).addHeader("X-Airlift-TraceToken", token).setBodyGenerator(createStaticBodyGenerator(requestBody, UTF_8)).build(), createStringResponseHandler());
        afterRequest = System.currentTimeMillis();
        assertEquals(response.getStatusCode(), responseCode);
        assertEquals(response.getBody(), responseBody);
        assertEquals(response.getHeader("Content-Type"), responseContentType);
        event = (HttpRequestEvent) eventClient.getEvent().get(10, TimeUnit.SECONDS);
    } finally {
        server.stop();
    }
    assertEquals(event.getClientAddress(), echoServlet.remoteAddress);
    assertEquals(event.getProtocol(), "http");
    assertEquals(event.getMethod(), "POST");
    assertEquals(event.getRequestUri(), requestUri.getPath());
    assertNull(event.getUser());
    assertEquals(event.getAgent(), userAgent);
    assertEquals(event.getReferrer(), referrer);
    assertEquals(event.getTraceToken(), token);
    assertEquals(event.getRequestSize(), requestBody.length());
    assertEquals(event.getRequestContentType(), requestContentType);
    assertEquals(event.getResponseSize(), responseBody.length());
    assertEquals(event.getResponseCode(), responseCode);
    assertEquals(event.getResponseContentType(), responseContentType);
    assertTrue(event.getTimeStamp().toEpochMilli() >= beforeRequest);
    assertTrue(event.getTimeToLastByte() <= afterRequest - beforeRequest);
    assertNotNull(event.getTimeToFirstByte());
    assertTrue(event.getTimeToDispatch() <= event.getTimeToFirstByte());
    assertTrue(event.getTimeToFirstByte() <= event.getTimeToLastByte());
}
Also used : TestingNodeModule(io.airlift.node.testing.TestingNodeModule) StringResponse(io.airlift.http.client.StringResponseHandler.StringResponse) URI(java.net.URI) ImmutableMap(com.google.common.collect.ImmutableMap) TraceTokenModule(io.airlift.tracetoken.TraceTokenModule) AbstractEventClient(io.airlift.event.client.AbstractEventClient) EventClient(io.airlift.event.client.EventClient) InMemoryEventModule(io.airlift.event.client.InMemoryEventModule) Injector(com.google.inject.Injector) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) Bootstrap(io.airlift.bootstrap.Bootstrap) File(java.io.File) Test(org.testng.annotations.Test)

Example 13 with JettyHttpClient

use of io.airlift.http.client.jetty.JettyHttpClient in project airlift by airlift.

the class TestHttpServerModule method testServer.

@Test
public void testServer() throws Exception {
    Map<String, String> properties = new ImmutableMap.Builder<String, String>().put("http-server.http.port", "0").put("http-server.log.path", new File(tempDir, "http-request.log").getAbsolutePath()).build();
    Bootstrap app = new Bootstrap(new HttpServerModule(), new TestingNodeModule(), new EventModule(), binder -> {
        binder.bind(Servlet.class).annotatedWith(TheServlet.class).to(DummyServlet.class);
        newSetBinder(binder, Filter.class, TheServlet.class).addBinding().to(DummyFilter.class).in(Scopes.SINGLETON);
        httpServerBinder(binder).bindResource("/", "webapp/user").withWelcomeFile("user-welcome.txt");
        httpServerBinder(binder).bindResource("/", "webapp/user2");
        httpServerBinder(binder).bindResource("path", "webapp/user").withWelcomeFile("user-welcome.txt");
        httpServerBinder(binder).bindResource("path", "webapp/user2");
    });
    Injector injector = app.setRequiredConfigurationProperties(properties).doNotInitializeLogging().initialize();
    HttpServerInfo httpServerInfo = injector.getInstance(HttpServerInfo.class);
    HttpServer server = injector.getInstance(HttpServer.class);
    server.start();
    try (HttpClient client = new JettyHttpClient()) {
        // test servlet bound correctly
        URI httpUri = httpServerInfo.getHttpUri();
        StatusResponse response = client.execute(prepareGet().setUri(httpUri).build(), createStatusResponseHandler());
        assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
        // test filter bound correctly
        response = client.execute(prepareGet().setUri(httpUri.resolve("/filter")).build(), createStatusResponseHandler());
        assertEquals(response.getStatusCode(), HttpServletResponse.SC_PAYMENT_REQUIRED);
        // test http resources
        assertResource(httpUri, client, "", "welcome user!");
        assertResource(httpUri, client, "user-welcome.txt", "welcome user!");
        assertResource(httpUri, client, "user.txt", "user");
        assertResource(httpUri, client, "user2.txt", "user2");
        assertRedirect(httpUri, client, "path", "/path/");
        assertResource(httpUri, client, "path/", "welcome user!");
        assertResource(httpUri, client, "path/user-welcome.txt", "welcome user!");
        assertResource(httpUri, client, "path/user.txt", "user");
        assertResource(httpUri, client, "path/user2.txt", "user2");
    } finally {
        server.stop();
    }
}
Also used : EventModule(io.airlift.event.client.EventModule) InMemoryEventModule(io.airlift.event.client.InMemoryEventModule) TestingNodeModule(io.airlift.node.testing.TestingNodeModule) StatusResponse(io.airlift.http.client.StatusResponseHandler.StatusResponse) URI(java.net.URI) ImmutableMap(com.google.common.collect.ImmutableMap) Filter(javax.servlet.Filter) Injector(com.google.inject.Injector) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) HttpClient(io.airlift.http.client.HttpClient) Bootstrap(io.airlift.bootstrap.Bootstrap) File(java.io.File) Test(org.testng.annotations.Test)

Example 14 with JettyHttpClient

use of io.airlift.http.client.jetty.JettyHttpClient in project airlift by airlift.

the class TestTestingHttpServer method testGuiceInjectionWithResources.

@Test
public void testGuiceInjectionWithResources() {
    DummyServlet servlet = new DummyServlet();
    Bootstrap app = new Bootstrap(new TestingNodeModule(), new TestingHttpServerModule(), binder -> {
        binder.bind(Servlet.class).annotatedWith(TheServlet.class).toInstance(servlet);
        binder.bind(new TypeLiteral<Map<String, String>>() {
        }).annotatedWith(TheServlet.class).toInstance(ImmutableMap.of());
        httpServerBinder(binder).bindResource("/", "webapp/user").withWelcomeFile("user-welcome.txt");
        httpServerBinder(binder).bindResource("/", "webapp/user2");
        httpServerBinder(binder).bindResource("path", "webapp/user").withWelcomeFile("user-welcome.txt");
        httpServerBinder(binder).bindResource("path", "webapp/user2");
    });
    Injector injector = app.doNotInitializeLogging().initialize();
    LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
    TestingHttpServer server = injector.getInstance(TestingHttpServer.class);
    try (HttpClient client = new JettyHttpClient(new HttpClientConfig().setConnectTimeout(new Duration(1, SECONDS)))) {
        // test http resources
        URI uri = server.getBaseUrl();
        assertResource(uri, client, "", "welcome user!");
        assertResource(uri, client, "user-welcome.txt", "welcome user!");
        assertResource(uri, client, "user.txt", "user");
        assertResource(uri, client, "user2.txt", "user2");
        assertResource(uri, client, "path", "welcome user!");
        assertResource(uri, client, "path/", "welcome user!");
        assertResource(uri, client, "path/user-welcome.txt", "welcome user!");
        assertResource(uri, client, "path/user.txt", "user");
        assertResource(uri, client, "path/user2.txt", "user2");
        // verify that servlet did not receive resource requests
        assertEquals(servlet.getCallCount(), 0);
    } finally {
        lifeCycleManager.stop();
    }
}
Also used : HttpClientConfig(io.airlift.http.client.HttpClientConfig) TestingNodeModule(io.airlift.node.testing.TestingNodeModule) Duration(io.airlift.units.Duration) TheServlet(io.airlift.http.server.TheServlet) URI(java.net.URI) LifeCycleManager(io.airlift.bootstrap.LifeCycleManager) Injector(com.google.inject.Injector) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) HttpClient(io.airlift.http.client.HttpClient) Bootstrap(io.airlift.bootstrap.Bootstrap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.testng.annotations.Test)

Example 15 with JettyHttpClient

use of io.airlift.http.client.jetty.JettyHttpClient in project airlift by airlift.

the class TestTestingHttpServer method testGuiceInjectionWithoutFilters.

@Test
public void testGuiceInjectionWithoutFilters() {
    DummyServlet servlet = new DummyServlet();
    Bootstrap app = new Bootstrap(new TestingNodeModule(), new TestingHttpServerModule(), binder -> {
        binder.bind(Servlet.class).annotatedWith(TheServlet.class).toInstance(servlet);
        binder.bind(new TypeLiteral<Map<String, String>>() {
        }).annotatedWith(TheServlet.class).toInstance(ImmutableMap.of());
    });
    Injector injector = app.doNotInitializeLogging().initialize();
    LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
    TestingHttpServer server = injector.getInstance(TestingHttpServer.class);
    try (HttpClient client = new JettyHttpClient(new HttpClientConfig().setConnectTimeout(new Duration(1, SECONDS)))) {
        StatusResponse response = client.execute(prepareGet().setUri(server.getBaseUrl()).build(), createStatusResponseHandler());
        assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
        assertEquals(servlet.getCallCount(), 1);
    } finally {
        lifeCycleManager.stop();
    }
}
Also used : HttpClientConfig(io.airlift.http.client.HttpClientConfig) TestingNodeModule(io.airlift.node.testing.TestingNodeModule) Duration(io.airlift.units.Duration) StatusResponse(io.airlift.http.client.StatusResponseHandler.StatusResponse) TheServlet(io.airlift.http.server.TheServlet) LifeCycleManager(io.airlift.bootstrap.LifeCycleManager) Injector(com.google.inject.Injector) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) HttpClient(io.airlift.http.client.HttpClient) Bootstrap(io.airlift.bootstrap.Bootstrap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.testng.annotations.Test)

Aggregations

JettyHttpClient (io.airlift.http.client.jetty.JettyHttpClient)29 Test (org.testng.annotations.Test)21 HttpClient (io.airlift.http.client.HttpClient)11 HttpClientConfig (io.airlift.http.client.HttpClientConfig)11 Injector (com.google.inject.Injector)10 Bootstrap (io.airlift.bootstrap.Bootstrap)10 Duration (io.airlift.units.Duration)9 LifeCycleManager (io.airlift.bootstrap.LifeCycleManager)8 TestingNodeModule (io.airlift.node.testing.TestingNodeModule)8 StatusResponse (io.airlift.http.client.StatusResponseHandler.StatusResponse)7 StringResponse (io.airlift.http.client.StringResponseHandler.StringResponse)7 URI (java.net.URI)7 ImmutableMap (com.google.common.collect.ImmutableMap)5 NodeInfo (io.airlift.node.NodeInfo)4 InMemoryEventModule (io.airlift.event.client.InMemoryEventModule)3 TheServlet (io.airlift.http.server.TheServlet)3 TraceTokenModule (io.airlift.tracetoken.TraceTokenModule)3 File (java.io.File)3 Map (java.util.Map)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3