### Question:
StaxResponseHandler implements HttpResponseHandler<AmazonWebServiceResponse<T>> { @Override public AmazonWebServiceResponse<T> handle(HttpResponse response) throws Exception { log.trace("Parsing service response XML"); InputStream content = response.getContent(); if (content == null) content = new ByteArrayInputStream("<eof/>".getBytes(StringUtils.UTF8)); XmlPullParser xpp = XML_PULL_PARSER_FACTORY.newPullParser(); xpp.setInput(content, null); AmazonWebServiceResponse<T> awsResponse = new AmazonWebServiceResponse<T>(); StaxUnmarshallerContext unmarshallerContext = new StaxUnmarshallerContext(xpp, response.getHeaders()); unmarshallerContext.registerMetadataExpression("ResponseMetadata/RequestId", 2, ResponseMetadata.AWS_REQUEST_ID); unmarshallerContext.registerMetadataExpression("requestId", 2, ResponseMetadata.AWS_REQUEST_ID); registerAdditionalMetadataExpressions(unmarshallerContext); T result = responseUnmarshaller.unmarshall(unmarshallerContext); awsResponse.setResult(result); Map<String, String> metadata = unmarshallerContext.getMetadata(); Map<String, String> responseHeaders = response.getHeaders(); if (responseHeaders != null) { if (responseHeaders.get("x-amzn-RequestId") != null) { metadata.put(ResponseMetadata.AWS_REQUEST_ID, responseHeaders.get("x-amzn-RequestId")); } } awsResponse.setResponseMetadata(new ResponseMetadata(metadata)); log.trace("Done parsing service response"); return awsResponse; } StaxResponseHandler(Unmarshaller<T, StaxUnmarshallerContext> responseUnmarshaller); @Override AmazonWebServiceResponse<T> handle(HttpResponse response); @Override boolean needsConnectionLeftOpen();  }### Answer:
@Test public void testHandleWithContent() throws Exception { final ByteArrayInputStream bais = new ByteArrayInputStream( ("<data>Content</data>").getBytes(StringUtils.UTF8)); final HttpResponse response = new HttpResponse.Builder().header("testKey", "testValue") .header("x-amzn-RequestId", "99") .content(bais).build(); Unmarshaller<String, StaxUnmarshallerContext> unmarshaller = new Unmarshaller<String, StaxUnmarshallerContext>() { @Override public String unmarshall(StaxUnmarshallerContext in) throws Exception { in.nextEvent(); String content = in.readText(); assertEquals(content, "Content"); assertEquals(in.getHeader("testKey"), "testValue"); return content; } }; StaxResponseHandler<String> handler = new StaxResponseHandler<String>(unmarshaller); AmazonWebServiceResponse<String> awsr = handler.handle(response); assertEquals(awsr.getResponseMetadata().getRequestId(), "99"); assertEquals(awsr.getResult(), "Content"); }
@Test public void testHandleWithNullContent() throws Exception { final HttpResponse response = new HttpResponse.Builder().header("testKey", "testValue") .content(null).build(); Unmarshaller<String, StaxUnmarshallerContext> unmarshaller = new Unmarshaller<String, StaxUnmarshallerContext>() { @Override public String unmarshall(StaxUnmarshallerContext in) throws Exception { assertEquals(in.getHeader("testKey"), "testValue"); return "Test"; } }; StaxResponseHandler<String> handler = new StaxResponseHandler<String>(unmarshaller); assertEquals(handler.handle(response).getResult(), "Test"); }