AwsErrorResponseHandler implements HttpResponseHandler<AmazonServiceException> { @Override public AmazonServiceException handle(HttpResponse response) throws Exception { final AmazonServiceException ase = handleAse(response); ase.setStatusCode(response.getStatusCode()); ase.setServiceName(response.getRequest().getServiceName()); ase.setProxyHost(clientConfiguration.getProxyHost()); awsRequestMetrics.addPropertyWith(AWSRequestMetrics.Field.AWSRequestID, ase.getRequestId()) .addPropertyWith(AWSRequestMetrics.Field.AWSErrorCode, ase.getErrorCode()) .addPropertyWith(AWSRequestMetrics.Field.StatusCode, ase.getStatusCode()); return ase; } AwsErrorResponseHandler(HttpResponseHandler<AmazonServiceException> errorResponseHandler,
                            AWSRequestMetrics awsRequestMetrics,
                            ClientConfiguration clientConfiguration); @Override AmazonServiceException handle(HttpResponse response); @Override boolean needsConnectionLeftOpen();  }
@Test public void handle_withProxy() throws Exception { HttpResponseHandler<AmazonServiceException> delegate = mock(HttpResponseHandler.class); AmazonServiceException ase = new AmazonServiceException(ERROR_MESSAGE); ase.setRequestId(REQUEST_ID); when(delegate.handle(any(HttpResponse.class))).thenReturn(ase); AWSRequestMetrics awsRequestMetrics = mock(AWSRequestMetrics.class); ClientConfiguration conf = new ClientConfiguration(); conf.setProxyHost(PROXY_HOST); AwsErrorResponseHandler responseHandler = new AwsErrorResponseHandler(delegate, awsRequestMetrics, conf); HttpResponse httpResponse = new HttpResponse(new DefaultRequest<String>(SERVICE_NAME), null); httpResponse.setStatusCode(HttpStatus.SC_FORBIDDEN); AmazonServiceException e = responseHandler.handle(httpResponse); assertNotNull(e); assertEquals(PROXY_HOST, e.getProxyHost()); assertNotNull(e.getMessage()); assertTrue(e.getMessage().contains("Proxy: " + PROXY_HOST)); }
@Test public void handle_withoutProxy() throws Exception { HttpResponseHandler<AmazonServiceException> delegate = mock(HttpResponseHandler.class); AmazonServiceException ase = new AmazonServiceException(ERROR_MESSAGE); when(delegate.handle(any(HttpResponse.class))).thenReturn(ase); AWSRequestMetrics awsRequestMetrics = mock(AWSRequestMetrics.class); ClientConfiguration conf = new ClientConfiguration(); AwsErrorResponseHandler responseHandler = new AwsErrorResponseHandler(delegate, awsRequestMetrics, conf); HttpResponse httpResponse = new HttpResponse(new DefaultRequest<String>(SERVICE_NAME), null); httpResponse.setStatusCode(HttpStatus.SC_FORBIDDEN); AmazonServiceException e = responseHandler.handle(httpResponse); assertNotNull(e); assertNull(e.getProxyHost()); assertNotNull(e.getMessage()); assertTrue(e.getMessage().contains("Proxy: null")); }