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  package org.apache.http.impl.client;
28  
29  import java.io.IOException;
30  import java.io.InputStream;
31  
32  import org.apache.http.HttpEntity;
33  import org.apache.http.HttpHost;
34  import org.apache.http.HttpRequest;
35  import org.apache.http.HttpResponse;
36  import org.apache.http.client.ClientProtocolException;
37  import org.apache.http.client.ResponseHandler;
38  import org.apache.http.client.methods.CloseableHttpResponse;
39  import org.apache.http.client.methods.HttpGet;
40  import org.apache.http.protocol.HttpContext;
41  import org.junit.Assert;
42  import org.junit.Before;
43  import org.junit.Test;
44  import org.mockito.Mockito;
45  
46  /**
47   *  Simple tests for {@link CloseableHttpClient}.
48   */
49  @SuppressWarnings({"boxing","static-access"}) // test code
50  public class TestCloseableHttpClient {
51  
52      static abstract class NoopCloseableHttpClient extends CloseableHttpClient {
53  
54          @Override
55          protected CloseableHttpResponse doExecute(
56                  final HttpHost target,
57                  final HttpRequest request,
58                  final HttpContext context) throws IOException, ClientProtocolException {
59              return null;
60          }
61  
62      }
63  
64      private NoopCloseableHttpClient client;
65      private InputStream content;
66      private HttpEntity entity;
67      private CloseableHttpResponse response;
68  
69      @Before
70      public void setup() throws Exception {
71          content = Mockito.mock(InputStream.class);
72          entity = Mockito.mock(HttpEntity.class);
73          response = Mockito.mock(CloseableHttpResponse.class);
74          Mockito.when(entity.getContent()).thenReturn(content);
75          Mockito.when(entity.isStreaming()).thenReturn(Boolean.TRUE);
76          Mockito.when(response.getEntity()).thenReturn(entity);
77          client = Mockito.mock(NoopCloseableHttpClient.class, Mockito.CALLS_REAL_METHODS);
78      }
79  
80      @Test
81      public void testExecuteRequestAbsoluteURI() throws Exception {
82          final HttpGet httpget = new HttpGet("https://somehost:444/stuff");
83          client.execute(httpget);
84  
85          Mockito.verify(client).doExecute(
86                  Mockito.eq(new HttpHost("somehost", 444, "https")),
87                  Mockito.same(httpget),
88                  (HttpContext) Mockito.isNull());
89      }
90  
91      @Test
92      public void testExecuteRequestRelativeURI() throws Exception {
93          final HttpGet httpget = new HttpGet("/stuff");
94          client.execute(httpget);
95  
96          Mockito.verify(client).doExecute(
97                  (HttpHost) Mockito.isNull(),
98                  Mockito.same(httpget),
99                  (HttpContext) Mockito.isNull());
100     }
101 
102     @Test
103     public void testExecuteRequestInvalidHost() throws Exception {
104         final HttpGet httpget = new HttpGet("http://_/stuff");
105         client.execute(httpget);
106 
107         Mockito.verify(client).doExecute(
108                 Mockito.eq(new HttpHost("_", -1, "http")),
109                 Mockito.same(httpget),
110                 (HttpContext) Mockito.isNull());
111     }
112 
113     @Test(expected=ClientProtocolException.class)
114     public void testExecuteRequestInvalidHost2() throws Exception {
115         final HttpGet httpget = new HttpGet("http://@/stuff");
116         client.execute(httpget);
117     }
118 
119     @Test
120     public void testExecuteRequest() throws Exception {
121         final HttpGet httpget = new HttpGet("https://somehost:444/stuff");
122 
123         Mockito.when(client.doExecute(
124                 new HttpHost("somehost", 444, "https"), httpget, null)).thenReturn(response);
125 
126         final CloseableHttpResponse result = client.execute(httpget);
127         Assert.assertSame(response, result);
128     }
129 
130     @Test
131     @SuppressWarnings("unchecked")
132     public void testExecuteRequestHandleResponse() throws Exception {
133         final HttpGet httpget = new HttpGet("https://somehost:444/stuff");
134 
135         Mockito.when(client.doExecute(
136                 new HttpHost("somehost", 444, "https"), httpget, null)).thenReturn(response);
137 
138         final ResponseHandler<HttpResponse> handler = Mockito.mock(ResponseHandler.class);
139 
140         client.execute(httpget, handler);
141 
142         Mockito.verify(client).doExecute(
143                 Mockito.eq(new HttpHost("somehost", 444, "https")),
144                 Mockito.same(httpget),
145                 (HttpContext) Mockito.isNull());
146         Mockito.verify(handler).handleResponse(response);
147         Mockito.verify(content).close();
148     }
149 
150     @Test(expected=IOException.class)
151     @SuppressWarnings("unchecked")
152     public void testExecuteRequestHandleResponseIOException() throws Exception {
153         final HttpGet httpget = new HttpGet("https://somehost:444/stuff");
154 
155         Mockito.when(client.doExecute(
156                 new HttpHost("somehost", 444, "https"), httpget, null)).thenReturn(response);
157 
158         final ResponseHandler<HttpResponse> handler = Mockito.mock(ResponseHandler.class);
159 
160         Mockito.when(handler.handleResponse(response)).thenThrow(new IOException());
161 
162         try {
163             client.execute(httpget, handler);
164         } catch (final IOException ex) {
165             Mockito.verify(client).doExecute(
166                     Mockito.eq(new HttpHost("somehost", 444, "https")),
167                     Mockito.same(httpget),
168                     (HttpContext) Mockito.isNull());
169             Mockito.verify(content).close();
170             throw ex;
171         }
172     }
173 
174     @Test(expected=RuntimeException.class)
175     @SuppressWarnings("unchecked")
176     public void testExecuteRequestHandleResponseHttpException() throws Exception {
177         final HttpGet httpget = new HttpGet("https://somehost:444/stuff");
178 
179         Mockito.when(client.doExecute(
180                 new HttpHost("somehost", 444, "https"), httpget, null)).thenReturn(response);
181 
182         final ResponseHandler<HttpResponse> handler = Mockito.mock(ResponseHandler.class);
183 
184         Mockito.when(handler.handleResponse(response)).thenThrow(new RuntimeException());
185 
186         try {
187             client.execute(httpget, handler);
188         } catch (final RuntimeException ex) {
189             Mockito.verify(client).doExecute(
190                     Mockito.eq(new HttpHost("somehost", 444, "https")),
191                     Mockito.same(httpget),
192                     (HttpContext) Mockito.isNull());
193             Mockito.verify(content).close();
194             throw ex;
195         }
196     }
197 
198 }