@Test public void send_CompressedResponseIsUncompressed() throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); GZIPOutputStream gzipOutputStream = new GZIPOutputStream(output); gzipOutputStream.write("{\"nextRequestWaitMillis\":3}".getBytes(Charset.forName("UTF-8"))); gzipOutputStream.close(); stubFor( post(urlEqualTo("/api")) .willReturn( aResponse() .withStatus(200) .withHeader("Content-Type", "application/json;charset=UTF8;hello=world") .withHeader("Content-Encoding", "gzip") .withBody(output.toByteArray()))); BackendRequest backendRequest = getCCTBackendRequest(); wallClock.tick(); uptimeClock.tick(); BackendResponse response = BACKEND.send(backendRequest); verify( postRequestedFor(urlEqualTo("/api")) .withHeader("Content-Type", equalTo("application/json")) .withHeader("Content-Encoding", equalTo("gzip"))); assertEquals(BackendResponse.ok(3), response); }
@Override public BackendResponse send(BackendRequest request) { BatchedLogRequest requestBody = getRequestBody(request); String apiKey = null; URL actualEndPoint = endPoint; if (request.getExtras() != null) { try { CCTDestination destination = CCTDestination.fromByteArray(request.getExtras()); if (destination.getAPIKey() != null) { apiKey = destination.getAPIKey(); } if (destination.getEndPoint() != null) { actualEndPoint = parseUrlOrThrow(destination.getEndPoint()); } } catch (IllegalArgumentException e) { return BackendResponse.fatalError(); } } try { HttpResponse response = retry( 5, new HttpRequest(actualEndPoint, requestBody, apiKey), this::doSend, (req, resp) -> { if (resp.redirectUrl != null) { Logging.d(LOG_TAG, "Following redirect to: %s", resp.redirectUrl); return req.withUrl(resp.redirectUrl); } return null; }); if (response.code == 200) { return BackendResponse.ok(response.nextRequestMillis); } else if (response.code >= 500 || response.code == 404) { return BackendResponse.transientError(); } else { return BackendResponse.fatalError(); } } catch (IOException e) { Logging.e(LOG_TAG, "Could not make request to the backend", e); return BackendResponse.transientError(); } }
CctTransportBackend implements TransportBackend { @Override public BackendResponse send(BackendRequest request) { BatchedLogRequest requestBody = getRequestBody(request); String apiKey = null; URL actualEndPoint = endPoint; if (request.getExtras() != null) { try { CCTDestination destination = CCTDestination.fromByteArray(request.getExtras()); if (destination.getAPIKey() != null) { apiKey = destination.getAPIKey(); } if (destination.getEndPoint() != null) { actualEndPoint = parseUrlOrThrow(destination.getEndPoint()); } } catch (IllegalArgumentException e) { return BackendResponse.fatalError(); } } try { HttpResponse response = retry( 5, new HttpRequest(actualEndPoint, requestBody, apiKey), this::doSend, (req, resp) -> { if (resp.redirectUrl != null) { Logging.d(LOG_TAG, "Following redirect to: %s", resp.redirectUrl); return req.withUrl(resp.redirectUrl); } return null; }); if (response.code == 200) { return BackendResponse.ok(response.nextRequestMillis); } else if (response.code >= 500 || response.code == 404) { return BackendResponse.transientError(); } else { return BackendResponse.fatalError(); } } catch (IOException e) { Logging.e(LOG_TAG, "Could not make request to the backend", e); return BackendResponse.transientError(); } } }
CctTransportBackend implements TransportBackend { @Override public BackendResponse send(BackendRequest request) { BatchedLogRequest requestBody = getRequestBody(request); String apiKey = null; URL actualEndPoint = endPoint; if (request.getExtras() != null) { try { CCTDestination destination = CCTDestination.fromByteArray(request.getExtras()); if (destination.getAPIKey() != null) { apiKey = destination.getAPIKey(); } if (destination.getEndPoint() != null) { actualEndPoint = parseUrlOrThrow(destination.getEndPoint()); } } catch (IllegalArgumentException e) { return BackendResponse.fatalError(); } } try { HttpResponse response = retry( 5, new HttpRequest(actualEndPoint, requestBody, apiKey), this::doSend, (req, resp) -> { if (resp.redirectUrl != null) { Logging.d(LOG_TAG, "Following redirect to: %s", resp.redirectUrl); return req.withUrl(resp.redirectUrl); } return null; }); if (response.code == 200) { return BackendResponse.ok(response.nextRequestMillis); } else if (response.code >= 500 || response.code == 404) { return BackendResponse.transientError(); } else { return BackendResponse.fatalError(); } } catch (IOException e) { Logging.e(LOG_TAG, "Could not make request to the backend", e); return BackendResponse.transientError(); } } CctTransportBackend(
      Context applicationContext, Clock wallTimeClock, Clock uptimeClock, int readTimeout);   CctTransportBackend(Context applicationContext, Clock wallTimeClock, Clock uptimeClock); }
