HttpFunctions { @NonNull public static Function<HttpRequest, Result<HttpResponse>> httpFunction() { return HTTP_FUNCTION; } private  HttpFunctions(); @NonNull static Function<HttpRequest, Result<HttpResponse>> httpFunction();  }
@Test public void shouldPassOnGetMethod() throws Throwable { assertThat(httpFunction() .apply(HTTP_GET_REQUEST), is(notNullValue())); verify(mockHttpURLConnection).setRequestMethod(GET_METHOD); verify(mockHttpURLConnection).disconnect(); }
@Test public void shouldPassOnPostMethod() throws Throwable { assertThat(httpFunction() .apply(HTTP_POST_REQUEST), is(notNullValue())); verify(mockHttpURLConnection).setRequestMethod(POST_METHOD); verify(mockHttpURLConnection).disconnect(); }
@Test public void shouldPassOnPutMethod() throws Throwable { assertThat(httpFunction() .apply(HTTP_PUT_REQUEST), is(notNullValue())); verify(mockHttpURLConnection).setRequestMethod(PUT_METHOD); verify(mockHttpURLConnection).disconnect(); }
@Test public void shouldPassOnDeleteMethod() throws Throwable { assertThat(httpFunction() .apply(HTTP_DELETE_REQUEST), is(notNullValue())); verify(mockHttpURLConnection).setRequestMethod(DELETE_METHOD); verify(mockHttpURLConnection).disconnect(); }
@Test public void shouldGracefullyHandleProtocolExceptionForInvalidMethod() throws Throwable { doThrow(ProtocolException.class).when(mockHttpURLConnection).setRequestMethod(anyString()); assertThat(httpFunction().apply(HTTP_DELETE_REQUEST).getFailure(), instanceOf(ProtocolException.class)); verify(mockHttpURLConnection).disconnect(); }
@Test public void shouldPassOnRequestHeaders() throws Throwable { assertThat(httpFunction().apply(HTTP_GET_REQUEST_WITH_HEADERS), is(notNullValue())); verify(mockHttpURLConnection).addRequestProperty("name", "value"); verify(mockHttpURLConnection).addRequestProperty("name2", "value2"); verify(mockHttpURLConnection).disconnect(); }
@Test public void shouldPassOnResponseHeadersAsLowerCase() throws Throwable { final ByteArrayInputStream inputStream = new ByteArrayInputStream(RESPONSE_BODY); when(mockHttpURLConnection.getInputStream()).thenReturn(inputStream); when(mockHttpURLConnection.getContentLength()).thenReturn(RESPONSE_BODY.length); final Map<String, List<String>> headerFields = new HashMap<>(); headerFields.put("NAmE", singletonList("value")); headerFields.put("naMe2", singletonList("value2")); when(mockHttpURLConnection.getHeaderFields()).thenReturn(headerFields); final HttpResponse httpResponse = httpFunction().apply(HTTP_GET_REQUEST).get(); assertThat(httpResponse.header.size(), is(2)); assertThat(httpResponse.header, hasEntry("name", "value")); assertThat(httpResponse.header, hasEntry("name2", "value2")); verify(mockHttpURLConnection).disconnect(); }
@Test public void shouldNotPassOnNullResponseHeader() throws Throwable { final ByteArrayInputStream inputStream = new ByteArrayInputStream(RESPONSE_BODY); when(mockHttpURLConnection.getInputStream()).thenReturn(inputStream); when(mockHttpURLConnection.getContentLength()).thenReturn(RESPONSE_BODY.length); final Map<String, List<String>> headerFields = new HashMap<>(); headerFields.put(null, singletonList("value")); headerFields.put("naMe2", singletonList("value2")); when(mockHttpURLConnection.getHeaderFields()).thenReturn(headerFields); final HttpResponse httpResponse = httpFunction().apply(HTTP_GET_REQUEST).get(); assertThat(httpResponse.header.size(), is(1)); assertThat(httpResponse.header, hasEntry("name2", "value2")); verify(mockHttpURLConnection).disconnect(); }
@Test public void shouldGetOutputStreamForPutWithBody() throws Throwable { final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final ByteArrayInputStream inputStream = new ByteArrayInputStream(RESPONSE_BODY); when(mockHttpURLConnection.getOutputStream()).thenReturn(outputStream); when(mockHttpURLConnection.getInputStream()).thenReturn(inputStream); assertThat(httpFunction().apply(HTTP_PUT_WITH_BODY_REQUEST), is(notNullValue())); verify(mockHttpURLConnection).setDoInput(true); verify(mockHttpURLConnection).disconnect(); assertThat(outputStream.toByteArray(), is(REQUEST_BODY)); }
@Test public void shouldGetOutputStreamForPostWithBody() throws Throwable { final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final ByteArrayInputStream inputStream = new ByteArrayInputStream(RESPONSE_BODY); when(mockHttpURLConnection.getOutputStream()).thenReturn(outputStream); when(mockHttpURLConnection.getInputStream()).thenReturn(inputStream); assertThat(httpFunction().apply(HTTP_POST_WITH_BODY_REQUEST), is(notNullValue())); verify(mockHttpURLConnection).setDoInput(true); verify(mockHttpURLConnection).disconnect(); assertThat(outputStream.toByteArray(), is(REQUEST_BODY)); }
@Test public void shouldGetByteArrayFromGetResponse() throws Throwable { final ByteArrayInputStream inputStream = new ByteArrayInputStream(RESPONSE_BODY); when(mockHttpURLConnection.getInputStream()).thenReturn(inputStream); when(mockHttpURLConnection.getContentLength()).thenReturn(RESPONSE_BODY.length); assertThat(httpFunction().apply(HTTP_GET_REQUEST).get().getBody(), is(RESPONSE_BODY)); verify(mockHttpURLConnection).disconnect(); }
@Test public void shouldGetByteArrayFromGetResponseOfUnknownLength() throws Throwable { final ByteArrayInputStream inputStream = new ByteArrayInputStream(RESPONSE_BODY); when(mockHttpURLConnection.getInputStream()).thenReturn(inputStream); when(mockHttpURLConnection.getContentLength()).thenReturn(-1); assertThat(httpFunction().apply(HTTP_GET_REQUEST).get().getBody(), is(RESPONSE_BODY)); verify(mockHttpURLConnection).disconnect(); }
@Test public void shouldGetEmptyBodyFromGetResponseOfZeroLength() throws Throwable { final InputStream inputStream = mock(InputStream.class); when(mockHttpURLConnection.getInputStream()).thenReturn(inputStream); when(mockHttpURLConnection.getContentLength()).thenReturn(0); assertThat(httpFunction().apply(HTTP_GET_REQUEST).get().getBody(), is(EMPTY_BODY)); verify(mockHttpURLConnection).disconnect(); verifyZeroInteractions(inputStream); }
@Test public void shouldReturnErrorStreamForFailingInputStream() throws Throwable { final ByteArrayInputStream inputStream = new ByteArrayInputStream(RESPONSE_BODY); when(mockHttpURLConnection.getContentLength()).thenReturn(-1); when(mockHttpURLConnection.getInputStream()).thenThrow(IOException.class); when(mockHttpURLConnection.getErrorStream()).thenReturn(inputStream); assertThat(httpFunction().apply(HTTP_GET_REQUEST).get().getBody(), is(RESPONSE_BODY)); verify(mockHttpURLConnection).disconnect(); }
@Test public void shouldReturnResponseCodeAndMessage() throws Throwable { when(mockHttpURLConnection.getResponseCode()).thenReturn(200); when(mockHttpURLConnection.getResponseMessage()).thenReturn("message"); final HttpResponse httpResponse = httpFunction().apply(HTTP_GET_REQUEST).get(); assertThat(httpResponse.getResponseCode(), is(200)); assertThat(httpResponse.getResponseMessage(), is("message")); verify(mockHttpURLConnection).disconnect(); }
@Test public void shouldReturnEmptyStringForNullResponseMessage() throws Throwable { when(mockHttpURLConnection.getResponseMessage()).thenReturn(null); final HttpResponse httpResponse = httpFunction().apply(HTTP_GET_REQUEST).get(); assertThat(httpResponse.getResponseMessage(), is("")); verify(mockHttpURLConnection).disconnect(); }