HttpTransport implements UnpartitionedTransport { public void sendBatch(TransportBuffer buf) throws TransportException { GenericTransportBuffer buffer = (GenericTransportBuffer) buf; sendBatch(buffer.getInternalBuffer().toByteArray()); } HttpTransport(HttpClient client, String url, boolean useGzip, int retries,
      long retryDelayMs); void sendBatch(TransportBuffer buf); void sendBatch(byte[] raw); void checkResponse(HttpResponse resp, String responseString);  }
@Test public void testOkResponse() throws TransportException, IOException { byte[] respPayload = "{}".getBytes(StandardCharsets.UTF_8); byte[] payload = "foo".getBytes(); HttpClient client = getMockClientWithResponse(respPayload, ContentType.APPLICATION_JSON, HttpStatus.SC_OK, false); HttpTransport transport = new HttpTransport(client, "", false, 1, 1); transport.sendBatch(payload); }
@Test(expected = TransportException.class) public void testNotOkResponse() throws TransportException, IOException { byte[] respPayload = "{\"foo\": \"bar\"}".getBytes(StandardCharsets.UTF_8); HttpClient client = getMockClientWithResponse(respPayload, ContentType.APPLICATION_JSON, HttpStatus.SC_INTERNAL_SERVER_ERROR, false); HttpTransport transport = new HttpTransport(client, "", false, 1, 1); transport.sendBatch("foo".getBytes()); }
@Test public void testNoErrorsResponse() throws TransportException, IOException { byte[] respPayload = "{}".getBytes(StandardCharsets.UTF_8); HttpClient client = getMockClientWithResponse(respPayload, ContentType.APPLICATION_JSON, HttpStatus.SC_OK, false); HttpTransport transport = new HttpTransport(client, "", false, 1, 1); transport.sendBatch("foo".getBytes()); }
@Test(expected = TransportException.class) public void testErrorsResponse() throws TransportException, IOException { byte[] respPayload = "resp".getBytes(StandardCharsets.UTF_8); HttpClient client = getMockClientWithResponse(respPayload, ContentType.APPLICATION_JSON, HttpStatus.SC_INTERNAL_SERVER_ERROR, false); HttpTransport transport = new HttpTransport(client, "", false, 1, 1); try { transport.sendBatch("foo".getBytes()); } catch (Exception e) { assertEquals( "http transport call failed because \"expected failure\" payload response \"resp\"", e.getCause().getMessage()); throw e; } }
@Test(expected = TransportException.class) public void testGzipErrorsResponse() throws TransportException, IOException { byte[] respPayload = "gzip resp".getBytes(StandardCharsets.UTF_8); ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream os = new GZIPOutputStream(baos); os.write(respPayload); os.close(); byte[] compressedResponse = baos.toByteArray(); HttpClient client = getMockClientWithResponse(compressedResponse, ContentType.DEFAULT_BINARY, HttpStatus.SC_INTERNAL_SERVER_ERROR, true); HttpTransport transport = new HttpTransport(client, "", true, 1, 1); try { transport.sendBatch("foo".getBytes()); } catch (Exception e) { assertEquals( "http transport call failed because \"expected failure\" payload response \"gzip resp\"", e.getCause().getMessage()); throw e; } }
@Test(expected = TransportException.class) public void testRetries() throws Exception { byte[] respPayload = "resp".getBytes(StandardCharsets.UTF_8); HttpClient client = getMockClientWithResponse(respPayload, ContentType.APPLICATION_JSON, HttpStatus.SC_INTERNAL_SERVER_ERROR, false); HttpTransport transport = new HttpTransport(client, "", false, 3, 10); try { transport.sendBatch("foo".getBytes()); } catch (Exception e) { assertEquals( "http transport call failed because \"expected failure\" payload response \"resp\"", e.getCause().getMessage()); throw e; } }
@Test(expected = IOException.class) public void testIOException() throws Throwable { byte[] arr = "{".getBytes(); HttpClient client = getMockClientWithResponse(arr, ContentType.DEFAULT_BINARY, HttpStatus.SC_INTERNAL_SERVER_ERROR, false); doThrow(new IOException()).when(client).execute(any(HttpPost.class)); HttpTransport transport = new HttpTransport(client, "", true, 3, 1); try { transport.sendBatch("foo".getBytes()); } catch (Exception e) { throw e.getCause().getCause(); } }
@Test public void testHttpPostUrl() throws TransportException, IOException { byte[] respPayload = "{}".getBytes(StandardCharsets.UTF_8); byte[] payload = "foo".getBytes(); String url = "https: HttpClient client = getMockClientWithResponse(respPayload, ContentType.APPLICATION_JSON, HttpStatus.SC_OK, false); HttpTransport transport = spy(new HttpTransport(client, url, false, 1, 1)); transport.sendBatch(payload); ArgumentCaptor<HttpPost> captor = ArgumentCaptor.forClass(HttpPost.class); verify(client, times(1)).execute(captor.capture()); assertEquals(url, captor.getValue().getURI().toString()); }
@Test public void testHttpPostGzipHeader() throws TransportException, IOException { byte[] respPayload = "{}".getBytes(StandardCharsets.UTF_8); byte[] payload = "foo".getBytes(); String url = "https: HttpClient client = getMockClientWithResponse(respPayload, ContentType.APPLICATION_JSON, HttpStatus.SC_OK, false); HttpTransport transport = spy(new HttpTransport(client, url, true, 1, 1)); transport.sendBatch(payload); ArgumentCaptor<HttpPost> captor = ArgumentCaptor.forClass(HttpPost.class); verify(client, times(1)).execute(captor.capture()); assertEquals("gzip", captor.getValue().getFirstHeader("Accept-Encoding").getValue()); }