hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1603443 [3/3] - in /httpcomponents/httpclient/trunk: fluent-hc/src/test/java/org/apache/http/client/fluent/ httpclient-cache/src/test/java/org/apache/http/impl/client/cache/ httpclient/src/test/java/org/apache/http/conn/ssl/ httpclient/src...
Date Wed, 18 Jun 2014 13:06:44 GMT
Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestRedirects.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestRedirects.java?rev=1603443&r1=1603442&r2=1603443&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestRedirects.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestRedirects.java
Wed Jun 18 13:06:43 2014
@@ -51,28 +51,22 @@ import org.apache.http.client.utils.URIU
 import org.apache.http.cookie.SM;
 import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.client.BasicCookieStore;
-import org.apache.http.impl.client.HttpClients;
 import org.apache.http.impl.cookie.BasicClientCookie;
+import org.apache.http.localserver.LocalServerTestBase;
 import org.apache.http.message.BasicHeader;
 import org.apache.http.protocol.HTTP;
 import org.apache.http.protocol.HttpContext;
 import org.apache.http.protocol.HttpCoreContext;
 import org.apache.http.protocol.HttpRequestHandler;
+import org.apache.http.protocol.UriHttpRequestHandlerMapper;
 import org.apache.http.util.EntityUtils;
 import org.junit.Assert;
-import org.junit.Before;
 import org.junit.Test;
 
 /**
  * Redirection test cases.
  */
