Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public class HttpClientDefaultTest {
public void testGetInputStreamThrows() throws IOException {
HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
when(connection.getInputStream()).thenThrow(IOException.class);
when(connection.getOutputStream()).thenReturn(new ByteArrayOutputStream());
when(connection.getResponseCode()).thenReturn(200);
setupHttpURLConnectionGetResponseCodeAndGetOutputStreamStubs(connection, 200);
try {
HttpClientDefault.request(connection, new byte[0]);
Assert.fail();
Expand All @@ -36,8 +35,7 @@ public void testGetInputStreamThrows() throws IOException {
public void testDisconnectThrows() throws IOException {
HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
when(connection.getInputStream()).thenThrow(IOException.class);
when(connection.getOutputStream()).thenReturn(new ByteArrayOutputStream());
when(connection.getResponseCode()).thenReturn(200);
setupHttpURLConnectionGetResponseCodeAndGetOutputStreamStubs(connection, 200);
Mockito.doThrow(RuntimeException.class).when(connection).disconnect();
try {
HttpClientDefault.request(connection, new byte[0]);
Expand All @@ -51,9 +49,7 @@ public void testDisconnectThrows() throws IOException {
@Test
public void testGetInputStreamReturnsNull() throws IOException {
HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
when(connection.getInputStream()).thenReturn(null);
when(connection.getOutputStream()).thenReturn(new ByteArrayOutputStream());
when(connection.getResponseCode()).thenReturn(200);
setupHttpURLConnectionGetResponseCodeAndGetInputStreamAnd1MoreStubs(connection, 200);
try (ResponseInputStream response = HttpClientDefault.request(connection, new byte[0])) {
assertEquals(200, response.statusCode());
assertEquals(-1, response.read());
Expand All @@ -64,9 +60,7 @@ public void testGetInputStreamReturnsNull() throws IOException {
@Test
public void testIsOKWhenNotOk() throws IOException {
HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
when(connection.getInputStream()).thenReturn(null);
when(connection.getOutputStream()).thenReturn(new ByteArrayOutputStream());
when(connection.getResponseCode()).thenReturn(500);
setupHttpURLConnectionGetResponseCodeAndGetInputStreamAnd1MoreStubs(connection, 500);
try (ResponseInputStream response = HttpClientDefault.request(connection, new byte[0])) {
assertEquals(500, response.statusCode());
assertEquals(-1, response.read());
Expand All @@ -77,14 +71,21 @@ public void testIsOKWhenNotOk() throws IOException {
@Test
public void testIsOKWhenNotOkStatusCodeLessThan200() throws IOException {
HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
when(connection.getInputStream()).thenReturn(null);
when(connection.getOutputStream()).thenReturn(new ByteArrayOutputStream());
when(connection.getResponseCode()).thenReturn(100);
setupHttpURLConnectionGetResponseCodeAndGetInputStreamAnd1MoreStubs(connection, 100);
try (ResponseInputStream response = HttpClientDefault.request(connection, new byte[0])) {
assertEquals(100, response.statusCode());
assertEquals(-1, response.read());
assertTrue(response.headers().isEmpty());
}
}

private void setupHttpURLConnectionGetResponseCodeAndGetOutputStreamStubs(HttpURLConnection connection, int getResponseCodeReturnValue) throws IOException {
when(connection.getOutputStream()).thenReturn(new ByteArrayOutputStream());
when(connection.getResponseCode()).thenReturn(getResponseCodeReturnValue);
}

private void setupHttpURLConnectionGetResponseCodeAndGetInputStreamAnd1MoreStubs(HttpURLConnection connection, int getResponseCodeReturnValue) throws IOException {
setupHttpURLConnectionGetResponseCodeAndGetOutputStreamStubs(connection, getResponseCodeReturnValue);
when(connection.getInputStream()).thenReturn(null);
}
}