View Javadoc
1   package com.github.davidmoten.aws.lw.client.internal;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.assertTrue;
5   import static org.mockito.Mockito.when;
6   
7   import java.io.ByteArrayOutputStream;
8   import java.io.IOException;
9   import java.io.UncheckedIOException;
10  import java.net.HttpURLConnection;
11  
12  import org.junit.Assert;
13  import org.junit.Test;
14  import org.mockito.Mockito;
15  
16  import com.github.davidmoten.aws.lw.client.ResponseInputStream;
17  
18  public class HttpClientDefaultTest {
19  
20      @Test
21      public void testGetInputStreamThrows() throws IOException {
22          HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
23          when(connection.getInputStream()).thenThrow(IOException.class);
24          when(connection.getOutputStream()).thenReturn(new ByteArrayOutputStream());
25          when(connection.getResponseCode()).thenReturn(200);
26          try {
27              HttpClientDefault.request(connection, new byte[0]);
28              Assert.fail();
29          } catch (UncheckedIOException e) {
30              // expected
31          }
32          Mockito.verify(connection, Mockito.times(1)).disconnect();
33      }
34  
35      @Test
36      public void testDisconnectThrows() throws IOException {
37          HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
38          when(connection.getInputStream()).thenThrow(IOException.class);
39          when(connection.getOutputStream()).thenReturn(new ByteArrayOutputStream());
40          when(connection.getResponseCode()).thenReturn(200);
41          Mockito.doThrow(RuntimeException.class).when(connection).disconnect();
42          try {
43              HttpClientDefault.request(connection, new byte[0]);
44              Assert.fail();
45          } catch (UncheckedIOException e) {
46              // expected
47          }
48          Mockito.verify(connection, Mockito.times(1)).disconnect();
49      }
50  
51      @Test
52      public void testGetInputStreamReturnsNull() throws IOException {
53          HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
54          when(connection.getInputStream()).thenReturn(null);
55          when(connection.getOutputStream()).thenReturn(new ByteArrayOutputStream());
56          when(connection.getResponseCode()).thenReturn(200);
57          try (ResponseInputStream response = HttpClientDefault.request(connection, new byte[0])) {
58              assertEquals(200, response.statusCode());
59              assertEquals(-1, response.read());
60              assertTrue(response.headers().isEmpty());
61          }
62      }
63      
64      @Test
65      public void testIsOKWhenNotOk() throws IOException {
66          HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
67          when(connection.getInputStream()).thenReturn(null);
68          when(connection.getOutputStream()).thenReturn(new ByteArrayOutputStream());
69          when(connection.getResponseCode()).thenReturn(500);
70          try (ResponseInputStream response = HttpClientDefault.request(connection, new byte[0])) {
71              assertEquals(500, response.statusCode());
72              assertEquals(-1, response.read());
73              assertTrue(response.headers().isEmpty());
74          }
75      }
76      
77      @Test
78      public void testIsOKWhenNotOkStatusCodeLessThan200() throws IOException {
79          HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
80          when(connection.getInputStream()).thenReturn(null);
81          when(connection.getOutputStream()).thenReturn(new ByteArrayOutputStream());
82          when(connection.getResponseCode()).thenReturn(100);
83          try (ResponseInputStream response = HttpClientDefault.request(connection, new byte[0])) {
84              assertEquals(100, response.statusCode());
85              assertEquals(-1, response.read());
86              assertTrue(response.headers().isEmpty());
87          }
88      }
89  
90  }