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