private static Response createResponse(int status, Map<String, Collection<String>> headers) { return Response.create(status, EMPTY_REASON, headers, EMPTY_BODY); } }
private Response createResponse(Map<String, Collection<String>> headerMap) { return Response.create(HTTP_OK, REASON, headerMap, mock(Response.Body.class)); }
private static Response createResponse(int status, byte[] bodyBytes) { return Response.create( status, EMPTY_REASON, ImmutableMap.of(HttpHeaders.CONTENT_TYPE, ImmutableSet.of(MediaType.APPLICATION_JSON)), bodyBytes); }
@Test public void passesRequestsThroughToTheDelegate() throws IOException { when(clientSupplier.get()).thenReturn(client); when(client.execute(request, options)).thenReturn( Response.create(204, "no content", ImmutableMap.of(), new byte[0])); Client refreshingClient = CounterBackedRefreshingClient.createRefreshingClient(clientSupplier); refreshingClient.execute(request, options); verify(client, times(1)).execute(request, options); verifyNoMoreInteractions(client); }
@Test public void requestsContinueWithOldClientIfDelegateSupplierThrows() throws IOException { when(clientSupplier.get()).thenReturn(client); when(client.execute(request, options)).thenReturn( Response.create(204, "no content", ImmutableMap.of(), new byte[0])); Client refreshingClient = new CounterBackedRefreshingClient(clientSupplier, 2); refreshingClient.execute(request, options); when(clientSupplier.get()).thenThrow(new IllegalStateException("bad")); refreshingClient.execute(request, options); refreshingClient.execute(request, options); // Creation failed, so we delegate to the old client still. verify(clientSupplier, times(2)).get(); verify(client, times(3)).execute(request, options); verifyNoMoreInteractions(clientSupplier, client); } }
@Test public void createsClientOnlyOnceIfMakingMultipleRequests() throws IOException { when(clientSupplier.get()).thenReturn(client); when(client.execute(request, options)).thenReturn( Response.create(204, "no content", ImmutableMap.of(), new byte[0])); Client refreshingClient = CounterBackedRefreshingClient.createRefreshingClient(clientSupplier); refreshingClient.execute(request, options); refreshingClient.execute(request, options); refreshingClient.execute(request, options); verify(clientSupplier).get(); verify(client, times(3)).execute(request, options); verifyNoMoreInteractions(clientSupplier, client); }
@Test public void reinvokesSupplierAfterTheSpecifiedNumberOfRequestsHaveBeenMade() throws IOException { when(clientSupplier.get()).thenReturn(client); when(client.execute(request, options)).thenReturn( Response.create(204, "no content", ImmutableMap.of(), new byte[0])); Client refreshingClient = new CounterBackedRefreshingClient(clientSupplier, 2); refreshingClient.execute(request, options); refreshingClient.execute(request, options); refreshingClient.execute(request, options); verify(clientSupplier, times(2)).get(); verify(client, times(3)).execute(request, options); verifyNoMoreInteractions(clientSupplier, client); }
@Test public void works_fine_when_body_is_not_null() throws Exception { Response response = Response.create(200, "OK", headers, "[1, 2, 3]", StandardCharsets.UTF_8); Object decodedObject = textDelegateDecoder.decode(response, List.class); assertThat(decodedObject).isEqualTo(ImmutableList.of(1, 2, 3)); } }
@Test public void http_200_uses_delegate_decoder() throws IOException { when(delegate.decode(any(), eq(String.class))).thenReturn("text response"); Response http200 = Response.create(200, "OK", ImmutableMap.of( HttpHeaders.CONTENT_TYPE, ImmutableSet.of(MediaType.TEXT_PLAIN)), "text response", StandardCharsets.UTF_8); emptyContainerDecoder.decode(http200, String.class); verify(delegate, times(1)).decode(any(), any()); }
@Test public void testUsesDelegateWithComplexHeader() throws Exception { headers.put(HttpHeaders.CONTENT_TYPE, ImmutableSet.of(MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON)); when(delegate.decode(any(), any())).thenReturn(DELEGATE_RESPONSE); Response response = Response.create(200, "OK", headers, new byte[0]); Object decodedObject = textDelegateDecoder.decode(response, String.class); assertEquals(decodedObject, DELEGATE_RESPONSE); }
@Test public void testUsesDelegateWithNonTextContentType() throws Exception { headers.put(HttpHeaders.CONTENT_TYPE, ImmutableSet.of(MediaType.APPLICATION_JSON)); when(delegate.decode(any(), any())).thenReturn(DELEGATE_RESPONSE); Response response = Response.create(200, "OK", headers, new byte[0]); Object decodedObject = textDelegateDecoder.decode(response, String.class); assertEquals(decodedObject, DELEGATE_RESPONSE); }
@Test public void testUsesStringDecoderWithTextPlain() throws Exception { headers.put(HttpHeaders.CONTENT_TYPE, ImmutableSet.of(MediaType.TEXT_PLAIN)); Response response = Response.create(200, "OK", headers, "text response", StandardCharsets.UTF_8); Object decodedObject = textDelegateDecoder.decode(response, String.class); assertEquals(decodedObject, "text response"); verifyZeroInteractions(delegate); }
@Test public void testUsesStringDecoderWithTextPlainWithWeirdHeaderCapitalization() throws Exception { headers.put("content-TYPE", ImmutableSet.of(MediaType.TEXT_PLAIN)); Response response = Response.create(200, "OK", headers, "text response", StandardCharsets.UTF_8); Object decodedObject = textDelegateDecoder.decode(response, String.class); assertEquals(decodedObject, "text response"); verifyZeroInteractions(delegate); }
@Test public void testUsesDelegateWhenReturnTypeNotInputStream() throws Exception { String returned = "string"; when(delegate.decode(any(), any())).thenReturn(returned); Response response = Response.create(200, "OK", ImmutableMap.of(), returned, StandardCharsets.UTF_8); String decodedObject = (String) inputStreamDelegateDecoder.decode(response, String.class); assertEquals(returned, decodedObject); }
@Test public void testUsesDelegateWithNoHeader() throws Exception { when(delegate.decode(any(), any())).thenReturn(DELEGATE_RESPONSE); Response response = Response.create(200, "OK", headers, new byte[0]); Object decodedObject = textDelegateDecoder.decode(response, String.class); assertEquals(decodedObject, DELEGATE_RESPONSE); }
@Test public void testDecodesAsInputStream() throws Exception { String data = "data"; Response response = Response.create(200, "OK", ImmutableMap.of(), data, StandardCharsets.UTF_8); InputStream decoded = (InputStream) inputStreamDelegateDecoder.decode(response, InputStream.class); assertThat(new String(Util.toByteArray(decoded), StandardCharsets.UTF_8), is(data)); }
@Test public void testSupportsNullBody() throws Exception { String data = ""; Response response = Response.create(200, "OK", ImmutableMap.of(), (Response.Body) null); InputStream decoded = (InputStream) inputStreamDelegateDecoder.decode(response, InputStream.class); assertThat(new String(Util.toByteArray(decoded), StandardCharsets.UTF_8), is(data)); }
@Test public void throws_nullpointerexception_when_body_is_string_null() { Response response = Response.create(200, "OK", headers, "null", StandardCharsets.UTF_8); Assertions.assertThatLoggableExceptionThrownBy(() -> textDelegateDecoder.decode(response, List.class)) .isInstanceOf(NullPointerException.class) .hasLogMessage("Unexpected null body") .hasArgs(SafeArg.of("status", 200)); }
@Test public void throws_nullpointerexception_when_body_is_null() { Response response = Response.create(200, "OK", headers, null, StandardCharsets.UTF_8); Assertions.assertThatLoggableExceptionThrownBy(() -> textDelegateDecoder.decode(response, List.class)) .isInstanceOf(NullPointerException.class) .hasLogMessage("Unexpected null body") .hasArgs(SafeArg.of("status", 200)); }
@Test public void throws_nullpointerexception_when_body_is_empty_string() { Response response = Response.create(200, "OK", headers, "", StandardCharsets.UTF_8); Assertions.assertThatLoggableExceptionThrownBy(() -> textDelegateDecoder.decode(response, List.class)) .isInstanceOf(NullPointerException.class) .hasLogMessage("Unexpected null body") .hasArgs(SafeArg.of("status", 200)); }