@Test(expected = RuntimeException.class) public void testServerError() throws Exception { // HTTP 500 should error out URL url = new URL("http://127.0.0.1:8765"); final HttpURLConnection cnxn = Mockito.mock(HttpURLConnection.class); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // Create the HTTP client AvaticaHttpClientImpl client = new AvaticaHttpClientImpl(url) { @Override HttpURLConnection openConnection() throws IOException { return cnxn; } }; // HTTP 500 Mockito.when(cnxn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR); Mockito.when(cnxn.getOutputStream()).thenReturn(baos); // Should throw an RTE client.send(REQUEST.getBytes(StandardCharsets.UTF_8)); }
@Test(expected = RuntimeException.class) public void testServerError() throws Exception { // HTTP 500 should error out URL url = new URL("http://127.0.0.1:8765"); final HttpURLConnection cnxn = Mockito.mock(HttpURLConnection.class); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // Create the HTTP client AvaticaHttpClientImpl client = new AvaticaHttpClientImpl(url) { @Override HttpURLConnection openConnection() throws IOException { return cnxn; } }; // HTTP 500 Mockito.when(cnxn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR); Mockito.when(cnxn.getOutputStream()).thenReturn(baos); // Should throw an RTE client.send(REQUEST.getBytes(StandardCharsets.UTF_8)); }
@Test(expected = RuntimeException.class) public void testServerError() throws Exception { // HTTP 500 should error out URL url = new URL("http://127.0.0.1:8765"); final HttpURLConnection cnxn = Mockito.mock(HttpURLConnection.class); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // Create the HTTP client AvaticaHttpClientImpl client = new AvaticaHttpClientImpl(url) { @Override HttpURLConnection openConnection() throws IOException { return cnxn; } }; // HTTP 500 Mockito.when(cnxn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR); Mockito.when(cnxn.getOutputStream()).thenReturn(baos); // Should throw an RTE client.send(REQUEST.getBytes(StandardCharsets.UTF_8)); }
@Test public void testRetryOnUnavailable() throws Exception { // HTTP-503, try again URL url = new URL("http://127.0.0.1:8765"); final HttpURLConnection cnxn = Mockito.mock(HttpURLConnection.class); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayInputStream bais = new ByteArrayInputStream(RESPONSE.getBytes(StandardCharsets.UTF_8)); // Create the HTTP client AvaticaHttpClientImpl client = new AvaticaHttpClientImpl(url) { @Override HttpURLConnection openConnection() throws IOException { return cnxn; } }; // HTTP 503 then 200 Mockito.when(cnxn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_UNAVAILABLE, HttpURLConnection.HTTP_OK); Mockito.when(cnxn.getOutputStream()).thenReturn(baos); Mockito.when(cnxn.getInputStream()).thenReturn(bais); byte[] response = client.send(REQUEST.getBytes(StandardCharsets.UTF_8)); assertArrayEquals(RESPONSE.getBytes(StandardCharsets.UTF_8), response); }
@Test public void testRetryOnUnavailable() throws Exception { // HTTP-503, try again URL url = new URL("http://127.0.0.1:8765"); final HttpURLConnection cnxn = Mockito.mock(HttpURLConnection.class); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayInputStream bais = new ByteArrayInputStream(RESPONSE.getBytes(StandardCharsets.UTF_8)); // Create the HTTP client AvaticaHttpClientImpl client = new AvaticaHttpClientImpl(url) { @Override HttpURLConnection openConnection() throws IOException { return cnxn; } }; // HTTP 503 then 200 Mockito.when(cnxn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_UNAVAILABLE, HttpURLConnection.HTTP_OK); Mockito.when(cnxn.getOutputStream()).thenReturn(baos); Mockito.when(cnxn.getInputStream()).thenReturn(bais); byte[] response = client.send(REQUEST.getBytes(StandardCharsets.UTF_8)); assertArrayEquals(RESPONSE.getBytes(StandardCharsets.UTF_8), response); }
@Test public void testRetryOnUnavailable() throws Exception { // HTTP-503, try again URL url = new URL("http://127.0.0.1:8765"); final HttpURLConnection cnxn = Mockito.mock(HttpURLConnection.class); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayInputStream bais = new ByteArrayInputStream(RESPONSE.getBytes(StandardCharsets.UTF_8)); // Create the HTTP client AvaticaHttpClientImpl client = new AvaticaHttpClientImpl(url) { @Override HttpURLConnection openConnection() throws IOException { return cnxn; } }; // HTTP 503 then 200 Mockito.when(cnxn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_UNAVAILABLE, HttpURLConnection.HTTP_OK); Mockito.when(cnxn.getOutputStream()).thenReturn(baos); Mockito.when(cnxn.getInputStream()).thenReturn(bais); byte[] response = client.send(REQUEST.getBytes(StandardCharsets.UTF_8)); assertArrayEquals(RESPONSE.getBytes(StandardCharsets.UTF_8), response); }