### Question:
HttpClientHandler extends XRayHandler { @Override public void handleResponse(Event event) { HttpServiceDownstreamResponseEvent responseEvent = (HttpServiceDownstreamResponseEvent) event; HttpServiceDownstreamRequestEvent requestEvent = (HttpServiceDownstreamRequestEvent) responseEvent.getRequest(); URI uri = getUriFromEvent(requestEvent); if (isWithinAWSCall() || isXRaySamplingCall(uri) || isXRayPluginCall(uri)) { return; } Optional<Subsegment> subsegmentOptional = getSubsegmentOptional(); if (subsegmentOptional.isPresent()) { addResponseInformation(subsegmentOptional.get(), responseEvent); endSubsegment(); } }  @Override void handleRequest(Event event); @Override void handleResponse(Event event);  }### Answer:
@Test public void testHandleGetResponse() { Subsegment httpClientSubsegment = AWSXRay.beginSubsegment("responseSubsegment"); httpClientHandler.handleResponse(httpClientResponseEvent); Map<String, String> responseMap = (Map<String, String>) httpClientSubsegment.getHttp().get("response"); Assert.assertFalse(httpClientSubsegment.isInProgress()); Assert.assertEquals(200,responseMap.get("status")); Assert.assertEquals(1, parentSegment.getSubsegments().size()); }
@Test public void testHandleInvalidRequest() { HttpServiceDownstreamResponseEvent failedResponseEvent = new HttpServiceDownstreamResponseEvent(ORIGIN, SERVICE, OPERATION, httpClientRequestEvent); failedResponseEvent.withStatusCode(500); failedResponseEvent.withContentLength(1000); Throwable ourThrowable = new IllegalArgumentException("Some illegal exception"); failedResponseEvent.withThrown(ourThrowable); Subsegment httpClientSubsegment = AWSXRay.beginSubsegment("failedResponseSubsegment"); httpClientHandler.handleResponse(failedResponseEvent); Assert.assertTrue(httpClientSubsegment.isFault()); Assert.assertEquals(1, httpClientSubsegment.getCause().getExceptions().size()); Assert.assertEquals(ourThrowable, httpClientSubsegment.getCause().getExceptions().get(0).getThrowable()); }
@Test public void testHandle4xxStatusCode() { httpClientResponseEvent.withStatusCode(400); Subsegment httpClientSubsegment = AWSXRay.beginSubsegment("failedResponseSubsegment"); httpClientHandler.handleResponse(httpClientResponseEvent); Assert.assertTrue(httpClientSubsegment.isError()); Assert.assertFalse(httpClientSubsegment.isFault()); Assert.assertFalse(httpClientSubsegment.isThrottle()); Map<String, Integer> httpResponseMap = (Map<String, Integer>) httpClientSubsegment.getHttp().get("response"); Assert.assertEquals(400, (int) httpResponseMap.get("status")); }
@Test public void testHandleThrottlingStatusCode() { httpClientResponseEvent.withStatusCode(429); Subsegment httpClientSubsegment = AWSXRay.beginSubsegment("failedResponseSubsegment"); httpClientHandler.handleResponse(httpClientResponseEvent); Assert.assertTrue(httpClientSubsegment.isError()); Assert.assertFalse(httpClientSubsegment.isFault()); Assert.assertTrue(httpClientSubsegment.isThrottle()); Map<String, Integer> httpResponseMap = (Map<String, Integer>) httpClientSubsegment.getHttp().get("response"); Assert.assertEquals(429, (int) httpResponseMap.get("status")); }
@Test public void testHandle5xxStatusCode() { httpClientResponseEvent.withStatusCode(500); Subsegment httpClientSubsegment = AWSXRay.beginSubsegment("failedResponseSubsegment"); httpClientHandler.handleResponse(httpClientResponseEvent); Assert.assertTrue(httpClientSubsegment.isFault()); Assert.assertFalse(httpClientSubsegment.isError()); Assert.assertFalse(httpClientSubsegment.isThrottle()); Map<String, Integer> httpResponseMap = (Map<String, Integer>) httpClientSubsegment.getHttp().get("response"); Assert.assertEquals(500, (int) httpResponseMap.get("status")); }