You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2007/02/11 01:37:20 UTC
svn commit: r505811 - in
/jakarta/httpcomponents/httpcore/trunk/module-niossl: ./
src/test/java/org/apache/http/impl/nio/mockup/
src/test/java/org/apache/http/impl/nio/reactor/
Author: olegk
Date: Sat Feb 10 16:37:19 2007
New Revision: 505811
URL: http://svn.apache.org/viewvc?view=rev&rev=505811
Log:
Added end to end test for basic GET, POST (content length delimited) and POST (chunked) requests
Added:
jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/mockup/
jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/mockup/TestHttpSSLClient.java (with props)
jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/mockup/TestHttpSSLServer.java (with props)
jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/mockup/TestHttpSSLServiceBase.java (with props)
jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestAll.java (with props)
jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestNIOSSLHttp.java (with props)
Modified:
jakarta/httpcomponents/httpcore/trunk/module-niossl/ (props changed)
Propchange: jakarta/httpcomponents/httpcore/trunk/module-niossl/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Sat Feb 10 16:37:19 2007
@@ -0,0 +1,5 @@
+
+target
+.settings
+.classpath
+.project
Added: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/mockup/TestHttpSSLClient.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/mockup/TestHttpSSLClient.java?view=auto&rev=505811
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/mockup/TestHttpSSLClient.java (added)
+++ jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/mockup/TestHttpSSLClient.java Sat Feb 10 16:37:19 2007
@@ -0,0 +1,122 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ * 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.impl.nio.mockup;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.net.URL;
+import java.security.KeyStore;
+
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.TrustManagerFactory;
+
+import org.apache.http.impl.DefaultConnectionReuseStrategy;
+import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
+import org.apache.http.impl.nio.reactor.SSLClientIOEventDispatch;
+import org.apache.http.nio.protocol.BufferingHttpClientHandler;
+import org.apache.http.nio.protocol.HttpRequestExecutionHandler;
+import org.apache.http.nio.reactor.ConnectingIOReactor;
+import org.apache.http.nio.reactor.IOEventDispatch;
+import org.apache.http.params.HttpConnectionParams;
+import org.apache.http.params.HttpProtocolParams;
+import org.apache.http.protocol.BasicHttpProcessor;
+import org.apache.http.protocol.RequestConnControl;
+import org.apache.http.protocol.RequestContent;
+import org.apache.http.protocol.RequestExpectContinue;
+import org.apache.http.protocol.RequestTargetHost;
+import org.apache.http.protocol.RequestUserAgent;
+
+public class TestHttpSSLClient extends TestHttpSSLServiceBase {
+
+ private final SSLContext sslcontext;
+ private HttpRequestExecutionHandler execHandler;
+
+ public TestHttpSSLClient() throws Exception {
+ super();
+
+ ClassLoader cl = getClass().getClassLoader();
+ URL url = cl.getResource("test.keystore");
+ KeyStore keystore = KeyStore.getInstance("jks");
+ keystore.load(url.openStream(), "nopassword".toCharArray());
+ TrustManagerFactory tmfactory = TrustManagerFactory.getInstance(
+ TrustManagerFactory.getDefaultAlgorithm());
+ tmfactory.init(keystore);
+ TrustManager[] trustmanagers = tmfactory.getTrustManagers();
+ this.sslcontext = SSLContext.getInstance("TLS");
+ this.sslcontext.init(null, trustmanagers, null);
+
+ this.params
+ .setIntParameter(HttpConnectionParams.SO_TIMEOUT, 2000)
+ .setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 2000)
+ .setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE, 8 * 1024)
+ .setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, false)
+ .setBooleanParameter(HttpConnectionParams.TCP_NODELAY, true)
+ .setParameter(HttpProtocolParams.USER_AGENT, "TEST-SSL-CLIENT/1.1");
+
+ this.ioReactor = new DefaultConnectingIOReactor(2, this.params);
+ }
+
+ public void setHttpRequestExecutionHandler(final HttpRequestExecutionHandler handler) {
+ this.execHandler = handler;
+ }
+
+ protected void execute() throws IOException {
+ BasicHttpProcessor httpproc = new BasicHttpProcessor();
+ httpproc.addInterceptor(new RequestContent());
+ httpproc.addInterceptor(new RequestTargetHost());
+ httpproc.addInterceptor(new RequestConnControl());
+ httpproc.addInterceptor(new RequestUserAgent());
+ httpproc.addInterceptor(new RequestExpectContinue());
+
+ BufferingHttpClientHandler clientHandler = new BufferingHttpClientHandler(
+ httpproc,
+ this.execHandler,
+ new DefaultConnectionReuseStrategy(),
+ this.params);
+
+ clientHandler.setEventListener(new EventLogger());
+
+ IOEventDispatch ioEventDispatch = new SSLClientIOEventDispatch(
+ clientHandler,
+ this.sslcontext,
+ this.params);
+
+ this.ioReactor.execute(ioEventDispatch);
+ }
+
+ public void openConnection(final InetSocketAddress address, final Object attachment) {
+ ((ConnectingIOReactor) this.ioReactor).connect(
+ address, null, attachment);
+ }
+
+}
Propchange: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/mockup/TestHttpSSLClient.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/mockup/TestHttpSSLClient.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/mockup/TestHttpSSLServer.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/mockup/TestHttpSSLServer.java?view=auto&rev=505811
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/mockup/TestHttpSSLServer.java (added)
+++ jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/mockup/TestHttpSSLServer.java Sat Feb 10 16:37:19 2007
@@ -0,0 +1,142 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ * 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.impl.nio.mockup;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
+import java.net.URL;
+import java.security.KeyStore;
+
+import javax.net.ssl.KeyManager;
+import javax.net.ssl.KeyManagerFactory;
+import javax.net.ssl.SSLContext;
+
+import org.apache.http.impl.DefaultConnectionReuseStrategy;
+import org.apache.http.impl.DefaultHttpResponseFactory;
+import org.apache.http.impl.nio.reactor.DefaultListeningIOReactor;
+import org.apache.http.impl.nio.reactor.SSLServerIOEventDispatch;
+import org.apache.http.nio.protocol.BufferingHttpServiceHandler;
+import org.apache.http.nio.reactor.IOEventDispatch;
+import org.apache.http.nio.reactor.ListeningIOReactor;
+import org.apache.http.params.HttpConnectionParams;
+import org.apache.http.params.HttpProtocolParams;
+import org.apache.http.protocol.BasicHttpProcessor;
+import org.apache.http.protocol.HttpRequestHandler;
+import org.apache.http.protocol.HttpRequestHandlerRegistry;
+import org.apache.http.protocol.ResponseConnControl;
+import org.apache.http.protocol.ResponseContent;
+import org.apache.http.protocol.ResponseDate;
+import org.apache.http.protocol.ResponseServer;
+
+/**
+ * Trivial test server based on HttpCore NIO SSL
+ *
+ * @author Oleg Kalnichevski
+ */
+public class TestHttpSSLServer extends TestHttpSSLServiceBase {
+
+ private final SSLContext sslcontext;
+ private final HttpRequestHandlerRegistry reqistry;
+ private volatile SocketAddress address;
+ private final Object mutex;
+
+ public TestHttpSSLServer() throws Exception {
+ super();
+
+ ClassLoader cl = getClass().getClassLoader();
+ URL url = cl.getResource("test.keystore");
+ KeyStore keystore = KeyStore.getInstance("jks");
+ keystore.load(url.openStream(), "nopassword".toCharArray());
+ KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(
+ KeyManagerFactory.getDefaultAlgorithm());
+ kmfactory.init(keystore, "nopassword".toCharArray());
+ KeyManager[] keymanagers = kmfactory.getKeyManagers();
+ this.sslcontext = SSLContext.getInstance("TLS");
+ this.sslcontext.init(keymanagers, null, null);
+
+ this.params
+ .setIntParameter(HttpConnectionParams.SO_TIMEOUT, 2000)
+ .setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE, 8 * 1024)
+ .setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, false)
+ .setBooleanParameter(HttpConnectionParams.TCP_NODELAY, true)
+ .setParameter(HttpProtocolParams.ORIGIN_SERVER, "TEST-SSL-SERVER/1.1");
+
+ this.reqistry = new HttpRequestHandlerRegistry();
+ this.ioReactor = new DefaultListeningIOReactor(2, this.params);
+ this.mutex = new Object();
+ }
+
+ public void registerHandler(
+ final String pattern,
+ final HttpRequestHandler handler) {
+ this.reqistry.register(pattern, handler);
+ }
+
+ protected void execute() throws IOException {
+ synchronized (this.mutex) {
+ this.address = ((ListeningIOReactor) this.ioReactor).listen(new InetSocketAddress(0));
+ this.mutex.notifyAll();
+ }
+ BasicHttpProcessor httpproc = new BasicHttpProcessor();
+ httpproc.addInterceptor(new ResponseDate());
+ httpproc.addInterceptor(new ResponseServer());
+ httpproc.addInterceptor(new ResponseContent());
+ httpproc.addInterceptor(new ResponseConnControl());
+
+ BufferingHttpServiceHandler serviceHandler = new BufferingHttpServiceHandler(
+ httpproc,
+ new DefaultHttpResponseFactory(),
+ new DefaultConnectionReuseStrategy(),
+ this.params);
+
+ serviceHandler.setEventListener(new EventLogger());
+
+ serviceHandler.setHandlerResolver(this.reqistry);
+
+ IOEventDispatch ioEventDispatch = new SSLServerIOEventDispatch(
+ serviceHandler,
+ this.sslcontext,
+ this.params);
+ this.ioReactor.execute(ioEventDispatch);
+ }
+
+ public SocketAddress getSocketAddress() throws InterruptedException {
+ synchronized (this.mutex) {
+ while (this.address == null) {
+ this.mutex.wait();
+ }
+ }
+ return this.address;
+ }
+
+}
Propchange: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/mockup/TestHttpSSLServer.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/mockup/TestHttpSSLServer.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/mockup/TestHttpSSLServiceBase.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/mockup/TestHttpSSLServiceBase.java?view=auto&rev=505811
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/mockup/TestHttpSSLServiceBase.java (added)
+++ jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/mockup/TestHttpSSLServiceBase.java Sat Feb 10 16:37:19 2007
@@ -0,0 +1,130 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ * 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.impl.nio.mockup;
+
+import java.io.IOException;
+import java.net.InetAddress;
+
+import org.apache.http.HttpException;
+import org.apache.http.impl.params.DefaultHttpParams;
+import org.apache.http.nio.protocol.EventListener;
+import org.apache.http.nio.reactor.IOReactor;
+import org.apache.http.params.HttpParams;
+
+abstract class TestHttpSSLServiceBase {
+
+ private volatile IOReactorThread thread;
+ private volatile int connCount = 0;
+ private final Object mutex;
+
+ protected final HttpParams params;
+ protected IOReactor ioReactor;
+
+ public TestHttpSSLServiceBase() {
+ super();
+ this.mutex = new Object();
+ this.params = new DefaultHttpParams(null);
+ }
+
+ protected abstract void execute() throws IOException;
+
+ public void start() {
+ this.thread = new IOReactorThread();
+ this.thread.start();
+ }
+
+ public void shutdown() throws IOException {
+ this.ioReactor.shutdown();
+ try {
+ this.thread.join(500);
+ } catch (InterruptedException ignore) {
+ }
+ }
+
+ public int getConnCount() {
+ return this.connCount;
+ }
+
+ public HttpParams getParams() {
+ return this.params;
+ }
+
+ private void incrementConnCount() {
+ synchronized (this.mutex) {
+ this.connCount++;
+ this.mutex.notifyAll();
+ }
+ }
+
+ public void await(int connCount, long timeout) throws InterruptedException {
+ synchronized (this.mutex) {
+ while (this.connCount < connCount) {
+ this.mutex.wait(timeout);
+ }
+ }
+ }
+
+ private class IOReactorThread extends Thread {
+
+ public void run() {
+ try {
+ execute();
+ } catch (IOException ex) {
+ ex.printStackTrace();
+ }
+ }
+
+ }
+
+ protected class EventLogger implements EventListener {
+
+ public void connectionOpen(final InetAddress address) {
+ }
+
+ public void connectionTimeout(final InetAddress address) {
+ }
+
+ public void connectionClosed(InetAddress address) {
+ incrementConnCount();
+ }
+
+ public void fatalIOException(IOException ex) {
+ ex.printStackTrace();
+ }
+
+ public void fatalProtocolException(HttpException ex) {
+ ex.printStackTrace();
+ }
+
+ }
+
+}
Propchange: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/mockup/TestHttpSSLServiceBase.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/mockup/TestHttpSSLServiceBase.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestAll.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestAll.java?view=auto&rev=505811
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestAll.java (added)
+++ jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestAll.java Sat Feb 10 16:37:19 2007
@@ -0,0 +1,52 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ * ====================================================================
+ * 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.impl.nio.reactor;
+
+import junit.framework.*;
+
+public class TestAll extends TestCase {
+
+ public TestAll(String testName) {
+ super(testName);
+ }
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite();
+ suite.addTest(TestNIOSSLHttp.suite());
+ return suite;
+ }
+
+ public static void main(String args[]) {
+ String[] testCaseName = { TestAll.class.getName() };
+ junit.textui.TestRunner.main(testCaseName);
+ }
+
+}
Propchange: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestAll.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestAll.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestNIOSSLHttp.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestNIOSSLHttp.java?view=auto&rev=505811
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestNIOSSLHttp.java (added)
+++ jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestNIOSSLHttp.java Sat Feb 10 16:37:19 2007
@@ -0,0 +1,682 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ * ====================================================================
+ * 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.impl.nio.reactor;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpEntityEnclosingRequest;
+import org.apache.http.HttpException;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpVersion;
+import org.apache.http.entity.ByteArrayEntity;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.nio.mockup.TestHttpSSLClient;
+import org.apache.http.impl.nio.mockup.TestHttpSSLServer;
+import org.apache.http.message.HttpGet;
+import org.apache.http.message.HttpPost;
+import org.apache.http.nio.NHttpConnection;
+import org.apache.http.nio.protocol.HttpRequestExecutionHandler;
+import org.apache.http.params.HttpProtocolParams;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.HttpExecutionContext;
+import org.apache.http.protocol.HttpRequestHandler;
+import org.apache.http.util.EntityUtils;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * HttpCore NIO SSL tests.
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ *
+ * @version $Id$
+ */
+public class TestNIOSSLHttp extends TestCase {
+
+ // ------------------------------------------------------------ Constructor
+ public TestNIOSSLHttp(String testName) {
+ super(testName);
+ }
+
+ // ------------------------------------------------------------------- Main
+ public static void main(String args[]) {
+ String[] testCaseName = { TestNIOSSLHttp.class.getName() };
+ junit.textui.TestRunner.main(testCaseName);
+ }
+
+ // ------------------------------------------------------- TestCase Methods
+
+ public static Test suite() {
+ return new TestSuite(TestNIOSSLHttp.class);
+ }
+
+ private TestHttpSSLServer server;
+ private TestHttpSSLClient client;
+
+ protected void setUp() throws Exception {
+ this.server = new TestHttpSSLServer();
+ this.client = new TestHttpSSLClient();
+ }
+
+ protected void tearDown() throws Exception {
+ this.server.shutdown();
+ this.client.shutdown();
+ }
+
+ /**
+ * This test case executes a series of simple (non-pipelined) GET requests
+ * over multiple connections.
+ */
+ @SuppressWarnings("unchecked")
+ public void testSimpleHttpGets() throws Exception {
+
+ final int connNo = 3;
+ final int reqNo = 20;
+
+ Random rnd = new Random();
+
+ // Prepare some random data
+ final List testData = new ArrayList(reqNo);
+ for (int i = 0; i < reqNo; i++) {
+ int size = rnd.nextInt(5000);
+ byte[] data = new byte[size];
+ rnd.nextBytes(data);
+ testData.add(data);
+ }
+
+ List[] responseData = new List[connNo];
+ for (int i = 0; i < responseData.length; i++) {
+ responseData[i] = new ArrayList();
+ }
+
+ // Initialize the server-side request handler
+ this.server.registerHandler("*", new HttpRequestHandler() {
+
+ public void handle(
+ final HttpRequest request,
+ final HttpResponse response,
+ final HttpContext context) throws HttpException, IOException {
+
+ String s = request.getRequestLine().getUri();
+ URI uri;
+ try {
+ uri = new URI(s);
+ } catch (URISyntaxException ex) {
+ throw new HttpException("Invalid request URI: " + s);
+ }
+ int index = Integer.parseInt(uri.getQuery());
+ byte[] data = (byte []) testData.get(index);
+ ByteArrayEntity entity = new ByteArrayEntity(data);
+ response.setEntity(entity);
+ }
+
+ });
+
+ // Initialize the client side request executor
+ this.client.setHttpRequestExecutionHandler(new HttpRequestExecutionHandler() {
+
+ public void initalizeContext(final HttpContext context, final Object attachment) {
+ context.setAttribute("LIST", (List) attachment);
+ context.setAttribute("STATUS", "ready");
+ }
+
+ public HttpRequest submitRequest(final HttpContext context) {
+ NHttpConnection conn = (NHttpConnection) context.getAttribute(
+ HttpExecutionContext.HTTP_CONNECTION);
+ String status = (String) context.getAttribute("STATUS");
+ if (!status.equals("ready")) {
+ return null;
+ }
+ int index = 0;
+
+ Integer intobj = (Integer) context.getAttribute("INDEX");
+ if (intobj != null) {
+ index = intobj.intValue();
+ }
+
+ HttpGet get = null;
+ if (index < reqNo) {
+ get = new HttpGet("/?" + index);
+ context.setAttribute("INDEX", new Integer(index + 1));
+ context.setAttribute("STATUS", "busy");
+ } else {
+ try {
+ conn.close();
+ } catch (IOException ex) {
+ ex.printStackTrace();
+ }
+ }
+
+ return get;
+ }
+
+ public void handleResponse(final HttpResponse response, final HttpContext context) {
+ NHttpConnection conn = (NHttpConnection) context.getAttribute(
+ HttpExecutionContext.HTTP_CONNECTION);
+
+ List list = (List) context.getAttribute("LIST");
+ try {
+ HttpEntity entity = response.getEntity();
+ byte[] data = EntityUtils.toByteArray(entity);
+ list.add(data);
+ } catch (IOException ex) {
+ fail(ex.getMessage());
+ }
+
+ context.setAttribute("STATUS", "ready");
+ conn.requestInput();
+ }
+
+ });
+
+ this.server.start();
+ this.client.start();
+
+ InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
+
+ for (int i = 0; i < responseData.length; i++) {
+ this.client.openConnection(
+ new InetSocketAddress("localhost", serverAddress.getPort()),
+ responseData[i]);
+ }
+
+ this.client.await(connNo, 1000);
+ assertEquals(connNo, this.client.getConnCount());
+
+ this.server.await(connNo, 1000);
+ assertEquals(connNo, this.server.getConnCount());
+
+ for (int c = 0; c < responseData.length; c++) {
+ List receivedPackets = responseData[c];
+ List expectedPackets = testData;
+ assertEquals(receivedPackets.size(), expectedPackets.size());
+ for (int p = 0; p < testData.size(); p++) {
+ byte[] expected = (byte[]) testData.get(p);
+ byte[] received = (byte[]) receivedPackets.get(p);
+
+ assertEquals(expected.length, received.length);
+ for (int i = 0; i < expected.length; i++) {
+ assertEquals(expected[i], received[i]);
+ }
+ }
+ }
+
+ }
+
+ /**
+ * This test case executes a series of simple (non-pipelined) POST requests
+ * with content length delimited content over multiple connections.
+ */
+ @SuppressWarnings("unchecked")
+ public void testSimpleHttpPostsWithContentLength() throws Exception {
+
+ final int connNo = 3;
+ final int reqNo = 20;
+
+ Random rnd = new Random();
+
+ // Prepare some random data
+ final List testData = new ArrayList(reqNo);
+ for (int i = 0; i < reqNo; i++) {
+ int size = rnd.nextInt(5000);
+ byte[] data = new byte[size];
+ rnd.nextBytes(data);
+ testData.add(data);
+ }
+
+ List[] responseData = new List[connNo];
+ for (int i = 0; i < responseData.length; i++) {
+ responseData[i] = new ArrayList();
+ }
+
+ // Initialize the server-side request handler
+ this.server.registerHandler("*", new HttpRequestHandler() {
+
+ public void handle(
+ final HttpRequest request,
+ final HttpResponse response,
+ final HttpContext context) throws HttpException, IOException {
+
+ if (request instanceof HttpEntityEnclosingRequest) {
+ HttpEntity incoming = ((HttpEntityEnclosingRequest) request).getEntity();
+ byte[] data = EntityUtils.toByteArray(incoming);
+
+ ByteArrayEntity outgoing = new ByteArrayEntity(data);
+ outgoing.setChunked(false);
+ response.setEntity(outgoing);
+ } else {
+ StringEntity outgoing = new StringEntity("No content");
+ response.setEntity(outgoing);
+ }
+ }
+
+ });
+
+ // Initialize the client side request executor
+ this.client.setHttpRequestExecutionHandler(new HttpRequestExecutionHandler() {
+
+ public void initalizeContext(final HttpContext context, final Object attachment) {
+ context.setAttribute("LIST", (List) attachment);
+ context.setAttribute("STATUS", "ready");
+ }
+
+ public HttpRequest submitRequest(final HttpContext context) {
+ NHttpConnection conn = (NHttpConnection) context.getAttribute(
+ HttpExecutionContext.HTTP_CONNECTION);
+ String status = (String) context.getAttribute("STATUS");
+ if (!status.equals("ready")) {
+ return null;
+ }
+ int index = 0;
+
+ Integer intobj = (Integer) context.getAttribute("INDEX");
+ if (intobj != null) {
+ index = intobj.intValue();
+ }
+
+ HttpPost post = null;
+ if (index < reqNo) {
+ post = new HttpPost("/?" + index);
+ byte[] data = (byte[]) testData.get(index);
+ ByteArrayEntity outgoing = new ByteArrayEntity(data);
+ post.setEntity(outgoing);
+
+ context.setAttribute("INDEX", new Integer(index + 1));
+ context.setAttribute("STATUS", "busy");
+ } else {
+ try {
+ conn.close();
+ } catch (IOException ex) {
+ ex.printStackTrace();
+ }
+ }
+
+ return post;
+ }
+
+ public void handleResponse(final HttpResponse response, final HttpContext context) {
+ NHttpConnection conn = (NHttpConnection) context.getAttribute(
+ HttpExecutionContext.HTTP_CONNECTION);
+
+ List list = (List) context.getAttribute("LIST");
+ try {
+ HttpEntity entity = response.getEntity();
+ byte[] data = EntityUtils.toByteArray(entity);
+ list.add(data);
+ } catch (IOException ex) {
+ fail(ex.getMessage());
+ }
+
+ context.setAttribute("STATUS", "ready");
+ conn.requestInput();
+ }
+
+ });
+
+ this.server.start();
+ this.client.start();
+
+ InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
+
+ for (int i = 0; i < responseData.length; i++) {
+ this.client.openConnection(
+ new InetSocketAddress("localhost", serverAddress.getPort()),
+ responseData[i]);
+ }
+
+ this.client.await(connNo, 1000);
+ assertEquals(connNo, this.client.getConnCount());
+
+ this.server.await(connNo, 1000);
+ assertEquals(connNo, this.server.getConnCount());
+
+ for (int c = 0; c < responseData.length; c++) {
+ List receivedPackets = responseData[c];
+ List expectedPackets = testData;
+ assertEquals(receivedPackets.size(), expectedPackets.size());
+ for (int p = 0; p < testData.size(); p++) {
+ byte[] expected = (byte[]) testData.get(p);
+ byte[] received = (byte[]) receivedPackets.get(p);
+
+ assertEquals(expected.length, received.length);
+ for (int i = 0; i < expected.length; i++) {
+ assertEquals(expected[i], received[i]);
+ }
+ }
+ }
+
+ }
+
+ /**
+ * This test case executes a series of simple (non-pipelined) POST requests
+ * with chunk coded content content over multiple connections.
+ */
+ @SuppressWarnings("unchecked")
+ public void testSimpleHttpPostsChunked() throws Exception {
+
+ final int connNo = 3;
+ final int reqNo = 20;
+
+ Random rnd = new Random();
+
+ // Prepare some random data
+ final List testData = new ArrayList(reqNo);
+ for (int i = 0; i < reqNo; i++) {
+ int size = rnd.nextInt(20000);
+ byte[] data = new byte[size];
+ rnd.nextBytes(data);
+ testData.add(data);
+ }
+
+ List[] responseData = new List[connNo];
+ for (int i = 0; i < responseData.length; i++) {
+ responseData[i] = new ArrayList();
+ }
+
+ // Initialize the server-side request handler
+ this.server.registerHandler("*", new HttpRequestHandler() {
+
+ public void handle(
+ final HttpRequest request,
+ final HttpResponse response,
+ final HttpContext context) throws HttpException, IOException {
+
+ if (request instanceof HttpEntityEnclosingRequest) {
+ HttpEntity incoming = ((HttpEntityEnclosingRequest) request).getEntity();
+ byte[] data = EntityUtils.toByteArray(incoming);
+ ByteArrayEntity outgoing = new ByteArrayEntity(data);
+ outgoing.setChunked(true);
+ response.setEntity(outgoing);
+ } else {
+ StringEntity outgoing = new StringEntity("No content");
+ response.setEntity(outgoing);
+ }
+ }
+
+ });
+
+ // Initialize the client side request executor
+ this.client.setHttpRequestExecutionHandler(new HttpRequestExecutionHandler() {
+
+ public void initalizeContext(final HttpContext context, final Object attachment) {
+ context.setAttribute("LIST", (List) attachment);
+ context.setAttribute("STATUS", "ready");
+ }
+
+ public HttpRequest submitRequest(final HttpContext context) {
+ NHttpConnection conn = (NHttpConnection) context.getAttribute(
+ HttpExecutionContext.HTTP_CONNECTION);
+ String status = (String) context.getAttribute("STATUS");
+ if (!status.equals("ready")) {
+ return null;
+ }
+ int index = 0;
+
+ Integer intobj = (Integer) context.getAttribute("INDEX");
+ if (intobj != null) {
+ index = intobj.intValue();
+ }
+
+ HttpPost post = null;
+ if (index < reqNo) {
+ post = new HttpPost("/?" + index);
+ byte[] data = (byte[]) testData.get(index);
+ ByteArrayEntity outgoing = new ByteArrayEntity(data);
+ outgoing.setChunked(true);
+ post.setEntity(outgoing);
+
+ context.setAttribute("INDEX", new Integer(index + 1));
+ context.setAttribute("STATUS", "busy");
+ } else {
+ try {
+ conn.close();
+ } catch (IOException ex) {
+ ex.printStackTrace();
+ }
+ }
+
+ return post;
+ }
+
+ public void handleResponse(final HttpResponse response, final HttpContext context) {
+ NHttpConnection conn = (NHttpConnection) context.getAttribute(
+ HttpExecutionContext.HTTP_CONNECTION);
+
+ List list = (List) context.getAttribute("LIST");
+
+ try {
+ HttpEntity entity = response.getEntity();
+ byte[] data = EntityUtils.toByteArray(entity);
+ list.add(data);
+ } catch (IOException ex) {
+ fail(ex.getMessage());
+ }
+
+ context.setAttribute("STATUS", "ready");
+ conn.requestInput();
+ }
+
+ });
+
+ this.server.start();
+ this.client.start();
+
+ InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
+
+ for (int i = 0; i < responseData.length; i++) {
+ this.client.openConnection(
+ new InetSocketAddress("localhost", serverAddress.getPort()),
+ responseData[i]);
+ }
+
+ this.client.await(connNo, 1000);
+ assertEquals(connNo, this.client.getConnCount());
+
+ this.server.await(connNo, 1000);
+ assertEquals(connNo, this.server.getConnCount());
+
+ for (int c = 0; c < responseData.length; c++) {
+ List receivedPackets = responseData[c];
+ List expectedPackets = testData;
+ assertEquals(receivedPackets.size(), expectedPackets.size());
+ for (int p = 0; p < testData.size(); p++) {
+ byte[] expected = (byte[]) testData.get(p);
+ byte[] received = (byte[]) receivedPackets.get(p);
+
+ assertEquals(expected.length, received.length);
+ for (int i = 0; i < expected.length; i++) {
+ assertEquals(expected[i], received[i]);
+ }
+ }
+ }
+
+ }
+
+ /**
+ * This test case executes a series of simple (non-pipelined) HTTP/1.0
+ * POST requests over multiple persistent connections.
+ */
+ @SuppressWarnings("unchecked")
+ public void testSimpleHttpPostsHTTP10() throws Exception {
+
+ final int connNo = 3;
+ final int reqNo = 20;
+
+ Random rnd = new Random();
+
+ // Prepare some random data
+ final List testData = new ArrayList(reqNo);
+ for (int i = 0; i < reqNo; i++) {
+ int size = rnd.nextInt(5000);
+ byte[] data = new byte[size];
+ rnd.nextBytes(data);
+ testData.add(data);
+ }
+
+ List[] responseData = new List[connNo];
+ for (int i = 0; i < responseData.length; i++) {
+ responseData[i] = new ArrayList();
+ }
+
+ // Initialize the server-side request handler
+ this.server.registerHandler("*", new HttpRequestHandler() {
+
+
+ public void handle(
+ final HttpRequest request,
+ final HttpResponse response,
+ final HttpContext context) throws HttpException, IOException {
+
+ if (request instanceof HttpEntityEnclosingRequest) {
+ HttpEntity incoming = ((HttpEntityEnclosingRequest) request).getEntity();
+ byte[] data = EntityUtils.toByteArray(incoming);
+
+ ByteArrayEntity outgoing = new ByteArrayEntity(data);
+ outgoing.setChunked(false);
+ response.setEntity(outgoing);
+ } else {
+ StringEntity outgoing = new StringEntity("No content");
+ response.setEntity(outgoing);
+ }
+ }
+
+ });
+
+ // Initialize the client side request executor
+ // Set protocol level to HTTP/1.0
+ this.client.getParams().setParameter(
+ HttpProtocolParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_0);
+ this.client.setHttpRequestExecutionHandler(new HttpRequestExecutionHandler() {
+
+ public void initalizeContext(final HttpContext context, final Object attachment) {
+ context.setAttribute("LIST", (List) attachment);
+ context.setAttribute("STATUS", "ready");
+ }
+
+ public HttpRequest submitRequest(final HttpContext context) {
+ NHttpConnection conn = (NHttpConnection) context.getAttribute(
+ HttpExecutionContext.HTTP_CONNECTION);
+ String status = (String) context.getAttribute("STATUS");
+ if (!status.equals("ready")) {
+ return null;
+ }
+ int index = 0;
+
+ Integer intobj = (Integer) context.getAttribute("INDEX");
+ if (intobj != null) {
+ index = intobj.intValue();
+ }
+
+ HttpPost post = null;
+ if (index < reqNo) {
+ post = new HttpPost("/?" + index);
+ byte[] data = (byte[]) testData.get(index);
+ ByteArrayEntity outgoing = new ByteArrayEntity(data);
+ post.setEntity(outgoing);
+
+ context.setAttribute("INDEX", new Integer(index + 1));
+ context.setAttribute("STATUS", "busy");
+ } else {
+ try {
+ conn.close();
+ } catch (IOException ex) {
+ ex.printStackTrace();
+ }
+ }
+
+ return post;
+ }
+
+ public void handleResponse(final HttpResponse response, final HttpContext context) {
+ NHttpConnection conn = (NHttpConnection) context.getAttribute(
+ HttpExecutionContext.HTTP_CONNECTION);
+
+ List list = (List) context.getAttribute("LIST");
+ try {
+ HttpEntity entity = response.getEntity();
+ byte[] data = EntityUtils.toByteArray(entity);
+ list.add(data);
+ } catch (IOException ex) {
+ fail(ex.getMessage());
+ }
+
+ context.setAttribute("STATUS", "ready");
+ conn.requestInput();
+ }
+
+ });
+
+ this.server.start();
+ this.client.start();
+
+ InetSocketAddress serverAddress = (InetSocketAddress) this.server.getSocketAddress();
+
+ for (int i = 0; i < responseData.length; i++) {
+ this.client.openConnection(
+ new InetSocketAddress("localhost", serverAddress.getPort()),
+ responseData[i]);
+ }
+
+ this.client.await(connNo, 1000);
+ assertEquals(connNo, this.client.getConnCount());
+
+ this.server.await(connNo, 1000);
+ assertEquals(connNo, this.server.getConnCount());
+
+ for (int c = 0; c < responseData.length; c++) {
+ List receivedPackets = responseData[c];
+ List expectedPackets = testData;
+ assertEquals(receivedPackets.size(), expectedPackets.size());
+ for (int p = 0; p < testData.size(); p++) {
+ byte[] expected = (byte[]) testData.get(p);
+ byte[] received = (byte[]) receivedPackets.get(p);
+
+ assertEquals(expected.length, received.length);
+ for (int i = 0; i < expected.length; i++) {
+ assertEquals(expected[i], received[i]);
+ }
+ }
+ }
+
+ }
+
+
+}
Propchange: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestNIOSSLHttp.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpcomponents/httpcore/trunk/module-niossl/src/test/java/org/apache/http/impl/nio/reactor/TestNIOSSLHttp.java
------------------------------------------------------------------------------
svn:mime-type = text/plain