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.nio.protocol;
29  
30  import org.apache.http.HttpRequest;
31  import org.apache.http.HttpResponse;
32  import org.apache.http.HttpVersion;
33  import org.apache.http.message.BasicRequestLine;
34  import org.apache.http.protocol.BasicHttpContext;
35  import org.apache.http.protocol.HttpContext;
36  import org.apache.http.protocol.HttpRequestHandler;
37  import org.junit.After;
38  import org.junit.Assert;
39  import org.junit.Before;
40  import org.junit.Test;
41  import org.mockito.Matchers;
42  import org.mockito.Mockito;
43  
44  public class TestBasicAsyncRequestHandler {
45  
46      private HttpRequestHandler requestHandler;
47      private BasicAsyncRequestHandler asyncRequestHandler;
48      private HttpContext context;
49      private HttpRequest request;
50      private HttpResponse response;
51      private HttpAsyncExchange httpexchange;
52  
53      @Before
54      public void setUp() throws Exception {
55          this.requestHandler = Mockito.mock(HttpRequestHandler.class);
56          this.asyncRequestHandler = new BasicAsyncRequestHandler(this.requestHandler);
57          this.context = new BasicHttpContext();
58          this.request = Mockito.mock(HttpRequest.class);
59          this.response = Mockito.mock(HttpResponse.class);
60          this.httpexchange = Mockito.mock(HttpAsyncExchange.class);
61          Mockito.when(this.httpexchange.getRequest()).thenReturn(this.request);
62          Mockito.when(this.httpexchange.getResponse()).thenReturn(this.response);
63      }
64  
65      @After
66      public void tearDown() throws Exception {
67      }
68  
69  
70      @Test
71      public void testInvalidConstruction() throws Exception {
72          try {
73              new BasicAsyncRequestHandler(null);
74              Assert.fail("IllegalArgumentException expected");
75          } catch (final IllegalArgumentException ex) {
76          }
77      }
78  
79      @Test
80      public void testProcessRequest() throws Exception {
81          final HttpAsyncRequestConsumer<HttpRequest> requestConsumer = this.asyncRequestHandler.processRequest(
82                  this.request, this.context);
83          Assert.assertTrue(requestConsumer instanceof BasicAsyncRequestConsumer);
84      }
85  
86      @Test
87      public void testHandleRequest() throws Exception {
88          Mockito.when(this.request.getRequestLine()).thenReturn(new BasicRequestLine("GET", "/", HttpVersion.HTTP_1_0));
89  
90          this.asyncRequestHandler.handle(this.request, this.httpexchange, this.context);
91  
92          Mockito.verify(this.requestHandler).handle(
93                  Matchers.eq(this.request), Matchers.eq(this.response), Matchers.eq(this.context));
94          Mockito.verify(this.httpexchange).submitResponse();
95      }
96  
97  }