CctTransportBackend implements TransportBackend { @Override public BackendResponse send(BackendRequest request) { BatchedLogRequest requestBody = getRequestBody(request); String apiKey = null; URL actualEndPoint = endPoint; if (request.getExtras() != null) { try { CCTDestination destination = CCTDestination.fromByteArray(request.getExtras()); if (destination.getAPIKey() != null) { apiKey = destination.getAPIKey(); } if (destination.getEndPoint() != null) { actualEndPoint = parseUrlOrThrow(destination.getEndPoint()); } } catch (IllegalArgumentException e) { return BackendResponse.fatalError(); } } try { HttpResponse response = retry( 5, new HttpRequest(actualEndPoint, requestBody, apiKey), this::doSend, (req, resp) -> { if (resp.redirectUrl != null) { Logging.d(LOG_TAG, "Following redirect to: %s", resp.redirectUrl); return req.withUrl(resp.redirectUrl); } return null; }); if (response.code == 200) { return BackendResponse.ok(response.nextRequestMillis); } else if (response.code >= 500 || response.code == 404) { return BackendResponse.transientError(); } else { return BackendResponse.fatalError(); } } catch (IOException e) { Logging.e(LOG_TAG, "Could not make request to the backend", e); return BackendResponse.transientError(); } } CctTransportBackend(
      Context applicationContext, Clock wallTimeClock, Clock uptimeClock, int readTimeout);   CctTransportBackend(Context applicationContext, Clock wallTimeClock, Clock uptimeClock); @Override EventInternal decorate(EventInternal eventInternal); @Override BackendResponse send(BackendRequest request); }
CctTransportBackend implements TransportBackend { @Override public BackendResponse send(BackendRequest request) { BatchedLogRequest requestBody = getRequestBody(request); String apiKey = null; URL actualEndPoint = endPoint; if (request.getExtras() != null) { try { CCTDestination destination = CCTDestination.fromByteArray(request.getExtras()); if (destination.getAPIKey() != null) { apiKey = destination.getAPIKey(); } if (destination.getEndPoint() != null) { actualEndPoint = parseUrlOrThrow(destination.getEndPoint()); } } catch (IllegalArgumentException e) { return BackendResponse.fatalError(); } } try { HttpResponse response = retry( 5, new HttpRequest(actualEndPoint, requestBody, apiKey), this::doSend, (req, resp) -> { if (resp.redirectUrl != null) { Logging.d(LOG_TAG, "Following redirect to: %s", resp.redirectUrl); return req.withUrl(resp.redirectUrl); } return null; }); if (response.code == 200) { return BackendResponse.ok(response.nextRequestMillis); } else if (response.code >= 500 || response.code == 404) { return BackendResponse.transientError(); } else { return BackendResponse.fatalError(); } } catch (IOException e) { Logging.e(LOG_TAG, "Could not make request to the backend", e); return BackendResponse.transientError(); } } CctTransportBackend(
      Context applicationContext, Clock wallTimeClock, Clock uptimeClock, int readTimeout);   CctTransportBackend(Context applicationContext, Clock wallTimeClock, Clock uptimeClock); @Override EventInternal decorate(EventInternal eventInternal); @Override BackendResponse send(BackendRequest request);  }