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");
}
}
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();
}
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");
}
}
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");
}
}
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");
}
}
Aggregations