1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
44
45 class TestAbstractHttpClientResponseHandler {
46
47 @Test
48 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 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 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 }