WebClient { protected static int send( String customerId, byte[] bytes, HttpPost httpPost, HttpHost proxy, CloseableHttpClient httpClient, ResponseHandler responseHandler ) { int statusCode = DEFAULT_STATUS_CODE; if (bytes != null && bytes.length > 0 && httpPost != null && customerId != null) { MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); builder.addTextBody("cid", customerId); builder.addBinaryBody("file", bytes, ContentType.DEFAULT_BINARY, "filename"); httpPost.setEntity(builder.build()); httpPost.addHeader("api-version", "phone-home-v1"); RequestConfig config = RequestConfig.custom() .setConnectTimeout(requestTimeoutMs) .setConnectionRequestTimeout(requestTimeoutMs) .setSocketTimeout(requestTimeoutMs) .build(); CloseableHttpResponse response = null; try { if (proxy != null) { log.debug("setting proxy to {}", proxy); config = RequestConfig.copy(config).setProxy(proxy).build(); httpPost.setConfig(config); DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy); if (httpClient == null) { httpClient = HttpClientBuilder .create() .setRoutePlanner(routePlanner) .setDefaultRequestConfig(config) .build(); } } else { if (httpClient == null) { httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build(); } } response = httpClient.execute(httpPost); if (responseHandler != null) { responseHandler.handle(response); } log.debug("POST request returned {}", response.getStatusLine().toString()); statusCode = response.getStatusLine().getStatusCode(); } catch (IOException e) { log.error("Could not submit metrics to Confluent: {}", e.getMessage()); } finally { try { httpClient.close(); } catch (IOException e) { log.warn("could not close http client", e); } if (response != null) { try { response.close(); } catch (IOException e) { log.warn("could not close http response", e); } } } } else { statusCode = HttpStatus.SC_BAD_REQUEST; } return statusCode; }  static int send(
      String customerId,
      byte[] bytes,
      HttpPost httpPost,
      HttpHost proxy,
      ResponseHandler responseHandler
  ); static int send(
      String customerId,
      byte[] bytes,
      HttpPost httpPost,
      ResponseHandler responseHandler
  ); static final int DEFAULT_STATUS_CODE; }
@Test public void testSubmitIgnoresNullInput() { HttpPost p = mock(HttpPost.class); byte[] nullData = null; WebClient.send(customerId, nullData, p, null); verifyZeroInteractions(p); }
@Test public void testSubmitIgnoresEmptyInput() { HttpPost p = mock(HttpPost.class); byte[] emptyData = new byte[0]; WebClient.send(customerId, emptyData, p, null); verifyZeroInteractions(p); }
@Test public void testSubmitInvalidCustomer() { HttpPost p = new HttpPost(secureLiveTestEndpoint); byte[] anyData = "anyData".getBytes(); int randomIndex = generator.nextInt(CustomerIdExamples.invalidCustomerIds.length); String invalidCustomerId = CustomerIdExamples.invalidCustomerIds[randomIndex]; assertNotEquals("customerId=" + invalidCustomerId, HttpStatus.SC_OK, WebClient.send(invalidCustomerId, anyData, p, null)); }
@Test public void testSubmitInvalidAnonymousUser() { HttpPost p = new HttpPost(secureLiveTestEndpoint); byte[] anyData = "anyData".getBytes(); int randomIndex = generator.nextInt(CustomerIdExamples.invalidAnonymousIds.length); String invalidCustomerId = CustomerIdExamples.invalidAnonymousIds[randomIndex]; assertNotEquals("customerId=" + invalidCustomerId, HttpStatus.SC_OK, WebClient.send(invalidCustomerId, anyData, p, null)); }
@Test public void testSubmitValidCustomer() { HttpPost p = new HttpPost(secureLiveTestEndpoint); byte[] anyData = "anyData".getBytes(); int randomIndex = generator.nextInt(CustomerIdExamples.validCustomerIds.length); String validCustomerId = CustomerIdExamples.validCustomerIds[randomIndex]; int status = WebClient.send(validCustomerId, anyData, p, null); assertTrue("customerId=" + validCustomerId, status == HttpStatus.SC_OK || status == HttpStatus.SC_BAD_GATEWAY); }
@Test public void testSubmitValidAnonymousUser() { HttpPost p = new HttpPost(secureLiveTestEndpoint); byte[] anyData = "anyData".getBytes(); int randomIndex = generator.nextInt(CustomerIdExamples.validAnonymousIds.length); String validCustomerId = CustomerIdExamples.validAnonymousIds[randomIndex]; int status = WebClient.send(validCustomerId, anyData, p, null); assertTrue("customerId=" + validCustomerId, status == HttpStatus.SC_OK || status == HttpStatus.SC_BAD_GATEWAY); }