hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject [1/3] httpcomponents-core git commit: Improved handling of 1xx status messages by the classic transport; server expectation (expect-continue) handshake can now be implemented as a cross-cutting aspect by both the classic and asynchronous transports
Date Mon, 07 Aug 2017 19:13:21 GMT
Repository: httpcomponents-core
Updated Branches:
  refs/heads/master c7d4ee28b -> 76c3a52e4


http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/76c3a52e/httpcore5/src/test/java/org/apache/hc/core5/http/impl/io/TestHttpService.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/impl/io/TestHttpService.java
b/httpcore5/src/test/java/org/apache/hc/core5/http/impl/io/TestHttpService.java
index b8a30c4..656c677 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/http/impl/io/TestHttpService.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/http/impl/io/TestHttpService.java
@@ -27,7 +27,9 @@
 
 package org.apache.hc.core5.http.impl.io;
 
+import java.io.IOException;
 import java.io.InputStream;
+import java.util.List;
 
 import org.apache.hc.core5.http.ClassicHttpRequest;
 import org.apache.hc.core5.http.ClassicHttpResponse;
@@ -36,12 +38,12 @@ import org.apache.hc.core5.http.HeaderElements;
 import org.apache.hc.core5.http.HttpException;
 import org.apache.hc.core5.http.HttpHeaders;
 import org.apache.hc.core5.http.HttpRequestMapper;
+import org.apache.hc.core5.http.HttpResponse;
 import org.apache.hc.core5.http.HttpResponseFactory;
 import org.apache.hc.core5.http.HttpStatus;
 import org.apache.hc.core5.http.MethodNotSupportedException;
 import org.apache.hc.core5.http.ProtocolException;
 import org.apache.hc.core5.http.UnsupportedHttpVersionException;
-import org.apache.hc.core5.http.io.HttpExpectationVerifier;
 import org.apache.hc.core5.http.io.HttpRequestHandler;
 import org.apache.hc.core5.http.io.HttpServerConnection;
 import org.apache.hc.core5.http.io.entity.InputStreamEntity;
@@ -53,9 +55,12 @@ import org.apache.hc.core5.http.protocol.HttpProcessor;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+import org.mockito.ArgumentMatcher;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
+import org.mockito.Spy;
 
 public class TestHttpService {
 
@@ -63,6 +68,8 @@ public class TestHttpService {
     private HttpProcessor httprocessor;
     @Mock
     private ConnectionReuseStrategy connReuseStrategy;
+    @Spy
+    private ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK);
     @Mock
     private HttpResponseFactory<ClassicHttpResponse> responseFactory;
     @Mock
@@ -79,9 +86,9 @@ public class TestHttpService {
         MockitoAnnotations.initMocks(this);
         httpservice = new HttpService(
                 httprocessor,
+                handlerResolver,
                 connReuseStrategy,
-                responseFactory,
-                handlerResolver);
+                responseFactory);
     }
 
     @Test
@@ -89,9 +96,9 @@ public class TestHttpService {
         try {
             new HttpService(
                     null,
+                    handlerResolver,
                     connReuseStrategy,
-                    responseFactory,
-                    handlerResolver);
+                    responseFactory);
             Assert.fail("IllegalArgumentException expected");
         } catch (final IllegalArgumentException expected) {
         }
