View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.protocol;
29  
30  import java.io.InputStream;
31  
32  import org.apache.http.ConnectionReuseStrategy;
33  import org.apache.http.HttpEntityEnclosingRequest;
34  import org.apache.http.HttpException;
35  import org.apache.http.HttpRequest;
36  import org.apache.http.HttpResponse;
37  import org.apache.http.HttpResponseFactory;
38  import org.apache.http.HttpServerConnection;
39  import org.apache.http.HttpStatus;
40  import org.apache.http.HttpVersion;
41  import org.apache.http.MethodNotSupportedException;
42  import org.apache.http.ProtocolException;
43  import org.apache.http.UnsupportedHttpVersionException;
44  import org.apache.http.entity.InputStreamEntity;
45  import org.apache.http.message.BasicHttpEntityEnclosingRequest;
46  import org.apache.http.message.BasicHttpRequest;
47  import org.apache.http.message.BasicHttpResponse;
48  import org.junit.Assert;
49  import org.junit.Test;
50  import org.mockito.Mockito;
51  
52  public class TestHttpService {
53  
54      @Test
55      public void testInvalidInitialization() throws Exception {
56          final ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
57          final HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
58          final HttpRequestHandlerMapper handlerResolver = Mockito.mock(HttpRequestHandlerMapper.class);
59          try {
60              new HttpService(
61                      null,
62                      connReuseStrategy,
63                      responseFactory,
64                      handlerResolver);
65              Assert.fail("IllegalArgumentException expected");
66          } catch (final IllegalArgumentException expected) {
67          }
68      }
69  
70      @Test
71      public void testBasicExecution() throws Exception {
72          final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
73          final ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
74          final HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
75          final HttpRequestHandlerMapper handlerResolver = Mockito.mock(HttpRequestHandlerMapper.class);
76  
77          final HttpService httpservice = new HttpService(
78                  httprocessor,
79                  connReuseStrategy,
80                  responseFactory,
81                  handlerResolver);
82          final HttpCoreContext context = HttpCoreContext.create();
83          final HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
84          final HttpRequest request = new BasicHttpRequest("GET", "/");
85          Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
86          final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
87          Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 200, context)).thenReturn(response);
88          Mockito.when(connReuseStrategy.keepAlive(response, context)).thenReturn(Boolean.FALSE);
89  
90          httpservice.handleRequest(conn, context);
91  
92          Assert.assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, response.getStatusLine().getStatusCode());
93  
94          Assert.assertSame(conn, context.getConnection());
95          Assert.assertSame(request, context.getRequest());
96          Assert.assertSame(response, context.getResponse());
97  
98          Mockito.verify(httprocessor).process(request, context);
99          Mockito.verify(httprocessor).process(response, context);
100         Mockito.verify(conn).sendResponseHeader(response);
101         Mockito.verify(conn).sendResponseEntity(response);
102         Mockito.verify(conn).flush();
103         Mockito.verify(conn).close();
104     }
105 
106     @Test
107     public void testBasicExecutionHTTP10() throws Exception {
108         final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
109         final ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
110         final HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
111         final HttpRequestHandlerMapper handlerResolver = Mockito.mock(HttpRequestHandlerMapper.class);
112 
113         final HttpService httpservice = new HttpService(
114                 httprocessor,
115                 connReuseStrategy,
116                 responseFactory,
117                 handlerResolver);
118         final HttpCoreContext context = HttpCoreContext.create();
119         final HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
120         final HttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_0);
121         Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
122         final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_0, 200, "OK");
123         Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 200, context)).thenReturn(response);
124         Mockito.when(connReuseStrategy.keepAlive(response, context)).thenReturn(Boolean.FALSE);
125 
126         httpservice.handleRequest(conn, context);
127 
128         Mockito.verify(responseFactory).newHttpResponse(HttpVersion.HTTP_1_1, 200, context);
129     }
130 
131     @Test
132     public void testBasicProtocolDowngrade() throws Exception {
133         final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
134         final ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
135         final HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
136         final HttpRequestHandlerMapper handlerResolver = Mockito.mock(HttpRequestHandlerMapper.class);
137 
138         final HttpService httpservice = new HttpService(
139                 httprocessor,
140                 connReuseStrategy,
141                 responseFactory,
142                 handlerResolver);
143         final HttpCoreContext context = HttpCoreContext.create();
144         final HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
145         final HttpRequest request = new BasicHttpRequest("GET", "/", new HttpVersion(20, 45));
146         Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
147         final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
148         Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 200, context)).thenReturn(response);
149         Mockito.when(connReuseStrategy.keepAlive(response, context)).thenReturn(Boolean.FALSE);
150 
151         httpservice.handleRequest(conn, context);
152 
153         Mockito.verify(responseFactory).newHttpResponse(HttpVersion.HTTP_1_1, 200, context);
154     }
155 
156     @Test
157     public void testExecutionEntityEnclosingRequest() throws Exception {
158         final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
159         final ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
160         final HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
161         final HttpRequestHandlerMapper handlerResolver = Mockito.mock(HttpRequestHandlerMapper.class);
162 
163         final HttpService httpservice = new HttpService(
164                 httprocessor,
165                 connReuseStrategy,
166                 responseFactory,
167                 handlerResolver);
168         final HttpCoreContext context = HttpCoreContext.create();
169         final HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
170         final HttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/");
171         final InputStream instream = Mockito.mock(InputStream.class);
172         final InputStreamEntity entity = new InputStreamEntity(instream, -1);
173         request.setEntity(entity);
174 
175         Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
176         final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
177         Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 200, context)).thenReturn(response);
178         Mockito.when(connReuseStrategy.keepAlive(response, context)).thenReturn(Boolean.FALSE);
179 
180         httpservice.handleRequest(conn, context);
181 
182         Assert.assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, response.getStatusLine().getStatusCode());
183 
184         Assert.assertSame(conn, context.getConnection());
185         Assert.assertSame(request, context.getRequest());
186         Assert.assertSame(response, context.getResponse());
187 
188         Mockito.verify(conn).receiveRequestEntity(request);
189         Mockito.verify(httprocessor).process(request, context);
190         Mockito.verify(instream).close();
191         Mockito.verify(httprocessor).process(response, context);
192         Mockito.verify(conn).sendResponseHeader(response);
193         Mockito.verify(conn).sendResponseEntity(response);
194         Mockito.verify(conn).flush();
195         Mockito.verify(conn).close();
196     }
197 
198     @Test
199     public void testExecutionEntityEnclosingRequestWithExpectContinue() throws Exception {
200         final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
201         final ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
202         final HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
203         final HttpRequestHandlerMapper handlerResolver = Mockito.mock(HttpRequestHandlerMapper.class);
204 
205         final HttpService httpservice = new HttpService(
206                 httprocessor,
207                 connReuseStrategy,
208                 responseFactory,
209                 handlerResolver);
210         final HttpCoreContext context = HttpCoreContext.create();
211         final HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
212         final HttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/");
213         request.addHeader(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE);
214         final InputStream instream = Mockito.mock(InputStream.class);
215         final InputStreamEntity entity = new InputStreamEntity(instream, -1);
216         request.setEntity(entity);
217 
218         Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
219         final HttpResponse resp100 = new BasicHttpResponse(HttpVersion.HTTP_1_1, 100, "Continue");
220         Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 100, context)).thenReturn(resp100);
221         final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
222         Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 200, context)).thenReturn(response);
223         Mockito.when(connReuseStrategy.keepAlive(response, context)).thenReturn(Boolean.FALSE);
224 
225         httpservice.handleRequest(conn, context);
226 
227         Assert.assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, response.getStatusLine().getStatusCode());
228 
229         Assert.assertSame(conn, context.getConnection());
230         Assert.assertSame(request, context.getRequest());
231         Assert.assertSame(response, context.getResponse());
232 
233         Mockito.verify(conn).sendResponseHeader(resp100);
234         Mockito.verify(conn).receiveRequestEntity(request);
235         Mockito.verify(httprocessor).process(request, context);
236         Mockito.verify(instream).close();
237         Mockito.verify(httprocessor).process(response, context);
238         Mockito.verify(conn).sendResponseHeader(response);
239         Mockito.verify(conn).sendResponseEntity(response);
240         Mockito.verify(conn, Mockito.times(2)).flush();
241         Mockito.verify(conn).close();
242     }
243 
244     @Test
245     public void testExecutionEntityEnclosingRequestCustomExpectationVerifier() throws Exception {
246         final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
247         final ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
248         final HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
249         final HttpRequestHandlerMapper handlerResolver = Mockito.mock(HttpRequestHandlerMapper.class);
250 
251         final HttpExpectationVerifier expectationVerifier = new HttpExpectationVerifier() {
252 
253             @Override
254             public void verify(
255                     final HttpRequest request,
256                     final HttpResponse response,
257                     final HttpContext context) throws HttpException {
258                 response.setStatusCode(HttpStatus.SC_UNAUTHORIZED);
259             }
260 
261         };
262 
263         final HttpService httpservice = new HttpService(
264                 httprocessor,
265                 connReuseStrategy,
266                 responseFactory,
267                 handlerResolver,
268                 expectationVerifier);
269         final HttpCoreContext context = HttpCoreContext.create();
270         final HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
271         final HttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/");
272         request.addHeader(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE);
273         final InputStream instream = Mockito.mock(InputStream.class);
274         final InputStreamEntity entity = new InputStreamEntity(instream, -1);
275         request.setEntity(entity);
276 
277         Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
278         final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 100, "Continue");
279         Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 100, context)).thenReturn(response);
280         Mockito.when(connReuseStrategy.keepAlive(response, context)).thenReturn(Boolean.FALSE);
281 
282         httpservice.handleRequest(conn, context);
283 
284         Assert.assertSame(conn, context.getConnection());
285         Assert.assertSame(request, context.getRequest());
286         Assert.assertSame(response, context.getResponse());
287 
288         Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode());
289 
290         Mockito.verify(conn).sendResponseHeader(response);
291         Mockito.verify(conn, Mockito.never()).receiveRequestEntity(request);
292         Mockito.verify(httprocessor).process(response, context);
293         Mockito.verify(conn).sendResponseHeader(response);
294         Mockito.verify(conn).sendResponseEntity(response);
295         Mockito.verify(conn).flush();
296         Mockito.verify(conn).close();
297     }
298 
299     @Test
300     public void testExecutionExceptionInCustomExpectationVerifier() throws Exception {
301         final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
302         final ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
303         final HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
304         final HttpExpectationVerifier expectationVerifier = Mockito.mock(HttpExpectationVerifier.class);
305         final HttpRequestHandlerMapper handlerResolver = Mockito.mock(HttpRequestHandlerMapper.class);
306 
307         final HttpService httpservice = new HttpService(
308                 httprocessor,
309                 connReuseStrategy,
310                 responseFactory,
311                 handlerResolver,
312                 expectationVerifier);
313         final HttpCoreContext context = HttpCoreContext.create();
314         final HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
315         final HttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST", "/");
316         request.addHeader(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE);
317         final InputStream instream = Mockito.mock(InputStream.class);
318         final InputStreamEntity entity = new InputStreamEntity(instream, -1);
319         request.setEntity(entity);
320 
321         Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
322         final HttpResponse resp100 = new BasicHttpResponse(HttpVersion.HTTP_1_1, 100, "Continue");
323         Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 100, context)).thenReturn(resp100);
324         final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_0, 500, "Oppsie");
325         Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_0, 500, context)).thenReturn(response);
326         Mockito.doThrow(new HttpException("Oopsie")).when(expectationVerifier).verify(request, resp100, context);
327         Mockito.when(connReuseStrategy.keepAlive(response, context)).thenReturn(Boolean.FALSE);
328 
329         httpservice.handleRequest(conn, context);
330 
331         Assert.assertSame(conn, context.getConnection());
332         Assert.assertSame(request, context.getRequest());
333         Assert.assertSame(response, context.getResponse());
334 
335         Assert.assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, response.getStatusLine().getStatusCode());
336 
337         Mockito.verify(conn).sendResponseHeader(response);
338         Mockito.verify(conn, Mockito.never()).receiveRequestEntity(request);
339         Mockito.verify(httprocessor).process(response, context);
340         Mockito.verify(conn).sendResponseHeader(response);
341         Mockito.verify(conn).sendResponseEntity(response);
342         Mockito.verify(conn).flush();
343         Mockito.verify(conn).close();
344     }
345 
346     @Test
347     public void testMethodNotSupported() throws Exception {
348         final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
349         final ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
350         final HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
351         final HttpRequestHandlerMapper handlerResolver = Mockito.mock(HttpRequestHandlerMapper.class);
352         final HttpRequestHandler requestHandler = Mockito.mock(HttpRequestHandler.class);
353 
354         final HttpService httpservice = new HttpService(
355                 httprocessor,
356                 connReuseStrategy,
357                 responseFactory,
358                 handlerResolver);
359         final HttpCoreContext context = HttpCoreContext.create();
360         final HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
361         final HttpRequest request = new BasicHttpRequest("whatever", "/");
362 
363         Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
364         final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
365         Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 200, context)).thenReturn(response);
366         final HttpResponse error = new BasicHttpResponse(HttpVersion.HTTP_1_0, 500, "Oppsie");
367         Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_0, 500, context)).thenReturn(error);
368         Mockito.when(handlerResolver.lookup(request)).thenReturn(requestHandler);
369         Mockito.doThrow(new MethodNotSupportedException("whatever")).when(
370                 requestHandler).handle(request, response, context);
371         Mockito.when(connReuseStrategy.keepAlive(error, context)).thenReturn(Boolean.FALSE);
372 
373         httpservice.handleRequest(conn, context);
374 
375         Assert.assertSame(conn, context.getConnection());
376         Assert.assertSame(request, context.getRequest());
377         Assert.assertSame(error, context.getResponse());
378 
379         Assert.assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, error.getStatusLine().getStatusCode());
380 
381         Mockito.verify(conn).sendResponseHeader(error);
382         Mockito.verify(httprocessor).process(error, context);
383         Mockito.verify(conn).sendResponseHeader(error);
384         Mockito.verify(conn).sendResponseEntity(error);
385         Mockito.verify(conn).flush();
386         Mockito.verify(conn).close();
387     }
388 
389     @Test
390     public void testUnsupportedHttpVersionException() throws Exception {
391         final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
392         final ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
393         final HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
394         final HttpRequestHandlerMapper handlerResolver = Mockito.mock(HttpRequestHandlerMapper.class);
395         final HttpRequestHandler requestHandler = Mockito.mock(HttpRequestHandler.class);
396 
397         final HttpService httpservice = new HttpService(
398                 httprocessor,
399                 connReuseStrategy,
400                 responseFactory,
401                 handlerResolver);
402         final HttpCoreContext context = HttpCoreContext.create();
403         final HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
404         final HttpRequest request = new BasicHttpRequest("whatever", "/");
405 
406         Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
407         final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
408         Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 200, context)).thenReturn(response);
409         final HttpResponse error = new BasicHttpResponse(HttpVersion.HTTP_1_0, 500, "Oppsie");
410         Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_0, 500, context)).thenReturn(error);
411         Mockito.when(handlerResolver.lookup(request)).thenReturn(requestHandler);
412         Mockito.doThrow(new UnsupportedHttpVersionException()).when(
413                 requestHandler).handle(request, response, context);
414         Mockito.when(connReuseStrategy.keepAlive(error, context)).thenReturn(Boolean.FALSE);
415 
416         httpservice.handleRequest(conn, context);
417 
418         Assert.assertSame(conn, context.getConnection());
419         Assert.assertSame(request, context.getRequest());
420         Assert.assertSame(error, context.getResponse());
421 
422         Assert.assertEquals(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED, error.getStatusLine().getStatusCode());
423 
424         Mockito.verify(conn).sendResponseHeader(error);
425         Mockito.verify(httprocessor).process(error, context);
426         Mockito.verify(conn).sendResponseHeader(error);
427         Mockito.verify(conn).sendResponseEntity(error);
428         Mockito.verify(conn).flush();
429         Mockito.verify(conn).close();
430     }
431 
432     @Test
433     public void testProtocolException() throws Exception {
434         final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
435         final ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
436         final HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
437         final HttpRequestHandlerMapper handlerResolver = Mockito.mock(HttpRequestHandlerMapper.class);
438         final HttpRequestHandler requestHandler = Mockito.mock(HttpRequestHandler.class);
439 
440         final HttpService httpservice = new HttpService(
441                 httprocessor,
442                 connReuseStrategy,
443                 responseFactory,
444                 handlerResolver);
445         final HttpCoreContext context = HttpCoreContext.create();
446         final HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
447         final HttpRequest request = new BasicHttpRequest("whatever", "/");
448 
449         Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
450         final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
451         Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 200, context)).thenReturn(response);
452         final HttpResponse error = new BasicHttpResponse(HttpVersion.HTTP_1_0, 500, "Oppsie");
453         Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_0, 500, context)).thenReturn(error);
454         Mockito.when(handlerResolver.lookup(request)).thenReturn(requestHandler);
455         Mockito.doThrow(new ProtocolException("oh, this world is wrong")).when(
456                 requestHandler).handle(request, response, context);
457         Mockito.when(connReuseStrategy.keepAlive(error, context)).thenReturn(Boolean.FALSE);
458 
459         httpservice.handleRequest(conn, context);
460 
461         Assert.assertSame(conn, context.getConnection());
462         Assert.assertSame(request, context.getRequest());
463         Assert.assertSame(error, context.getResponse());
464 
465         Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, error.getStatusLine().getStatusCode());
466 
467         Mockito.verify(conn).sendResponseHeader(error);
468         Mockito.verify(httprocessor).process(error, context);
469         Mockito.verify(conn).sendResponseHeader(error);
470         Mockito.verify(conn).sendResponseEntity(error);
471         Mockito.verify(conn).flush();
472         Mockito.verify(conn).close();
473     }
474 
475     @Test
476     public void testConnectionKeepAlive() throws Exception {
477         final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
478         final ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
479         final HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
480         final HttpRequestHandlerMapper handlerResolver = Mockito.mock(HttpRequestHandlerMapper.class);
481         final HttpRequestHandler requestHandler = Mockito.mock(HttpRequestHandler.class);
482 
483         final HttpService httpservice = new HttpService(
484                 httprocessor,
485                 connReuseStrategy,
486                 responseFactory,
487                 handlerResolver);
488         final HttpCoreContext context = HttpCoreContext.create();
489         final HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
490         final HttpRequest request = new BasicHttpRequest("GET", "/");
491         Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
492         final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
493         Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 200, context)).thenReturn(response);
494         Mockito.when(handlerResolver.lookup(request)).thenReturn(requestHandler);
495         Mockito.when(connReuseStrategy.keepAlive(response, context)).thenReturn(Boolean.TRUE);
496 
497         httpservice.handleRequest(conn, context);
498 
499         Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
500 
501         Assert.assertSame(conn, context.getConnection());
502         Assert.assertSame(request, context.getRequest());
503         Assert.assertSame(response, context.getResponse());
504 
505         Mockito.verify(httprocessor).process(request, context);
506         Mockito.verify(httprocessor).process(response, context);
507         Mockito.verify(conn).sendResponseHeader(response);
508         Mockito.verify(conn).sendResponseEntity(response);
509         Mockito.verify(conn).flush();
510         Mockito.verify(conn, Mockito.never()).close();
511     }
512 
513     @Test
514     public void testNoContentResponse() throws Exception {
515         final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
516         final ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
517         final HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
518         final HttpRequestHandlerMapper handlerResolver = Mockito.mock(HttpRequestHandlerMapper.class);
519         final HttpRequestHandler requestHandler = Mockito.mock(HttpRequestHandler.class);
520 
521         final HttpService httpservice = new HttpService(
522                 httprocessor,
523                 connReuseStrategy,
524                 responseFactory,
525                 handlerResolver);
526         final HttpCoreContext context = HttpCoreContext.create();
527         final HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
528         final HttpRequest request = new BasicHttpRequest("GET", "/");
529 
530         Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
531         final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_NO_CONTENT, "No Content");
532         Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 200, context)).thenReturn(response);
533         Mockito.when(handlerResolver.lookup(request)).thenReturn(requestHandler);
534         Mockito.when(connReuseStrategy.keepAlive(response, context)).thenReturn(Boolean.TRUE);
535 
536         httpservice.handleRequest(conn, context);
537 
538         Assert.assertSame(conn, context.getConnection());
539         Assert.assertSame(request, context.getRequest());
540 
541         Mockito.verify(httprocessor).process(response, context);
542         Mockito.verify(requestHandler).handle(request, response, context);
543 
544         Mockito.verify(conn).sendResponseHeader(response);
545         Mockito.verify(conn, Mockito.never()).sendResponseEntity(Mockito.<HttpResponse>any());
546         Mockito.verify(conn).flush();
547         Mockito.verify(conn, Mockito.never()).close();
548     }
549 
550     @Test
551     public void testResponseToHead() throws Exception {
552         final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
553         final ConnectionReuseStrategy connReuseStrategy = Mockito.mock(ConnectionReuseStrategy.class);
554         final HttpResponseFactory responseFactory = Mockito.mock(HttpResponseFactory.class);
555         final HttpRequestHandlerMapper handlerResolver = Mockito.mock(HttpRequestHandlerMapper.class);
556         final HttpRequestHandler requestHandler = Mockito.mock(HttpRequestHandler.class);
557 
558         final HttpService httpservice = new HttpService(
559                 httprocessor,
560                 connReuseStrategy,
561                 responseFactory,
562                 handlerResolver);
563         final HttpCoreContext context = HttpCoreContext.create();
564         final HttpServerConnection conn = Mockito.mock(HttpServerConnection.class);
565         final HttpRequest request = new BasicHttpRequest("HEAD", "/");
566 
567         Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
568         final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
569         Mockito.when(responseFactory.newHttpResponse(HttpVersion.HTTP_1_1, 200, context)).thenReturn(response);
570         Mockito.when(handlerResolver.lookup(request)).thenReturn(requestHandler);
571         Mockito.when(connReuseStrategy.keepAlive(response, context)).thenReturn(Boolean.TRUE);
572 
573         httpservice.handleRequest(conn, context);
574 
575         Assert.assertSame(conn, context.getConnection());
576         Assert.assertSame(request, context.getRequest());
577 
578         Mockito.verify(httprocessor).process(response, context);
579         Mockito.verify(requestHandler).handle(request, response, context);
580 
581         Mockito.verify(conn).sendResponseHeader(response);
582         Mockito.verify(conn, Mockito.never()).sendResponseEntity(Mockito.<HttpResponse>any());
583         Mockito.verify(conn).flush();
584         Mockito.verify(conn, Mockito.never()).close();
585     }
586 
587 }