### Question:
S3ErrorResponseHandler implements HttpResponseHandler<AmazonServiceException> { @Override public AmazonServiceException handle(HttpResponse errorResponse) throws IOException { final InputStream is = errorResponse.getContent(); if (is == null) { return newAmazonS3Exception(errorResponse.getStatusText(), errorResponse); } String content = ""; try { content = IOUtils.toString(is); } catch (final IOException ex) { if (log.isDebugEnabled()) { log.debug("Failed in reading the error response", ex); } return newAmazonS3Exception(errorResponse.getStatusText(), errorResponse); } try { final Document document = XpathUtils.documentFrom(content); final String message = XpathUtils.asString("Error/Message", document); final String errorCode = XpathUtils.asString("Error/Code", document); final String requestId = XpathUtils.asString("Error/RequestId", document); final String extendedRequestId = XpathUtils.asString("Error/HostId", document); final AmazonS3Exception ase = new AmazonS3Exception(message); final int statusCode = errorResponse.getStatusCode(); ase.setStatusCode(statusCode); ase.setErrorType(errorTypeOf(statusCode)); ase.setErrorCode(errorCode); ase.setRequestId(requestId); ase.setExtendedRequestId(extendedRequestId); ase.setCloudFrontId(errorResponse.getHeaders().get(Headers.CLOUD_FRONT_ID)); return ase; } catch (final Exception ex) { if (log.isDebugEnabled()) { log.debug("Failed in parsing the response as XML: " + content, ex); } return newAmazonS3Exception(content, errorResponse); } }  @Override AmazonServiceException handle(HttpResponse errorResponse); @Override boolean needsConnectionLeftOpen();  }### Answer:
@Test public void testHandleErrorResponseWithNoBody() throws IOException { HttpResponse errorResponse = new HttpResponse.Builder() .statusCode(500) .content(null) .statusText("testError") .header(Headers.REQUEST_ID, "testId") .header(Headers.EXTENDED_REQUEST_ID, "extendedId").build(); S3ErrorResponseHandler handler = new S3ErrorResponseHandler(); AmazonS3Exception ase = (AmazonS3Exception) handler.handle(errorResponse); assertEquals(ase.getErrorMessage(), "testError"); assertEquals(ase.getErrorType(), ErrorType.Service); assertEquals(ase.getRequestId(), "testId"); assertEquals(ase.getExtendedRequestId(), "extendedId"); assertEquals(ase.getStatusCode(), 500); }
@Test public void testHandleErrorResponse() throws IOException { String response = "<Error>" + "<Message>testError</Message>" + "<Code>testCode</Code>" + "<RequestId>testId</RequestId>" + "<HostId>testHost</HostId>" + "</Error>"; ByteArrayInputStream content = new ByteArrayInputStream(response.getBytes(StringUtils.UTF8)); HttpResponse errorResponse = new HttpResponse.Builder() .statusCode(200) .content(content) .statusText("testError").build(); S3ErrorResponseHandler handler = new S3ErrorResponseHandler(); AmazonS3Exception ase = (AmazonS3Exception) handler.handle(errorResponse); assertEquals(ase.getErrorMessage(), "testError"); assertEquals(ase.getErrorType(), ErrorType.Client); assertEquals(ase.getRequestId(), "testId"); assertEquals(ase.getExtendedRequestId(), "testHost"); assertEquals(ase.getStatusCode(), 200); assertEquals(ase.getErrorCode(), "testCode"); }
@Test public void testErrorResponseFromUnparsableXML() throws IOException { String response = "<Error>" + "<Message>testError"; ByteArrayInputStream content = new ByteArrayInputStream(response.getBytes(StringUtils.UTF8)); HttpResponse errorResponse = new HttpResponse.Builder() .statusCode(200) .content(content) .statusText("testError").build(); S3ErrorResponseHandler handler = new S3ErrorResponseHandler(); AmazonS3Exception ase = (AmazonS3Exception) handler.handle(errorResponse); assertEquals(ase.getStatusCode(), 200); assertEquals(ase.getErrorType(), ErrorType.Client); assertEquals(ase.getErrorCode(), "200 testError"); }