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.Assert;
39  import org.junit.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          Assert.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          try {
79              handler.handleResponse(response);
80              Assert.fail("HttpResponseException expected");
81          } catch (final HttpResponseException ex) {
82              Assert.assertEquals(404, ex.getStatusCode());
83              Assert.assertEquals("NOT FOUND", ex.getReasonPhrase());
84              Assert.assertEquals("status code: 404, reason phrase: NOT FOUND", ex.getMessage());
85          }
86          Mockito.verify(entity).getContent();
87          Mockito.verify(inStream).close();
88      }
89  
90      @SuppressWarnings("boxing")
91      @Test
92      public void testUnsuccessfulResponseEmptyReason() throws Exception {
93          final InputStream inStream = Mockito.mock(InputStream.class);
94          final HttpEntity entity = Mockito.mock(HttpEntity.class);
95          Mockito.when(entity.isStreaming()).thenReturn(true);
96          Mockito.when(entity.getContent()).thenReturn(inStream);
97          final ClassicHttpResponse response = Mockito.mock(ClassicHttpResponse.class);
98          Mockito.when(response.getCode()).thenReturn(404);
99          Mockito.when(response.getEntity()).thenReturn(entity);
100 
101         final BasicHttpClientResponseHandler handler = new BasicHttpClientResponseHandler();
102         try {
103             handler.handleResponse(response);
104             Assert.fail("HttpResponseException expected");
105         } catch (final HttpResponseException ex) {
106             Assert.assertEquals(404, ex.getStatusCode());
107             Assert.assertNull(ex.getReasonPhrase());
108             Assert.assertEquals("status code: 404", ex.getMessage());
109         }
110         Mockito.verify(entity).getContent();
111         Mockito.verify(inStream).close();
112     }
113 }