@@ -102,7 +109,6 @@ public class TestHttpService {
         final HttpCoreContext context = HttpCoreContext.create();
         final ClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
         Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
         Mockito.when(responseFactory.newHttpResponse(200)).thenReturn(response);
         Mockito.when(connReuseStrategy.keepAlive(request, response, context)).thenReturn(Boolean.FALSE);
 
@@ -119,6 +125,7 @@ public class TestHttpService {
         Mockito.verify(conn).sendResponseEntity(response);
         Mockito.verify(conn).flush();
         Mockito.verify(conn).close();
+        Mockito.verify(response).close();
     }
 
     @Test
@@ -130,25 +137,34 @@ public class TestHttpService {
         request.setEntity(entity);
 
         Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
         Mockito.when(responseFactory.newHttpResponse(200)).thenReturn(response);
-        Mockito.when(connReuseStrategy.keepAlive(request, response, context)).thenReturn(Boolean.FALSE);
+        Mockito.when(connReuseStrategy.keepAlive(Mockito.eq(request), Mockito.argThat(new
ArgumentMatcher<HttpResponse>() {
+
+            @Override
+            public boolean matches(final HttpResponse errorResponse) {
+                return errorResponse.getCode() == HttpStatus.SC_NOT_IMPLEMENTED;
+            }
+
+        }), Mockito.eq(context))).thenReturn(Boolean.TRUE);
 
         httpservice.handleRequest(conn, context);
+        final ArgumentCaptor<ClassicHttpResponse> responseCaptor = ArgumentCaptor.forClass(ClassicHttpResponse.class);
+        Mockito.verify(conn).sendResponseHeader(responseCaptor.capture());
+        final ClassicHttpResponse response = responseCaptor.getValue();
+        Assert.assertNotNull(response);
 
         Assert.assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, response.getCode());
 
         Assert.assertSame(request, context.getRequest());
-        Assert.assertSame(response, context.getResponse());
 
-        Mockito.verify(conn).receiveRequestEntity(request);
         Mockito.verify(httprocessor).process(request, request.getEntity(), context);
         Mockito.verify(instream).close();
         Mockito.verify(httprocessor).process(response, response.getEntity(), context);
         Mockito.verify(conn).sendResponseHeader(response);
         Mockito.verify(conn).sendResponseEntity(response);
         Mockito.verify(conn).flush();
-        Mockito.verify(conn).close();
+        Mockito.verify(conn, Mockito.never()).close();
+        Mockito.verify(response).close();
     }
 
     @Test