-public class TestRedirects extends IntegrationTestBase {
-
-    @Before
-    public void setUp() throws Exception {
-        startServer();
-        this.httpclient = HttpClients.createDefault();
-    }
+public class TestRedirects extends LocalServerTestBase {
 
     private static class BasicRedirectService implements HttpRequestHandler {
 
@@ -93,7 +87,10 @@ public class TestRedirects extends Integ
                 final HttpResponse response,
                 final HttpContext context) throws HttpException, IOException {
             final HttpInetConnection conn = (HttpInetConnection) context.getAttribute(HttpCoreContext.HTTP_CONNECTION);
-            final String localhost = conn.getLocalAddress().getHostName();
+            String localhost = conn.getLocalAddress().getHostName();
+            if (localhost.equals("127.0.0.1")) {
+                localhost = "localhost";
+            }
             final int port = conn.getLocalPort();
             final String uri = request.getRequestLine().getUri();
             if (uri.equals("/oldlocation/")) {
@@ -238,10 +235,11 @@ public class TestRedirects extends Integ
 
     @Test
     public void testBasicRedirect300() throws Exception {
-        final HttpHost target = getServerHttp();
-        this.localServer.register("*",
+        this.serverBootstrap.registerHandler("*",
                 new BasicRedirectService(HttpStatus.SC_MULTIPLE_CHOICES));
 
+        final HttpHost target = start();
+
         final HttpClientContext context = HttpClientContext.create();
 
         final HttpGet httpget = new HttpGet("/oldlocation/");
@@ -260,10 +258,11 @@ public class TestRedirects extends Integ
 
     @Test
     public void testBasicRedirect301() throws Exception {
-        final HttpHost target = getServerHttp();
-        this.localServer.register("*",
+        this.serverBootstrap.registerHandler("*",
                 new BasicRedirectService(HttpStatus.SC_MOVED_PERMANENTLY));
 
+        final HttpHost target = start();
+
         final HttpClientContext context = HttpClientContext.create();
 
         final HttpGet httpget = new HttpGet("/oldlocation/");
@@ -288,10 +287,11 @@ public class TestRedirects extends Integ
 
     @Test
     public void testBasicRedirect302() throws Exception {
-        final HttpHost target = getServerHttp();
-        this.localServer.register("*",
+        this.serverBootstrap.registerHandler("*",
                 new BasicRedirectService(HttpStatus.SC_MOVED_TEMPORARILY));
 
+        final HttpHost target = start();
+
         final HttpClientContext context = HttpClientContext.create();
 
         final HttpGet httpget = new HttpGet("/oldlocation/");
@@ -309,8 +309,7 @@ public class TestRedirects extends Integ
 
     @Test
     public void testBasicRedirect302NoLocation() throws Exception {
-        final HttpHost target = getServerHttp();
-        this.localServer.register("*", new HttpRequestHandler() {
+        this.serverBootstrap.registerHandler("*", new HttpRequestHandler() {
 
             @Override
             public void handle(
@@ -322,6 +321,8 @@ public class TestRedirects extends Integ
 
         });
 
+        final HttpHost target = start();
+
         final HttpClientContext context = HttpClientContext.create();
 
         final HttpGet httpget = new HttpGet("/oldlocation/");
@@ -339,10 +340,11 @@ public class TestRedirects extends Integ
 
     @Test
     public void testBasicRedirect303() throws Exception {
-        final HttpHost target = getServerHttp();
-        this.localServer.register("*",
+        this.serverBootstrap.registerHandler("*",
                 new BasicRedirectService(HttpStatus.SC_SEE_OTHER));
 
+        final HttpHost target = start();
+
         final HttpClientContext context = HttpClientContext.create();
 
         final HttpGet httpget = new HttpGet("/oldlocation/");
@@ -360,10 +362,11 @@ public class TestRedirects extends Integ
 
     @Test
     public void testBasicRedirect304() throws Exception {
-        final HttpHost target = getServerHttp();
-        this.localServer.register("*",
+        this.serverBootstrap.registerHandler("*",
                 new BasicRedirectService(HttpStatus.SC_NOT_MODIFIED));
 
+        final HttpHost target = start();
+
         final HttpClientContext context = HttpClientContext.create();
 
         final HttpGet httpget = new HttpGet("/oldlocation/");
@@ -379,9 +382,10 @@ public class TestRedirects extends Integ
 
     @Test
     public void testBasicRedirect305() throws Exception {
-        final HttpHost target = getServerHttp();
-        this.localServer.register("*",
+        this.serverBootstrap.registerHandler("*",
                 new BasicRedirectService(HttpStatus.SC_USE_PROXY));
+        final HttpHost target = start();
+
         final HttpClientContext context = HttpClientContext.create();
 
         final HttpGet httpget = new HttpGet("/oldlocation/");
@@ -397,10 +401,11 @@ public class TestRedirects extends Integ
 
     @Test
     public void testBasicRedirect307() throws Exception {
-        final HttpHost target = getServerHttp();
-        this.localServer.register("*",
+        this.serverBootstrap.registerHandler("*",
                 new BasicRedirectService(HttpStatus.SC_TEMPORARY_REDIRECT));
 
+        final HttpHost target = start();
+
         final HttpClientContext context = HttpClientContext.create();
 
         final HttpGet httpget = new HttpGet("/oldlocation/");
@@ -418,8 +423,9 @@ public class TestRedirects extends Integ
 
     @Test(expected=ClientProtocolException.class)
     public void testMaxRedirectCheck() throws Exception {
-        final HttpHost target = getServerHttp();
-        this.localServer.register("*", new CircularRedirectService());
+        this.serverBootstrap.registerHandler("*", new CircularRedirectService());
+
+        final HttpHost target = start();
 
         final RequestConfig config = RequestConfig.custom()
             .setCircularRedirectsAllowed(true)
@@ -438,8 +444,9 @@ public class TestRedirects extends Integ
 
     @Test(expected=ClientProtocolException.class)
     public void testCircularRedirect() throws Exception {
-        final HttpHost target = getServerHttp();
-        this.localServer.register("*", new CircularRedirectService());
+        this.serverBootstrap.registerHandler("*", new CircularRedirectService());
+
+        final HttpHost target = start();
 
         final RequestConfig config = RequestConfig.custom()
             .setCircularRedirectsAllowed(false)
@@ -457,8 +464,9 @@ public class TestRedirects extends Integ
 
     @Test
     public void testRepeatRequest() throws Exception {
-        final HttpHost target = getServerHttp();
-        this.localServer.register("*", new RomeRedirectService());
+        this.serverBootstrap.registerHandler("*", new RomeRedirectService());
+
+        final HttpHost target = start();
 
         final HttpClientContext context = HttpClientContext.create();
 
@@ -484,8 +492,9 @@ public class TestRedirects extends Integ
 
     @Test
     public void testRepeatRequestRedirect() throws Exception {
-        final HttpHost target = getServerHttp();
-        this.localServer.register("*", new RomeRedirectService());
+        this.serverBootstrap.registerHandler("*", new RomeRedirectService());
+
+        final HttpHost target = start();
 
         final HttpClientContext context = HttpClientContext.create();
 
@@ -512,8 +521,9 @@ public class TestRedirects extends Integ
 
     @Test
     public void testDifferentRequestSameRedirect() throws Exception {
-        final HttpHost target = getServerHttp();
-        this.localServer.register("*", new RomeRedirectService());
+        this.serverBootstrap.registerHandler("*", new RomeRedirectService());
+
+        final HttpHost target = start();
 
         final HttpClientContext context = HttpClientContext.create();
 
@@ -540,8 +550,9 @@ public class TestRedirects extends Integ
 
     @Test
     public void testPostNoRedirect() throws Exception {
-        final HttpHost target = getServerHttp();
-        this.localServer.register("*", new BasicRedirectService());
+        this.serverBootstrap.registerHandler("*", new BasicRedirectService());
+
+        final HttpHost target = start();
 
         final HttpClientContext context = HttpClientContext.create();
 
@@ -560,8 +571,9 @@ public class TestRedirects extends Integ
 
     @Test
     public void testPostRedirectSeeOther() throws Exception {
-        final HttpHost target = getServerHttp();
-        this.localServer.register("*", new BasicRedirectService(HttpStatus.SC_SEE_OTHER));
+        this.serverBootstrap.registerHandler("*", new BasicRedirectService(HttpStatus.SC_SEE_OTHER));
+
+        final HttpHost target = start();
 
         final HttpClientContext context = HttpClientContext.create();
 
@@ -580,8 +592,9 @@ public class TestRedirects extends Integ
 
     @Test
     public void testRelativeRedirect() throws Exception {
-        final HttpHost target = getServerHttp();
-        this.localServer.register("*", new RelativeRedirectService());
+        this.serverBootstrap.registerHandler("*", new RelativeRedirectService());
+
+        final HttpHost target = start();
 
         final HttpClientContext context = HttpClientContext.create();
 
@@ -602,8 +615,9 @@ public class TestRedirects extends Integ
 
     @Test
     public void testRelativeRedirect2() throws Exception {
-        final HttpHost target = getServerHttp();
-        this.localServer.register("*", new RelativeRedirectService2());
+        this.serverBootstrap.registerHandler("*", new RelativeRedirectService2());
+
+        final HttpHost target = start();
 
         final HttpClientContext context = HttpClientContext.create();
 
@@ -624,8 +638,9 @@ public class TestRedirects extends Integ
 
     @Test(expected=ClientProtocolException.class)
     public void testRejectRelativeRedirect() throws Exception {
-        final HttpHost target = getServerHttp();
-        this.localServer.register("*", new RelativeRedirectService());
+        this.serverBootstrap.registerHandler("*", new RelativeRedirectService());
+
+        final HttpHost target = start();
 
         final RequestConfig config = RequestConfig.custom().setRelativeRedirectsAllowed(false).build();
         final HttpGet httpget = new HttpGet("/oldlocation/");
@@ -640,8 +655,9 @@ public class TestRedirects extends Integ
 
     @Test(expected=ClientProtocolException.class)
     public void testRejectBogusRedirectLocation() throws Exception {
-        final HttpHost target = getServerHttp();
-        this.localServer.register("*", new BogusRedirectService("xxx://bogus"));
+        this.serverBootstrap.registerHandler("*", new BogusRedirectService("xxx://bogus"));
+
+        final HttpHost target = start();
 
         final HttpGet httpget = new HttpGet("/oldlocation/");
 
@@ -656,8 +672,12 @@ public class TestRedirects extends Integ
 
     @Test(expected=ClientProtocolException.class)
     public void testRejectInvalidRedirectLocation() throws Exception {
-        final HttpHost target = getServerHttp();
-        this.localServer.register("*",
+        final UriHttpRequestHandlerMapper reqistry = new UriHttpRequestHandlerMapper();
+        this.serverBootstrap.setHandlerMapper(reqistry);
+
+        final HttpHost target = start();
+
+        reqistry.register("*",
                 new BogusRedirectService("http://" + target.toHostString() +
                         "/newlocation/?p=I have spaces"));
 
@@ -673,9 +693,9 @@ public class TestRedirects extends Integ
 
     @Test
     public void testRedirectWithCookie() throws Exception {
-        final HttpHost target = getServerHttp();
+        this.serverBootstrap.registerHandler("*", new BasicRedirectService());
 
-        this.localServer.register("*", new BasicRedirectService());
+        final HttpHost target = start();
 
         final CookieStore cookieStore = new BasicCookieStore();
 
@@ -689,7 +709,6 @@ public class TestRedirects extends Integ
         context.setCookieStore(cookieStore);
         final HttpGet httpget = new HttpGet("/oldlocation/");
 
-
         final HttpResponse response = this.httpclient.execute(target, httpget, context);
         EntityUtils.consume(response.getEntity());
 
@@ -704,12 +723,11 @@ public class TestRedirects extends Integ
 
     @Test
     public void testDefaultHeadersRedirect() throws Exception {
-        this.httpclient = HttpClients.custom()
-            .setDefaultHeaders(Arrays.asList(new BasicHeader(HTTP.USER_AGENT, "my-test-client")))
-            .build();
-        final HttpHost target = getServerHttp();
+        this.clientBuilder.setDefaultHeaders(Arrays.asList(new BasicHeader(HTTP.USER_AGENT,
"my-test-client")));
+
+        this.serverBootstrap.registerHandler("*", new BasicRedirectService());
 
-        this.localServer.register("*", new BasicRedirectService());
+        final HttpHost target = start();
 
         final HttpClientContext context = HttpClientContext.create();
 

Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestSPNegoScheme.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestSPNegoScheme.java?rev=1603443&r1=1603442&r2=1603443&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestSPNegoScheme.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestSPNegoScheme.java
Wed Jun 18 13:06:43 2014
@@ -47,6 +47,7 @@ import org.apache.http.entity.StringEnti
 import org.apache.http.impl.auth.SPNegoScheme;
 import org.apache.http.impl.client.BasicCredentialsProvider;
 import org.apache.http.impl.client.HttpClients;
+import org.apache.http.localserver.LocalServerTestBase;
 import org.apache.http.message.BasicHeader;
 import org.apache.http.protocol.HttpContext;
 import org.apache.http.protocol.HttpRequestHandler;
@@ -57,7 +58,6 @@ import org.ietf.jgss.GSSManager;
 import org.ietf.jgss.GSSName;
 import org.ietf.jgss.Oid;
 import org.junit.Assert;
-import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Matchers;
 import org.mockito.Mockito;
@@ -65,12 +65,7 @@ import org.mockito.Mockito;
 /**
  * Tests for {@link SPNegoScheme}.
  */
-public class TestSPNegoScheme extends IntegrationTestBase {
-
-    @Before
-    public void setUp() throws Exception {
-        startServer();
-    }
+public class TestSPNegoScheme extends LocalServerTestBase {
 
     /**
      * This service will continue to ask for authentication.
@@ -156,10 +151,8 @@ public class TestSPNegoScheme extends In
      */
     @Test
     public void testDontTryToAuthenticateEndlessly() throws Exception {
-        final int port = this.localServer.getServiceAddress().getPort();
-        this.localServer.register("*", new PleaseNegotiateService());
-
-        final HttpHost target = new HttpHost("localhost", port);
+        this.serverBootstrap.registerHandler("*", new PleaseNegotiateService());
+        final HttpHost target = start();
 
         final AuthSchemeProvider nsf = new NegotiateSchemeProviderWithMockGssManager();
         final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
@@ -188,10 +181,8 @@ public class TestSPNegoScheme extends In
      */
     @Test
     public void testNoTokenGeneratedError() throws Exception {
-        final int port = this.localServer.getServiceAddress().getPort();
-        this.localServer.register("*", new PleaseNegotiateService());
-
-        final HttpHost target = new HttpHost("localhost", port);
+        this.serverBootstrap.registerHandler("*", new PleaseNegotiateService());
+        final HttpHost target = start();
 
         final AuthSchemeProvider nsf = new NegotiateSchemeProviderWithMockGssManager();
 

Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestStatefulConnManagement.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestStatefulConnManagement.java?rev=1603443&r1=1603442&r2=1603443&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestStatefulConnManagement.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestStatefulConnManagement.java
Wed Jun 18 13:06:43 2014
@@ -39,25 +39,18 @@ import org.apache.http.client.UserTokenH
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.protocol.HttpClientContext;
 import org.apache.http.entity.StringEntity;
-import org.apache.http.impl.client.HttpClients;
-import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
+import org.apache.http.localserver.LocalServerTestBase;
 import org.apache.http.protocol.BasicHttpContext;
 import org.apache.http.protocol.HttpContext;
 import org.apache.http.protocol.HttpRequestHandler;
 import org.apache.http.util.EntityUtils;
 import org.junit.Assert;
-import org.junit.Before;
 import org.junit.Test;
 
 /**
  * Test cases for state-ful connections.
  */
-public class TestStatefulConnManagement extends IntegrationTestBase {
-
-    @Before
-    public void setUp() throws Exception {
-        startServer();
-    }
+public class TestStatefulConnManagement extends LocalServerTestBase {
 
     private static class SimpleService implements HttpRequestHandler {
 
@@ -82,14 +75,10 @@ public class TestStatefulConnManagement 
         final int workerCount = 5;
         final int requestCount = 5;
 
-        final int port = this.localServer.getServiceAddress().getPort();
-        this.localServer.register("*", new SimpleService());
+        this.serverBootstrap.registerHandler("*", new SimpleService());
 
-        final HttpHost target = new HttpHost("localhost", port);
-
-        final PoolingHttpClientConnectionManager mgr = new PoolingHttpClientConnectionManager();
-        mgr.setMaxTotal(workerCount);
-        mgr.setDefaultMaxPerRoute(workerCount);
+        this.connManager.setMaxTotal(workerCount);
+        this.connManager.setDefaultMaxPerRoute(workerCount);
 
         final UserTokenHandler userTokenHandler = new UserTokenHandler() {
 
@@ -100,11 +89,9 @@ public class TestStatefulConnManagement 
             }
 
         };
+        this.clientBuilder.setUserTokenHandler(userTokenHandler);
 
-        this.httpclient = HttpClients.custom()
-            .setConnectionManager(mgr)
-            .setUserTokenHandler(userTokenHandler)
-            .build();
+        final HttpHost target = start();
 
         final HttpClientContext[] contexts = new HttpClientContext[workerCount];
         final HttpWorker[] workers = new HttpWorker[workerCount];
@@ -214,13 +201,10 @@ public class TestStatefulConnManagement 
 
         final int maxConn = 2;
 
-        final int port = this.localServer.getServiceAddress().getPort();
-        this.localServer.register("*", new SimpleService());
+        this.serverBootstrap.registerHandler("*", new SimpleService());
 
-        // We build a client with 2 max active // connections, and 2 max per route.
-        final PoolingHttpClientConnectionManager connMngr = new PoolingHttpClientConnectionManager();
-        connMngr.setMaxTotal(maxConn);
-        connMngr.setDefaultMaxPerRoute(maxConn);
+        this.connManager.setMaxTotal(maxConn);
+        this.connManager.setDefaultMaxPerRoute(maxConn);
 
         final UserTokenHandler userTokenHandler = new UserTokenHandler() {
 
@@ -231,16 +215,15 @@ public class TestStatefulConnManagement 
 
         };
 
-        this.httpclient = HttpClients.custom()
-            .setConnectionManager(connMngr)
-            .setUserTokenHandler(userTokenHandler)
-            .build();
+        this.clientBuilder.setUserTokenHandler(userTokenHandler);
+
+        final HttpHost target = start();
 
         // Bottom of the pool : a *keep alive* connection to Route 1.
         final HttpContext context1 = new BasicHttpContext();
         context1.setAttribute("user", "stuff");
         final HttpResponse response1 = this.httpclient.execute(
-                new HttpHost("localhost", port), new HttpGet("/"), context1);
+                target, new HttpGet("/"), context1);
         EntityUtils.consume(response1.getEntity());
 
         // The ConnPoolByRoute now has 1 free connection, out of 2 max
@@ -253,7 +236,7 @@ public class TestStatefulConnManagement 
         // Send it to another route. Must be a keepalive.
         final HttpContext context2 = new BasicHttpContext();
         final HttpResponse response2 = this.httpclient.execute(
-                new HttpHost("127.0.0.1", port), new HttpGet("/"), context2);
+                new HttpHost("127.0.0.1", this.server.getLocalPort()), new HttpGet("/"),
context2);
         EntityUtils.consume(response2.getEntity());
         // ConnPoolByRoute now has 2 free connexions, out of its 2 max.
         // The [localhost][stuff] RouteSpcfcPool is the same as earlier
@@ -268,7 +251,7 @@ public class TestStatefulConnManagement 
         // When this happens, the RouteSpecificPool becomes empty.
         final HttpContext context3 = new BasicHttpContext();
         final HttpResponse response3 = this.httpclient.execute(
-                new HttpHost("localhost", port), new HttpGet("/"), context3);
+                target, new HttpGet("/"), context3);
 
         // If the ConnPoolByRoute did not behave coherently with the RouteSpecificPool
         // this may fail. Ex : if the ConnPool discared the route pool because it was empty,

Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/LocalServerTestBase.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/LocalServerTestBase.java?rev=1603443&r1=1603442&r2=1603443&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/LocalServerTestBase.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/LocalServerTestBase.java
Wed Jun 18 13:06:43 2014
@@ -27,10 +27,17 @@
 
 package org.apache.http.localserver;
 
-import java.net.InetSocketAddress;
+import java.util.concurrent.TimeUnit;
 
 import org.apache.http.HttpHost;
+import org.apache.http.config.SocketConfig;
+import org.apache.http.impl.bootstrap.HttpServer;
+import org.apache.http.impl.bootstrap.ServerBootstrap;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
 import org.junit.After;
+import org.junit.Before;
 
 /**
  * Base class for tests using {@link LocalTestServer}. The server will not be started
@@ -38,37 +45,69 @@ import org.junit.After;
  */
 public abstract class LocalServerTestBase {
 
-    /** The local server for testing. */
-    protected LocalTestServer localServer;
+    public enum ProtocolScheme { http, https };
 
-    @After
-    public void shutDownServer() throws Exception {
-        if (localServer != null) {
-            localServer.stop();
+    public static final String ORIGIN = "TEST/1.1";
+
+    protected final ProtocolScheme scheme;
+
+    protected ServerBootstrap serverBootstrap;
+    protected HttpServer server;
+    protected PoolingHttpClientConnectionManager connManager;
+    protected HttpClientBuilder clientBuilder;
+    protected CloseableHttpClient httpclient;
+
+    public LocalServerTestBase(final ProtocolScheme scheme) {
+        this.scheme = scheme;
+    }
+
+    public LocalServerTestBase() {
+        this(ProtocolScheme.http);
+    }
+
+    public String getSchemeName() {
+        return this.scheme.name();
+    }
+
+    @Before
+    public void setUp() throws Exception {
+        final SocketConfig socketConfig = SocketConfig.custom()
+                .setSoTimeout(15000)
+                .build();
+        this.serverBootstrap = ServerBootstrap.bootstrap()
+                .setSocketConfig(socketConfig)
+                .setServerInfo(ORIGIN)
+                .registerHandler("/echo/*", new EchoHandler())
+                .registerHandler("/random/*", new RandomHandler());
+        if (this.scheme.equals(ProtocolScheme.https)) {
+            this.serverBootstrap.setSslContext(SSLTestContexts.createServerSSLContext());
         }
-        localServer = null;
+
+        this.connManager = new PoolingHttpClientConnectionManager();
+        this.clientBuilder = HttpClientBuilder.create()
+                .setDefaultSocketConfig(socketConfig)
+                .setConnectionManager(this.connManager);
     }
 
-    protected void startServer() throws Exception {
-        if (localServer == null) {
-            localServer = new LocalTestServer(null, null);
-            localServer.registerDefaultHandlers();
+    @After
+    public void shutDown() throws Exception {
+        if (this.httpclient != null) {
+            this.httpclient.close();
+        }
+        if (this.server != null) {
+            this.server.shutdown(10, TimeUnit.SECONDS);
         }
-        localServer.setTimeout(5000);
-        localServer.start();
     }
 
-    /**
-     * Obtains the address of the local test server.
-     *
-     * @return  the test server host, with a scheme name of "http"
-     */
-    protected HttpHost getServerHttp() {
-        final InetSocketAddress address = localServer.getServiceAddress();
-        return new HttpHost(
-                address.getHostName(),
-                address.getPort(),
-                "http");
+    public HttpHost start() throws Exception {
+        this.server = this.serverBootstrap.create();
+        this.server.start();
+
+        if (this.httpclient == null) {
+            this.httpclient = this.clientBuilder.build();
+        }
+
+        return new HttpHost("localhost", this.server.getLocalPort(), this.scheme.name());
     }
 
 }

Added: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/SSLTestContexts.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/SSLTestContexts.java?rev=1603443&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/SSLTestContexts.java
(added)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/SSLTestContexts.java
Wed Jun 18 13:06:43 2014
@@ -0,0 +1,86 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.localserver;
+
+import java.net.URL;
+import java.security.KeyStore;
+import java.security.NoSuchAlgorithmException;
+
+import javax.net.ssl.KeyManager;
+import javax.net.ssl.KeyManagerFactory;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.TrustManagerFactory;
+
+public class SSLTestContexts {
+
+    private static KeyManagerFactory createKeyManagerFactory() throws NoSuchAlgorithmException
{
+        final String algo = KeyManagerFactory.getDefaultAlgorithm();
+        try {
+            return KeyManagerFactory.getInstance(algo);
+        } catch (final NoSuchAlgorithmException ex) {
+            return KeyManagerFactory.getInstance("SunX509");
+        }
+    }
+
+    public static SSLContext createServerSSLContext() throws Exception {
+        final ClassLoader cl = SSLTestContexts.class.getClassLoader();
+        final URL url = cl.getResource("test.keystore");
+        final KeyStore keystore  = KeyStore.getInstance("jks");
+        keystore.load(url.openStream(), "nopassword".toCharArray());
+        final KeyManagerFactory kmfactory = createKeyManagerFactory();
+        kmfactory.init(keystore, "nopassword".toCharArray());
+        final KeyManager[] keymanagers = kmfactory.getKeyManagers();
+        final SSLContext sslcontext = SSLContext.getInstance("TLS");
+        sslcontext.init(keymanagers, null, null);
+        return sslcontext;
+    }
+
+    private static TrustManagerFactory createTrustManagerFactory() throws NoSuchAlgorithmException
{
+        final String algo = TrustManagerFactory.getDefaultAlgorithm();
+        try {
+            return TrustManagerFactory.getInstance(algo);
+        } catch (final NoSuchAlgorithmException ex) {
+            return TrustManagerFactory.getInstance("SunX509");
+        }
+    }
+
+    public static SSLContext createClientSSLContext() throws Exception {
+        final ClassLoader cl = SSLTestContexts.class.getClassLoader();
+        final URL url = cl.getResource("test.keystore");
+        final KeyStore keystore  = KeyStore.getInstance("jks");
+        keystore.load(url.openStream(), "nopassword".toCharArray());
+        final TrustManagerFactory tmfactory = createTrustManagerFactory();
+        tmfactory.init(keystore);
+        final TrustManager[] trustmanagers = tmfactory.getTrustManagers();
+        final SSLContext sslcontext = SSLContext.getInstance("TLS");
+        sslcontext.init(null, trustmanagers, null);
+        return sslcontext;
+    }
+
+}

Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/SSLTestContexts.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/SSLTestContexts.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/SSLTestContexts.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: httpcomponents/httpclient/trunk/httpclient/src/test/resources/test.keystore
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/resources/test.keystore?rev=1603443&view=auto
==============================================================================
Files httpcomponents/httpclient/trunk/httpclient/src/test/resources/test.keystore (added)
and httpcomponents/httpclient/trunk/httpclient/src/test/resources/test.keystore Wed Jun 18
13:06:43 2014 differ



Mime
View raw message