Search in sources :

Example 1 with StringResponse

use of io.airlift.http.client.StringResponseHandler.StringResponse 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 StringResponse

use of io.airlift.http.client.StringResponseHandler.StringResponse 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 3 with StringResponse

use of io.airlift.http.client.StringResponseHandler.StringResponse 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 4 with StringResponse

use of io.airlift.http.client.StringResponseHandler.StringResponse 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)

Example 5 with StringResponse

use of io.airlift.http.client.StringResponseHandler.StringResponse in project airlift by airlift.

the class TestMBeanResource method testGetHtmlPage.

@Test
public void testGetHtmlPage() {
    StringResponse response = client.execute(prepareGet().setUri(uriFor("/v1/jmx")).build(), createStringResponseHandler());
    assertEquals(response.getStatusCode(), 200);
    assertContentType(response, HTML_UTF_8);
    assertContains(response.getBody(), "<html>");
}
Also used : StringResponse(io.airlift.http.client.StringResponseHandler.StringResponse) Test(org.testng.annotations.Test)

Aggregations

StringResponse (io.airlift.http.client.StringResponseHandler.StringResponse)21 Test (org.testng.annotations.Test)13 JettyHttpClient (io.airlift.http.client.jetty.JettyHttpClient)7 Request (io.airlift.http.client.Request)6 HttpClient (io.airlift.http.client.HttpClient)4 HttpUriBuilder (io.airlift.http.client.HttpUriBuilder)3 URI (java.net.URI)3 MediaType (com.google.common.net.MediaType)2 ObjectMapperProvider (io.airlift.json.ObjectMapperProvider)2 File (java.io.File)2 Throwables.getStackTraceAsString (com.google.common.base.Throwables.getStackTraceAsString)1 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ListMultimap (com.google.common.collect.ListMultimap)1 CONTENT_TYPE (com.google.common.net.HttpHeaders.CONTENT_TYPE)1 Injector (com.google.inject.Injector)1 Bootstrap (io.airlift.bootstrap.Bootstrap)1 AbstractEventClient (io.airlift.event.client.AbstractEventClient)1 EventClient (io.airlift.event.client.EventClient)1 InMemoryEventModule (io.airlift.event.client.InMemoryEventModule)1