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.jupiter.api.Assertions;
41  import org.junit.jupiter.api.BeforeEach;
42  import org.junit.jupiter.api.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      @BeforeEach
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          Mockito.when(client.doExecute(
85                  Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(response);
86          client.execute(httpget, response -> null);
87  
88          Mockito.verify(client).doExecute(
89                  Mockito.eq(new HttpHost("https", "somehost", 444)),
90                  Mockito.same(httpget),
91                  (HttpContext) Mockito.isNull());
92      }
93  
94      @Test
95      public void testExecuteRequestRelativeURI() throws Exception {
96          final HttpGet httpget = new HttpGet("/stuff");
97          Mockito.when(client.doExecute(
98                  Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(response);
99          client.execute(httpget, response -> null);
100 
101         Mockito.verify(client).doExecute(
102                 (HttpHost) Mockito.isNull(),
103                 Mockito.same(httpget),
104                 (HttpContext) Mockito.isNull());
105     }
106 
107     @Test
108     public void testExecuteRequestHandleResponse() throws Exception {
109         final HttpGet httpget = new HttpGet("https://somehost:444/stuff");
110 
111         Mockito.when(client.doExecute(
112                 new HttpHost("https", "somehost", 444), httpget, null)).thenReturn(response);
113 
114         final HttpClientResponseHandler<HttpResponse> handler = Mockito.mock(HttpClientResponseHandler.class);
115 
116         client.execute(httpget, handler);
117 
118         Mockito.verify(client).doExecute(
119                 Mockito.eq(new HttpHost("https", "somehost", 444)),
120                 Mockito.same(httpget),
121                 (HttpContext) Mockito.isNull());
122         Mockito.verify(handler).handleResponse(response);
123         Mockito.verify(content).close();
124     }
125 
126     @Test
127     public void testExecuteRequestHandleResponseIOException() throws Exception {
128         final HttpGet httpget = new HttpGet("https://somehost:444/stuff");
129 
130         Mockito.when(client.doExecute(
131                 new HttpHost("https", "somehost", 444), httpget, null)).thenReturn(response);
132 
133         final HttpClientResponseHandler<HttpResponse> handler = Mockito.mock(HttpClientResponseHandler.class);
134 
135         Mockito.when(handler.handleResponse(response)).thenThrow(new IOException());
136 
137         Assertions.assertThrows(IOException.class, () ->
138             client.execute(httpget, handler));
139         Mockito.verify(client).doExecute(
140                 Mockito.eq(new HttpHost("https", "somehost", 444)),
141                 Mockito.same(httpget),
142                 (HttpContext) Mockito.isNull());
143         Mockito.verify(originResponse).close();
144     }
145 
146     @Test
147     public void testExecuteRequestHandleResponseHttpException() throws Exception {
148         final HttpGet httpget = new HttpGet("https://somehost:444/stuff");
149 
150         Mockito.when(client.doExecute(
151                 new HttpHost("https", "somehost", 444), httpget, null)).thenReturn(response);
152 
153         final HttpClientResponseHandler<HttpResponse> handler = Mockito.mock(HttpClientResponseHandler.class);
154 
155         Mockito.when(handler.handleResponse(response)).thenThrow(new RuntimeException());
156 
157         Assertions.assertThrows(RuntimeException.class, () ->
158                 client.execute(httpget, handler));
159         Mockito.verify(client).doExecute(
160                 Mockito.eq(new HttpHost("https", "somehost", 444)),
161                 Mockito.same(httpget),
162                 (HttpContext) Mockito.isNull());
163         Mockito.verify(originResponse).close();
164     }
165 
166 }