Improved parameterized tests
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpasyncclient/trunk@1603109 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/httpasyncclient/src/test/java/org/apache/http/HttpAsyncTestBase.java b/httpasyncclient/src/test/java/org/apache/http/HttpAsyncTestBase.java
index eeadefa..9724b28 100644
--- a/httpasyncclient/src/test/java/org/apache/http/HttpAsyncTestBase.java
+++ b/httpasyncclient/src/test/java/org/apache/http/HttpAsyncTestBase.java
@@ -27,83 +27,95 @@
package org.apache.http;
-import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.util.concurrent.TimeUnit;
-import org.apache.http.config.ConnectionConfig;
-import org.apache.http.impl.nio.DefaultNHttpServerConnection;
+import org.apache.http.config.Registry;
+import org.apache.http.config.RegistryBuilder;
+import org.apache.http.impl.nio.bootstrap.HttpServer;
+import org.apache.http.impl.nio.bootstrap.ServerBootstrap;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
+import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
import org.apache.http.impl.nio.reactor.IOReactorConfig;
-import org.apache.http.localserver.HttpServerNio;
-import org.apache.http.nio.NHttpConnectionFactory;
-import org.apache.http.nio.reactor.IOReactorExceptionHandler;
-import org.apache.http.protocol.HttpProcessor;
-import org.apache.http.protocol.ImmutableHttpProcessor;
-import org.apache.http.protocol.ResponseConnControl;
-import org.apache.http.protocol.ResponseContent;
-import org.apache.http.protocol.ResponseDate;
-import org.apache.http.protocol.ResponseServer;
+import org.apache.http.nio.conn.NoopIOSessionStrategy;
+import org.apache.http.nio.conn.SchemeIOSessionStrategy;
+import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy;
+import org.apache.http.nio.reactor.ListenerEndpoint;
import org.junit.After;
+import org.junit.Before;
-@SuppressWarnings("RedundantArrayCreation")
public abstract class HttpAsyncTestBase {
- protected HttpServerNio server;
- protected IOReactorConfig serverReactorConfig;
- protected ConnectionConfig serverConnectionConfig;
- protected HttpProcessor serverHttpProc;
- protected DefaultConnectingIOReactor clientIOReactor;
- protected IOReactorConfig clientReactorConfig;
- protected ConnectionConfig clientrConnectionConfig;
+ public enum ProtocolScheme { http, https };
+
+ protected final ProtocolScheme scheme;
+
+ protected ServerBootstrap serverBootstrap;
+ protected HttpServer server;
+ protected HttpAsyncClientBuilder clientBuilder;
protected PoolingNHttpClientConnectionManager connMgr;
protected CloseableHttpAsyncClient httpclient;
- protected abstract NHttpConnectionFactory<DefaultNHttpServerConnection> createServerConnectionFactory(
- ConnectionConfig config) throws Exception;
-
- protected abstract String getSchemeName();
-
- public static class SimpleIOReactorExceptionHandler implements IOReactorExceptionHandler {
-
- public boolean handle(final RuntimeException ex) {
- ex.printStackTrace(System.out);
- return false;
- }
-
- public boolean handle(final IOException ex) {
- ex.printStackTrace(System.out);
- return false;
- }
-
+ public HttpAsyncTestBase(final ProtocolScheme scheme) {
+ this.scheme = scheme;
}
- public void initServer() throws Exception {
- this.server = new HttpServerNio(
- this.serverReactorConfig, createServerConnectionFactory(this.serverConnectionConfig));
- this.server.setExceptionHandler(new SimpleIOReactorExceptionHandler());
- this.serverHttpProc = new ImmutableHttpProcessor(new ResponseDate(),
- new ResponseServer("TEST-SERVER/1.1"),
- new ResponseContent(),
- new ResponseConnControl());
+ public HttpAsyncTestBase() {
+ this(ProtocolScheme.http);
}
- public void initConnectionManager() throws Exception {
- this.clientIOReactor = new DefaultConnectingIOReactor(this.clientReactorConfig);
- this.connMgr = new PoolingNHttpClientConnectionManager(this.clientIOReactor);
- }
+ public String getSchemeName() {
+ return this.scheme.name();
+ }
+
+ public HttpHost start() throws Exception {
+ this.server = this.serverBootstrap.create();
+ this.server.start();
+
+ this.httpclient = this.clientBuilder.build();
+ this.httpclient.start();
+
+ final ListenerEndpoint endpoint = this.server.getEndpoint();
+ endpoint.waitFor();
+
+ final InetSocketAddress address = (InetSocketAddress) endpoint.getAddress();
+ return new HttpHost("localhost", address.getPort(), this.scheme.name());
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ this.serverBootstrap = ServerBootstrap.bootstrap();
+ final IOReactorConfig ioReactorConfig = IOReactorConfig.custom()
+ .setSoTimeout(15000)
+ .build();
+ this.serverBootstrap.setServerInfo("TEST/1.1");
+ this.serverBootstrap.setIOReactorConfig(ioReactorConfig);
+ this.serverBootstrap.setExceptionLogger(ExceptionLogger.STD_ERR);
+ if (this.scheme.equals(ProtocolScheme.https)) {
+ this.serverBootstrap.setSslContext(SSLTestContexts.createServerSSLContext());
+ }
+
+ this.clientBuilder = HttpAsyncClientBuilder.create();
+ final RegistryBuilder<SchemeIOSessionStrategy> builder = RegistryBuilder.create();
+ builder.register("http", NoopIOSessionStrategy.INSTANCE);
+ if (this.scheme.equals(ProtocolScheme.https)) {
+ builder.register("https", new SSLIOSessionStrategy(SSLTestContexts.createClientSSLContext()));
+ }
+ final Registry<SchemeIOSessionStrategy> registry = builder.build();
+ final DefaultConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(ioReactorConfig);
+ this.connMgr = new PoolingNHttpClientConnectionManager(ioReactor, registry);
+ this.clientBuilder.setConnectionManager(this.connMgr);
+ }
@After
- public void shutDownClient() throws Exception {
+ public void shutDown() throws Exception {
if (this.httpclient != null) {
this.httpclient.close();
}
- }
-
- @After
- public void shutDownServer() throws Exception {
if (this.server != null) {
- this.server.shutdown();
+ this.server.shutdown(10, TimeUnit.SECONDS);
}
}
diff --git a/httpasyncclient/src/test/java/org/apache/http/localserver/HttpServerNio.java b/httpasyncclient/src/test/java/org/apache/http/localserver/HttpServerNio.java
deleted file mode 100644
index 9b88947..0000000
--- a/httpasyncclient/src/test/java/org/apache/http/localserver/HttpServerNio.java
+++ /dev/null
@@ -1,147 +0,0 @@
-/*
- * ====================================================================
- * 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.io.IOException;
-import java.net.InetSocketAddress;
-import java.util.List;
-
-import org.apache.http.impl.nio.DefaultHttpServerIODispatch;
-import org.apache.http.impl.nio.DefaultNHttpServerConnection;
-import org.apache.http.impl.nio.reactor.DefaultListeningIOReactor;
-import org.apache.http.impl.nio.reactor.ExceptionEvent;
-import org.apache.http.impl.nio.reactor.IOReactorConfig;
-import org.apache.http.nio.NHttpConnectionFactory;
-import org.apache.http.nio.NHttpServerEventHandler;
-import org.apache.http.nio.reactor.IOEventDispatch;
-import org.apache.http.nio.reactor.IOReactorExceptionHandler;
-import org.apache.http.nio.reactor.IOReactorStatus;
-import org.apache.http.nio.reactor.ListenerEndpoint;
-import org.apache.http.nio.reactor.ListeningIOReactor;
-
-public class HttpServerNio {
-
- private final DefaultListeningIOReactor ioReactor;
- private final NHttpConnectionFactory<DefaultNHttpServerConnection> connFactory;
-
- private volatile IOReactorThread thread;
- private ListenerEndpoint endpoint;
-
- public HttpServerNio(
- final IOReactorConfig ioReactorConfig,
- final NHttpConnectionFactory<DefaultNHttpServerConnection> connFactory) throws IOException {
- super();
- this.ioReactor = new DefaultListeningIOReactor(ioReactorConfig);
- this.connFactory = connFactory;
- }
-
- public void setExceptionHandler(final IOReactorExceptionHandler exceptionHandler) {
- this.ioReactor.setExceptionHandler(exceptionHandler);
- }
-
- private void execute(final NHttpServerEventHandler serviceHandler) throws IOException {
- final IOEventDispatch ioEventDispatch = new DefaultHttpServerIODispatch(serviceHandler,
- this.connFactory);
- this.ioReactor.execute(ioEventDispatch);
- }
-
- public ListenerEndpoint getListenerEndpoint() {
- return this.endpoint;
- }
-
- public void setEndpoint(final ListenerEndpoint endpoint) {
- this.endpoint = endpoint;
- }
-
- public void start(final NHttpServerEventHandler serviceHandler) {
- this.endpoint = this.ioReactor.listen(new InetSocketAddress(0));
- this.thread = new IOReactorThread(serviceHandler);
- this.thread.start();
- }
-
- public ListeningIOReactor getIoReactor() {
- return this.ioReactor;
- }
-
- public IOReactorStatus getStatus() {
- return this.ioReactor.getStatus();
- }
-
- public List<ExceptionEvent> getAuditLog() {
- return this.ioReactor.getAuditLog();
- }
-
- public void join(final long timeout) throws InterruptedException {
- if (this.thread != null) {
- this.thread.join(timeout);
- }
- }
-
- public Exception getException() {
- if (this.thread != null) {
- return this.thread.getException();
- } else {
- return null;
- }
- }
-
- public void shutdown() throws IOException {
- this.ioReactor.shutdown();
- try {
- join(500);
- } catch (final InterruptedException ignore) {
- }
- }
-
- private class IOReactorThread extends Thread {
-
- private final NHttpServerEventHandler serviceHandler;
-
- private volatile Exception ex;
-
- public IOReactorThread(final NHttpServerEventHandler serviceHandler) {
- super();
- this.serviceHandler = serviceHandler;
- }
-
- @Override
- public void run() {
- try {
- execute(this.serviceHandler);
- } catch (final Exception ex) {
- this.ex = ex;
- }
- }
-
- public Exception getException() {
- return this.ex;
- }
-
- }
-
-}
diff --git a/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestClientAuthentication.java b/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestClientAuthentication.java
index 1526c13..c72b772 100644
--- a/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestClientAuthentication.java
+++ b/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestClientAuthentication.java
@@ -27,7 +27,8 @@
package org.apache.http.nio.client.integration;
import java.io.IOException;
-import java.net.InetSocketAddress;
+import java.util.Arrays;
+import java.util.Collection;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
@@ -38,9 +39,7 @@
import org.apache.http.HttpHost;
import org.apache.http.HttpInetConnection;
import org.apache.http.HttpRequest;
-import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
-import org.apache.http.HttpResponseInterceptor;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.ProtocolVersion;
@@ -52,106 +51,50 @@
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
-import org.apache.http.config.ConnectionConfig;
-import org.apache.http.impl.DefaultConnectionReuseStrategy;
-import org.apache.http.impl.DefaultHttpResponseFactory;
+import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.TargetAuthenticationStrategy;
-import org.apache.http.impl.nio.DefaultNHttpServerConnection;
-import org.apache.http.impl.nio.DefaultNHttpServerConnectionFactory;
-import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.localserver.BasicAuthTokenExtractor;
import org.apache.http.localserver.RequestBasicAuth;
import org.apache.http.localserver.ResponseBasicUnauthorized;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicHttpResponse;
-import org.apache.http.nio.NHttpConnectionFactory;
import org.apache.http.nio.entity.NByteArrayEntity;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.nio.protocol.BasicAsyncRequestHandler;
import org.apache.http.nio.protocol.BasicAsyncResponseProducer;
import org.apache.http.nio.protocol.HttpAsyncExchange;
import org.apache.http.nio.protocol.HttpAsyncExpectationVerifier;
-import org.apache.http.nio.protocol.HttpAsyncRequestHandlerMapper;
-import org.apache.http.nio.protocol.HttpAsyncService;
-import org.apache.http.nio.protocol.UriHttpAsyncRequestHandlerMapper;
-import org.apache.http.nio.reactor.IOReactorStatus;
-import org.apache.http.nio.reactor.ListenerEndpoint;
-import org.apache.http.protocol.BasicHttpContext;
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.ImmutableHttpProcessor;
-import org.apache.http.protocol.ResponseConnControl;
-import org.apache.http.protocol.ResponseContent;
-import org.apache.http.protocol.ResponseDate;
-import org.apache.http.protocol.ResponseServer;
-import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+@RunWith(Parameterized.class)
public class TestClientAuthentication extends HttpAsyncTestBase {
- @Before
+ @Parameterized.Parameters(name = "{0}")
+ public static Collection<Object[]> protocols() {
+ return Arrays.asList(new Object[][]{
+ {ProtocolScheme.http},
+ {ProtocolScheme.https},
+ });
+ }
+
+ public TestClientAuthentication(final ProtocolScheme scheme) {
+ super(scheme);
+ }
+
+ @Before @Override
public void setUp() throws Exception {
- initServer();
- initConnectionManager();
- }
-
- @After
- public void tearDown() throws Exception {
- shutDownClient();
- shutDownServer();
- }
-
- @Override
- public void initServer() throws Exception {
- super.initServer();
- this.serverHttpProc = new ImmutableHttpProcessor(
- new HttpRequestInterceptor[] {
- new RequestBasicAuth()
- },
- new HttpResponseInterceptor[] {
- new ResponseDate(),
- new ResponseServer(),
- new ResponseContent(),
- new ResponseConnControl(),
- new ResponseBasicUnauthorized()
- }
- );
- }
-
- @Override
- protected NHttpConnectionFactory<DefaultNHttpServerConnection> createServerConnectionFactory(
- final ConnectionConfig config) throws Exception {
- return new DefaultNHttpServerConnectionFactory(config);
- }
-
- @Override
- protected String getSchemeName() {
- return "http";
- }
-
- private HttpHost start(
- final HttpAsyncRequestHandlerMapper requestHandlerResolver,
- final HttpAsyncExpectationVerifier expectationVerifier) throws Exception {
- final HttpAsyncService serviceHandler = new HttpAsyncService(
- this.serverHttpProc,
- DefaultConnectionReuseStrategy.INSTANCE,
- DefaultHttpResponseFactory.INSTANCE,
- requestHandlerResolver,
- expectationVerifier);
- this.server.start(serviceHandler);
- this.httpclient.start();
-
- final ListenerEndpoint endpoint = this.server.getListenerEndpoint();
- endpoint.waitFor();
-
- Assert.assertEquals("Test server status", IOReactorStatus.ACTIVE, this.server.getStatus());
- final InetSocketAddress address = (InetSocketAddress) endpoint.getAddress();
- return new HttpHost("localhost", address.getPort(), getSchemeName());
+ super.setUp();
+ this.serverBootstrap.addInterceptorFirst(new RequestBasicAuth());
+ this.serverBootstrap.addInterceptorLast(new ResponseBasicUnauthorized());
}
static class AuthHandler implements HttpRequestHandler {
@@ -273,19 +216,14 @@
@Test
public void testBasicAuthenticationNoCreds() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(new AuthHandler()));
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(new AuthHandler()));
+ final HttpHost target = start();
final TestCredentialsProvider credsProvider = new TestCredentialsProvider(null);
- this.httpclient = HttpAsyncClients.custom()
- .setConnectionManager(this.connMgr)
- .setDefaultCredentialsProvider(credsProvider)
- .build();
-
- final HttpHost target = start(registry, null);
-
+ final HttpClientContext context = HttpClientContext.create();
+ context.setCredentialsProvider(credsProvider);
final HttpGet httpget = new HttpGet("/");
- final Future<HttpResponse> future = this.httpclient.execute(target, httpget, null);
+ final Future<HttpResponse> future = this.httpclient.execute(target, httpget, context, null);
final HttpResponse response = future.get();
Assert.assertNotNull(response);
Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode());
@@ -296,20 +234,15 @@
@Test
public void testBasicAuthenticationFailure() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(new AuthHandler()));
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(new AuthHandler()));
+ final HttpHost target = start();
final TestCredentialsProvider credsProvider = new TestCredentialsProvider(
new UsernamePasswordCredentials("test", "all-wrong"));
- this.httpclient = HttpAsyncClients.custom()
- .setConnectionManager(this.connMgr)
- .setDefaultCredentialsProvider(credsProvider)
- .build();
-
- final HttpHost target = start(registry, null);
-
+ final HttpClientContext context = HttpClientContext.create();
+ context.setCredentialsProvider(credsProvider);
final HttpGet httpget = new HttpGet("/");
- final Future<HttpResponse> future = this.httpclient.execute(target, httpget, null);
+ final Future<HttpResponse> future = this.httpclient.execute(target, httpget, context, null);
final HttpResponse response = future.get();
Assert.assertNotNull(response);
Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode());
@@ -320,20 +253,15 @@
@Test
public void testBasicAuthenticationSuccess() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(new AuthHandler()));
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(new AuthHandler()));
+ final HttpHost target = start();
final TestCredentialsProvider credsProvider = new TestCredentialsProvider(
new UsernamePasswordCredentials("test", "test"));
- this.httpclient = HttpAsyncClients.custom()
- .setConnectionManager(this.connMgr)
- .setDefaultCredentialsProvider(credsProvider)
- .build();
-
- final HttpHost target = start(registry, null);
-
+ final HttpClientContext context = HttpClientContext.create();
+ context.setCredentialsProvider(credsProvider);
final HttpGet httpget = new HttpGet("/");
- final Future<HttpResponse> future = this.httpclient.execute(target, httpget, null);
+ final Future<HttpResponse> future = this.httpclient.execute(target, httpget, context, null);
final HttpResponse response = future.get();
Assert.assertNotNull(response);
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
@@ -344,20 +272,16 @@
@Test
public void testBasicAuthenticationSuccessNonPersistentConnection() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(new AuthHandler(false)));
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(new AuthHandler(false)));
+ final HttpHost target = start();
final TestCredentialsProvider credsProvider = new TestCredentialsProvider(
new UsernamePasswordCredentials("test", "test"));
- this.httpclient = HttpAsyncClients.custom()
- .setConnectionManager(this.connMgr)
- .setDefaultCredentialsProvider(credsProvider)
- .build();
-
- final HttpHost target = start(registry, null);
+ final HttpClientContext context = HttpClientContext.create();
+ context.setCredentialsProvider(credsProvider);
final HttpGet httpget = new HttpGet("/");
- final Future<HttpResponse> future = this.httpclient.execute(target, httpget, null);
+ final Future<HttpResponse> future = this.httpclient.execute(target, httpget, context, null);
final HttpResponse response = future.get();
Assert.assertNotNull(response);
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
@@ -368,20 +292,14 @@
@Test
public void testBasicAuthenticationSuccessWithNonRepeatableExpectContinue() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(new AuthHandler()));
- final AuthExpectationVerifier expectationVerifier = new AuthExpectationVerifier();
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(new AuthHandler()));
+ this.serverBootstrap.setExpectationVerifier(new AuthExpectationVerifier());
+ final HttpHost target = start();
final TestCredentialsProvider credsProvider = new TestCredentialsProvider(
new UsernamePasswordCredentials("test", "test"));
-
- this.httpclient = HttpAsyncClients.custom()
- .setConnectionManager(this.connMgr)
- .setDefaultCredentialsProvider(credsProvider)
- .build();
-
- final HttpHost target = start(registry, expectationVerifier);
-
+ final HttpClientContext context = HttpClientContext.create();
+ context.setCredentialsProvider(credsProvider);
final HttpPut httpput = new HttpPut("/");
final NByteArrayEntity entity = new NByteArrayEntity(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }) {
@@ -396,7 +314,7 @@
httpput.setEntity(entity);
httpput.setConfig(RequestConfig.custom().setExpectContinueEnabled(true).build());
- final Future<HttpResponse> future = this.httpclient.execute(target, httpput, null);
+ final Future<HttpResponse> future = this.httpclient.execute(target, httpput, context, null);
final HttpResponse response = future.get();
Assert.assertNotNull(response);
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
@@ -404,19 +322,13 @@
@Test(expected=ExecutionException.class)
public void testBasicAuthenticationFailureWithNonRepeatableEntityExpectContinueOff() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(new AuthHandler()));
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(new AuthHandler()));
+ final HttpHost target = start();
final TestCredentialsProvider credsProvider = new TestCredentialsProvider(
new UsernamePasswordCredentials("test", "test"));
-
- this.httpclient = HttpAsyncClients.custom()
- .setConnectionManager(this.connMgr)
- .setDefaultCredentialsProvider(credsProvider)
- .build();
-
- final HttpHost target = start(registry, null);
-
+ final HttpClientContext context = HttpClientContext.create();
+ context.setCredentialsProvider(credsProvider);
final HttpPut httpput = new HttpPut("/");
final NByteArrayEntity requestEntity = new NByteArrayEntity(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }) {
@@ -432,7 +344,7 @@
httpput.setConfig(RequestConfig.custom().setExpectContinueEnabled(false).build());
try {
- final Future<HttpResponse> future = this.httpclient.execute(target, httpput, null);
+ final Future<HttpResponse> future = this.httpclient.execute(target, httpput, context, null);
future.get();
Assert.fail("ExecutionException should have been thrown");
} catch (final ExecutionException ex) {
@@ -444,23 +356,17 @@
@Test
public void testBasicAuthenticationSuccessOnRepeatablePost() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(new AuthHandler()));
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(new AuthHandler()));
+ final HttpHost target = start();
final TestCredentialsProvider credsProvider = new TestCredentialsProvider(
new UsernamePasswordCredentials("test", "test"));
-
- this.httpclient = HttpAsyncClients.custom()
- .setConnectionManager(this.connMgr)
- .setDefaultCredentialsProvider(credsProvider)
- .build();
-
- final HttpHost target = start(registry, null);
-
+ final HttpClientContext context = HttpClientContext.create();
+ context.setCredentialsProvider(credsProvider);
final HttpPost httppost = new HttpPost("/");
httppost.setEntity(new NStringEntity("some important stuff", Consts.ISO_8859_1));
- final Future<HttpResponse> future = this.httpclient.execute(target, httppost, null);
+ final Future<HttpResponse> future = this.httpclient.execute(target, httppost, context, null);
final HttpResponse response = future.get();
Assert.assertNotNull(response);
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
@@ -471,24 +377,16 @@
@Test
public void testBasicAuthenticationCredentialsCaching() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(new AuthHandler()));
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(new AuthHandler()));
+ final TestTargetAuthenticationStrategy authStrategy = new TestTargetAuthenticationStrategy();
+ this.clientBuilder.setTargetAuthenticationStrategy(authStrategy);
+ final HttpHost target = start();
final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("test", "test"));
-
- final TestTargetAuthenticationStrategy authStrategy = new TestTargetAuthenticationStrategy();
-
- this.httpclient = HttpAsyncClients.custom()
- .setConnectionManager(this.connMgr)
- .setTargetAuthenticationStrategy(authStrategy)
- .setDefaultCredentialsProvider(credsProvider)
- .build();
-
- final HttpHost target = start(registry, null);
-
- final HttpContext context = new BasicHttpContext();
+ final HttpClientContext context = HttpClientContext.create();
+ context.setCredentialsProvider(credsProvider);
final HttpGet httpget1 = new HttpGet("/");
final Future<HttpResponse> future1 = this.httpclient.execute(target, httpget1, context, null);
@@ -507,14 +405,8 @@
@Test
public void testAuthenticationUserinfoInRequestSuccess() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(new AuthHandler()));
-
- this.httpclient = HttpAsyncClients.custom()
- .setConnectionManager(this.connMgr)
- .build();
-
- final HttpHost target = start(registry, null);
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(new AuthHandler()));
+ final HttpHost target = start();
final HttpGet httpget = new HttpGet("http://test:test@" + target.toHostString() + "/");
final Future<HttpResponse> future = this.httpclient.execute(target, httpget, null);
@@ -527,14 +419,8 @@
@Test
public void testAuthenticationUserinfoInRequestFailure() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(new AuthHandler()));
-
- this.httpclient = HttpAsyncClients.custom()
- .setConnectionManager(this.connMgr)
- .build();
-
- final HttpHost target = start(registry, null);
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(new AuthHandler()));
+ final HttpHost target = start();
final HttpGet httpget = new HttpGet("http://test:all-wrong@" + target.toHostString() + "/");
@@ -546,7 +432,7 @@
Assert.assertNotNull(entity);
}
- private static class RedirectHandler implements HttpRequestHandler {
+ private class RedirectHandler implements HttpRequestHandler {
public RedirectHandler() {
super();
@@ -557,28 +443,20 @@
final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
final HttpInetConnection conn = (HttpInetConnection) context.getAttribute(HttpCoreContext.HTTP_CONNECTION);
- final String localhost = conn.getLocalAddress().getHostName();
final int port = conn.getLocalPort();
response.setStatusCode(HttpStatus.SC_MOVED_PERMANENTLY);
- response.addHeader(new BasicHeader("Location",
- "http://test:test@" + localhost + ":" + port + "/"));
+ response.addHeader(new BasicHeader("Location", getSchemeName() + "://test:test@localhost:" + port + "/"));
}
}
@Test
public void testAuthenticationUserinfoInRedirectSuccess() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(new AuthHandler()));
- registry.register("/thatway", new BasicAsyncRequestHandler(new RedirectHandler()));
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(new AuthHandler()));
+ this.serverBootstrap.registerHandler("/thatway", new BasicAsyncRequestHandler(new RedirectHandler()));
+ final HttpHost target = start();
- this.httpclient = HttpAsyncClients.custom()
- .setConnectionManager(this.connMgr)
- .build();
-
- final HttpHost target = start(registry, null);
-
- final HttpGet httpget = new HttpGet("http://test:test@" + target.toHostString() + "/thatway");
+ final HttpGet httpget = new HttpGet(target.getSchemeName() + "://test:test@" + target.toHostString() + "/thatway");
final Future<HttpResponse> future = this.httpclient.execute(target, httpget, null);
final HttpResponse response = future.get();
Assert.assertNotNull(response);
diff --git a/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestClientAuthenticationFallBack.java b/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestClientAuthenticationFallBack.java
index f56a44f..0e2b459 100644
--- a/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestClientAuthenticationFallBack.java
+++ b/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestClientAuthenticationFallBack.java
@@ -27,7 +27,6 @@
package org.apache.http.nio.client.integration;
import java.io.IOException;
-import java.net.InetSocketAddress;
import java.util.concurrent.Future;
import org.apache.http.Consts;
@@ -36,7 +35,6 @@
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
-import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.HttpStatus;
@@ -46,31 +44,13 @@
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpGet;
-import org.apache.http.config.ConnectionConfig;
+import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.StringEntity;
-import org.apache.http.impl.DefaultConnectionReuseStrategy;
-import org.apache.http.impl.DefaultHttpResponseFactory;
-import org.apache.http.impl.nio.DefaultNHttpServerConnection;
-import org.apache.http.impl.nio.DefaultNHttpServerConnectionFactory;
-import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.localserver.RequestBasicAuth;
-import org.apache.http.nio.NHttpConnectionFactory;
import org.apache.http.nio.protocol.BasicAsyncRequestHandler;
-import org.apache.http.nio.protocol.HttpAsyncExpectationVerifier;
-import org.apache.http.nio.protocol.HttpAsyncRequestHandlerMapper;
-import org.apache.http.nio.protocol.HttpAsyncService;
-import org.apache.http.nio.protocol.UriHttpAsyncRequestHandlerMapper;
-import org.apache.http.nio.reactor.IOReactorStatus;
-import org.apache.http.nio.reactor.ListenerEndpoint;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
-import org.apache.http.protocol.ImmutableHttpProcessor;
-import org.apache.http.protocol.ResponseConnControl;
-import org.apache.http.protocol.ResponseContent;
-import org.apache.http.protocol.ResponseDate;
-import org.apache.http.protocol.ResponseServer;
import org.apache.http.util.EntityUtils;
-import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@@ -78,63 +58,11 @@
public class TestClientAuthenticationFallBack extends HttpAsyncTestBase {
@Before
+ @Override
public void setUp() throws Exception {
- initServer();
- initConnectionManager();
- }
-
- @After
- public void tearDown() throws Exception {
- shutDownClient();
- shutDownServer();
- }
-
- @Override
- public void initServer() throws Exception {
- super.initServer();
- this.serverHttpProc = new ImmutableHttpProcessor(
- new HttpRequestInterceptor[] {
- new RequestBasicAuth()
- },
- new HttpResponseInterceptor[] {
- new ResponseDate(),
- new ResponseServer(),
- new ResponseContent(),
- new ResponseConnControl(),
- new ResponseBasicUnauthorized()
- }
- );
- }
-
- @Override
- protected NHttpConnectionFactory<DefaultNHttpServerConnection> createServerConnectionFactory(
- final ConnectionConfig config) throws Exception {
- return new DefaultNHttpServerConnectionFactory(config);
- }
-
- @Override
- protected String getSchemeName() {
- return "http";
- }
-
- private HttpHost start(
- final HttpAsyncRequestHandlerMapper requestHandlerResolver,
- final HttpAsyncExpectationVerifier expectationVerifier) throws Exception {
- final HttpAsyncService serviceHandler = new HttpAsyncService(
- this.serverHttpProc,
- DefaultConnectionReuseStrategy.INSTANCE,
- DefaultHttpResponseFactory.INSTANCE,
- requestHandlerResolver,
- expectationVerifier);
- this.server.start(serviceHandler);
- this.httpclient.start();
-
- final ListenerEndpoint endpoint = this.server.getListenerEndpoint();
- endpoint.waitFor();
-
- Assert.assertEquals("Test server status", IOReactorStatus.ACTIVE, this.server.getStatus());
- final InetSocketAddress address = (InetSocketAddress) endpoint.getAddress();
- return new HttpHost("localhost", address.getPort(), getSchemeName());
+ super.setUp();
+ this.serverBootstrap.addInterceptorFirst(new RequestBasicAuth());
+ this.serverBootstrap.addInterceptorLast(new ResponseBasicUnauthorized());
}
public class ResponseBasicUnauthorized implements HttpResponseInterceptor {
@@ -197,22 +125,17 @@
@Test
public void testBasicAuthenticationSuccess() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(new AuthHandler()));
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(new AuthHandler()));
+ final HttpHost target = start();
final TestCredentialsProvider credsProvider = new TestCredentialsProvider(
new UsernamePasswordCredentials("test", "test"));
-
- this.httpclient = HttpAsyncClients.custom()
- .setConnectionManager(this.connMgr)
- .setDefaultCredentialsProvider(credsProvider)
- .build();
-
- final HttpHost target = start(registry, null);
+ final HttpClientContext context = HttpClientContext.create();
+ context.setCredentialsProvider(credsProvider);
final HttpGet httpget = new HttpGet("/");
- final Future<HttpResponse> future = this.httpclient.execute(target, httpget, null);
+ final Future<HttpResponse> future = this.httpclient.execute(target, httpget, context, null);
final HttpResponse response = future.get();
Assert.assertNotNull(response);
final HttpEntity entity = response.getEntity();
diff --git a/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestClientReauthentication.java b/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestClientReauthentication.java
index fb6b50c..d2db5b1 100644
--- a/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestClientReauthentication.java
+++ b/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestClientReauthentication.java
@@ -27,7 +27,6 @@
package org.apache.http.nio.client.integration;
import java.io.IOException;
-import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicLong;
@@ -38,7 +37,6 @@
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
-import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.HttpStatus;
@@ -51,101 +49,29 @@
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
-import org.apache.http.config.ConnectionConfig;
+import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.entity.StringEntity;
-import org.apache.http.impl.DefaultConnectionReuseStrategy;
-import org.apache.http.impl.DefaultHttpResponseFactory;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.auth.BasicSchemeFactory;
import org.apache.http.impl.client.TargetAuthenticationStrategy;
-import org.apache.http.impl.nio.DefaultNHttpServerConnection;
-import org.apache.http.impl.nio.DefaultNHttpServerConnectionFactory;
-import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.localserver.RequestBasicAuth;
-import org.apache.http.nio.NHttpConnectionFactory;
import org.apache.http.nio.protocol.BasicAsyncRequestHandler;
-import org.apache.http.nio.protocol.HttpAsyncExpectationVerifier;
-import org.apache.http.nio.protocol.HttpAsyncRequestHandlerMapper;
-import org.apache.http.nio.protocol.HttpAsyncService;
-import org.apache.http.nio.protocol.UriHttpAsyncRequestHandlerMapper;
-import org.apache.http.nio.reactor.IOReactorStatus;
-import org.apache.http.nio.reactor.ListenerEndpoint;
-import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
-import org.apache.http.protocol.ImmutableHttpProcessor;
-import org.apache.http.protocol.ResponseConnControl;
-import org.apache.http.protocol.ResponseContent;
-import org.apache.http.protocol.ResponseDate;
-import org.apache.http.protocol.ResponseServer;
import org.apache.http.util.EntityUtils;
-import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class TestClientReauthentication extends HttpAsyncTestBase {
- @Before
+ @Before @Override
public void setUp() throws Exception {
- initServer();
- initConnectionManager();
- }
-
- @After
- public void tearDown() throws Exception {
- shutDownClient();
- shutDownServer();
- }
-
- @Override
- public void initServer() throws Exception {
- super.initServer();
- this.serverHttpProc = new ImmutableHttpProcessor(
- new HttpRequestInterceptor[] {
- new RequestBasicAuth()
- },
- new HttpResponseInterceptor[] {
- new ResponseDate(),
- new ResponseServer(),
- new ResponseContent(),
- new ResponseConnControl(),
- new ResponseBasicUnauthorized()
- }
- );
- }
-
- @Override
- protected NHttpConnectionFactory<DefaultNHttpServerConnection> createServerConnectionFactory(
- final ConnectionConfig config) throws Exception {
- return new DefaultNHttpServerConnectionFactory(config);
- }
-
- @Override
- protected String getSchemeName() {
- return "http";
- }
-
- private HttpHost start(
- final HttpAsyncRequestHandlerMapper requestHandlerResolver,
- final HttpAsyncExpectationVerifier expectationVerifier) throws Exception {
- final HttpAsyncService serviceHandler = new HttpAsyncService(
- this.serverHttpProc,
- DefaultConnectionReuseStrategy.INSTANCE,
- DefaultHttpResponseFactory.INSTANCE,
- requestHandlerResolver,
- expectationVerifier);
- this.server.start(serviceHandler);
- this.httpclient.start();
-
- final ListenerEndpoint endpoint = this.server.getListenerEndpoint();
- endpoint.waitFor();
-
- Assert.assertEquals("Test server status", IOReactorStatus.ACTIVE, this.server.getStatus());
- final InetSocketAddress address = (InetSocketAddress) endpoint.getAddress();
- return new HttpHost("localhost", address.getPort(), getSchemeName());
+ super.setUp();
+ this.serverBootstrap.addInterceptorFirst(new RequestBasicAuth());
+ this.serverBootstrap.addInterceptorLast(new ResponseBasicUnauthorized());
}
public class ResponseBasicUnauthorized implements HttpResponseInterceptor {
@@ -214,8 +140,7 @@
@Test
public void testBasicAuthenticationSuccess() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(new AuthHandler()));
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(new AuthHandler()));
final BasicSchemeFactory myBasicAuthSchemeFactory = new BasicSchemeFactory() {
@@ -245,21 +170,19 @@
final TestCredentialsProvider credsProvider = new TestCredentialsProvider(
new UsernamePasswordCredentials("test", "test"));
- final RequestConfig config = RequestConfig.custom()
- .setTargetPreferredAuthSchemes(Arrays.asList("MyBasic"))
- .build();
final Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create()
.register("MyBasic", myBasicAuthSchemeFactory)
.build();
- this.httpclient = HttpAsyncClients.custom()
- .setDefaultAuthSchemeRegistry(authSchemeRegistry)
- .setTargetAuthenticationStrategy(myAuthStrategy)
- .setDefaultCredentialsProvider(credsProvider)
- .build();
+ this.clientBuilder.setDefaultAuthSchemeRegistry(authSchemeRegistry);
+ this.clientBuilder.setTargetAuthenticationStrategy(myAuthStrategy);
+ final HttpHost target = start();
- final HttpHost target = start(registry, null);
+ final RequestConfig config = RequestConfig.custom()
+ .setTargetPreferredAuthSchemes(Arrays.asList("MyBasic"))
+ .build();
+ final HttpClientContext context = HttpClientContext.create();
+ context.setCredentialsProvider(credsProvider);
- final HttpContext context = new BasicHttpContext();
for (int i = 0; i < 10; i++) {
final HttpGet httpget = new HttpGet("/");
httpget.setConfig(config);
diff --git a/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestHttpAsync.java b/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestHttpAsync.java
index 0e942a8..e324c86 100644
--- a/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestHttpAsync.java
+++ b/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestHttpAsync.java
@@ -27,7 +27,8 @@
package org.apache.http.nio.client.integration;
import java.io.IOException;
-import java.net.InetSocketAddress;
+import java.util.Arrays;
+import java.util.Collection;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
@@ -40,88 +41,45 @@
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
-import org.apache.http.config.ConnectionConfig;
-import org.apache.http.impl.DefaultConnectionReuseStrategy;
-import org.apache.http.impl.DefaultHttpResponseFactory;
-import org.apache.http.impl.nio.DefaultNHttpServerConnection;
-import org.apache.http.impl.nio.DefaultNHttpServerConnectionFactory;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.localserver.EchoHandler;
import org.apache.http.localserver.RandomHandler;
import org.apache.http.nio.ContentDecoder;
import org.apache.http.nio.IOControl;
-import org.apache.http.nio.NHttpConnectionFactory;
import org.apache.http.nio.client.methods.HttpAsyncMethods;
+import org.apache.http.nio.client.util.HttpAsyncClientUtils;
import org.apache.http.nio.entity.NByteArrayEntity;
import org.apache.http.nio.protocol.BasicAsyncRequestHandler;
import org.apache.http.nio.protocol.BasicAsyncResponseConsumer;
-import org.apache.http.nio.protocol.HttpAsyncExpectationVerifier;
-import org.apache.http.nio.protocol.HttpAsyncRequestHandlerMapper;
import org.apache.http.nio.protocol.HttpAsyncRequestProducer;
-import org.apache.http.nio.protocol.HttpAsyncService;
-import org.apache.http.nio.protocol.UriHttpAsyncRequestHandlerMapper;
-import org.apache.http.nio.reactor.IOReactorStatus;
-import org.apache.http.nio.reactor.ListenerEndpoint;
import org.apache.http.util.EntityUtils;
-import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+@RunWith(Parameterized.class)
public class TestHttpAsync extends HttpAsyncTestBase {
- @Before
+ @Parameterized.Parameters(name = "{0}")
+ public static Collection<Object[]> protocols() {
+ return Arrays.asList(new Object[][]{
+ { ProtocolScheme.http },
+ { ProtocolScheme.https },
+ });
+ }
+
+ public TestHttpAsync(final ProtocolScheme scheme) {
+ super(scheme);
+ }
+
+ @Before @Override
public void setUp() throws Exception {
- initServer();
- initConnectionManager();
- this.httpclient = HttpAsyncClients.custom()
- .setConnectionManager(this.connMgr)
- .build();
- }
-
- @After
- public void tearDown() throws Exception {
- shutDownClient();
- shutDownServer();
- }
-
- @Override
- protected NHttpConnectionFactory<DefaultNHttpServerConnection> createServerConnectionFactory(
- final ConnectionConfig config) throws Exception {
- return new DefaultNHttpServerConnectionFactory(config);
- }
-
- @Override
- protected String getSchemeName() {
- return "http";
- }
-
- private HttpHost start(
- final HttpAsyncRequestHandlerMapper requestHandlerResolver,
- final HttpAsyncExpectationVerifier expectationVerifier) throws Exception {
- final HttpAsyncService serviceHandler = new HttpAsyncService(
- this.serverHttpProc,
- DefaultConnectionReuseStrategy.INSTANCE,
- DefaultHttpResponseFactory.INSTANCE,
- requestHandlerResolver,
- expectationVerifier);
- this.server.start(serviceHandler);
- this.httpclient.start();
-
- final ListenerEndpoint endpoint = this.server.getListenerEndpoint();
- endpoint.waitFor();
-
- Assert.assertEquals("Test server status", IOReactorStatus.ACTIVE, this.server.getStatus());
- final InetSocketAddress address = (InetSocketAddress) endpoint.getAddress();
- return new HttpHost("localhost", address.getPort(), getSchemeName());
- }
-
- private HttpHost start() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("/echo/*", new BasicAsyncRequestHandler(new EchoHandler()));
- registry.register("/random/*", new BasicAsyncRequestHandler(new RandomHandler()));
- return start(registry, null);
+ super.setUp();
+ this.serverBootstrap.registerHandler("/echo/*", new BasicAsyncRequestHandler(new EchoHandler()));
+ this.serverBootstrap.registerHandler("/random/*", new BasicAsyncRequestHandler(new RandomHandler()));
}
@Test
@@ -277,4 +235,18 @@
Assert.assertEquals(200, response3.getStatusLine().getStatusCode());
}
+ @Test
+ public void testClientCloseloseQuietly() throws Exception {
+ final HttpHost target = start();
+ final HttpGet httpget = new HttpGet("/random/2048");
+ final Future<HttpResponse> future = this.httpclient.execute(target, httpget, null);
+ final HttpResponse response = future.get();
+ Assert.assertNotNull(response);
+ Assert.assertEquals(200, response.getStatusLine().getStatusCode());
+
+ HttpAsyncClientUtils.closeQuietly(this.httpclient);
+ // Close it twice
+ HttpAsyncClientUtils.closeQuietly(this.httpclient);
+ }
+
}
diff --git a/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestHttpAsyncClientUtils.java b/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestHttpAsyncClientUtils.java
deleted file mode 100644
index 378bfaa..0000000
--- a/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestHttpAsyncClientUtils.java
+++ /dev/null
@@ -1,128 +0,0 @@
-/*
- * ====================================================================
- * 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.nio.client.integration;
-
-import java.net.InetSocketAddress;
-import java.util.concurrent.Future;
-
-import org.apache.http.HttpAsyncTestBase;
-import org.apache.http.HttpHost;
-import org.apache.http.HttpResponse;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.config.ConnectionConfig;
-import org.apache.http.impl.DefaultConnectionReuseStrategy;
-import org.apache.http.impl.DefaultHttpResponseFactory;
-import org.apache.http.impl.nio.DefaultNHttpServerConnection;
-import org.apache.http.impl.nio.DefaultNHttpServerConnectionFactory;
-import org.apache.http.impl.nio.client.HttpAsyncClients;
-import org.apache.http.localserver.EchoHandler;
-import org.apache.http.localserver.RandomHandler;
-import org.apache.http.nio.NHttpConnectionFactory;
-import org.apache.http.nio.client.util.HttpAsyncClientUtils;
-import org.apache.http.nio.protocol.BasicAsyncRequestHandler;
-import org.apache.http.nio.protocol.HttpAsyncExpectationVerifier;
-import org.apache.http.nio.protocol.HttpAsyncRequestHandlerMapper;
-import org.apache.http.nio.protocol.HttpAsyncService;
-import org.apache.http.nio.protocol.UriHttpAsyncRequestHandlerMapper;
-import org.apache.http.nio.reactor.IOReactorStatus;
-import org.apache.http.nio.reactor.ListenerEndpoint;
-import org.junit.After;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
-
-public class TestHttpAsyncClientUtils extends HttpAsyncTestBase {
-
- @Before
- public void setUp() throws Exception {
- initServer();
- initConnectionManager();
- }
-
- @After
- public void tearDown() throws Exception {
- shutDownClient();
- shutDownServer();
- }
-
- @Override
- protected NHttpConnectionFactory<DefaultNHttpServerConnection> createServerConnectionFactory(
- final ConnectionConfig config) throws Exception {
- return new DefaultNHttpServerConnectionFactory(config);
- }
-
- @Override
- protected String getSchemeName() {
- return "http";
- }
-
- @Test
- public void testAsync() throws Exception {
- final HttpHost target = start();
- final HttpGet httpget = new HttpGet("/random/2048");
- final Future<HttpResponse> future = this.httpclient.execute(target, httpget, null);
- final HttpResponse response = future.get();
- Assert.assertNotNull(response);
- Assert.assertEquals(200, response.getStatusLine().getStatusCode());
-
- HttpAsyncClientUtils.closeQuietly(this.httpclient);
- // Close it twice
- HttpAsyncClientUtils.closeQuietly(this.httpclient);
- }
-
- private HttpHost start() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("/echo/*", new BasicAsyncRequestHandler(new EchoHandler()));
- registry.register("/random/*", new BasicAsyncRequestHandler(new RandomHandler()));
- return start(registry, null);
- }
-
- private HttpHost start(final HttpAsyncRequestHandlerMapper requestHandlerResolver,
- final HttpAsyncExpectationVerifier expectationVerifier) throws Exception {
- final HttpAsyncService serviceHandler = new HttpAsyncService(
- this.serverHttpProc,
- DefaultConnectionReuseStrategy.INSTANCE,
- DefaultHttpResponseFactory.INSTANCE,
- requestHandlerResolver,
- expectationVerifier);
-
- this.httpclient = HttpAsyncClients.custom()
- .setConnectionManager(this.connMgr)
- .build();
-
- this.server.start(serviceHandler);
- this.httpclient.start();
-
- final ListenerEndpoint endpoint = this.server.getListenerEndpoint();
- endpoint.waitFor();
-
- Assert.assertEquals("Test server status", IOReactorStatus.ACTIVE, this.server.getStatus());
- final InetSocketAddress address = (InetSocketAddress) endpoint.getAddress();
- return new HttpHost("localhost", address.getPort(), getSchemeName());
- }
-
-}
diff --git a/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestHttpAsyncMinimal.java b/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestHttpAsyncMinimal.java
index 53e79fd..2207903 100644
--- a/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestHttpAsyncMinimal.java
+++ b/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestHttpAsyncMinimal.java
@@ -26,7 +26,8 @@
*/
package org.apache.http.nio.client.integration;
-import java.net.InetSocketAddress;
+import java.util.Arrays;
+import java.util.Collection;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
@@ -38,80 +39,37 @@
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
-import org.apache.http.config.ConnectionConfig;
-import org.apache.http.impl.DefaultConnectionReuseStrategy;
-import org.apache.http.impl.DefaultHttpResponseFactory;
-import org.apache.http.impl.nio.DefaultNHttpServerConnection;
-import org.apache.http.impl.nio.DefaultNHttpServerConnectionFactory;
-import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.localserver.EchoHandler;
import org.apache.http.localserver.RandomHandler;
-import org.apache.http.nio.NHttpConnectionFactory;
import org.apache.http.nio.entity.NByteArrayEntity;
import org.apache.http.nio.protocol.BasicAsyncRequestHandler;
-import org.apache.http.nio.protocol.HttpAsyncExpectationVerifier;
-import org.apache.http.nio.protocol.HttpAsyncRequestHandlerMapper;
-import org.apache.http.nio.protocol.HttpAsyncService;
-import org.apache.http.nio.protocol.UriHttpAsyncRequestHandlerMapper;
-import org.apache.http.nio.reactor.IOReactorStatus;
-import org.apache.http.nio.reactor.ListenerEndpoint;
import org.apache.http.util.EntityUtils;
-import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+@RunWith(Parameterized.class)
public class TestHttpAsyncMinimal extends HttpAsyncTestBase {
- @Before
+ @Parameterized.Parameters(name = "{0}")
+ public static Collection<Object[]> protocols() {
+ return Arrays.asList(new Object[][]{
+ {ProtocolScheme.http},
+ {ProtocolScheme.https},
+ });
+ }
+
+ public TestHttpAsyncMinimal(final ProtocolScheme scheme) {
+ super(scheme);
+ }
+
+ @Before @Override
public void setUp() throws Exception {
- initServer();
- initConnectionManager();
- this.httpclient = HttpAsyncClients.createMinimal(this.connMgr);
- }
-
- @After
- public void tearDown() throws Exception {
- shutDownClient();
- shutDownServer();
- }
-
- @Override
- protected NHttpConnectionFactory<DefaultNHttpServerConnection> createServerConnectionFactory(
- final ConnectionConfig config) throws Exception {
- return new DefaultNHttpServerConnectionFactory(config);
- }
-
- @Override
- protected String getSchemeName() {
- return "http";
- }
-
- private HttpHost start(
- final HttpAsyncRequestHandlerMapper requestHandlerResolver,
- final HttpAsyncExpectationVerifier expectationVerifier) throws Exception {
- final HttpAsyncService serviceHandler = new HttpAsyncService(
- this.serverHttpProc,
- DefaultConnectionReuseStrategy.INSTANCE,
- DefaultHttpResponseFactory.INSTANCE,
- requestHandlerResolver,
- expectationVerifier);
- this.server.start(serviceHandler);
- this.httpclient.start();
-
- final ListenerEndpoint endpoint = this.server.getListenerEndpoint();
- endpoint.waitFor();
-
- Assert.assertEquals("Test server status", IOReactorStatus.ACTIVE, this.server.getStatus());
- final InetSocketAddress address = (InetSocketAddress) endpoint.getAddress();
- return new HttpHost("localhost", address.getPort(), getSchemeName());
- }
-
- private HttpHost start() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("/echo/*", new BasicAsyncRequestHandler(new EchoHandler()));
- registry.register("/random/*", new BasicAsyncRequestHandler(new RandomHandler()));
- return start(registry, null);
+ super.setUp();
+ this.serverBootstrap.registerHandler("/echo/*", new BasicAsyncRequestHandler(new EchoHandler()));
+ this.serverBootstrap.registerHandler("/random/*", new BasicAsyncRequestHandler(new RandomHandler()));
}
@Test
diff --git a/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestHttpAsyncPrematureTermination.java b/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestHttpAsyncPrematureTermination.java
index fdb3d49..a0c773b 100644
--- a/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestHttpAsyncPrematureTermination.java
+++ b/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestHttpAsyncPrematureTermination.java
@@ -27,7 +27,6 @@
package org.apache.http.nio.client.integration;
import java.io.IOException;
-import java.net.InetSocketAddress;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@@ -39,89 +38,25 @@
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.concurrent.FutureCallback;
-import org.apache.http.config.ConnectionConfig;
import org.apache.http.entity.ContentType;
-import org.apache.http.impl.DefaultConnectionReuseStrategy;
-import org.apache.http.impl.DefaultHttpResponseFactory;
-import org.apache.http.impl.nio.DefaultNHttpServerConnection;
-import org.apache.http.impl.nio.DefaultNHttpServerConnectionFactory;
-import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.nio.ContentEncoder;
import org.apache.http.nio.IOControl;
-import org.apache.http.nio.NHttpConnectionFactory;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.nio.protocol.BasicAsyncRequestConsumer;
import org.apache.http.nio.protocol.BasicAsyncResponseProducer;
import org.apache.http.nio.protocol.HttpAsyncExchange;
-import org.apache.http.nio.protocol.HttpAsyncExpectationVerifier;
import org.apache.http.nio.protocol.HttpAsyncRequestConsumer;
import org.apache.http.nio.protocol.HttpAsyncRequestHandler;
-import org.apache.http.nio.protocol.HttpAsyncRequestHandlerMapper;
-import org.apache.http.nio.protocol.HttpAsyncService;
-import org.apache.http.nio.protocol.UriHttpAsyncRequestHandlerMapper;
-import org.apache.http.nio.reactor.IOReactorStatus;
-import org.apache.http.nio.reactor.ListenerEndpoint;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpCoreContext;
-import org.junit.After;
import org.junit.Assert;
-import org.junit.Before;
import org.junit.Test;
public class TestHttpAsyncPrematureTermination extends HttpAsyncTestBase {
- @Before
- public void setUp() throws Exception {
- initServer();
- initConnectionManager();
- }
-
- @After
- public void tearDown() throws Exception {
- shutDownClient();
- shutDownServer();
- }
-
- @Override
- protected NHttpConnectionFactory<DefaultNHttpServerConnection> createServerConnectionFactory(
- final ConnectionConfig config) throws Exception {
- return new DefaultNHttpServerConnectionFactory(config);
- }
-
- @Override
- protected String getSchemeName() {
- return "http";
- }
-
- private HttpHost start(
- final HttpAsyncRequestHandlerMapper requestHandlerResolver,
- final HttpAsyncExpectationVerifier expectationVerifier) throws Exception {
- final HttpAsyncService serviceHandler = new HttpAsyncService(
- this.serverHttpProc,
- DefaultConnectionReuseStrategy.INSTANCE,
- DefaultHttpResponseFactory.INSTANCE,
- requestHandlerResolver,
- expectationVerifier);
-
- this.httpclient = HttpAsyncClients.custom()
- .setConnectionManager(this.connMgr)
- .build();
-
- this.server.start(serviceHandler);
- this.httpclient.start();
-
- final ListenerEndpoint endpoint = this.server.getListenerEndpoint();
- endpoint.waitFor();
-
- Assert.assertEquals("Test server status", IOReactorStatus.ACTIVE, this.server.getStatus());
- final InetSocketAddress address = (InetSocketAddress) endpoint.getAddress();
- return new HttpHost("localhost", address.getPort(), getSchemeName());
- }
-
@Test
public void testConnectionTerminatedProcessingRequest() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new HttpAsyncRequestHandler<HttpRequest>() {
+ this.serverBootstrap.registerHandler("*", new HttpAsyncRequestHandler<HttpRequest>() {
public HttpAsyncRequestConsumer<HttpRequest> processRequest(
final HttpRequest request,
@@ -143,11 +78,7 @@
});
- this.httpclient = HttpAsyncClients.custom()
- .setConnectionManager(this.connMgr)
- .build();
-
- final HttpHost target = start(registry, null);
+ final HttpHost target = start();
final HttpGet httpget = new HttpGet("/");
final CountDownLatch latch = new CountDownLatch(1);
@@ -174,8 +105,7 @@
@Test
public void testConnectionTerminatedHandlingRequest() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new HttpAsyncRequestHandler<HttpRequest>() {
+ this.serverBootstrap.registerHandler("*", new HttpAsyncRequestHandler<HttpRequest>() {
public HttpAsyncRequestConsumer<HttpRequest> processRequest(
final HttpRequest request,
@@ -197,11 +127,7 @@
});
- this.httpclient = HttpAsyncClients.custom()
- .setConnectionManager(this.connMgr)
- .build();
-
- final HttpHost target = start(registry, null);
+ final HttpHost target = start();
final HttpGet httpget = new HttpGet("/");
final CountDownLatch latch = new CountDownLatch(1);
@@ -228,8 +154,7 @@
@Test
public void testConnectionTerminatedSendingResponse() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new HttpAsyncRequestHandler<HttpRequest>() {
+ this.serverBootstrap.registerHandler("*", new HttpAsyncRequestHandler<HttpRequest>() {
public HttpAsyncRequestConsumer<HttpRequest> processRequest(
final HttpRequest request,
@@ -257,11 +182,7 @@
});
- this.httpclient = HttpAsyncClients.custom()
- .setConnectionManager(this.connMgr)
- .build();
-
- final HttpHost target = start(registry, null);
+ final HttpHost target = start();
final HttpGet httpget = new HttpGet("/");
final CountDownLatch latch = new CountDownLatch(1);
diff --git a/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestHttpsAsync.java b/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestHttpsAsync.java
deleted file mode 100644
index 834cea6..0000000
--- a/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestHttpsAsync.java
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * ====================================================================
- * 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.nio.client.integration;
-
-import org.apache.http.SSLTestContexts;
-import org.apache.http.config.ConnectionConfig;
-import org.apache.http.config.Registry;
-import org.apache.http.config.RegistryBuilder;
-import org.apache.http.impl.nio.DefaultNHttpServerConnection;
-import org.apache.http.impl.nio.SSLNHttpServerConnectionFactory;
-import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
-import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
-import org.apache.http.nio.NHttpConnectionFactory;
-import org.apache.http.nio.conn.NoopIOSessionStrategy;
-import org.apache.http.nio.conn.SchemeIOSessionStrategy;
-import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy;
-
-public class TestHttpsAsync extends TestHttpAsync {
-
- @Override
- protected NHttpConnectionFactory<DefaultNHttpServerConnection> createServerConnectionFactory(
- final ConnectionConfig config) throws Exception {
- return new SSLNHttpServerConnectionFactory(SSLTestContexts.createServerSSLContext(), null, config);
- }
-
- @Override
- protected String getSchemeName() {
- return "https";
- }
-
- @Override
- public void initConnectionManager() throws Exception {
- final Registry<SchemeIOSessionStrategy> schemereg = RegistryBuilder.<SchemeIOSessionStrategy>create()
- .register("http", NoopIOSessionStrategy.INSTANCE)
- .register("https", new SSLIOSessionStrategy(SSLTestContexts.createClientSSLContext()))
- .build();
- this.clientIOReactor = new DefaultConnectingIOReactor(this.clientReactorConfig);
- this.connMgr = new PoolingNHttpClientConnectionManager(this.clientIOReactor, schemereg);
- }
-
-}
diff --git a/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestHttpsRedirects.java b/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestHttpsRedirects.java
deleted file mode 100644
index 1ccb00f..0000000
--- a/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestHttpsRedirects.java
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * ====================================================================
- * 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.nio.client.integration;
-
-import org.apache.http.SSLTestContexts;
-import org.apache.http.config.ConnectionConfig;
-import org.apache.http.config.Registry;
-import org.apache.http.config.RegistryBuilder;
-import org.apache.http.impl.nio.DefaultNHttpServerConnection;
-import org.apache.http.impl.nio.SSLNHttpServerConnectionFactory;
-import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
-import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
-import org.apache.http.nio.NHttpConnectionFactory;
-import org.apache.http.nio.conn.NoopIOSessionStrategy;
-import org.apache.http.nio.conn.SchemeIOSessionStrategy;
-import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy;
-
-public class TestHttpsRedirects extends TestRedirects {
-
- @Override
- protected NHttpConnectionFactory<DefaultNHttpServerConnection> createServerConnectionFactory(
- final ConnectionConfig config) throws Exception {
- return new SSLNHttpServerConnectionFactory(SSLTestContexts.createServerSSLContext(), null, config);
- }
-
- @Override
- protected String getSchemeName() {
- return "https";
- }
-
- @Override
- public void initConnectionManager() throws Exception {
- final Registry<SchemeIOSessionStrategy> schemereg = RegistryBuilder.<SchemeIOSessionStrategy>create()
- .register("http", NoopIOSessionStrategy.INSTANCE)
- .register("https", new SSLIOSessionStrategy(SSLTestContexts.createClientSSLContext()))
- .build();
- this.clientIOReactor = new DefaultConnectingIOReactor(this.clientReactorConfig);
- this.connMgr = new PoolingNHttpClientConnectionManager(this.clientIOReactor, schemereg);
- }
-
-}
diff --git a/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestRedirects.java b/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestRedirects.java
index 9e4da82..2d41047 100644
--- a/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestRedirects.java
+++ b/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestRedirects.java
@@ -30,11 +30,14 @@
import java.net.InetSocketAddress;
import java.net.URISyntaxException;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
import org.apache.http.Header;
import org.apache.http.HttpAsyncTestBase;
@@ -54,86 +57,41 @@
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIBuilder;
-import org.apache.http.config.ConnectionConfig;
import org.apache.http.cookie.SM;
import org.apache.http.entity.StringEntity;
-import org.apache.http.impl.DefaultConnectionReuseStrategy;
-import org.apache.http.impl.DefaultHttpResponseFactory;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.cookie.BasicClientCookie;
-import org.apache.http.impl.nio.DefaultNHttpServerConnection;
-import org.apache.http.impl.nio.DefaultNHttpServerConnectionFactory;
-import org.apache.http.impl.nio.client.HttpAsyncClients;
-import org.apache.http.localserver.HttpServerNio;
+import org.apache.http.impl.nio.bootstrap.HttpServer;
import org.apache.http.localserver.RandomHandler;
import org.apache.http.message.BasicHeader;
-import org.apache.http.nio.NHttpConnectionFactory;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.nio.protocol.BasicAsyncRequestHandler;
-import org.apache.http.nio.protocol.HttpAsyncExpectationVerifier;
-import org.apache.http.nio.protocol.HttpAsyncRequestHandlerMapper;
-import org.apache.http.nio.protocol.HttpAsyncService;
-import org.apache.http.nio.protocol.UriHttpAsyncRequestHandlerMapper;
-import org.apache.http.nio.reactor.IOReactorStatus;
import org.apache.http.nio.reactor.ListenerEndpoint;
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.junit.After;
import org.junit.Assert;
-import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
/**
* Redirection test cases.
*/
+@RunWith(Parameterized.class)
public class TestRedirects extends HttpAsyncTestBase {
- @Before
- public void setUp() throws Exception {
- initServer();
- initConnectionManager();
- this.httpclient = HttpAsyncClients.custom()
- .setConnectionManager(this.connMgr)
- .build();
+ @Parameterized.Parameters(name = "{0}")
+ public static Collection<Object[]> protocols() {
+ return Arrays.asList(new Object[][]{
+ {ProtocolScheme.http},
+ {ProtocolScheme.https},
+ });
}
- @After
- public void tearDown() throws Exception {
- shutDownClient();
- shutDownServer();
- }
-
- @Override
- protected NHttpConnectionFactory<DefaultNHttpServerConnection> createServerConnectionFactory(
- final ConnectionConfig config) throws Exception {
- return new DefaultNHttpServerConnectionFactory(config);
- }
-
- @Override
- protected String getSchemeName() {
- return "http";
- }
-
- private HttpHost start(
- final HttpAsyncRequestHandlerMapper requestHandlerResolver,
- final HttpAsyncExpectationVerifier expectationVerifier) throws Exception {
- final HttpAsyncService serviceHandler = new HttpAsyncService(
- this.serverHttpProc,
- DefaultConnectionReuseStrategy.INSTANCE,
- DefaultHttpResponseFactory.INSTANCE,
- requestHandlerResolver,
- expectationVerifier);
- this.server.start(serviceHandler);
- this.httpclient.start();
-
- final ListenerEndpoint endpoint = this.server.getListenerEndpoint();
- endpoint.waitFor();
-
- Assert.assertEquals("Test server status", IOReactorStatus.ACTIVE, this.server.getStatus());
- final InetSocketAddress address = (InetSocketAddress) endpoint.getAddress();
- return new HttpHost("localhost", address.getPort(), getSchemeName());
+ public TestRedirects(final ProtocolScheme scheme) {
+ super(scheme);
}
static class BasicRedirectService implements HttpRequestHandler {
@@ -307,10 +265,9 @@
@Test
public void testBasicRedirect300() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(
new BasicRedirectService(getSchemeName(), HttpStatus.SC_MULTIPLE_CHOICES)));
- final HttpHost target = start(registry, null);
+ final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
@@ -328,10 +285,9 @@
@Test
public void testBasicRedirect301() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(
new BasicRedirectService(getSchemeName(), HttpStatus.SC_MOVED_PERMANENTLY)));
- final HttpHost target = start(registry, null);
+ final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
@@ -351,10 +307,9 @@
@Test
public void testBasicRedirect302() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(
new BasicRedirectService(getSchemeName(), HttpStatus.SC_MOVED_TEMPORARILY)));
- final HttpHost target = start(registry, null);
+ final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
@@ -374,8 +329,7 @@
@Test
public void testBasicRedirect302NoLocation() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(new HttpRequestHandler() {
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(new HttpRequestHandler() {
public void handle(
final HttpRequest request,
@@ -385,7 +339,7 @@
}
}));
- final HttpHost target = start(registry, null);
+ final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
@@ -405,10 +359,9 @@
@Test
public void testBasicRedirect303() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(
new BasicRedirectService(getSchemeName(), HttpStatus.SC_SEE_OTHER)));
- final HttpHost target = start(registry, null);
+ final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
@@ -428,10 +381,9 @@
@Test
public void testBasicRedirect304() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(
new BasicRedirectService(getSchemeName(), HttpStatus.SC_NOT_MODIFIED)));
- final HttpHost target = start(registry, null);
+ final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
@@ -449,10 +401,9 @@
@Test
public void testBasicRedirect305() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(
new BasicRedirectService(getSchemeName(), HttpStatus.SC_USE_PROXY)));
- final HttpHost target = start(registry, null);
+ final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
@@ -470,10 +421,9 @@
@Test
public void testBasicRedirect307() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(
new BasicRedirectService(getSchemeName(), HttpStatus.SC_TEMPORARY_REDIRECT)));
- final HttpHost target = start(registry, null);
+ final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
@@ -493,9 +443,8 @@
@Test(expected=ExecutionException.class)
public void testMaxRedirectCheck() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(new CircularRedirectService()));
- final HttpHost target = start(registry, null);
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(new CircularRedirectService()));
+ final HttpHost target = start();
final RequestConfig config = RequestConfig.custom()
.setCircularRedirectsAllowed(true)
@@ -514,9 +463,8 @@
@Test(expected=ExecutionException.class)
public void testCircularRedirect() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(new CircularRedirectService()));
- final HttpHost target = start(registry, null);
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(new CircularRedirectService()));
+ final HttpHost target = start();
final RequestConfig config = RequestConfig.custom()
.setCircularRedirectsAllowed(false)
@@ -536,10 +484,9 @@
@Test
public void testPostNoRedirect() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(
new BasicRedirectService(getSchemeName(), HttpStatus.SC_MOVED_TEMPORARILY)));
- final HttpHost target = start(registry, null);
+ final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
@@ -559,10 +506,9 @@
@Test
public void testPostRedirectSeeOther() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(
new BasicRedirectService(getSchemeName(), HttpStatus.SC_SEE_OTHER)));
- final HttpHost target = start(registry, null);
+ final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
@@ -582,9 +528,8 @@
@Test
public void testRelativeRedirect() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(new RelativeRedirectService()));
- final HttpHost target = start(registry, null);
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(new RelativeRedirectService()));
+ final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
@@ -609,9 +554,8 @@
@Test
public void testRelativeRedirect2() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(new RelativeRedirectService2()));
- final HttpHost target = start(registry, null);
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(new RelativeRedirectService2()));
+ final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
@@ -636,9 +580,8 @@
@Test(expected=ExecutionException.class)
public void testRejectRelativeRedirect() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(new RelativeRedirectService()));
- final HttpHost target = start(registry, null);
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(new RelativeRedirectService()));
+ final HttpHost target = start();
final RequestConfig config = RequestConfig.custom()
.setRelativeRedirectsAllowed(false)
@@ -657,10 +600,9 @@
@Test(expected=ExecutionException.class)
public void testRejectBogusRedirectLocation() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(
new BogusRedirectService(getSchemeName(), "xxx://bogus", true)));
- final HttpHost target = start(registry, null);
+ final HttpHost target = start();
final HttpGet httpget = new HttpGet("/oldlocation/");
@@ -675,10 +617,9 @@
@Test(expected=ExecutionException.class)
public void testRejectInvalidRedirectLocation() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(
new BogusRedirectService(getSchemeName(), "/newlocation/?p=I have spaces", false)));
- final HttpHost target = start(registry, null);
+ final HttpHost target = start();
final HttpGet httpget = new HttpGet("/oldlocation/");
try {
@@ -692,10 +633,9 @@
@Test
public void testRedirectWithCookie() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(
new BasicRedirectService(getSchemeName(), HttpStatus.SC_MOVED_TEMPORARILY)));
- final HttpHost target = start(registry, null);
+ final HttpHost target = start();
final CookieStore cookieStore = new BasicCookieStore();
final HttpClientContext context = HttpClientContext.create();
@@ -724,19 +664,14 @@
@Test
public void testDefaultHeadersRedirect() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(
new BasicRedirectService(getSchemeName(), HttpStatus.SC_MOVED_TEMPORARILY)));
final List<Header> defaultHeaders = new ArrayList<Header>(1);
defaultHeaders.add(new BasicHeader(HTTP.USER_AGENT, "my-test-client"));
+ this.clientBuilder.setDefaultHeaders(defaultHeaders);
- this.httpclient = HttpAsyncClients.custom()
- .setConnectionManager(this.connMgr)
- .setDefaultHeaders(defaultHeaders)
- .build();
-
- final HttpHost target = start(registry, null);
+ final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
@@ -787,29 +722,19 @@
@Test
public void testCrossSiteRedirect() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("/random/*", new BasicAsyncRequestHandler(
+ this.serverBootstrap.registerHandler("/random/*", new BasicAsyncRequestHandler(
new RandomHandler()));
- final HttpHost redirectTarget = start(registry, null);
+ final HttpHost redirectTarget = start();
- final UriHttpAsyncRequestHandlerMapper registry2 = new UriHttpAsyncRequestHandlerMapper();
- registry2.register("/redirect/*", new BasicAsyncRequestHandler(
+ this.serverBootstrap.registerHandler("/redirect/*", new BasicAsyncRequestHandler(
new CrossSiteRedirectService(redirectTarget)));
- final HttpServerNio secondServer = new HttpServerNio(this.serverReactorConfig,
- createServerConnectionFactory(this.serverConnectionConfig));
- secondServer.setExceptionHandler(new SimpleIOReactorExceptionHandler());
- final HttpAsyncService serviceHandler = new HttpAsyncService(
- this.serverHttpProc,
- new DefaultConnectionReuseStrategy(),
- new DefaultHttpResponseFactory(),
- registry2,
- null);
- secondServer.start(serviceHandler);
+
+ final HttpServer secondServer = this.serverBootstrap.create();
try {
- final ListenerEndpoint endpoint2 = secondServer.getListenerEndpoint();
+ secondServer.start();
+ final ListenerEndpoint endpoint2 = secondServer.getEndpoint();
endpoint2.waitFor();
- Assert.assertEquals("Test server status", IOReactorStatus.ACTIVE, secondServer.getStatus());
final InetSocketAddress address2 = (InetSocketAddress) endpoint2.getAddress();
final HttpHost initialTarget = new HttpHost("localhost", address2.getPort(), getSchemeName());
@@ -826,15 +751,14 @@
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
}
} finally {
- this.server.shutdown();
+ this.server.shutdown(10, TimeUnit.SECONDS);
}
}
@Test
public void testRepeatRequest() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(new RomeRedirectService()));
- final HttpHost target = start(registry, null);
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(new RomeRedirectService()));
+ final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
@@ -863,9 +787,8 @@
@Test
public void testRepeatRequestRedirect() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(new RomeRedirectService()));
- final HttpHost target = start(registry, null);
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(new RomeRedirectService()));
+ final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
@@ -894,9 +817,8 @@
@Test
public void testDifferentRequestSameRedirect() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(new RomeRedirectService()));
- final HttpHost target = start(registry, null);
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(new RomeRedirectService()));
+ final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
diff --git a/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestStatefulConnManagement.java b/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestStatefulConnManagement.java
index 5a5db50..c91c457 100644
--- a/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestStatefulConnManagement.java
+++ b/httpasyncclient/src/test/java/org/apache/http/nio/client/integration/TestStatefulConnManagement.java
@@ -27,7 +27,6 @@
package org.apache.http.nio.client.integration;
import java.io.IOException;
-import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.util.concurrent.Future;
@@ -40,84 +39,26 @@
import org.apache.http.HttpStatus;
import org.apache.http.client.UserTokenHandler;
import org.apache.http.client.methods.HttpGet;
-import org.apache.http.config.ConnectionConfig;
import org.apache.http.entity.ContentType;
-import org.apache.http.impl.DefaultConnectionReuseStrategy;
-import org.apache.http.impl.DefaultHttpResponseFactory;
-import org.apache.http.impl.nio.DefaultNHttpServerConnection;
-import org.apache.http.impl.nio.DefaultNHttpServerConnectionFactory;
-import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.impl.nio.conn.CPoolUtils;
import org.apache.http.nio.ContentDecoder;
import org.apache.http.nio.IOControl;
import org.apache.http.nio.NHttpClientConnection;
-import org.apache.http.nio.NHttpConnectionFactory;
import org.apache.http.nio.client.HttpAsyncClient;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.nio.protocol.AbstractAsyncResponseConsumer;
import org.apache.http.nio.protocol.BasicAsyncRequestHandler;
import org.apache.http.nio.protocol.BasicAsyncRequestProducer;
-import org.apache.http.nio.protocol.HttpAsyncExpectationVerifier;
-import org.apache.http.nio.protocol.HttpAsyncRequestHandlerMapper;
-import org.apache.http.nio.protocol.HttpAsyncService;
-import org.apache.http.nio.protocol.UriHttpAsyncRequestHandlerMapper;
import org.apache.http.nio.reactor.IOEventDispatch;
-import org.apache.http.nio.reactor.IOReactorStatus;
-import org.apache.http.nio.reactor.ListenerEndpoint;
import org.apache.http.pool.PoolEntry;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
-import org.junit.After;
import org.junit.Assert;
-import org.junit.Before;
import org.junit.Test;
public class TestStatefulConnManagement extends HttpAsyncTestBase {
- @Before
- public void setUp() throws Exception {
- initServer();
- initConnectionManager();
- }
-
- @After
- public void tearDown() throws Exception {
- shutDownClient();
- shutDownServer();
- }
-
- @Override
- protected NHttpConnectionFactory<DefaultNHttpServerConnection> createServerConnectionFactory(
- final ConnectionConfig config) throws Exception {
- return new DefaultNHttpServerConnectionFactory(config);
- }
-
- @Override
- protected String getSchemeName() {
- return "http";
- }
-
- private HttpHost start(
- final HttpAsyncRequestHandlerMapper requestHandlerResolver,
- final HttpAsyncExpectationVerifier expectationVerifier) throws Exception {
- final HttpAsyncService serviceHandler = new HttpAsyncService(
- this.serverHttpProc,
- new DefaultConnectionReuseStrategy(),
- new DefaultHttpResponseFactory(),
- requestHandlerResolver,
- expectationVerifier);
- this.server.start(serviceHandler);
- this.httpclient.start();
-
- final ListenerEndpoint endpoint = this.server.getListenerEndpoint();
- endpoint.waitFor();
-
- Assert.assertEquals("Test server status", IOReactorStatus.ACTIVE, this.server.getStatus());
- final InetSocketAddress address = (InetSocketAddress) endpoint.getAddress();
- return new HttpHost("localhost", address.getPort(), getSchemeName());
- }
-
static class SimpleService implements HttpRequestHandler {
public SimpleService() {
@@ -136,8 +77,7 @@
@Test
public void testStatefulConnections() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(new SimpleService()));
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(new SimpleService()));
final UserTokenHandler userTokenHandler = new UserTokenHandler() {
@@ -146,13 +86,8 @@
}
};
-
- this.httpclient = HttpAsyncClients.custom()
- .setConnectionManager(this.connMgr)
- .setUserTokenHandler(userTokenHandler)
- .build();
-
- final HttpHost target = start(registry, null);
+ this.clientBuilder.setUserTokenHandler(userTokenHandler);
+ final HttpHost target = start();
final int workerCount = 2;
final int requestCount = 5;
@@ -280,6 +215,7 @@
@Test
public void testRouteSpecificPoolRecylcing() throws Exception {
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(new SimpleService()));
// This tests what happens when a maxed connection pool needs
// to kill the last idle connection to a route to build a new
// one to the same route.
@@ -290,22 +226,14 @@
}
};
+ this.clientBuilder.setUserTokenHandler(userTokenHandler);
- this.httpclient = HttpAsyncClients.custom()
- .setConnectionManager(this.connMgr)
- .setUserTokenHandler(userTokenHandler)
- .build();
-
+ final HttpHost target = start();
final int maxConn = 2;
// We build a client with 2 max active // connections, and 2 max per route.
this.connMgr.setMaxTotal(maxConn);
this.connMgr.setDefaultMaxPerRoute(maxConn);
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(new SimpleService()));
-
- final HttpHost target = start(registry, null);
-
// Bottom of the pool : a *keep alive* connection to Route 1.
final HttpContext context1 = new BasicHttpContext();
context1.setAttribute("user", "stuff");
diff --git a/httpasyncclient/src/test/java/org/apache/http/nio/client/methods/TestAsyncConsumers.java b/httpasyncclient/src/test/java/org/apache/http/nio/client/methods/TestAsyncConsumers.java
index 02811ec..aeb1405 100644
--- a/httpasyncclient/src/test/java/org/apache/http/nio/client/methods/TestAsyncConsumers.java
+++ b/httpasyncclient/src/test/java/org/apache/http/nio/client/methods/TestAsyncConsumers.java
@@ -27,7 +27,6 @@
package org.apache.http.nio.client.methods;
import java.io.IOException;
-import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.util.concurrent.ExecutionException;
@@ -39,27 +38,13 @@
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
-import org.apache.http.config.ConnectionConfig;
import org.apache.http.entity.ContentType;
-import org.apache.http.impl.DefaultConnectionReuseStrategy;
-import org.apache.http.impl.DefaultHttpResponseFactory;
-import org.apache.http.impl.nio.DefaultNHttpServerConnection;
-import org.apache.http.impl.nio.DefaultNHttpServerConnectionFactory;
-import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.localserver.EchoHandler;
import org.apache.http.localserver.RandomHandler;
import org.apache.http.nio.IOControl;
-import org.apache.http.nio.NHttpConnectionFactory;
import org.apache.http.nio.protocol.BasicAsyncRequestHandler;
-import org.apache.http.nio.protocol.HttpAsyncExpectationVerifier;
-import org.apache.http.nio.protocol.HttpAsyncRequestHandlerMapper;
import org.apache.http.nio.protocol.HttpAsyncRequestProducer;
-import org.apache.http.nio.protocol.HttpAsyncService;
-import org.apache.http.nio.protocol.UriHttpAsyncRequestHandlerMapper;
-import org.apache.http.nio.reactor.IOReactorStatus;
-import org.apache.http.nio.reactor.ListenerEndpoint;
import org.apache.http.protocol.HttpContext;
-import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@@ -67,57 +52,11 @@
public class TestAsyncConsumers extends HttpAsyncTestBase {
- @Before
+ @Before @Override
public void setUp() throws Exception {
- initServer();
- initConnectionManager();
- this.httpclient = HttpAsyncClients.custom()
- .setConnectionManager(this.connMgr)
- .build();
- }
-
- @After
- public void tearDown() throws Exception {
- shutDownClient();
- shutDownServer();
- }
-
- @Override
- protected NHttpConnectionFactory<DefaultNHttpServerConnection> createServerConnectionFactory(
- final ConnectionConfig config) throws Exception {
- return new DefaultNHttpServerConnectionFactory(config);
- }
-
- @Override
- protected String getSchemeName() {
- return "http";
- }
-
- private HttpHost start(
- final HttpAsyncRequestHandlerMapper requestHandlerResolver,
- final HttpAsyncExpectationVerifier expectationVerifier) throws Exception {
- final HttpAsyncService serviceHandler = new HttpAsyncService(
- this.serverHttpProc,
- new DefaultConnectionReuseStrategy(),
- new DefaultHttpResponseFactory(),
- requestHandlerResolver,
- expectationVerifier);
- this.server.start(serviceHandler);
- this.httpclient.start();
-
- final ListenerEndpoint endpoint = this.server.getListenerEndpoint();
- endpoint.waitFor();
-
- Assert.assertEquals("Test server status", IOReactorStatus.ACTIVE, this.server.getStatus());
- final InetSocketAddress address = (InetSocketAddress) endpoint.getAddress();
- return new HttpHost("localhost", address.getPort(), getSchemeName());
- }
-
- private HttpHost start() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("/echo/*", new BasicAsyncRequestHandler(new EchoHandler()));
- registry.register("/random/*", new BasicAsyncRequestHandler(new RandomHandler()));
- return start(registry, null);
+ super.setUp();
+ this.serverBootstrap.registerHandler("/echo/*", new BasicAsyncRequestHandler(new EchoHandler()));
+ this.serverBootstrap.registerHandler("/random/*", new BasicAsyncRequestHandler(new RandomHandler()));
}
static class ByteCountingConsumer extends AsyncByteConsumer<Long> {
diff --git a/httpasyncclient/src/test/java/org/apache/http/nio/client/methods/TestZeroCopy.java b/httpasyncclient/src/test/java/org/apache/http/nio/client/methods/TestZeroCopy.java
index 04bd425..9bd8648 100644
--- a/httpasyncclient/src/test/java/org/apache/http/nio/client/methods/TestZeroCopy.java
+++ b/httpasyncclient/src/test/java/org/apache/http/nio/client/methods/TestZeroCopy.java
@@ -31,7 +31,6 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
-import java.net.InetSocketAddress;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.concurrent.Future;
@@ -50,81 +49,21 @@
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
-import org.apache.http.config.ConnectionConfig;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.entity.ContentType;
-import org.apache.http.impl.DefaultConnectionReuseStrategy;
-import org.apache.http.impl.DefaultHttpResponseFactory;
-import org.apache.http.impl.nio.DefaultNHttpServerConnection;
-import org.apache.http.impl.nio.DefaultNHttpServerConnectionFactory;
-import org.apache.http.impl.nio.client.HttpAsyncClients;
-import org.apache.http.nio.NHttpConnectionFactory;
import org.apache.http.nio.entity.NFileEntity;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.nio.protocol.BasicAsyncRequestHandler;
-import org.apache.http.nio.protocol.HttpAsyncExpectationVerifier;
-import org.apache.http.nio.protocol.HttpAsyncRequestHandlerMapper;
-import org.apache.http.nio.protocol.HttpAsyncService;
-import org.apache.http.nio.protocol.UriHttpAsyncRequestHandlerMapper;
-import org.apache.http.nio.reactor.IOReactorStatus;
-import org.apache.http.nio.reactor.ListenerEndpoint;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
-import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class TestZeroCopy extends HttpAsyncTestBase {
- @Before
- public void setUp() throws Exception {
- initServer();
- initConnectionManager();
- this.httpclient = HttpAsyncClients.custom()
- .setConnectionManager(this.connMgr)
- .build();
- }
-
- @After
- public void tearDown() throws Exception {
- shutDownClient();
- shutDownServer();
- }
-
- @Override
- protected NHttpConnectionFactory<DefaultNHttpServerConnection> createServerConnectionFactory(
- final ConnectionConfig config) throws Exception {
- return new DefaultNHttpServerConnectionFactory(config);
- }
-
- @Override
- protected String getSchemeName() {
- return "http";
- }
-
- private HttpHost start(
- final HttpAsyncRequestHandlerMapper requestHandlerResolver,
- final HttpAsyncExpectationVerifier expectationVerifier) throws Exception {
- final HttpAsyncService serviceHandler = new HttpAsyncService(
- this.serverHttpProc,
- new DefaultConnectionReuseStrategy(),
- new DefaultHttpResponseFactory(),
- requestHandlerResolver,
- expectationVerifier);
- this.server.start(serviceHandler);
- this.httpclient.start();
-
- final ListenerEndpoint endpoint = this.server.getListenerEndpoint();
- endpoint.waitFor();
-
- Assert.assertEquals("Test server status", IOReactorStatus.ACTIVE, this.server.getStatus());
- final InetSocketAddress address = (InetSocketAddress) endpoint.getAddress();
- return new HttpHost("localhost", address.getPort(), getSchemeName());
- }
-
private static final String[] TEXT = {
"blah blah blah blah blah blah blah blah blah blah blah blah blah blah",
"yada yada yada yada yada yada yada yada yada yada yada yada yada yada",
@@ -271,9 +210,8 @@
@Test
public void testTwoWayZeroCopy() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(new TestHandler(false)));
- final HttpHost target = start(registry, null);
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(new TestHandler(false)));
+ final HttpHost target = start();
final File tmpdir = FileUtils.getTempDirectory();
this.tmpfile = new File(tmpdir, "dst.test");
@@ -301,9 +239,8 @@
@Test
public void testZeroCopyFallback() throws Exception {
- final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
- registry.register("*", new BasicAsyncRequestHandler(new TestHandler(true)));
- final HttpHost target = start(registry, null);
+ this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(new TestHandler(true)));
+ final HttpHost target = start();
final File tmpdir = FileUtils.getTempDirectory();
this.tmpfile = new File(tmpdir, "dst.test");
final TestZeroCopyPost httppost = new TestZeroCopyPost(target.toURI() + "/bounce", true);
diff --git a/pom.xml b/pom.xml
index fa99d7d..6b5910b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -66,7 +66,7 @@
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
- <httpcore.version>4.3.2</httpcore.version>
+ <httpcore.version>4.4-alpha1</httpcore.version>
<httpclient.version>4.3.3</httpclient.version>
<commons-logging.version>1.1.3</commons-logging.version>
<commons-io.version>2.4</commons-io.version>