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/22 18:39:57 UTC
svn commit: r510602 - in
/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http:
mockup/TestHttpClient.java mockup/TestHttpServer.java
protocol/TestAllProtocol.java protocol/TestHttpServiceAndExecutor.java
Author: olegk
Date: Thu Feb 22 09:39:56 2007
New Revision: 510602
URL: http://svn.apache.org/viewvc?view=rev&rev=510602
Log:
HTTPCORE-1: Added simple test server and test client based on HttpCore
Added:
jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/mockup/TestHttpClient.java (with props)
jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/mockup/TestHttpServer.java (with props)
jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/protocol/TestHttpServiceAndExecutor.java (with props)
Modified:
jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/protocol/TestAllProtocol.java
Added: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/mockup/TestHttpClient.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/mockup/TestHttpClient.java?view=auto&rev=510602
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/mockup/TestHttpClient.java (added)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/mockup/TestHttpClient.java Thu Feb 22 09:39:56 2007
@@ -0,0 +1,103 @@
+/*
+ * $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.mockup;
+
+import java.io.IOException;
+
+import org.apache.http.ConnectionReuseStrategy;
+import org.apache.http.HttpClientConnection;
+import org.apache.http.HttpException;
+import org.apache.http.HttpHost;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpVersion;
+import org.apache.http.impl.DefaultConnectionReuseStrategy;
+import org.apache.http.params.BasicHttpParams;
+import org.apache.http.params.HttpParams;
+import org.apache.http.params.HttpProtocolParams;
+import org.apache.http.protocol.BasicHttpProcessor;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.HttpExecutionContext;
+import org.apache.http.protocol.HttpRequestExecutor;
+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 TestHttpClient {
+
+ private final HttpParams params;
+ private final BasicHttpProcessor httpproc;
+ private final HttpRequestExecutor httpexecutor;
+ private final ConnectionReuseStrategy connStrategy;
+ private final HttpContext context;
+
+ public TestHttpClient() {
+ super();
+ this.params = new BasicHttpParams(null);
+ this.params
+ .setParameter(HttpProtocolParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1)
+ .setParameter(HttpProtocolParams.USER_AGENT, "TEST-CLIENT/1.1");
+
+ this.httpproc = new BasicHttpProcessor();
+ // Required protocol interceptors
+ this.httpproc.addInterceptor(new RequestContent());
+ this.httpproc.addInterceptor(new RequestTargetHost());
+ // Recommended protocol interceptors
+ this.httpproc.addInterceptor(new RequestConnControl());
+ this.httpproc.addInterceptor(new RequestUserAgent());
+ this.httpproc.addInterceptor(new RequestExpectContinue());
+
+ this.httpexecutor = new HttpRequestExecutor(this.httpproc);
+ this.httpexecutor.setParams(this.params);
+ this.connStrategy = new DefaultConnectionReuseStrategy();
+ this.context = new HttpExecutionContext(null);
+ }
+
+ public HttpParams getParams() {
+ return this.params;
+ }
+
+ public HttpResponse execute(
+ final HttpRequest request,
+ final HttpHost targetHost,
+ final HttpClientConnection conn) throws HttpException, IOException {
+ this.context.setAttribute(HttpExecutionContext.HTTP_TARGET_HOST, targetHost);
+ return this.httpexecutor.execute(request, conn, this.context);
+ }
+
+ public boolean keepAlive(final HttpResponse response) {
+ return this.connStrategy.keepAlive(response, this.context);
+ }
+
+}
Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/mockup/TestHttpClient.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/mockup/TestHttpClient.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/mockup/TestHttpServer.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/mockup/TestHttpServer.java?view=auto&rev=510602
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/mockup/TestHttpServer.java (added)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/mockup/TestHttpServer.java Thu Feb 22 09:39:56 2007
@@ -0,0 +1,206 @@
+/*
+ * $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.mockup;
+
+import java.io.IOException;
+import java.io.InterruptedIOException;
+import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+
+import org.apache.http.ConnectionClosedException;
+import org.apache.http.ConnectionReuseStrategy;
+import org.apache.http.HttpException;
+import org.apache.http.HttpResponseFactory;
+import org.apache.http.HttpServerConnection;
+import org.apache.http.impl.DefaultConnectionReuseStrategy;
+import org.apache.http.impl.DefaultHttpResponseFactory;
+import org.apache.http.impl.DefaultHttpServerConnection;
+import org.apache.http.params.BasicHttpParams;
+import org.apache.http.params.HttpConnectionParams;
+import org.apache.http.params.HttpParams;
+import org.apache.http.params.HttpProtocolParams;
+import org.apache.http.protocol.BasicHttpProcessor;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.HttpExecutionContext;
+import org.apache.http.protocol.HttpExpectationVerifier;
+import org.apache.http.protocol.HttpRequestHandler;
+import org.apache.http.protocol.HttpRequestHandlerRegistry;
+import org.apache.http.protocol.HttpService;
+import org.apache.http.protocol.ResponseConnControl;
+import org.apache.http.protocol.ResponseContent;
+import org.apache.http.protocol.ResponseDate;
+import org.apache.http.protocol.ResponseServer;
+
+public class TestHttpServer {
+
+ private final HttpParams params;
+ private final BasicHttpProcessor httpproc;
+ private final ConnectionReuseStrategy connStrategy;
+ private final HttpResponseFactory responseFactory;
+ private final HttpRequestHandlerRegistry reqistry;
+ private final ServerSocket serversocket;
+
+ private HttpExpectationVerifier expectationVerifier;
+
+ private Thread listener;
+ private volatile boolean shutdown;
+
+ public TestHttpServer() throws IOException {
+ super();
+ this.params = new BasicHttpParams();
+ this.params
+ .setIntParameter(HttpConnectionParams.SO_TIMEOUT, 5000)
+ .setIntParameter(HttpConnectionParams.SOCKET_BUFFER_SIZE, 8 * 1024)
+ .setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, false)
+ .setBooleanParameter(HttpConnectionParams.TCP_NODELAY, true)
+ .setParameter(HttpProtocolParams.ORIGIN_SERVER, "TEST-SERVER/1.1");
+ this.httpproc = new BasicHttpProcessor();
+ this.httpproc.addInterceptor(new ResponseDate());
+ this.httpproc.addInterceptor(new ResponseServer());
+ this.httpproc.addInterceptor(new ResponseContent());
+ this.httpproc.addInterceptor(new ResponseConnControl());
+ this.connStrategy = new DefaultConnectionReuseStrategy();
+ this.responseFactory = new DefaultHttpResponseFactory();
+ this.reqistry = new HttpRequestHandlerRegistry();
+ this.serversocket = new ServerSocket(0);
+ }
+
+ public void registerHandler(
+ final String pattern,
+ final HttpRequestHandler handler) {
+ this.reqistry.register(pattern, handler);
+ }
+
+ public void setExpectationVerifier(final HttpExpectationVerifier expectationVerifier) {
+ this.expectationVerifier = expectationVerifier;
+ }
+
+ private HttpServerConnection acceptConnection() throws IOException {
+ Socket socket = this.serversocket.accept();
+ DefaultHttpServerConnection conn = new DefaultHttpServerConnection();
+ conn.bind(socket, this.params);
+ return conn;
+ }
+
+ public int getPort() {
+ return this.serversocket.getLocalPort();
+ }
+
+ public InetAddress getInetAddress() {
+ return this.serversocket.getInetAddress();
+ }
+
+ public void start() {
+ if (this.listener != null) {
+ throw new IllegalStateException("Listener already running");
+ }
+ this.listener = new Thread(new Runnable() {
+
+ public void run() {
+ while (!shutdown && !Thread.interrupted()) {
+ try {
+ // Set up HTTP connection
+ HttpServerConnection conn = acceptConnection();
+ // Set up the HTTP service
+ HttpService httpService = new HttpService(
+ httpproc,
+ connStrategy,
+ responseFactory);
+ httpService.setParams(params);
+ httpService.setExpectationVerifier(expectationVerifier);
+ httpService.setHandlerResolver(reqistry);
+
+ // Start worker thread
+ Thread t = new WorkerThread(httpService, conn);
+ t.setDaemon(true);
+ t.start();
+ } catch (InterruptedIOException ex) {
+ break;
+ } catch (IOException e) {
+ break;
+ }
+ }
+ }
+
+ });
+ this.listener.start();
+ }
+
+ public void shutdown() {
+ if (this.shutdown) {
+ return;
+ }
+ this.shutdown = true;
+ try {
+ this.serversocket.close();
+ } catch (IOException ignore) {}
+ this.listener.interrupt();
+ try {
+ this.listener.join(1000);
+ } catch (InterruptedException ignore) {}
+ }
+
+ static class WorkerThread extends Thread {
+
+ private final HttpService httpservice;
+ private final HttpServerConnection conn;
+
+ public WorkerThread(
+ final HttpService httpservice,
+ final HttpServerConnection conn) {
+ super();
+ this.httpservice = httpservice;
+ this.conn = conn;
+ }
+
+ public void run() {
+ HttpContext context = new HttpExecutionContext(null);
+ try {
+ while (!Thread.interrupted() && this.conn.isOpen()) {
+ this.httpservice.handleRequest(this.conn, context);
+ }
+ } catch (ConnectionClosedException ex) {
+ } catch (IOException ex) {
+ System.err.println("I/O error: " + ex.getMessage());
+ } catch (HttpException ex) {
+ System.err.println("Unrecoverable HTTP protocol violation: " + ex.getMessage());
+ } finally {
+ try {
+ this.conn.shutdown();
+ } catch (IOException ignore) {}
+ }
+ }
+
+ }
+
+}
Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/mockup/TestHttpServer.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/mockup/TestHttpServer.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/protocol/TestAllProtocol.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/protocol/TestAllProtocol.java?view=diff&rev=510602&r1=510601&r2=510602
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/protocol/TestAllProtocol.java (original)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/protocol/TestAllProtocol.java Thu Feb 22 09:39:56 2007
@@ -43,6 +43,7 @@
suite.addTest(TestHttpExecutionContext.suite());
suite.addTest(TestStandardInterceptors.suite());
suite.addTest(TestHttpRequestHandlerRegistry.suite());
+ suite.addTest(TestHttpServiceAndExecutor.suite());
return suite;
}
Added: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/protocol/TestHttpServiceAndExecutor.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/protocol/TestHttpServiceAndExecutor.java?view=auto&rev=510602
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/protocol/TestHttpServiceAndExecutor.java (added)
+++ jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/protocol/TestHttpServiceAndExecutor.java Thu Feb 22 09:39:56 2007
@@ -0,0 +1,152 @@
+/*
+ * $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.protocol;
+
+import java.io.IOException;
+import java.net.Socket;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+
+import org.apache.http.HttpException;
+import org.apache.http.HttpHost;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.entity.ByteArrayEntity;
+import org.apache.http.impl.DefaultHttpClientConnection;
+import org.apache.http.message.HttpGet;
+import org.apache.http.mockup.TestHttpClient;
+import org.apache.http.mockup.TestHttpServer;
+import org.apache.http.util.EntityUtils;
+
+import junit.framework.*;
+
+public class TestHttpServiceAndExecutor extends TestCase {
+
+ // ------------------------------------------------------------ Constructor
+ public TestHttpServiceAndExecutor(String testName) {
+ super(testName);
+ }
+
+ // ------------------------------------------------------------------- Main
+ public static void main(String args[]) {
+ String[] testCaseName = { TestHttpServiceAndExecutor.class.getName() };
+ junit.textui.TestRunner.main(testCaseName);
+ }
+
+ // ------------------------------------------------------- TestCase Methods
+
+ public static Test suite() {
+ return new TestSuite(TestHttpServiceAndExecutor.class);
+ }
+
+ private TestHttpServer server;
+ private TestHttpClient client;
+
+ protected void setUp() throws Exception {
+ this.server = new TestHttpServer();
+ this.client = new TestHttpClient();
+ }
+
+ protected void tearDown() throws Exception {
+ this.server.shutdown();
+ }
+
+ /**
+ * This test case executes a series of simple GET requests
+ */
+ public void testSimpleHttpGets() throws Exception {
+
+ 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);
+ }
+
+ // 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();
+ if (s.startsWith("/?")) {
+ s = s.substring(2);
+ }
+ int index = Integer.parseInt(s);
+ byte[] data = (byte []) testData.get(index);
+ ByteArrayEntity entity = new ByteArrayEntity(data);
+ response.setEntity(entity);
+ }
+
+ });
+
+ this.server.start();
+
+ DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
+ HttpHost host = new HttpHost("localhost", this.server.getPort());
+
+ try {
+ for (int r = 0; r < reqNo; r++) {
+ if (!conn.isOpen()) {
+ Socket socket = new Socket(host.getHostName(), host.getPort());
+ conn.bind(socket, this.client.getParams());
+ }
+
+ HttpGet get = new HttpGet("/?" + r);
+ HttpResponse response = this.client.execute(get, host, conn);
+ byte[] received = EntityUtils.toByteArray(response.getEntity());
+ byte[] expected = (byte[]) testData.get(r);
+
+ assertEquals(expected.length, received.length);
+ for (int i = 0; i < expected.length; i++) {
+ assertEquals(expected[i], received[i]);
+ }
+ if (!this.client.keepAlive(response)) {
+ conn.close();
+ }
+ }
+ } finally {
+ conn.close();
+ this.server.shutdown();
+ }
+ }
+
+}
Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/protocol/TestHttpServiceAndExecutor.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpcomponents/httpcore/trunk/module-main/src/test/java/org/apache/http/protocol/TestHttpServiceAndExecutor.java
------------------------------------------------------------------------------
svn:mime-type = text/plain