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.InputStream;
31
32 import org.apache.hc.client5.http.HttpResponseException;
33 import org.apache.hc.core5.http.ClassicHttpResponse;
34 import org.apache.hc.core5.http.HttpEntity;
35 import org.apache.hc.core5.http.io.entity.StringEntity;
36 import org.junit.jupiter.api.Assertions;
37 import org.junit.jupiter.api.Test;
38 import org.mockito.Mockito;
39
40
41
42
43 @SuppressWarnings("boxing")
44 class TestBasicResponseHandler {
45
46 @Test
47 void testSuccessfulResponse() throws Exception {
48 final ClassicHttpResponse response = Mockito.mock(ClassicHttpResponse.class);
49 final HttpEntity entity = new StringEntity("stuff");
50 Mockito.when(response.getCode()).thenReturn(200);
51 Mockito.when(response.getEntity()).thenReturn(entity);
52
53 final BasicHttpClientResponseHandler handler = new BasicHttpClientResponseHandler();
54 final String s = handler.handleResponse(response);
55 Assertions.assertEquals("stuff", s);
56 }
57
58 @Test
59 void testUnsuccessfulResponse() throws Exception {
60 final InputStream inStream = Mockito.mock(InputStream.class);
61 final HttpEntity entity = Mockito.mock(HttpEntity.class);
62 Mockito.when(entity.isStreaming()).thenReturn(true);
63 Mockito.when(entity.getContent()).thenReturn(inStream);
64 final ClassicHttpResponse response = Mockito.mock(ClassicHttpResponse.class);
65 Mockito.when(response.getCode()).thenReturn(404);
66 Mockito.when(response.getEntity()).thenReturn(entity);
67
68 final BasicHttpClientResponseHandler handler = new BasicHttpClientResponseHandler();
69 final HttpResponseException exception = Assertions.assertThrows(HttpResponseException.class, () ->
70 handler.handleResponse(response));
71 Assertions.assertEquals(404, exception.getStatusCode());
72 Mockito.verify(entity).getContent();
73 Mockito.verify(inStream).close();
74 }
75
76 }