@@ -161,116 +177,38 @@ public class TestHttpService {
         request.setEntity(entity);
 
         Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
-        final ClassicHttpResponse resp100 = new BasicClassicHttpResponse(100, "Continue");
-        Mockito.when(responseFactory.newHttpResponse(100)).thenReturn(resp100);
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
         Mockito.when(responseFactory.newHttpResponse(200)).thenReturn(response);
-        Mockito.when(connReuseStrategy.keepAlive(request, response, context)).thenReturn(Boolean.FALSE);
-
-        httpservice.handleRequest(conn, context);
-
-        Assert.assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, response.getCode());
-
-        Assert.assertSame(request, context.getRequest());
-        Assert.assertSame(response, context.getResponse());
-
-        Mockito.verify(conn).sendResponseHeader(resp100);
-        Mockito.verify(conn).receiveRequestEntity(request);
-        Mockito.verify(httprocessor).process(request, request.getEntity(), context);
-        Mockito.verify(instream).close();
-        Mockito.verify(httprocessor).process(response, response.getEntity(), 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 testExecutionEntityEnclosingRequestCustomExpectationVerifier() throws Exception
{
-        final HttpExpectationVerifier expectationVerifier = new HttpExpectationVerifier()
{
+        Mockito.when(connReuseStrategy.keepAlive(Mockito.eq(request), Mockito.argThat(new
ArgumentMatcher<HttpResponse>() {
 
             @Override
-            public void verify(
-                    final ClassicHttpRequest request,
-                    final ClassicHttpResponse response,
-                    final HttpContext context) throws HttpException {
-                response.setCode(HttpStatus.SC_UNAUTHORIZED);
+            public boolean matches(final HttpResponse errorResponse) {
+                return errorResponse.getCode() == HttpStatus.SC_NOT_IMPLEMENTED;
             }
 
-        };
-
-        final HttpService httpservice = new HttpService(
-                httprocessor,
-                connReuseStrategy,
-                responseFactory,
-                handlerResolver,
-                expectationVerifier,
-                null);
-        final HttpCoreContext context = HttpCoreContext.create();
-        final ClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
-        request.addHeader(HttpHeaders.EXPECT, HeaderElements.CONTINUE);
-        final InputStream instream = Mockito.mock(InputStream.class);
-        final InputStreamEntity entity = new InputStreamEntity(instream, -1);
-        request.setEntity(entity);
-
-        Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(100, "Continue");
-        Mockito.when(responseFactory.newHttpResponse(100)).thenReturn(response);
-        Mockito.when(connReuseStrategy.keepAlive(request, response, context)).thenReturn(Boolean.FALSE);
-
-        httpservice.handleRequest(conn, context);
-
-        Assert.assertSame(request, context.getRequest());
-        Assert.assertSame(response, context.getResponse());
-
-        Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getCode());
-
-        Mockito.verify(conn).sendResponseHeader(response);
-        Mockito.verify(httprocessor).process(response, response.getEntity(), context);
-        Mockito.verify(conn).sendResponseHeader(response);
-        Mockito.verify(conn).sendResponseEntity(response);
-        Mockito.verify(conn).flush();
-        Mockito.verify(conn).receiveRequestEntity(request);
-    }
-
-    @Test
-    public void testExecutionExceptionInCustomExpectationVerifier() throws Exception {
-        final HttpExpectationVerifier expectationVerifier = Mockito.mock(HttpExpectationVerifier.class);
-        final HttpService httpservice = new HttpService(
-                httprocessor,
-                connReuseStrategy,
-                responseFactory,
-                handlerResolver,
-                expectationVerifier,
-                null);
-        final HttpCoreContext context = HttpCoreContext.create();
-        final ClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
-        request.addHeader(HttpHeaders.EXPECT, "100-continue");
-        final InputStream instream = Mockito.mock(InputStream.class);
-        final InputStreamEntity entity = new InputStreamEntity(instream, -1);
-        request.setEntity(entity);
-
-        Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
-        final ClassicHttpResponse resp100 = new BasicClassicHttpResponse(100, "Continue");
-        Mockito.when(responseFactory.newHttpResponse(100)).thenReturn(resp100);
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(500, "Oppsie");
-        Mockito.when(responseFactory.newHttpResponse(500)).thenReturn(response);
-        Mockito.doThrow(new HttpException("Oopsie")).when(expectationVerifier).verify(request,
resp100, context);
-        Mockito.when(connReuseStrategy.keepAlive(request, response, context)).thenReturn(Boolean.FALSE);
+        }), Mockito.eq(context))).thenReturn(Boolean.TRUE);
 
         httpservice.handleRequest(conn, context);
+        final ArgumentCaptor<ClassicHttpResponse> responseCaptor = ArgumentCaptor.forClass(ClassicHttpResponse.class);
+        Mockito.verify(conn, Mockito.times(2)).sendResponseHeader(responseCaptor.capture());
+        final List<ClassicHttpResponse> responses = responseCaptor.getAllValues();
+        Assert.assertNotNull(responses);
+        Assert.assertEquals(2, responses.size());
+        final ClassicHttpResponse ack = responses.get(0);
+        final ClassicHttpResponse response = responses.get(1);
+
+        Assert.assertEquals(HttpStatus.SC_CONTINUE, ack.getCode());
+        Assert.assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, response.getCode());
 
         Assert.assertSame(request, context.getRequest());
-        Assert.assertSame(response, context.getResponse());
-
-        Assert.assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, response.getCode());
 
-        Mockito.verify(conn).sendResponseHeader(response);
+        Mockito.verify(httprocessor).process(request, request.getEntity(), context);
+        Mockito.verify(instream).close();
         Mockito.verify(httprocessor).process(response, response.getEntity(), context);
         Mockito.verify(conn).sendResponseHeader(response);
         Mockito.verify(conn).sendResponseEntity(response);
-        Mockito.verify(conn).flush();
-        Mockito.verify(conn).receiveRequestEntity(request);
+        Mockito.verify(conn, Mockito.times(2)).flush();
+        Mockito.verify(conn, Mockito.never()).close();
+        Mockito.verify(response).close();
     }
 
     @Test
