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.client5.http.impl.classic;
29  
30  import java.io.IOException;
31  import java.io.InputStream;
32  
33  import org.apache.hc.client5.http.HttpResponseException;
34  import org.apache.hc.core5.http.ClassicHttpResponse;
35  import org.apache.hc.core5.http.HttpEntity;
36  import org.apache.hc.core5.http.io.entity.EntityUtils;
37  import org.apache.hc.core5.http.io.entity.StringEntity;
38  import org.junit.jupiter.api.Assertions;
39  import org.junit.jupiter.api.Test;
40  import org.mockito.Mockito;
41  
42  /**
43   * Unit tests for {@link BasicHttpClientResponseHandler}.
44   */
45  public class TestAbstractHttpClientResponseHandler {
46  
47      @Test
48      public void testSuccessfulResponse() throws Exception {
49          final ClassicHttpResponse response = Mockito.mock(ClassicHttpResponse.class);
50          final HttpEntity entity = new StringEntity("42");
51          Mockito.when(response.getCode()).thenReturn(200);
52          Mockito.when(response.getEntity()).thenReturn(entity);
53  
54          final AbstractHttpClientResponseHandler<Integer> handler = new AbstractHttpClientResponseHandler<Integer>() {
55  
56            @Override
57            public Integer handleEntity(final HttpEntity entity) throws IOException {
58              return Integer.valueOf(new String(EntityUtils.toByteArray(entity)));
59            }
60          };
61          final Integer number = handler.handleResponse(response);
62          Assertions.assertEquals(42, number.intValue());
63      }
64  
65      @SuppressWarnings("boxing")
66      @Test
67      public void testUnsuccessfulResponse() throws Exception {
68          final InputStream inStream = Mockito.mock(InputStream.class);
69          final HttpEntity entity = Mockito.mock(HttpEntity.class);
70          Mockito.when(entity.isStreaming()).thenReturn(true);
71          Mockito.when(entity.getContent()).thenReturn(inStream);
72          final ClassicHttpResponse response = Mockito.mock(ClassicHttpResponse.class);
73          Mockito.when(response.getCode()).thenReturn(404);
74          Mockito.when(response.getReasonPhrase()).thenReturn("NOT FOUND");
75          Mockito.when(response.getEntity()).thenReturn(entity);
76  
77          final BasicHttpClientResponseHandler handler = new BasicHttpClientResponseHandler();
78          final HttpResponseException exception = Assertions.assertThrows(HttpResponseException.class, () ->
79                  handler.handleResponse(response));
80          Assertions.assertEquals(404, exception.getStatusCode());
81          Assertions.assertEquals("NOT FOUND", exception.getReasonPhrase());
82          Assertions.assertEquals("status code: 404, reason phrase: NOT FOUND", exception.getMessage());
83          Mockito.verify(entity).getContent();
84          Mockito.verify(inStream).close();
85      }
86  
87      @SuppressWarnings("boxing")
88      @Test
89      public void testUnsuccessfulResponseEmptyReason() throws Exception {
90          final InputStream inStream = Mockito.mock(InputStream.class);
91          final HttpEntity entity = Mockito.mock(HttpEntity.class);
92          Mockito.when(entity.isStreaming()).thenReturn(true);
93          Mockito.when(entity.getContent()).thenReturn(inStream);
94          final ClassicHttpResponse response = Mockito.mock(ClassicHttpResponse.class);
95          Mockito.when(response.getCode()).thenReturn(404);
96          Mockito.when(response.getEntity()).thenReturn(entity);
97  
98          final BasicHttpClientResponseHandler handler = new BasicHttpClientResponseHandler();
99          final HttpResponseException exception = Assertions.assertThrows(HttpResponseException.class, () ->
100                 handler.handleResponse(response));
101         Assertions.assertEquals(404, exception.getStatusCode());
102         Assertions.assertNull(exception.getReasonPhrase());
103         Assertions.assertEquals("status code: 404", exception.getMessage());
104         Mockito.verify(entity).getContent();
105         Mockito.verify(inStream).close();
106     }
107 }