hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1143475 - in /httpcomponents/httpcore/trunk: httpcore-nio/src/main/java/org/apache/http/nio/protocol/ httpcore/src/main/java/org/apache/http/protocol/ httpcore/src/test/java/org/apache/http/protocol/
Date Wed, 06 Jul 2011 15:56:42 GMT
Author: olegk
Date: Wed Jul  6 15:56:41 2011
New Revision: 1143475

URL: http://svn.apache.org/viewvc?rev=1143475&view=rev
Log:
Generified UriPatternMatcher; added unit tests for HttpService and HttpRequestExecutor

Added:
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpRequestExecutor.java   (with props)
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpService.java   (with props)
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestProtocolIntegration.java
      - copied, changed from r1143472, httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpServiceAndExecutor.java
Removed:
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpServiceAndExecutor.java
Modified:
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/NHttpRequestHandlerRegistry.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpRequestExecutor.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpRequestHandlerRegistry.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpService.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/UriPatternMatcher.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpRequestHandlerRegistry.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestStandardInterceptors.java

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/NHttpRequestHandlerRegistry.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/NHttpRequestHandlerRegistry.java?rev=1143475&r1=1143474&r2=1143475&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/NHttpRequestHandlerRegistry.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/NHttpRequestHandlerRegistry.java Wed Jul  6 15:56:41 2011
@@ -50,10 +50,10 @@ import org.apache.http.protocol.UriPatte
  */
 public class NHttpRequestHandlerRegistry implements NHttpRequestHandlerResolver {
 
-    private final UriPatternMatcher matcher;
+    private final UriPatternMatcher<NHttpRequestHandler> matcher;
 
     public NHttpRequestHandlerRegistry() {
-        matcher = new UriPatternMatcher();
+        matcher = new UriPatternMatcher<NHttpRequestHandler>();
     }
 
     /**
@@ -80,12 +80,12 @@ public class NHttpRequestHandlerRegistry
      * Sets handlers from the given map.
      * @param map the map containing handlers keyed by their URI patterns.
      */
-    public void setHandlers(final Map<String, Object> map) {
+    public void setHandlers(final Map<String, NHttpRequestHandler> map) {
         matcher.setObjects(map);
     }
 
     public NHttpRequestHandler lookup(String requestURI) {
-        return (NHttpRequestHandler) matcher.lookup(requestURI);
+        return matcher.lookup(requestURI);
     }
 
 }

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpRequestExecutor.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpRequestExecutor.java?rev=1143475&r1=1143474&r2=1143475&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpRequestExecutor.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpRequestExecutor.java Wed Jul  6 15:56:41 2011
@@ -28,7 +28,6 @@
 package org.apache.http.protocol;
 
 import java.io.IOException;
-import java.net.ProtocolException;
 
 import org.apache.http.HttpClientConnection;
 import org.apache.http.HttpEntity;
@@ -38,6 +37,7 @@ import org.apache.http.HttpRequest;
 import org.apache.http.HttpResponse;
 import org.apache.http.HttpStatus;
 import org.apache.http.HttpVersion;
+import org.apache.http.ProtocolException;
 import org.apache.http.ProtocolVersion;
 import org.apache.http.annotation.Immutable;
 import org.apache.http.params.CoreProtocolPNames;

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpRequestHandlerRegistry.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpRequestHandlerRegistry.java?rev=1143475&r1=1143474&r2=1143475&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpRequestHandlerRegistry.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpRequestHandlerRegistry.java Wed Jul  6 15:56:41 2011
@@ -51,10 +51,10 @@ import org.apache.http.annotation.Thread
 @ThreadSafe // provided injected dependencies are thread-safe
 public class HttpRequestHandlerRegistry implements HttpRequestHandlerResolver {
 
-    private final UriPatternMatcher matcher;
+    private final UriPatternMatcher<HttpRequestHandler> matcher;
 
     public HttpRequestHandlerRegistry() {
-        matcher = new UriPatternMatcher();
+        matcher = new UriPatternMatcher<HttpRequestHandler>();
     }
 
     /**
@@ -87,12 +87,12 @@ public class HttpRequestHandlerRegistry 
      * Sets handlers from the given map.
      * @param map the map containing handlers keyed by their URI patterns.
      */
-    public void setHandlers(final Map<String, Object> map) {
+    public void setHandlers(final Map<String, HttpRequestHandler> map) {
         matcher.setObjects(map);
     }
 
     public HttpRequestHandler lookup(final String requestURI) {
-        return (HttpRequestHandler) matcher.lookup(requestURI);
+        return matcher.lookup(requestURI);
     }
 
 }

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpService.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpService.java?rev=1143475&r1=1143474&r2=1143475&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpService.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/HttpService.java Wed Jul  6 15:56:41 2011
@@ -287,14 +287,12 @@ public class HttpService {
                 }
             }
 
+            context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
+
             if (response == null) {
                 response = this.responseFactory.newHttpResponse(ver, HttpStatus.SC_OK, context);
                 response.setParams(
                         new DefaultedHttpParams(response.getParams(), this.params));
-
-                context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
-                context.setAttribute(ExecutionContext.HTTP_RESPONSE, response);
-
                 this.processor.process(request, context);
                 doService(request, response, context);
             }
@@ -314,6 +312,8 @@ public class HttpService {
             handleException(ex, response);
         }
 
+        context.setAttribute(ExecutionContext.HTTP_RESPONSE, response);
+
         this.processor.process(response, context);
         conn.sendResponseHeader(response);
         conn.sendResponseEntity(response);
@@ -342,7 +342,11 @@ public class HttpService {
         } else {
             response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
         }
-        byte[] msg = EncodingUtils.getAsciiBytes(ex.getMessage());
+        String message = ex.getMessage();
+        if (message == null) {
+            message = ex.toString();
+        }
+        byte[] msg = EncodingUtils.getAsciiBytes(message);
         ByteArrayEntity entity = new ByteArrayEntity(msg);
         entity.setContentType("text/plain; charset=US-ASCII");
         response.setEntity(entity);

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/UriPatternMatcher.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/UriPatternMatcher.java?rev=1143475&r1=1143474&r2=1143475&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/UriPatternMatcher.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/UriPatternMatcher.java Wed Jul  6 15:56:41 2011
@@ -50,14 +50,14 @@ import org.apache.http.annotation.Thread
  * @since 4.0
  */
 @ThreadSafe
-public class UriPatternMatcher {
+public class UriPatternMatcher<T> {
 
     @GuardedBy("this")
-    private final Map<String, Object> map;
+    private final Map<String, T> map;
 
     public UriPatternMatcher() {
         super();
-        this.map = new HashMap<String, Object>();
+        this.map = new HashMap<String, T>();
     }
 
     /**
@@ -66,7 +66,7 @@ public class UriPatternMatcher {
      * @param pattern the pattern to register the handler for.
      * @param obj the object.
      */
-    public synchronized void register(final String pattern, final Object obj) {
+    public synchronized void register(final String pattern, final T obj) {
         if (pattern == null) {
             throw new IllegalArgumentException("URI request pattern may not be null");
         }
@@ -89,7 +89,7 @@ public class UriPatternMatcher {
      * @deprecated use {@link #setObjects(Map)}
      */
     @Deprecated
-    public synchronized void setHandlers(final Map<String, Object> map) {
+    public synchronized void setHandlers(final Map<String, T> map) {
         if (map == null) {
             throw new IllegalArgumentException("Map of handlers may not be null");
         }
@@ -101,7 +101,7 @@ public class UriPatternMatcher {
      * Sets objects from the given map.
      * @param map the map containing objects keyed by their URI patterns.
      */
-    public synchronized void setObjects(final Map<String, Object> map) {
+    public synchronized void setObjects(final Map<String, T> map) {
         if (map == null) {
             throw new IllegalArgumentException("Map of handlers may not be null");
         }
@@ -115,7 +115,7 @@ public class UriPatternMatcher {
      * @param requestURI the request URI
      * @return object or <code>null</code> if no match is found.
      */
-    public synchronized Object lookup(String requestURI) {
+    public synchronized T lookup(String requestURI) {
         if (requestURI == null) {
             throw new IllegalArgumentException("Request URI may not be null");
         }
@@ -126,7 +126,7 @@ public class UriPatternMatcher {
         }
 
         // direct match?
-        Object obj = this.map.get(requestURI);
+        T obj = this.map.get(requestURI);
         if (obj == null) {
             // pattern match?
             String bestMatch = null;

Added: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpRequestExecutor.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpRequestExecutor.java?rev=1143475&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpRequestExecutor.java (added)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpRequestExecutor.java Wed Jul  6 15:56:41 2011
@@ -0,0 +1,452 @@
+/*
+ * ====================================================================
+ * 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 org.apache.http.HttpClientConnection;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpEntityEnclosingRequest;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpVersion;
+import org.apache.http.ProtocolException;
+import org.apache.http.message.BasicHttpEntityEnclosingRequest;
+import org.apache.http.message.BasicHttpRequest;
+import org.apache.http.message.BasicHttpResponse;
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+public class TestHttpRequestExecutor {
+
+
+    @Test
+    public void testInvalidInput() throws Exception {
+        HttpContext context = new BasicHttpContext();
+        HttpRequest request = new BasicHttpRequest("GET", "/");
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        HttpClientConnection conn = Mockito.mock(HttpClientConnection.class);
+        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
+        try {
+            HttpRequestExecutor executor = new HttpRequestExecutor();
+            executor.execute(null, conn, context);
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+        try {
+            HttpRequestExecutor executor = new HttpRequestExecutor();
+            executor.execute(request, null, context);
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+        try {
+            HttpRequestExecutor executor = new HttpRequestExecutor();
+            executor.execute(request, conn, null);
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+
+        try {
+            HttpRequestExecutor executor = new HttpRequestExecutor();
+            executor.doSendRequest(null, conn, context);
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+        try {
+            HttpRequestExecutor executor = new HttpRequestExecutor();
+            executor.doSendRequest(request, null, context);
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+        try {
+            HttpRequestExecutor executor = new HttpRequestExecutor();
+            executor.doSendRequest(request, conn, null);
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+
+        try {
+            HttpRequestExecutor executor = new HttpRequestExecutor();
+            executor.doReceiveResponse(null, conn, context);
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+        try {
+            HttpRequestExecutor executor = new HttpRequestExecutor();
+            executor.doReceiveResponse(request, null, context);
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+        try {
+            HttpRequestExecutor executor = new HttpRequestExecutor();
+            executor.doReceiveResponse(request, conn, null);
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+
+        try {
+            HttpRequestExecutor executor = new HttpRequestExecutor();
+            executor.preProcess(null, httprocessor, context);
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+        try {
+            HttpRequestExecutor executor = new HttpRequestExecutor();
+            executor.preProcess(request, null, context);
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+        try {
+            HttpRequestExecutor executor = new HttpRequestExecutor();
+            executor.preProcess(request, httprocessor, null);
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+
+        try {
+            HttpRequestExecutor executor = new HttpRequestExecutor();
+            executor.postProcess(null, httprocessor, context);
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+        try {
+            HttpRequestExecutor executor = new HttpRequestExecutor();
+            executor.postProcess(response, null, context);
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+        try {
+            HttpRequestExecutor executor = new HttpRequestExecutor();
+            executor.postProcess(response, httprocessor, null);
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+    }
+
+    @Test
+    public void testBasicExecution() throws Exception {
+        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
+        HttpClientConnection conn = Mockito.mock(HttpClientConnection.class);
+        HttpRequestExecutor executor = new HttpRequestExecutor();
+
+        HttpContext context = new BasicHttpContext();
+        HttpRequest request = new BasicHttpRequest("GET", "/");
+
+        executor.preProcess(request, httprocessor, context);
+        Mockito.verify(httprocessor).process(request, context);
+
+        Mockito.when(conn.receiveResponseHeader()).thenReturn(
+                new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"));
+
+        HttpResponse response = executor.execute(request, conn, context);
+        Mockito.verify(conn).sendRequestHeader(request);
+        Mockito.verify(conn).flush();
+        Mockito.verify(conn).receiveResponseHeader();
+        Mockito.verify(conn).receiveResponseEntity(response);
+
+        executor.postProcess(response, httprocessor, context);
+        Mockito.verify(httprocessor).process(response, context);
+
+        Assert.assertSame(conn, context.getAttribute(ExecutionContext.HTTP_CONNECTION));
+        Assert.assertSame(request, context.getAttribute(ExecutionContext.HTTP_REQUEST));
+        Assert.assertSame(response, context.getAttribute(ExecutionContext.HTTP_RESPONSE));
+    }
+
+    @Test
+    public void testExecutionSkipIntermediateResponses() throws Exception {
+        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
+        HttpClientConnection conn = Mockito.mock(HttpClientConnection.class);
+        HttpRequestExecutor executor = new HttpRequestExecutor();
+
+        HttpContext context = new BasicHttpContext();
+        HttpRequest request = new BasicHttpRequest("GET", "/");
+
+        executor.preProcess(request, httprocessor, context);
+        Mockito.verify(httprocessor).process(request, context);
+
+        Mockito.when(conn.receiveResponseHeader()).thenReturn(
+                new BasicHttpResponse(HttpVersion.HTTP_1_1, 100, "OK"),
+                new BasicHttpResponse(HttpVersion.HTTP_1_1, 101, "OK"),
+                new BasicHttpResponse(HttpVersion.HTTP_1_1, 102, "OK"),
+                new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"));
+
+        HttpResponse response = executor.execute(request, conn, context);
+        Mockito.verify(conn).sendRequestHeader(request);
+        Mockito.verify(conn).flush();
+        Mockito.verify(conn, Mockito.times(4)).receiveResponseHeader();
+        Mockito.verify(conn, Mockito.times(1)).receiveResponseEntity(response);
+
+        executor.postProcess(response, httprocessor, context);
+        Mockito.verify(httprocessor).process(response, context);
+
+        Assert.assertSame(conn, context.getAttribute(ExecutionContext.HTTP_CONNECTION));
+        Assert.assertSame(request, context.getAttribute(ExecutionContext.HTTP_REQUEST));
+        Assert.assertSame(response, context.getAttribute(ExecutionContext.HTTP_RESPONSE));
+    }
+
+    @Test
+    public void testExecutionNoResponseBody() throws Exception {
+        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
+        HttpClientConnection conn = Mockito.mock(HttpClientConnection.class);
+        HttpRequestExecutor executor = new HttpRequestExecutor();
+
+        HttpContext context = new BasicHttpContext();
+        HttpRequest request = new BasicHttpRequest("GET", "/");
+
+        executor.preProcess(request, httprocessor, context);
+        Mockito.verify(httprocessor).process(request, context);
+
+        Mockito.when(conn.receiveResponseHeader()).thenReturn(
+                new BasicHttpResponse(HttpVersion.HTTP_1_1, 204, "OK"));
+
+        HttpResponse response = executor.execute(request, conn, context);
+        Mockito.verify(conn).sendRequestHeader(request);
+        Mockito.verify(conn).flush();
+        Mockito.verify(conn).receiveResponseHeader();
+        Mockito.verify(conn, Mockito.never()).receiveResponseEntity(response);
+
+        executor.postProcess(response, httprocessor, context);
+        Mockito.verify(httprocessor).process(response, context);
+    }
+
+    @Test
+    public void testExecutionHead() throws Exception {
+        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
+        HttpClientConnection conn = Mockito.mock(HttpClientConnection.class);
+        HttpRequestExecutor executor = new HttpRequestExecutor();
+
+        HttpContext context = new BasicHttpContext();
+        HttpRequest request = new BasicHttpRequest("HEAD", "/");
+
+        executor.preProcess(request, httprocessor, context);
+        Mockito.verify(httprocessor).process(request, context);
+
+        Mockito.when(conn.receiveResponseHeader()).thenReturn(
+                new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"));
+
+        HttpResponse response = executor.execute(request, conn, context);
+        Mockito.verify(conn).sendRequestHeader(request);
+        Mockito.verify(conn).flush();
+        Mockito.verify(conn).receiveResponseHeader();
+        Mockito.verify(conn, Mockito.never()).receiveResponseEntity(response);
+
+        executor.postProcess(response, httprocessor, context);
+        Mockito.verify(httprocessor).process(response, context);
+    }
+
+    @Test
+    public void testExecutionEntityEnclosingRequest() throws Exception {
+        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
+        HttpClientConnection conn = Mockito.mock(HttpClientConnection.class);
+        HttpRequestExecutor executor = new HttpRequestExecutor();
+
+        HttpContext context = new BasicHttpContext();
+        HttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/");
+//        request.addHeader(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE);
+        HttpEntity entity = Mockito.mock(HttpEntity.class);
+        request.setEntity(entity);
+
+        executor.preProcess(request, httprocessor, context);
+        Mockito.verify(httprocessor).process(request, context);
+
+        Mockito.when(conn.receiveResponseHeader()).thenReturn(
+                new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"));
+
+        HttpResponse response = executor.execute(request, conn, context);
+        Mockito.verify(conn).sendRequestHeader(request);
+        Mockito.verify(conn).sendRequestEntity(request);
+        Mockito.verify(conn).flush();
+        Mockito.verify(conn).receiveResponseHeader();
+        Mockito.verify(conn).receiveResponseEntity(response);
+
+        executor.postProcess(response, httprocessor, context);
+        Mockito.verify(httprocessor).process(response, context);
+    }
+
+    @Test
+    public void testExecutionEntityEnclosingRequestWithExpectContinueSuccess() throws Exception {
+        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
+        HttpClientConnection conn = Mockito.mock(HttpClientConnection.class);
+        HttpRequestExecutor executor = new HttpRequestExecutor();
+
+        HttpContext context = new BasicHttpContext();
+        HttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/");
+        request.addHeader(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE);
+        HttpEntity entity = Mockito.mock(HttpEntity.class);
+        request.setEntity(entity);
+
+        executor.preProcess(request, httprocessor, context);
+        Mockito.verify(httprocessor).process(request, context);
+
+        Mockito.when(conn.receiveResponseHeader()).thenReturn(
+                new BasicHttpResponse(HttpVersion.HTTP_1_1, 100, "Continue"),
+                new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"));
+        Mockito.when(conn.isResponseAvailable(Mockito.anyInt())).thenReturn(true);
+
+        HttpResponse response = executor.execute(request, conn, context);
+        Mockito.verify(conn).sendRequestHeader(request);
+        Mockito.verify(conn).sendRequestEntity(request);
+        Mockito.verify(conn, Mockito.times(2)).flush();
+        Mockito.verify(conn).isResponseAvailable(2000);
+        Mockito.verify(conn, Mockito.times(2)).receiveResponseHeader();
+        Mockito.verify(conn).receiveResponseEntity(response);
+
+        executor.postProcess(response, httprocessor, context);
+        Mockito.verify(httprocessor).process(response, context);
+
+        Assert.assertEquals(Boolean.TRUE, context.getAttribute(ExecutionContext.HTTP_REQ_SENT));
+    }
+
+    @Test
+    public void testExecutionEntityEnclosingRequestWithExpectContinueFailure() throws Exception {
+        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
+        HttpClientConnection conn = Mockito.mock(HttpClientConnection.class);
+        HttpRequestExecutor executor = new HttpRequestExecutor();
+
+        HttpContext context = new BasicHttpContext();
+        HttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/");
+        request.addHeader(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE);
+        HttpEntity entity = Mockito.mock(HttpEntity.class);
+        request.setEntity(entity);
+
+        executor.preProcess(request, httprocessor, context);
+        Mockito.verify(httprocessor).process(request, context);
+
+        Mockito.when(conn.receiveResponseHeader()).thenReturn(
+                new BasicHttpResponse(HttpVersion.HTTP_1_1, 402, "OK"));
+        Mockito.when(conn.isResponseAvailable(Mockito.anyInt())).thenReturn(true);
+
+        HttpResponse response = executor.execute(request, conn, context);
+        Mockito.verify(conn).sendRequestHeader(request);
+        Mockito.verify(conn, Mockito.never()).sendRequestEntity(request);
+        Mockito.verify(conn, Mockito.times(2)).flush();
+        Mockito.verify(conn).isResponseAvailable(2000);
+        Mockito.verify(conn).receiveResponseHeader();
+        Mockito.verify(conn).receiveResponseEntity(response);
+
+        executor.postProcess(response, httprocessor, context);
+        Mockito.verify(httprocessor).process(response, context);
+    }
+
+    @Test
+    public void testExecutionEntityEnclosingRequestUnsupportedIntermediateResponse() throws Exception {
+        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
+        HttpClientConnection conn = Mockito.mock(HttpClientConnection.class);
+        HttpRequestExecutor executor = new HttpRequestExecutor();
+
+        HttpContext context = new BasicHttpContext();
+        HttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/");
+        request.addHeader(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE);
+        HttpEntity entity = Mockito.mock(HttpEntity.class);
+        request.setEntity(entity);
+
+        executor.preProcess(request, httprocessor, context);
+        Mockito.verify(httprocessor).process(request, context);
+
+        Mockito.when(conn.receiveResponseHeader()).thenReturn(
+                new BasicHttpResponse(HttpVersion.HTTP_1_1, 101, "OK"));
+        Mockito.when(conn.isResponseAvailable(Mockito.anyInt())).thenReturn(true);
+
+        try {
+            executor.execute(request, conn, context);
+            Assert.fail("ProtocolException should have been thrown");
+        } catch (ProtocolException ex) {
+            Mockito.verify(conn).close();
+            Assert.assertEquals(Boolean.FALSE, context.getAttribute(ExecutionContext.HTTP_REQ_SENT));
+        }
+    }
+
+    @Test
+    public void testExecutionEntityEnclosingRequestWithExpectContinueNoResponse() throws Exception {
+        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
+        HttpClientConnection conn = Mockito.mock(HttpClientConnection.class);
+        HttpRequestExecutor executor = new HttpRequestExecutor();
+
+        HttpContext context = new BasicHttpContext();
+        HttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/");
+        request.addHeader(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE);
+        HttpEntity entity = Mockito.mock(HttpEntity.class);
+        request.setEntity(entity);
+
+        executor.preProcess(request, httprocessor, context);
+        Mockito.verify(httprocessor).process(request, context);
+
+        Mockito.when(conn.receiveResponseHeader()).thenReturn(
+                new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"));
+        Mockito.when(conn.isResponseAvailable(Mockito.anyInt())).thenReturn(false);
+
+        HttpResponse response = executor.execute(request, conn, context);
+        Mockito.verify(conn).sendRequestHeader(request);
+        Mockito.verify(conn).sendRequestEntity(request);
+        Mockito.verify(conn, Mockito.times(2)).flush();
+        Mockito.verify(conn).isResponseAvailable(2000);
+        Mockito.verify(conn).receiveResponseHeader();
+        Mockito.verify(conn).receiveResponseEntity(response);
+
+        executor.postProcess(response, httprocessor, context);
+        Mockito.verify(httprocessor).process(response, context);
+    }
+
+    @Test
+    public void testExecutionIOException() throws Exception {
+        HttpClientConnection conn = Mockito.mock(HttpClientConnection.class);
+        HttpRequestExecutor executor = new HttpRequestExecutor();
+
+        HttpContext context = new BasicHttpContext();
+        HttpRequest request = new BasicHttpRequest("GET", "/");
+
+        Mockito.doThrow(new IOException("Oopsie")).when(conn).sendRequestHeader(request);
+        try {
+            executor.execute(request, conn, context);
+            Assert.fail("IOException should have been thrown");
+        } catch (IOException ex) {
+            Mockito.verify(conn).close();
+            Assert.assertEquals(Boolean.FALSE, context.getAttribute(ExecutionContext.HTTP_REQ_SENT));
+        }
+    }
+
+    @Test
+    public void testExecutionRuntimeException() throws Exception {
+        HttpClientConnection conn = Mockito.mock(HttpClientConnection.class);
+        HttpRequestExecutor executor = new HttpRequestExecutor();
+
+        HttpContext context = new BasicHttpContext();
+        HttpRequest request = new BasicHttpRequest("GET", "/");
+
+        Mockito.doThrow(new RuntimeException("Oopsie")).when(conn).receiveResponseHeader();
+        try {
+            executor.execute(request, conn, context);
+            Assert.fail("IOException should have been thrown");
+        } catch (RuntimeException ex) {
+            Mockito.verify(conn).close();
+            Assert.assertEquals(Boolean.TRUE, context.getAttribute(ExecutionContext.HTTP_REQ_SENT));
+        }
+    }
+
+}

Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpRequestExecutor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpRequestExecutor.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpRequestExecutor.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpRequestHandlerRegistry.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpRequestHandlerRegistry.java?rev=1143475&r1=1143474&r2=1143475&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpRequestHandlerRegistry.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpRequestHandlerRegistry.java Wed Jul  6 15:56:41 2011
@@ -37,8 +37,6 @@ import org.apache.http.HttpResponse;
 import org.junit.Assert;
 import org.junit.Test;
 
-/**
- */
 public class TestHttpRequestHandlerRegistry {
 
     private static class DummyHttpRequestHandler implements HttpRequestHandler {
@@ -78,7 +76,7 @@ public class TestHttpRequestHandlerRegis
         h = registry.lookup("/h1");
         Assert.assertNull(h);
 
-        Map<String, Object> map = new HashMap<String, Object>();
+        Map<String, HttpRequestHandler> map = new HashMap<String, HttpRequestHandler>();
         map.put("/a1", h1);
         map.put("/a2", h2);
         map.put("/a3", h3);
@@ -94,6 +92,12 @@ public class TestHttpRequestHandlerRegis
         Assert.assertTrue(h1 == h);
     }
 
+    @Test(expected=IllegalArgumentException.class)
+    public void testRegisterNull() throws Exception {
+        HttpRequestHandlerRegistry registry = new HttpRequestHandlerRegistry();
+        registry.register(null, null);
+    }
+
     @Test
     public void testWildCardMatching1() throws Exception {
         HttpRequestHandler h1 = new DummyHttpRequestHandler();

Added: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpService.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpService.java?rev=1143475&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpService.java (added)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpService.java Wed Jul  6 15:56:41 2011
@@ -0,0 +1,569 @@
+/*
+ * ====================================================================
+ * 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.InputStream;
+
+import org.apache.http.ConnectionReuseStrategy;
+import org.apache.http.HttpEntityEnclosingRequest;
+import org.apache.http.HttpException;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpResponseFactory;
+import org.apache.http.HttpServerConnection;
+import org.apache.http.HttpStatus;
+import org.apache.http.HttpVersion;
+import org.apache.http.MethodNotSupportedException;
+import org.apache.http.ProtocolException;
+import org.apache.http.UnsupportedHttpVersionException;
+import org.apache.http.entity.InputStreamEntity;
+import org.apache.http.message.BasicHttpEntityEnclosingRequest;
+import org.apache.http.message.BasicHttpRequest;
+import org.apache.http.message.BasicHttpResponse;
+import org.apache.http.params.HttpParams;
+import org.apache.http.params.SyncBasicHttpParams;
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+public class TestHttpService {
+
+    @Test
+    public void testInvalidInitialization() throws Exception {
+        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
+        ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
+        HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
+        HttpRequestHandlerResolver handlerResolver = Mockito.mock(HttpRequestHandlerResolver.class);
+        HttpParams params = new SyncBasicHttpParams();
+        try {
+            new HttpService(
+                    null,
+                    connReuseStrategy,
+                    responseFactory,
+                    handlerResolver,
+                    params);
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+        try {
+            new HttpService(
+                    httprocessor,
+                    null,
+                    responseFactory,
+                    handlerResolver,
+                    params);
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+        try {
+            new HttpService(
+                    httprocessor,
+                    connReuseStrategy,
+                    null,
+                    handlerResolver,
+                    params);
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+        try {
+            new HttpService(
+                    httprocessor,
+                    connReuseStrategy,
+                    responseFactory,
+                    handlerResolver,
+                    null);
+            Assert.fail("IllegalArgumentException expected");
+        } catch (IllegalArgumentException expected) {
+        }
+    }
+
+    @Test
+    public void testBasicExecution() throws Exception {
+        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
+        ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
+        HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
+        HttpRequestHandlerResolver handlerResolver = Mockito.mock(HttpRequestHandlerResolver.class);
+        HttpParams params = new SyncBasicHttpParams();
+
+        HttpService httpservice = new HttpService(
+                httprocessor,
+                connReuseStrategy,
+                responseFactory,
+                handlerResolver,
+                params);
+        HttpContext context = new BasicHttpContext();
+        HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
+        HttpRequest request = new BasicHttpRequest("GET", "/");
+        Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 200, context)).thenReturn(response);
+        Mockito.when(connReuseStrategy.keepAlive(response, context)).thenReturn(false);
+
+        httpservice.handleRequest(conn, context);
+
+        Assert.assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, response.getStatusLine().getStatusCode());
+
+        Assert.assertSame(conn, context.getAttribute(ExecutionContext.HTTP_CONNECTION));
+        Assert.assertSame(request, context.getAttribute(ExecutionContext.HTTP_REQUEST));
+        Assert.assertSame(response, context.getAttribute(ExecutionContext.HTTP_RESPONSE));
+
+        Mockito.verify(httprocessor).process(request, context);
+        Mockito.verify(httprocessor).process(response, context);
+        Mockito.verify(conn).sendResponseHeader(response);
+        Mockito.verify(conn).sendResponseEntity(response);
+        Mockito.verify(conn).flush();
+        Mockito.verify(conn).close();
+    }
+
+    @Test
+    public void testBasicExecutionHTTP10() throws Exception {
+        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
+        ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
+        HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
+        HttpRequestHandlerResolver handlerResolver = Mockito.mock(HttpRequestHandlerResolver.class);
+        HttpParams params = new SyncBasicHttpParams();
+
+        HttpService httpservice = new HttpService(
+                httprocessor,
+                connReuseStrategy,
+                responseFactory,
+                handlerResolver,
+                params);
+        HttpContext context = new BasicHttpContext();
+        HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
+        HttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_0);
+        Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_0, 200, "OK");
+        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_0, 200, context)).thenReturn(response);
+        Mockito.when(connReuseStrategy.keepAlive(response, context)).thenReturn(false);
+
+        httpservice.handleRequest(conn, context);
+
+        Mockito.verify(responseFactory).newHttpResponse(HttpVersion.HTTP_1_0, 200, context);
+    }
+
+    @Test
+    public void testBasicProtocolDowngrade() throws Exception {
+        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
+        ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
+        HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
+        HttpRequestHandlerResolver handlerResolver = Mockito.mock(HttpRequestHandlerResolver.class);
+        HttpParams params = new SyncBasicHttpParams();
+
+        HttpService httpservice = new HttpService(
+                httprocessor,
+                connReuseStrategy,
+                responseFactory,
+                handlerResolver,
+                params);
+        HttpContext context = new BasicHttpContext();
+        HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
+        HttpRequest request = new BasicHttpRequest("GET", "/", new HttpVersion(20, 45));
+        Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 200, context)).thenReturn(response);
+        Mockito.when(connReuseStrategy.keepAlive(response, context)).thenReturn(false);
+
+        httpservice.handleRequest(conn, context);
+
+        Mockito.verify(responseFactory).newHttpResponse(HttpVersion.HTTP_1_1, 200, context);
+    }
+
+    @Test
+    public void testExecutionEntityEclosingRequest() throws Exception {
+        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
+        ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
+        HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
+        HttpRequestHandlerResolver handlerResolver = Mockito.mock(HttpRequestHandlerResolver.class);
+        HttpParams params = new SyncBasicHttpParams();
+
+        HttpService httpservice = new HttpService(
+                httprocessor,
+                connReuseStrategy,
+                responseFactory,
+                handlerResolver,
+                params);
+        HttpContext context = new BasicHttpContext();
+        HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
+        HttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/");
+        InputStream instream = Mockito.mock(InputStream.class);
+        InputStreamEntity entity = new InputStreamEntity(instream, -1);
+        request.setEntity(entity);
+
+        Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 200, context)).thenReturn(response);
+        Mockito.when(connReuseStrategy.keepAlive(response, context)).thenReturn(false);
+
+        httpservice.handleRequest(conn, context);
+
+        Assert.assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, response.getStatusLine().getStatusCode());
+
+        Assert.assertSame(conn, context.getAttribute(ExecutionContext.HTTP_CONNECTION));
+        Assert.assertSame(request, context.getAttribute(ExecutionContext.HTTP_REQUEST));
+        Assert.assertSame(response, context.getAttribute(ExecutionContext.HTTP_RESPONSE));
+
+        Mockito.verify(conn).receiveRequestEntity(request);
+        Mockito.verify(httprocessor).process(request, context);
+        Mockito.verify(instream).close();
+        Mockito.verify(httprocessor).process(response, context);
+        Mockito.verify(conn).sendResponseHeader(response);
+        Mockito.verify(conn).sendResponseEntity(response);
+        Mockito.verify(conn).flush();
+        Mockito.verify(conn).close();
+    }
+
+    @Test
+    public void testExecutionEntityEclosingRequestWithExpectContinue() throws Exception {
+        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
+        ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
+        HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
+        HttpRequestHandlerResolver handlerResolver = Mockito.mock(HttpRequestHandlerResolver.class);
+        HttpParams params = new SyncBasicHttpParams();
+
+        HttpService httpservice = new HttpService(
+                httprocessor,
+                connReuseStrategy,
+                responseFactory,
+                handlerResolver,
+                params);
+        HttpContext context = new BasicHttpContext();
+        HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
+        HttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/");
+        request.addHeader(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE);
+        InputStream instream = Mockito.mock(InputStream.class);
+        InputStreamEntity entity = new InputStreamEntity(instream, -1);
+        request.setEntity(entity);
+
+        Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
+        HttpResponse resp100 = new BasicHttpResponse(HttpVersion.HTTP_1_1, 100, "Continue");
+        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 100, context)).thenReturn(resp100);
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 200, context)).thenReturn(response);
+        Mockito.when(connReuseStrategy.keepAlive(response, context)).thenReturn(false);
+
+        httpservice.handleRequest(conn, context);
+
+        Assert.assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, response.getStatusLine().getStatusCode());
+
+        Assert.assertSame(conn, context.getAttribute(ExecutionContext.HTTP_CONNECTION));
+        Assert.assertSame(request, context.getAttribute(ExecutionContext.HTTP_REQUEST));
+        Assert.assertSame(response, context.getAttribute(ExecutionContext.HTTP_RESPONSE));
+
+        Mockito.verify(conn).sendResponseHeader(resp100);
+        Mockito.verify(conn).receiveRequestEntity(request);
+        Mockito.verify(httprocessor).process(request, context);
+        Mockito.verify(instream).close();
+        Mockito.verify(httprocessor).process(response, context);
+        Mockito.verify(conn).sendResponseHeader(response);
+        Mockito.verify(conn).sendResponseEntity(response);
+        Mockito.verify(conn, Mockito.times(2)).flush();
+        Mockito.verify(conn).close();
+    }
+
+    @Test
+    public void testExecutionEntityEclosingRequestCustomExpectationVerifier() throws Exception {
+        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
+        ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
+        HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
+        HttpRequestHandlerResolver handlerResolver = Mockito.mock(HttpRequestHandlerResolver.class);
+        HttpParams params = new SyncBasicHttpParams();
+
+        HttpExpectationVerifier expectationVerifier = new HttpExpectationVerifier() {
+
+            public void verify(
+                    final HttpRequest request,
+                    final HttpResponse response,
+                    final HttpContext context) throws HttpException {
+                response.setStatusCode(HttpStatus.SC_UNAUTHORIZED);
+            }
+
+        };
+
+        HttpService httpservice = new HttpService(
+                httprocessor,
+                connReuseStrategy,
+                responseFactory,
+                handlerResolver,
+                expectationVerifier,
+                params);
+        HttpContext context = new BasicHttpContext();
+        HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
+        HttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/");
+        request.addHeader(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE);
+        InputStream instream = Mockito.mock(InputStream.class);
+        InputStreamEntity entity = new InputStreamEntity(instream, -1);
+        request.setEntity(entity);
+
+        Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 100, "Continue");
+        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 100, context)).thenReturn(response);
+        Mockito.when(connReuseStrategy.keepAlive(response, context)).thenReturn(false);
+
+        httpservice.handleRequest(conn, context);
+
+        Assert.assertSame(conn, context.getAttribute(ExecutionContext.HTTP_CONNECTION));
+        Assert.assertSame(request, context.getAttribute(ExecutionContext.HTTP_REQUEST));
+        Assert.assertSame(response, context.getAttribute(ExecutionContext.HTTP_RESPONSE));
+
+        Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode());
+
+        Mockito.verify(conn).sendResponseHeader(response);
+        Mockito.verify(conn, Mockito.never()).receiveRequestEntity(request);
+        Mockito.verify(httprocessor).process(response, context);
+        Mockito.verify(conn).sendResponseHeader(response);
+        Mockito.verify(conn).sendResponseEntity(response);
+        Mockito.verify(conn).flush();
+        Mockito.verify(conn).close();
+    }
+
+    @Test
+    public void testExecutionExceptionInCustomExpectationVerifier() throws Exception {
+        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
+        ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
+        HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
+        HttpExpectationVerifier expectationVerifier = Mockito.mock(HttpExpectationVerifier.class);
+        HttpRequestHandlerResolver handlerResolver = Mockito.mock(HttpRequestHandlerResolver.class);
+        HttpParams params = new SyncBasicHttpParams();
+
+        HttpService httpservice = new HttpService(
+                httprocessor,
+                connReuseStrategy,
+                responseFactory,
+                handlerResolver,
+                expectationVerifier,
+                params);
+        HttpContext context = new BasicHttpContext();
+        HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
+        HttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/");
+        request.addHeader(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE);
+        InputStream instream = Mockito.mock(InputStream.class);
+        InputStreamEntity entity = new InputStreamEntity(instream, -1);
+        request.setEntity(entity);
+
+        Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
+        HttpResponse resp100 = new BasicHttpResponse(HttpVersion.HTTP_1_1, 100, "Continue");
+        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 100, context)).thenReturn(resp100);
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_0, 500, "Oppsie");
+        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_0, 500, context)).thenReturn(response);
+        Mockito.doThrow(new HttpException("Oopsie")).when(expectationVerifier).verify(request, resp100, context);
+        Mockito.when(connReuseStrategy.keepAlive(response, context)).thenReturn(false);
+
+        httpservice.handleRequest(conn, context);
+
+        Assert.assertSame(conn, context.getAttribute(ExecutionContext.HTTP_CONNECTION));
+        Assert.assertSame(request, context.getAttribute(ExecutionContext.HTTP_REQUEST));
+        Assert.assertSame(response, context.getAttribute(ExecutionContext.HTTP_RESPONSE));
+
+        Assert.assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, response.getStatusLine().getStatusCode());
+
+        Mockito.verify(conn).sendResponseHeader(response);
+        Mockito.verify(conn, Mockito.never()).receiveRequestEntity(request);
+        Mockito.verify(httprocessor).process(response, context);
+        Mockito.verify(conn).sendResponseHeader(response);
+        Mockito.verify(conn).sendResponseEntity(response);
+        Mockito.verify(conn).flush();
+        Mockito.verify(conn).close();
+    }
+
+    @Test
+    public void testMethodNotSupported() throws Exception {
+        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
+        ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
+        HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
+        HttpRequestHandlerResolver handlerResolver = Mockito.mock(HttpRequestHandlerResolver.class);
+        HttpRequestHandler requestHandler = Mockito.mock(HttpRequestHandler.class);
+        HttpParams params = new SyncBasicHttpParams();
+
+        HttpService httpservice = new HttpService(
+                httprocessor,
+                connReuseStrategy,
+                responseFactory,
+                handlerResolver,
+                params);
+        HttpContext context = new BasicHttpContext();
+        HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
+        HttpRequest request = new BasicHttpRequest("whatever", "/");
+
+        Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 200, context)).thenReturn(response);
+        HttpResponse error = new BasicHttpResponse(HttpVersion.HTTP_1_0, 500, "Oppsie");
+        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_0, 500, context)).thenReturn(error);
+        Mockito.when(handlerResolver.lookup("/")).thenReturn(requestHandler);
+        Mockito.doThrow(new MethodNotSupportedException("whatever")).when(
+                requestHandler).handle(request, response, context);
+        Mockito.when(connReuseStrategy.keepAlive(error, context)).thenReturn(false);
+
+        httpservice.handleRequest(conn, context);
+
+        Assert.assertSame(conn, context.getAttribute(ExecutionContext.HTTP_CONNECTION));
+        Assert.assertSame(request, context.getAttribute(ExecutionContext.HTTP_REQUEST));
+        Assert.assertSame(error, context.getAttribute(ExecutionContext.HTTP_RESPONSE));
+
+        Assert.assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, error.getStatusLine().getStatusCode());
+
+        Mockito.verify(conn).sendResponseHeader(error);
+        Mockito.verify(httprocessor).process(error, context);
+        Mockito.verify(conn).sendResponseHeader(error);
+        Mockito.verify(conn).sendResponseEntity(error);
+        Mockito.verify(conn).flush();
+        Mockito.verify(conn).close();
+    }
+
+    @Test
+    public void testUnsupportedHttpVersionException() throws Exception {
+        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
+        ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
+        HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
+        HttpRequestHandlerResolver handlerResolver = Mockito.mock(HttpRequestHandlerResolver.class);
+        HttpRequestHandler requestHandler = Mockito.mock(HttpRequestHandler.class);
+        HttpParams params = new SyncBasicHttpParams();
+
+        HttpService httpservice = new HttpService(
+                httprocessor,
+                connReuseStrategy,
+                responseFactory,
+                handlerResolver,
+                params);
+        HttpContext context = new BasicHttpContext();
+        HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
+        HttpRequest request = new BasicHttpRequest("whatever", "/");
+
+        Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 200, context)).thenReturn(response);
+        HttpResponse error = new BasicHttpResponse(HttpVersion.HTTP_1_0, 500, "Oppsie");
+        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_0, 500, context)).thenReturn(error);
+        Mockito.when(handlerResolver.lookup("/")).thenReturn(requestHandler);
+        Mockito.doThrow(new UnsupportedHttpVersionException()).when(
+                requestHandler).handle(request, response, context);
+        Mockito.when(connReuseStrategy.keepAlive(error, context)).thenReturn(false);
+
+        httpservice.handleRequest(conn, context);
+
+        Assert.assertSame(conn, context.getAttribute(ExecutionContext.HTTP_CONNECTION));
+        Assert.assertSame(request, context.getAttribute(ExecutionContext.HTTP_REQUEST));
+        Assert.assertSame(error, context.getAttribute(ExecutionContext.HTTP_RESPONSE));
+
+        Assert.assertEquals(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED, error.getStatusLine().getStatusCode());
+
+        Mockito.verify(conn).sendResponseHeader(error);
+        Mockito.verify(httprocessor).process(error, context);
+        Mockito.verify(conn).sendResponseHeader(error);
+        Mockito.verify(conn).sendResponseEntity(error);
+        Mockito.verify(conn).flush();
+        Mockito.verify(conn).close();
+    }
+
+    @Test
+    public void testProtocolException() throws Exception {
+        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
+        ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
+        HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
+        HttpRequestHandlerResolver handlerResolver = Mockito.mock(HttpRequestHandlerResolver.class);
+        HttpRequestHandler requestHandler = Mockito.mock(HttpRequestHandler.class);
+        HttpParams params = new SyncBasicHttpParams();
+
+        HttpService httpservice = new HttpService(
+                httprocessor,
+                connReuseStrategy,
+                responseFactory,
+                handlerResolver,
+                params);
+        HttpContext context = new BasicHttpContext();
+        HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
+        HttpRequest request = new BasicHttpRequest("whatever", "/");
+
+        Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 200, context)).thenReturn(response);
+        HttpResponse error = new BasicHttpResponse(HttpVersion.HTTP_1_0, 500, "Oppsie");
+        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_0, 500, context)).thenReturn(error);
+        Mockito.when(handlerResolver.lookup("/")).thenReturn(requestHandler);
+        Mockito.doThrow(new ProtocolException("oh, this world is wrong")).when(
+                requestHandler).handle(request, response, context);
+        Mockito.when(connReuseStrategy.keepAlive(error, context)).thenReturn(false);
+
+        httpservice.handleRequest(conn, context);
+
+        Assert.assertSame(conn, context.getAttribute(ExecutionContext.HTTP_CONNECTION));
+        Assert.assertSame(request, context.getAttribute(ExecutionContext.HTTP_REQUEST));
+        Assert.assertSame(error, context.getAttribute(ExecutionContext.HTTP_RESPONSE));
+
+        Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, error.getStatusLine().getStatusCode());
+
+        Mockito.verify(conn).sendResponseHeader(error);
+        Mockito.verify(httprocessor).process(error, context);
+        Mockito.verify(conn).sendResponseHeader(error);
+        Mockito.verify(conn).sendResponseEntity(error);
+        Mockito.verify(conn).flush();
+        Mockito.verify(conn).close();
+    }
+
+    @Test
+    public void testConnectionKeepAlive() throws Exception {
+        HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
+        ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
+        HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
+        HttpRequestHandlerResolver handlerResolver = Mockito.mock(HttpRequestHandlerResolver.class);
+        HttpRequestHandler requestHandler = Mockito.mock(HttpRequestHandler.class);
+        HttpParams params = new SyncBasicHttpParams();
+
+        HttpService httpservice = new HttpService(
+                httprocessor,
+                connReuseStrategy,
+                responseFactory,
+                handlerResolver,
+                params);
+        HttpContext context = new BasicHttpContext();
+        HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
+        HttpRequest request = new BasicHttpRequest("GET", "/");
+        Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
+        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
+        Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 200, context)).thenReturn(response);
+        Mockito.when(handlerResolver.lookup("/")).thenReturn(requestHandler);
+        Mockito.when(connReuseStrategy.keepAlive(response, context)).thenReturn(true);
+
+        httpservice.handleRequest(conn, context);
+
+        Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+
+        Assert.assertSame(conn, context.getAttribute(ExecutionContext.HTTP_CONNECTION));
+        Assert.assertSame(request, context.getAttribute(ExecutionContext.HTTP_REQUEST));
+        Assert.assertSame(response, context.getAttribute(ExecutionContext.HTTP_RESPONSE));
+
+        Mockito.verify(httprocessor).process(request, context);
+        Mockito.verify(httprocessor).process(response, context);
+        Mockito.verify(conn).sendResponseHeader(response);
+        Mockito.verify(conn).sendResponseEntity(response);
+        Mockito.verify(conn).flush();
+        Mockito.verify(conn, Mockito.never()).close();
+    }
+
+}

Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpService.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpService.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpService.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Copied: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestProtocolIntegration.java (from r1143472, httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpServiceAndExecutor.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestProtocolIntegration.java?p2=httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestProtocolIntegration.java&p1=httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpServiceAndExecutor.java&r1=1143472&r2=1143475&rev=1143475&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestHttpServiceAndExecutor.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestProtocolIntegration.java Wed Jul  6 15:56:41 2011
@@ -64,7 +64,7 @@ import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 
-public class TestHttpServiceAndExecutor {
+public class TestProtocolIntegration {
 
     private HttpServer server;
     private HttpClient client;

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestStandardInterceptors.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestStandardInterceptors.java?rev=1143475&r1=1143474&r2=1143475&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestStandardInterceptors.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/protocol/TestStandardInterceptors.java Wed Jul  6 15:56:41 2011
@@ -27,8 +27,11 @@
 
 package org.apache.http.protocol;
 
+import java.net.InetAddress;
+
 import org.apache.http.Header;
 import org.apache.http.HttpHost;
+import org.apache.http.HttpInetConnection;
 import org.apache.http.HttpResponse;
 import org.apache.http.HttpStatus;
 import org.apache.http.HttpVersion;
@@ -42,6 +45,7 @@ import org.apache.http.message.BasicHttp
 import org.apache.http.params.CoreProtocolPNames;
 import org.junit.Assert;
 import org.junit.Test;
+import org.mockito.Mockito;
 
 public class TestStandardInterceptors {
 
@@ -57,6 +61,16 @@ public class TestStandardInterceptors {
     }
 
     @Test
+    public void testRequestConnControlConnectMethod() throws Exception {
+        HttpContext context = new BasicHttpContext(null);
+        BasicHttpRequest request = new BasicHttpRequest("CONNECT", "/");
+        RequestConnControl interceptor = new RequestConnControl();
+        interceptor.process(request, context);
+        Header header = request.getFirstHeader(HTTP.CONN_DIRECTIVE);
+        Assert.assertNull(header);
+    }
+
+    @Test
     public void testRequestConnControlCustom() throws Exception {
         HttpContext context = new BasicHttpContext(null);
         BasicHttpRequest request = new BasicHttpRequest("GET", "/");
@@ -349,6 +363,37 @@ public class TestStandardInterceptors {
     }
 
     @Test
+    public void testRequestTargetHostFallback() throws Exception {
+        HttpContext context = new BasicHttpContext(null);
+        BasicHttpRequest request = new BasicHttpRequest("GET", "/");
+        InetAddress address = Mockito.mock(InetAddress.class);
+        Mockito.when(address.getHostName()).thenReturn("somehost");
+        HttpInetConnection conn = Mockito.mock(HttpInetConnection.class);
+        Mockito.when(conn.getRemoteAddress()).thenReturn(address);
+        Mockito.when(conn.getRemotePort()).thenReturn(1234);
+        context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, null);
+        context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
+        RequestTargetHost interceptor = new RequestTargetHost();
+        interceptor.process(request, context);
+        Header header = request.getFirstHeader(HTTP.TARGET_HOST);
+        Assert.assertNotNull(header);
+        Assert.assertEquals("somehost:1234", header.getValue());
+    }
+
+    @Test(expected=ProtocolException.class)
+    public void testRequestTargetHostFallbackFailure() throws Exception {
+        HttpContext context = new BasicHttpContext(null);
+        BasicHttpRequest request = new BasicHttpRequest("GET", "/");
+        HttpInetConnection conn = Mockito.mock(HttpInetConnection.class);
+        Mockito.when(conn.getRemoteAddress()).thenReturn(null);
+        Mockito.when(conn.getRemotePort()).thenReturn(1234);
+        context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, null);
+        context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
+        RequestTargetHost interceptor = new RequestTargetHost();
+        interceptor.process(request, context);
+    }
+
+    @Test
     public void testRequestTargetHostNotGenerated() throws Exception {
         HttpContext context = new BasicHttpContext(null);
         HttpHost host = new HttpHost("somehost", 8080, "http");



Mime
View raw message