@@ -279,27 +217,24 @@ public class TestHttpService {
         final ClassicHttpRequest request = new BasicClassicHttpRequest("whatever", "/");
 
         Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
         Mockito.when(responseFactory.newHttpResponse(200)).thenReturn(response);
-        final ClassicHttpResponse error = new BasicClassicHttpResponse(500, "Oppsie");
-        Mockito.when(responseFactory.newHttpResponse(500)).thenReturn(error);
         Mockito.when(handlerResolver.resolve(request, context)).thenReturn(requestHandler);
         Mockito.doThrow(new MethodNotSupportedException("whatever")).when(
                 requestHandler).handle(request, response, context);
-        Mockito.when(connReuseStrategy.keepAlive(request, error, context)).thenReturn(Boolean.FALSE);
 
         httpservice.handleRequest(conn, context);
+        final ArgumentCaptor<ClassicHttpResponse> responseCaptor = ArgumentCaptor.forClass(ClassicHttpResponse.class);
+        Mockito.verify(conn).sendResponseHeader(responseCaptor.capture());
+        final ClassicHttpResponse error = responseCaptor.getValue();
+        Assert.assertNotNull(error);
 
         Assert.assertSame(request, context.getRequest());
-        Assert.assertSame(error, context.getResponse());
 
         Assert.assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, error.getCode());
 
-        Mockito.verify(conn).sendResponseHeader(error);
         Mockito.verify(httprocessor).process(error, error.getEntity(), context);
         Mockito.verify(conn).sendResponseHeader(error);
         Mockito.verify(conn).sendResponseEntity(error);
-        Mockito.verify(conn).flush();
         Mockito.verify(conn).close();
     }
 
@@ -309,27 +244,24 @@ public class TestHttpService {
         final ClassicHttpRequest request = new BasicClassicHttpRequest("whatever", "/");
 
         Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
         Mockito.when(responseFactory.newHttpResponse(200)).thenReturn(response);
-        final ClassicHttpResponse error = new BasicClassicHttpResponse(500, "Oppsie");
-        Mockito.when(responseFactory.newHttpResponse(500)).thenReturn(error);
         Mockito.when(handlerResolver.resolve(request, context)).thenReturn(requestHandler);
         Mockito.doThrow(new UnsupportedHttpVersionException()).when(
                 requestHandler).handle(request, response, context);
-        Mockito.when(connReuseStrategy.keepAlive(request, error, context)).thenReturn(Boolean.FALSE);
 
         httpservice.handleRequest(conn, context);
+        final ArgumentCaptor<ClassicHttpResponse> responseCaptor = ArgumentCaptor.forClass(ClassicHttpResponse.class);
+        Mockito.verify(conn).sendResponseHeader(responseCaptor.capture());
+        final ClassicHttpResponse error = responseCaptor.getValue();
+        Assert.assertNotNull(error);
 
         Assert.assertSame(request, context.getRequest());
-        Assert.assertSame(error, context.getResponse());
 
         Assert.assertEquals(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED, error.getCode());
 
-        Mockito.verify(conn).sendResponseHeader(error);
         Mockito.verify(httprocessor).process(error, error.getEntity(), context);
         Mockito.verify(conn).sendResponseHeader(error);
         Mockito.verify(conn).sendResponseEntity(error);
-        Mockito.verify(conn).flush();
         Mockito.verify(conn).close();
     }
 
@@ -339,27 +271,24 @@ public class TestHttpService {
         final ClassicHttpRequest request = new BasicClassicHttpRequest("whatever", "/");
 
         Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
         Mockito.when(responseFactory.newHttpResponse(200)).thenReturn(response);
-        final ClassicHttpResponse error = new BasicClassicHttpResponse(500, "Oppsie");
-        Mockito.when(responseFactory.newHttpResponse(500)).thenReturn(error);
         Mockito.when(handlerResolver.resolve(request, context)).thenReturn(requestHandler);
         Mockito.doThrow(new ProtocolException("oh, this world is wrong")).when(
                 requestHandler).handle(request, response, context);
-        Mockito.when(connReuseStrategy.keepAlive(request, error, context)).thenReturn(Boolean.FALSE);
 
         httpservice.handleRequest(conn, context);
+        final ArgumentCaptor<ClassicHttpResponse> responseCaptor = ArgumentCaptor.forClass(ClassicHttpResponse.class);
+        Mockito.verify(conn).sendResponseHeader(responseCaptor.capture());
+        final ClassicHttpResponse error = responseCaptor.getValue();
+        Assert.assertNotNull(error);
 
         Assert.assertSame(request, context.getRequest());
-        Assert.assertSame(error, context.getResponse());
 
         Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, error.getCode());
 
