Search in sources :

Example 1 with JettyHttpClient

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

the class TestHttpServerProvider method testClientCertificate.

@Test
public void testClientCertificate() throws Exception {
    HttpServlet servlet = new HttpServlet() {

        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            X509Certificate[] certs = (X509Certificate[]) request.getAttribute("javax.servlet.request.X509Certificate");
            if ((certs == null) || (certs.length == 0)) {
                throw new RuntimeException("No client certificate");
            }
            if (certs.length > 1) {
                throw new RuntimeException("Received multiple client certificates");
            }
            X509Certificate cert = certs[0];
            response.getWriter().write(cert.getSubjectX500Principal().getName());
            response.setStatus(HttpServletResponse.SC_OK);
        }
    };
    config.setHttpEnabled(false).setAdminEnabled(false).setHttpsEnabled(true).setHttpsPort(0).setKeystorePath(getResource("clientcert/server.keystore").toString()).setKeystorePassword("airlift");
    createAndStartServer(servlet);
    HttpClientConfig clientConfig = new HttpClientConfig().setKeyStorePath(getResource("clientcert/client.keystore").toString()).setKeyStorePassword("airlift").setTrustStorePath(getResource("clientcert/client.truststore").toString()).setTrustStorePassword("airlift");
    System.out.println(httpServerInfo.getHttpsUri());
    try (JettyHttpClient httpClient = new JettyHttpClient(clientConfig)) {
        StringResponse response = httpClient.execute(prepareGet().setUri(httpServerInfo.getHttpsUri()).build(), createStringResponseHandler());
        assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
        assertEquals(response.getBody(), "CN=testing,OU=Client,O=Airlift,L=Palo Alto,ST=CA,C=US");
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpClientConfig(io.airlift.http.client.HttpClientConfig) HttpServlet(javax.servlet.http.HttpServlet) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) HttpServletResponse(javax.servlet.http.HttpServletResponse) StringResponse(io.airlift.http.client.StringResponseHandler.StringResponse) X509Certificate(java.security.cert.X509Certificate) Test(org.testng.annotations.Test)

Example 2 with JettyHttpClient

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

the class TestHttpEventClient method setup.

@BeforeMethod
public void setup() throws Exception {
    httpClient = new JettyHttpClient(new HttpClientConfig().setConnectTimeout(new Duration(10, SECONDS)));
    servlet = new DummyServlet();
    server = createServer(servlet);
    server.start();
}
Also used : HttpClientConfig(io.airlift.http.client.HttpClientConfig) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) Duration(io.airlift.units.Duration) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 3 with JettyHttpClient

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

the class TestHttpServerProvider method testAuth.

@Test
public void testAuth() throws Exception {
    File file = File.createTempFile("auth", ".properties", tempDir);
    asCharSink(file, UTF_8).write("user: password");
    config.setUserAuthFile(file.getAbsolutePath());
    createServer();
    server.start();
    try (HttpClient client = new JettyHttpClient()) {
        StringResponse response = client.execute(prepareGet().setUri(httpServerInfo.getHttpUri()).addHeader("Authorization", "Basic " + Base64.getEncoder().encodeToString("user:password".getBytes()).trim()).build(), createStringResponseHandler());
        assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
        assertEquals(response.getBody(), "user");
    }
}
Also used : JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) HttpClient(io.airlift.http.client.HttpClient) StringResponse(io.airlift.http.client.StringResponseHandler.StringResponse) TempFile(io.airlift.testing.TempFile) File(java.io.File) Test(org.testng.annotations.Test)

Example 4 with JettyHttpClient

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

the class TestHttpServerProvider method assertClientCertificateRequest.

private void assertClientCertificateRequest(HttpClientConfig clientConfig, String name) {
    try (JettyHttpClient httpClient = createJettyClient(clientConfig)) {
        URI uri = URI.create(format("https://%s:%s", name, httpServerInfo.getHttpsUri().getPort()));
        StringResponse response = httpClient.execute(prepareGet().setUri(uri).build(), createStringResponseHandler());
        assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);
        assertEquals(response.getBody(), "CN=testing,OU=Client,O=Airlift,L=Palo Alto,ST=CA,C=US");
    }
}
Also used : JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) StringResponse(io.airlift.http.client.StringResponseHandler.StringResponse) URI(java.net.URI)

Example 5 with JettyHttpClient

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

the class TestHttpServerProvider method testShowStackTraceEnabled.

@Test
public void testShowStackTraceEnabled() throws Exception {
    createServer(new ErrorServlet());
    server.start();
    try (HttpClient client = new JettyHttpClient()) {
        StringResponse response = client.execute(prepareGet().setUri(httpServerInfo.getHttpUri()).build(), createStringResponseHandler());
        assertEquals(response.getStatusCode(), 500);
        assertContains(response.getBody(), "ErrorServlet.java");
    }
}
Also used : JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) HttpClient(io.airlift.http.client.HttpClient) StringResponse(io.airlift.http.client.StringResponseHandler.StringResponse) 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