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