-        Mockito.verify(conn).sendResponseHeader(error);
         Mockito.verify(httprocessor).process(error, error.getEntity(), context);
         Mockito.verify(conn).sendResponseHeader(error);
         Mockito.verify(conn).sendResponseEntity(error);
-        Mockito.verify(conn).flush();
         Mockito.verify(conn).close();
     }
 
@@ -368,7 +297,6 @@ public class TestHttpService {
         final HttpCoreContext context = HttpCoreContext.create();
         final ClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
         Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
         Mockito.when(responseFactory.newHttpResponse(200)).thenReturn(response);
         Mockito.when(handlerResolver.resolve(request, context)).thenReturn(requestHandler);
         Mockito.when(connReuseStrategy.keepAlive(request, response, context)).thenReturn(Boolean.TRUE);
@@ -386,6 +314,7 @@ public class TestHttpService {
         Mockito.verify(conn).sendResponseEntity(response);
         Mockito.verify(conn).flush();
         Mockito.verify(conn, Mockito.never()).close();
+        Mockito.verify(response).close();
     }
 
     @Test
@@ -394,9 +323,17 @@ public class TestHttpService {
         final ClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
 
         Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_NO_CONTENT,
"No Content");
         Mockito.when(responseFactory.newHttpResponse(200)).thenReturn(response);
-        Mockito.when(handlerResolver.resolve(request, context)).thenReturn(requestHandler);
+        Mockito.when(handlerResolver.resolve(request, context)).thenReturn(new HttpRequestHandler()
{
+
+            @Override
+            public void handle(
+                    final ClassicHttpRequest request,
+                    final ClassicHttpResponse response,
+                    final HttpContext context) throws HttpException, IOException {
+                response.setCode(HttpStatus.SC_NO_CONTENT);
+            }
+        });
         Mockito.when(connReuseStrategy.keepAlive(request, response, context)).thenReturn(Boolean.TRUE);
 
         httpservice.handleRequest(conn, context);
@@ -404,12 +341,12 @@ public class TestHttpService {
         Assert.assertSame(request, context.getRequest());
 
         Mockito.verify(httprocessor).process(response, response.getEntity(), context);
-        Mockito.verify(requestHandler).handle(request, response, context);
 
         Mockito.verify(conn).sendResponseHeader(response);
         Mockito.verify(conn, Mockito.never()).sendResponseEntity(Mockito.<ClassicHttpResponse>any());
         Mockito.verify(conn).flush();
         Mockito.verify(conn, Mockito.never()).close();
+        Mockito.verify(response).close();
     }
 
     @Test
@@ -418,7 +355,6 @@ public class TestHttpService {
         final ClassicHttpRequest request = new BasicClassicHttpRequest("HEAD", "/");
 
         Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
-        final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK");
         Mockito.when(responseFactory.newHttpResponse(200)).thenReturn(response);
         Mockito.when(handlerResolver.resolve(request, context)).thenReturn(requestHandler);
         Mockito.when(connReuseStrategy.keepAlive(request, response, context)).thenReturn(Boolean.TRUE);
@@ -434,6 +370,7 @@ public class TestHttpService {
         Mockito.verify(conn, Mockito.never()).sendResponseEntity(Mockito.<ClassicHttpResponse>any());
         Mockito.verify(conn).flush();
         Mockito.verify(conn, Mockito.never()).close();
+        Mockito.verify(response).close();
     }
 
 }


Mime
View raw message