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.hc.core5.http.impl.io;
29  
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.util.List;
33  
34  import org.apache.hc.core5.http.ClassicHttpRequest;
35  import org.apache.hc.core5.http.ClassicHttpResponse;
36  import org.apache.hc.core5.http.ConnectionReuseStrategy;
37  import org.apache.hc.core5.http.HeaderElements;
38  import org.apache.hc.core5.http.HttpException;
39  import org.apache.hc.core5.http.HttpHeaders;
40  import org.apache.hc.core5.http.HttpRequestMapper;
41  import org.apache.hc.core5.http.HttpResponse;
42  import org.apache.hc.core5.http.HttpResponseFactory;
43  import org.apache.hc.core5.http.HttpStatus;
44  import org.apache.hc.core5.http.MethodNotSupportedException;
45  import org.apache.hc.core5.http.ProtocolException;
46  import org.apache.hc.core5.http.UnsupportedHttpVersionException;
47  import org.apache.hc.core5.http.io.HttpRequestHandler;
48  import org.apache.hc.core5.http.io.HttpServerConnection;
49  import org.apache.hc.core5.http.io.entity.InputStreamEntity;
50  import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
51  import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
52  import org.apache.hc.core5.http.protocol.HttpContext;
53  import org.apache.hc.core5.http.protocol.HttpCoreContext;
54  import org.apache.hc.core5.http.protocol.HttpProcessor;
55  import org.junit.Assert;
56  import org.junit.Before;
57  import org.junit.Test;
58  import org.mockito.ArgumentCaptor;
59  import org.mockito.ArgumentMatcher;
60  import org.mockito.Mock;
61  import org.mockito.Mockito;
62  import org.mockito.MockitoAnnotations;
63  import org.mockito.Spy;
64  
65  public class TestHttpService {
66  
67      @Mock
68      private HttpProcessor httprocessor;
69      @Mock
70      private ConnectionReuseStrategy connReuseStrategy;
71      @Spy
72      private ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK);
73      @Mock
74      private HttpResponseFactory<ClassicHttpResponse> responseFactory;
75      @Mock
76      private HttpRequestMapper<HttpRequestHandler> handlerResolver;
77      @Mock
78      private HttpRequestHandler requestHandler;
79      @Mock
80      private HttpServerConnection conn;
81  
82      private HttpService httpservice;
83  
84      @Before
85      public void settup() {
86          MockitoAnnotations.initMocks(this);
87          httpservice = new HttpService(
88                  httprocessor,
89                  handlerResolver,
90                  connReuseStrategy,
91                  responseFactory);
92      }
93  
94      @Test
95      public void testInvalidInitialization() throws Exception {
96          try {
97              new HttpService(
98                      null,
99                      handlerResolver,
100                     connReuseStrategy,
101                     responseFactory);
102             Assert.fail("IllegalArgumentException expected");
103         } catch (final IllegalArgumentException expected) {
104         }
105     }
106 
107     @Test
108     public void testBasicExecution() throws Exception {
109         final HttpCoreContext context = HttpCoreContext.create();
110         final ClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
111         Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
112         Mockito.when(responseFactory.newHttpResponse(200)).thenReturn(response);
113         Mockito.when(connReuseStrategy.keepAlive(request, response, context)).thenReturn(Boolean.FALSE);
114 
115         httpservice.handleRequest(conn, context);
116 
117         Assert.assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, response.getCode());
118 
119         Assert.assertSame(request, context.getRequest());
120         Assert.assertSame(response, context.getResponse());
121 
122         Mockito.verify(httprocessor).process(request, request.getEntity(), context);
123         Mockito.verify(httprocessor).process(response, response.getEntity(), context);
124         Mockito.verify(conn).sendResponseHeader(response);
125         Mockito.verify(conn).sendResponseEntity(response);
126         Mockito.verify(conn).flush();
127         Mockito.verify(conn).close();
128         Mockito.verify(response).close();
129     }
130 
131     @Test
132     public void testExecutionEntityEnclosingRequest() throws Exception {
133         final HttpCoreContext context = HttpCoreContext.create();
134         final ClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
135         final InputStream instream = Mockito.mock(InputStream.class);
136         final InputStreamEntity entity = new InputStreamEntity(instream, -1);
137         request.setEntity(entity);
138 
139         Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
140         Mockito.when(responseFactory.newHttpResponse(200)).thenReturn(response);
141         Mockito.when(connReuseStrategy.keepAlive(Mockito.eq(request), Mockito.argThat(new ArgumentMatcher<HttpResponse>() {
142 
143             @Override
144             public boolean matches(final HttpResponse errorResponse) {
145                 return errorResponse.getCode() == HttpStatus.SC_NOT_IMPLEMENTED;
146             }
147 
148         }), Mockito.eq(context))).thenReturn(Boolean.TRUE);
149 
150         httpservice.handleRequest(conn, context);
151         final ArgumentCaptor<ClassicHttpResponse> responseCaptor = ArgumentCaptor.forClass(ClassicHttpResponse.class);
152         Mockito.verify(conn).sendResponseHeader(responseCaptor.capture());
153         final ClassicHttpResponse response = responseCaptor.getValue();
154         Assert.assertNotNull(response);
155 
156         Assert.assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, response.getCode());
157 
158         Assert.assertSame(request, context.getRequest());
159 
160         Mockito.verify(httprocessor).process(request, request.getEntity(), context);
161         Mockito.verify(instream).close();
162         Mockito.verify(httprocessor).process(response, response.getEntity(), context);
163         Mockito.verify(conn).sendResponseHeader(response);
164         Mockito.verify(conn).sendResponseEntity(response);
165         Mockito.verify(conn).flush();
166         Mockito.verify(conn, Mockito.never()).close();
167         Mockito.verify(response).close();
168     }
169 
170     @Test
171     public void testExecutionEntityEnclosingRequestWithExpectContinue() throws Exception {
172         final HttpCoreContext context = HttpCoreContext.create();
173         final ClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/");
174         request.addHeader(HttpHeaders.EXPECT, HeaderElements.CONTINUE);
175         final InputStream instream = Mockito.mock(InputStream.class);
176         final InputStreamEntity entity = new InputStreamEntity(instream, -1);
177         request.setEntity(entity);
178 
179         Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
180         Mockito.when(responseFactory.newHttpResponse(200)).thenReturn(response);
181         Mockito.when(connReuseStrategy.keepAlive(Mockito.eq(request), Mockito.argThat(new ArgumentMatcher<HttpResponse>() {
182 
183             @Override
184             public boolean matches(final HttpResponse errorResponse) {
185                 return errorResponse.getCode() == HttpStatus.SC_NOT_IMPLEMENTED;
186             }
187 
188         }), Mockito.eq(context))).thenReturn(Boolean.TRUE);
189 
190         httpservice.handleRequest(conn, context);
191         final ArgumentCaptor<ClassicHttpResponse> responseCaptor = ArgumentCaptor.forClass(ClassicHttpResponse.class);
192         Mockito.verify(conn, Mockito.times(2)).sendResponseHeader(responseCaptor.capture());
193         final List<ClassicHttpResponse> responses = responseCaptor.getAllValues();
194         Assert.assertNotNull(responses);
195         Assert.assertEquals(2, responses.size());
196         final ClassicHttpResponse ack = responses.get(0);
197         final ClassicHttpResponse response = responses.get(1);
198 
199         Assert.assertEquals(HttpStatus.SC_CONTINUE, ack.getCode());
200         Assert.assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, response.getCode());
201 
202         Assert.assertSame(request, context.getRequest());
203 
204         Mockito.verify(httprocessor).process(request, request.getEntity(), context);
205         Mockito.verify(instream).close();
206         Mockito.verify(httprocessor).process(response, response.getEntity(), context);
207         Mockito.verify(conn).sendResponseHeader(response);
208         Mockito.verify(conn).sendResponseEntity(response);
209         Mockito.verify(conn, Mockito.times(2)).flush();
210         Mockito.verify(conn, Mockito.never()).close();
211         Mockito.verify(response).close();
212     }
213 
214     @Test
215     public void testMethodNotSupported() throws Exception {
216         final HttpCoreContext context = HttpCoreContext.create();
217         final ClassicHttpRequest request = new BasicClassicHttpRequest("whatever", "/");
218 
219         Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
220         Mockito.when(responseFactory.newHttpResponse(200)).thenReturn(response);
221         Mockito.when(handlerResolver.resolve(request, context)).thenReturn(requestHandler);
222         Mockito.doThrow(new MethodNotSupportedException("whatever")).when(
223                 requestHandler).handle(request, response, context);
224 
225         httpservice.handleRequest(conn, context);
226         final ArgumentCaptor<ClassicHttpResponse> responseCaptor = ArgumentCaptor.forClass(ClassicHttpResponse.class);
227         Mockito.verify(conn).sendResponseHeader(responseCaptor.capture());
228         final ClassicHttpResponse error = responseCaptor.getValue();
229         Assert.assertNotNull(error);
230 
231         Assert.assertSame(request, context.getRequest());
232 
233         Assert.assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, error.getCode());
234 
235         Mockito.verify(httprocessor).process(error, error.getEntity(), context);
236         Mockito.verify(conn).sendResponseHeader(error);
237         Mockito.verify(conn).sendResponseEntity(error);
238         Mockito.verify(conn).close();
239     }
240 
241     @Test
242     public void testUnsupportedHttpVersionException() throws Exception {
243         final HttpCoreContext context = HttpCoreContext.create();
244         final ClassicHttpRequest request = new BasicClassicHttpRequest("whatever", "/");
245 
246         Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
247         Mockito.when(responseFactory.newHttpResponse(200)).thenReturn(response);
248         Mockito.when(handlerResolver.resolve(request, context)).thenReturn(requestHandler);
249         Mockito.doThrow(new UnsupportedHttpVersionException()).when(
250                 requestHandler).handle(request, response, context);
251 
252         httpservice.handleRequest(conn, context);
253         final ArgumentCaptor<ClassicHttpResponse> responseCaptor = ArgumentCaptor.forClass(ClassicHttpResponse.class);
254         Mockito.verify(conn).sendResponseHeader(responseCaptor.capture());
255         final ClassicHttpResponse error = responseCaptor.getValue();
256         Assert.assertNotNull(error);
257 
258         Assert.assertSame(request, context.getRequest());
259 
260         Assert.assertEquals(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED, error.getCode());
261 
262         Mockito.verify(httprocessor).process(error, error.getEntity(), context);
263         Mockito.verify(conn).sendResponseHeader(error);
264         Mockito.verify(conn).sendResponseEntity(error);
265         Mockito.verify(conn).close();
266     }
267 
268     @Test
269     public void testProtocolException() throws Exception {
270         final HttpCoreContext context = HttpCoreContext.create();
271         final ClassicHttpRequest request = new BasicClassicHttpRequest("whatever", "/");
272 
273         Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
274         Mockito.when(responseFactory.newHttpResponse(200)).thenReturn(response);
275         Mockito.when(handlerResolver.resolve(request, context)).thenReturn(requestHandler);
276         Mockito.doThrow(new ProtocolException("oh, this world is wrong")).when(
277                 requestHandler).handle(request, response, context);
278 
279         httpservice.handleRequest(conn, context);
280         final ArgumentCaptor<ClassicHttpResponse> responseCaptor = ArgumentCaptor.forClass(ClassicHttpResponse.class);
281         Mockito.verify(conn).sendResponseHeader(responseCaptor.capture());
282         final ClassicHttpResponse error = responseCaptor.getValue();
283         Assert.assertNotNull(error);
284 
285         Assert.assertSame(request, context.getRequest());
286 
287         Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, error.getCode());
288 
289         Mockito.verify(httprocessor).process(error, error.getEntity(), context);
290         Mockito.verify(conn).sendResponseHeader(error);
291         Mockito.verify(conn).sendResponseEntity(error);
292         Mockito.verify(conn).close();
293     }
294 
295     @Test
296     public void testConnectionKeepAlive() throws Exception {
297         final HttpCoreContext context = HttpCoreContext.create();
298         final ClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
299         Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
300         Mockito.when(responseFactory.newHttpResponse(200)).thenReturn(response);
301         Mockito.when(handlerResolver.resolve(request, context)).thenReturn(requestHandler);
302         Mockito.when(connReuseStrategy.keepAlive(request, response, context)).thenReturn(Boolean.TRUE);
303 
304         httpservice.handleRequest(conn, context);
305 
306         Assert.assertEquals(HttpStatus.SC_OK, response.getCode());
307 
308         Assert.assertSame(request, context.getRequest());
309         Assert.assertSame(response, context.getResponse());
310 
311         Mockito.verify(httprocessor).process(request, request.getEntity(), context);
312         Mockito.verify(httprocessor).process(response, response.getEntity(), context);
313         Mockito.verify(conn).sendResponseHeader(response);
314         Mockito.verify(conn).sendResponseEntity(response);
315         Mockito.verify(conn).flush();
316         Mockito.verify(conn, Mockito.never()).close();
317         Mockito.verify(response).close();
318     }
319 
320     @Test
321     public void testNoContentResponse() throws Exception {
322         final HttpCoreContext context = HttpCoreContext.create();
323         final ClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/");
324 
325         Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
326         Mockito.when(responseFactory.newHttpResponse(200)).thenReturn(response);
327         Mockito.when(handlerResolver.resolve(request, context)).thenReturn(new HttpRequestHandler() {
328 
329             @Override
330             public void handle(
331                     final ClassicHttpRequest request,
332                     final ClassicHttpResponse response,
333                     final HttpContext context) throws HttpException, IOException {
334                 response.setCode(HttpStatus.SC_NO_CONTENT);
335             }
336         });
337         Mockito.when(connReuseStrategy.keepAlive(request, response, context)).thenReturn(Boolean.TRUE);
338 
339         httpservice.handleRequest(conn, context);
340 
341         Assert.assertSame(request, context.getRequest());
342 
343         Mockito.verify(httprocessor).process(response, response.getEntity(), context);
344 
345         Mockito.verify(conn).sendResponseHeader(response);
346         Mockito.verify(conn, Mockito.never()).sendResponseEntity(Mockito.<ClassicHttpResponse>any());
347         Mockito.verify(conn).flush();
348         Mockito.verify(conn, Mockito.never()).close();
349         Mockito.verify(response).close();
350     }
351 
352     @Test
353     public void testResponseToHead() throws Exception {
354         final HttpCoreContext context = HttpCoreContext.create();
355         final ClassicHttpRequest request = new BasicClassicHttpRequest("HEAD", "/");
356 
357         Mockito.when(conn.receiveRequestHeader()).thenReturn(request);
358         Mockito.when(responseFactory.newHttpResponse(200)).thenReturn(response);
359         Mockito.when(handlerResolver.resolve(request, context)).thenReturn(requestHandler);
360         Mockito.when(connReuseStrategy.keepAlive(request, response, context)).thenReturn(Boolean.TRUE);
361 
362         httpservice.handleRequest(conn, context);
363 
364         Assert.assertSame(request, context.getRequest());
365 
366         Mockito.verify(httprocessor).process(response, response.getEntity(), context);
367         Mockito.verify(requestHandler).handle(request, response, context);
368 
369         Mockito.verify(conn).sendResponseHeader(response);
370         Mockito.verify(conn, Mockito.never()).sendResponseEntity(Mockito.<ClassicHttpResponse>any());
371         Mockito.verify(conn).flush();
372         Mockito.verify(conn, Mockito.never()).close();
373         Mockito.verify(response).close();
374     }
375 
376 }