@Test public void testBasicSSL() throws Exception { this.server = ServerBootstrap.bootstrap() .setServerInfo(LocalServerTestBase.ORIGIN) .setSslContext(SSLTestContexts.createServerSSLContext()) .create(); this.server.start(); final HttpContext context = new BasicHttpContext(); final TestX509HostnameVerifier hostVerifier = new TestX509HostnameVerifier(); final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( SSLTestContexts.createClientSSLContext(), hostVerifier); final Socket socket = socketFactory.createSocket(context); final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort()); final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https"); final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null, context); try { final SSLSession sslsession = sslSocket.getSession(); Assert.assertNotNull(sslsession); Assert.assertTrue(hostVerifier.isFired()); } finally { sslSocket.close(); } }
@Test public void testBasicDefaultHostnameVerifier() throws Exception { this.server = ServerBootstrap.bootstrap() .setServerInfo(LocalServerTestBase.ORIGIN) .setSslContext(SSLTestContexts.createServerSSLContext()) .create(); this.server.start(); final HttpContext context = new BasicHttpContext(); final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( SSLTestContexts.createClientSSLContext(), SSLConnectionSocketFactory.getDefaultHostnameVerifier()); final Socket socket = socketFactory.createSocket(context); final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort()); final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https"); final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null, context); try { final SSLSession sslsession = sslSocket.getSession(); Assert.assertNotNull(sslsession); } finally { sslSocket.close(); } }
@Test public void testClientAuthSSL() throws Exception { this.server = ServerBootstrap.bootstrap() .setServerInfo(LocalServerTestBase.ORIGIN) .setSslContext(SSLTestContexts.createServerSSLContext()) .create(); this.server.start(); final HttpContext context = new BasicHttpContext(); final TestX509HostnameVerifier hostVerifier = new TestX509HostnameVerifier(); final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( SSLTestContexts.createClientSSLContext(), hostVerifier); final Socket socket = socketFactory.createSocket(context); final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort()); final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https"); final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null, context); try { final SSLSession sslsession = sslSocket.getSession(); Assert.assertNotNull(sslsession); Assert.assertTrue(hostVerifier.isFired()); } finally { sslSocket.close(); } }
@Test(expected=SSLException.class) public void testSSLTrustVerification() throws Exception { this.server = ServerBootstrap.bootstrap() .setServerInfo(LocalServerTestBase.ORIGIN) .setSslContext(SSLTestContexts.createServerSSLContext()) .create(); this.server.start(); final HttpContext context = new BasicHttpContext(); // Use default SSL context final SSLContext defaultsslcontext = SSLContexts.createDefault(); final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(defaultsslcontext, NoopHostnameVerifier.INSTANCE); final Socket socket = socketFactory.createSocket(context); final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort()); final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https"); final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null, context); sslSocket.close(); }
public LocalTestServer(HttpRequestHandler handler) { try { setUp(); HttpProcessor httpproc = HttpProcessorBuilder.create() .add(new ResponseDate()) .add(new ResponseServer(LocalServerTestBase.ORIGIN)) .add(new ResponseContent()) .add(new ResponseConnControl()) .add(new RequestBasicAuth()) .add(new ResponseBasicUnauthorized()).build(); this.serverBootstrap.setHttpProcessor(httpproc); this.serverBootstrap.registerHandler("*", handler); host = start(); } catch (Exception e) { throw new RuntimeException(e); } }
@Before @Override public void setUp() throws Exception { super.setUp(); final HttpProcessor httpproc = HttpProcessorBuilder.create() .add(new ResponseDate()) .add(new ResponseServer(LocalServerTestBase.ORIGIN)) .add(new ResponseContent()) .add(new ResponseConnControl()) .add(new RequestBasicAuth()) .add(new ResponseBasicUnauthorized()).build(); this.serverBootstrap.setHttpProcessor(httpproc); }
@Test public void testBasicAuthenticationSuccessOnNonRepeatablePutExpectContinue() throws Exception { final HttpProcessor httpproc = HttpProcessorBuilder.create() .add(new ResponseDate()) .add(new ResponseServer(LocalServerTestBase.ORIGIN)) .add(new ResponseContent()) .add(new ResponseConnControl()) .add(new RequestBasicAuth()) .add(new ResponseBasicUnauthorized()).build(); this.serverBootstrap.setHttpProcessor(httpproc) .setExpectationVerifier(new AuthExpectationVerifier()) .registerHandler("*", new AuthHandler()); final HttpHost target = start(); final RequestConfig config = RequestConfig.custom() .setExpectContinueEnabled(true) .build(); final HttpPut httpput = new HttpPut("/"); httpput.setConfig(config); httpput.setEntity(new InputStreamEntity( new ByteArrayInputStream( new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 } ), -1)); final HttpClientContext context = HttpClientContext.create(); final TestCredentialsProvider credsProvider = new TestCredentialsProvider( new UsernamePasswordCredentials("test", "test")); context.setCredentialsProvider(credsProvider); final HttpResponse response = this.httpclient.execute(target, httpput, context); final HttpEntity entity = response.getEntity(); Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode()); Assert.assertNotNull(entity); }
@Test public void testSSLTrustVerificationOverride() throws Exception { this.server = ServerBootstrap.bootstrap() .setServerInfo(LocalServerTestBase.ORIGIN) .setSslContext(SSLTestContexts.createServerSSLContext()) .create(); this.server.start(); final HttpContext context = new BasicHttpContext(); final TrustStrategy trustStrategy = new TrustStrategy() { @Override public boolean isTrusted( final X509Certificate[] chain, final String authType) throws CertificateException { return chain.length == 1; } }; final SSLContext sslcontext = SSLContexts.custom() .loadTrustMaterial(null, trustStrategy) .build(); final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( sslcontext, NoopHostnameVerifier.INSTANCE); final Socket socket = socketFactory.createSocket(context); final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort()); final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https"); final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null, context); sslSocket.close(); }
@Test public void testKeepAliveHeaderRespected() throws Exception { final HttpProcessor httpproc = HttpProcessorBuilder.create() .add(new ResponseDate()) .add(new ResponseServer(LocalServerTestBase.ORIGIN)) .add(new ResponseContent()) .add(new ResponseConnControl()) .add(new ResponseKeepAlive()).build(); this.serverBootstrap.setHttpProcessor(httpproc) .registerHandler("/random/*", new RandomHandler()); this.connManager.setMaxTotal(1); this.connManager.setDefaultMaxPerRoute(1); final HttpHost target = start(); HttpResponse response = this.httpclient.execute(target, new HttpGet("/random/2000")); EntityUtils.consume(response.getEntity()); Assert.assertEquals(1, this.connManager.getTotalStats().getAvailable()); response = this.httpclient.execute(target, new HttpGet("/random/2000")); EntityUtils.consume(response.getEntity()); Assert.assertEquals(1, this.connManager.getTotalStats().getAvailable()); // Now sleep for 1.1 seconds and let the timeout do its work Thread.sleep(1100); response = this.httpclient.execute(target, new HttpGet("/random/2000")); EntityUtils.consume(response.getEntity()); Assert.assertEquals(1, this.connManager.getTotalStats().getAvailable()); // Do another request just under the 1 second limit & make // sure we reuse that connection. Thread.sleep(500); response = this.httpclient.execute(target, new HttpGet("/random/2000")); EntityUtils.consume(response.getEntity()); Assert.assertEquals(1, this.connManager.getTotalStats().getAvailable()); }