@Override public HttpResponse execute(HttpRequest request) throws IOException { HttpUriRequest httpRequest = createHttpRequest(request); org.apache.http.HttpResponse httpResponse = httpClient.execute(httpRequest); String statusText = httpResponse.getStatusLine().getReasonPhrase(); int statusCode = httpResponse.getStatusLine().getStatusCode(); InputStream content = null; if (httpResponse.getEntity() != null) { content = httpResponse.getEntity().getContent(); } HttpResponse.Builder builder = HttpResponse.builder() .statusCode(statusCode) .statusText(statusText) .content(content); for (Header header : httpResponse.getAllHeaders()) { builder.header(header.getName(), header.getValue()); } return builder.build(); }
@Test public void testHandleWithCRC32() throws Exception { ByteArrayInputStream bais = new ByteArrayInputStream( "{\"key\" :\"Content\"}".getBytes(StringUtils.UTF8)); CRC32 crc32 = new CRC32(); crc32.update("{\"key\" :\"Content\"}".getBytes(StringUtils.UTF8)); HttpResponse response = new HttpResponse.Builder().statusText("testResponse") .statusCode(200).header("testKey", "testValue") .header("x-amz-crc32", String.valueOf(crc32.getValue())).content(bais).build(); Unmarshaller<String, JsonUnmarshallerContext> unmarshaller = new Unmarshaller<String, JsonUnmarshallerContext>() { @Override public String unmarshall(JsonUnmarshallerContext in) throws Exception { in.getReader().beginObject(); in.getReader().nextName(); return in.getReader().nextString(); } }; JsonResponseHandler<String> toTest = new JsonResponseHandler<String>(unmarshaller); // Is a no-op. just adding for code coverage of the no-op toTest.registerAdditionalMetadataExpressions(null); AmazonWebServiceResponse<String> awsResponse = toTest.handle(response); assertEquals(awsResponse.getResult(), "Content"); }
@Test public void testHandleWithNoCRC32() throws Exception { ByteArrayInputStream bais = new ByteArrayInputStream( "{\"key\" :\"Content\"}".getBytes(StringUtils.UTF8)); HttpResponse response = new HttpResponse.Builder().statusText("testResponse") .statusCode(200).header("testKey", "testValue").content(bais).build(); Unmarshaller<String, JsonUnmarshallerContext> unmarshaller = new Unmarshaller<String, JsonUnmarshallerContext>() { @Override public String unmarshall(JsonUnmarshallerContext in) throws Exception { in.getReader().beginObject(); in.getReader().nextName(); return in.getReader().nextString(); } }; JsonResponseHandler<String> toTest = new JsonResponseHandler<String>(unmarshaller); AmazonWebServiceResponse<String> awsResponse = toTest.handle(response); assertEquals(awsResponse.getResult(), "Content"); }
@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 testDefaultErrorResponseHandlerCannotGetContent() throws Exception { List<Unmarshaller<AmazonServiceException, Node>> unmarshallerList = new ArrayList<Unmarshaller<AmazonServiceException, Node>>(); HttpResponse errorResponse = new HttpResponse.Builder() .statusCode(400) .statusText("Error") .content(new InputStream() { @Override public int read() throws IOException { throw new IOException("Test IOException"); } }) .build(); DefaultErrorResponseHandler handler = new DefaultErrorResponseHandler(unmarshallerList); AmazonServiceException e = handler.handle(errorResponse); assertEquals(e.getErrorCode(), errorResponse.getStatusCode() + " " + errorResponse.getStatusText()); assertEquals(e.getStatusCode(), 400); assertEquals(e.getErrorType(), ErrorType.Unknown); }
@Test(expected = AmazonClientException.class) public void testHandleErrorResponseHandlerFailsWithUnknownException() throws IOException { Request<?> request = new DefaultRequest<String>("ServiceName"); final HttpResponse httpResponse = new HttpResponse.Builder().statusText("TestResponse") .statusCode(400).build(); HttpResponseHandler<AmazonServiceException> errorResponseHandler = new HttpResponseHandler<AmazonServiceException>() { @Override public AmazonServiceException handle(HttpResponse response) throws Exception { assertSame(response, httpResponse); throw new Exception("test"); } @Override public boolean needsConnectionLeftOpen() { return false; } }; AmazonServiceException e = client.handleErrorResponse(request, errorResponseHandler, httpResponse); }
@Test(expected = IOException.class) public void testHandleResponseThrowsIOException() throws IOException { Request<?> request = new DefaultRequest<String>("ServiceName"); final HttpResponse httpResponse = new HttpResponse.Builder().statusText("TestResponse") .statusCode(200).build(); HttpResponseHandler<AmazonWebServiceResponse<String>> responseHandler = new HttpResponseHandler<AmazonWebServiceResponse<String>>() { @Override public AmazonWebServiceResponse<String> handle(HttpResponse response) throws Exception { assertSame(response, httpResponse); throw new IOException("test"); } @Override public boolean needsConnectionLeftOpen() { return false; } }; client.handleResponse(request, responseHandler, httpResponse, new ExecutionContext()); }
@Test(expected = CRC32MismatchException.class) public void testHandleResponseThrowsCRC32MisMatch() throws IOException { Request<?> request = new DefaultRequest<String>("ServiceName"); final HttpResponse httpResponse = new HttpResponse.Builder().statusText("TestResponse") .statusCode(200).build(); HttpResponseHandler<AmazonWebServiceResponse<String>> responseHandler = new HttpResponseHandler<AmazonWebServiceResponse<String>>() { @Override public AmazonWebServiceResponse<String> handle(HttpResponse response) throws Exception { assertSame(response, httpResponse); throw new CRC32MismatchException("test"); } @Override public boolean needsConnectionLeftOpen() { return false; } }; client.handleResponse(request, responseHandler, httpResponse, new ExecutionContext()); }
@Test public void testHandleResponse() throws IOException { Request<?> request = new DefaultRequest<String>("ServiceName"); final HttpResponse httpResponse = new HttpResponse.Builder().statusText("TestResponse") .statusCode(200).build(); HttpResponseHandler<AmazonWebServiceResponse<String>> responseHandler = new HttpResponseHandler<AmazonWebServiceResponse<String>>() { @Override public AmazonWebServiceResponse<String> handle(HttpResponse response) throws Exception { assertSame(response, httpResponse); AmazonWebServiceResponse<String> awsResponse = new AmazonWebServiceResponse<String>(); awsResponse.setResult("Result"); return awsResponse; } @Override public boolean needsConnectionLeftOpen() { return false; } }; assertEquals("Result", client.handleResponse(request, responseHandler, httpResponse, new ExecutionContext())); }
@Test(expected = Exception.class) public void testHandleResponseThrowsGenericException() throws IOException { Request<?> request = new DefaultRequest<String>("ServiceName"); final HttpResponse httpResponse = new HttpResponse.Builder().statusText("TestResponse") .statusCode(200).build(); HttpResponseHandler<AmazonWebServiceResponse<String>> responseHandler = new HttpResponseHandler<AmazonWebServiceResponse<String>>() { @Override public AmazonWebServiceResponse<String> handle(HttpResponse response) throws Exception { assertSame(response, httpResponse); throw new Exception("test"); } @Override public boolean needsConnectionLeftOpen() { return false; } }; client.handleResponse(request, responseHandler, httpResponse, new ExecutionContext()); }
@Test(expected = RuntimeException.class) public void testHandleResponseWithNullResult() throws IOException { Request<?> request = new DefaultRequest<String>("ServiceName"); final HttpResponse httpResponse = new HttpResponse.Builder().statusText("TestResponse") .statusCode(200).build(); HttpResponseHandler<AmazonWebServiceResponse<String>> responseHandler = new HttpResponseHandler<AmazonWebServiceResponse<String>>() { @Override public AmazonWebServiceResponse<String> handle(HttpResponse response) throws Exception { assertSame(response, httpResponse); return null; } @Override public boolean needsConnectionLeftOpen() { return false; } }; client.handleResponse(request, responseHandler, httpResponse, new ExecutionContext()); }
@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 testBuilder() throws Exception { builder = HttpResponse.builder() .statusCode(statusCode) .statusText(statusText) .content(content); for (int i = 0; i < 10; i++) { builder.header("key" + i, "value" + i); } response = builder.build(); assertEquals("status text", statusText, response.getStatusText()); assertTrue("status code", statusCode == response.getStatusCode()); assertTrue("has headers", 10 == response.getHeaders().size()); assertEquals("content", content, response.getContent()); }
@Test public void testHandleWithNullContent() throws Exception { HttpResponse response = new HttpResponse.Builder().statusText("testResponse") .statusCode(200).header("testKey", "testValue").content(null).build(); Unmarshaller<String, JsonUnmarshallerContext> unmarshaller = new Unmarshaller<String, JsonUnmarshallerContext>() { @Override public String unmarshall(JsonUnmarshallerContext in) throws Exception { in.getReader().beginObject(); assertFalse(in.getReader().hasNext()); return "NullContent"; } }; JsonResponseHandler<String> toTest = new JsonResponseHandler<String>(unmarshaller); AmazonWebServiceResponse<String> awsResponse = toTest.handle(response); assertEquals(awsResponse.getResult(), "NullContent"); }
@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"); }
@Test public void testClockskewOffsetWithDateInBodyOfException() { HttpResponse httpResponse = new HttpResponse.Builder().statusText("TestResponse") .statusCode(400).build(); AmazonServiceException ase = new AmazonServiceException("(20041106T084937Z - 15)"); // assert date is > 10 years int offset = client.parseClockSkewOffset(httpResponse, ase); assertTrue(offset > 315400000); }
@Test public void testClockskewOffset() { HttpResponse httpResponse = new HttpResponse.Builder().statusText("TestResponse") .header("Date", "Sat, 06 Nov 2004 08:49:37 GMT") .statusCode(400).build(); AmazonServiceException ase = new AmazonServiceException("ClockSkew"); // assert date is > 10 years int offset = client.parseClockSkewOffset(httpResponse, ase); assertTrue(offset > 315400000); }
@Override public HttpResponse execute(HttpRequest request) throws IOException { return HttpResponse.builder() .statusCode(statusCode) .statusText(reasonPhrase) .build(); }
@Test(expected = UnsupportedOperationException.class) public void testUnmodifierableHeaders() { builder = HttpResponse.builder() .statusCode(statusCode) .statusText(statusText) .content(content); response = builder.build(); response.getHeaders().put("key", "value"); }
@Test public void testClockskewOffsetWithBogusDateAsssumesOffsetIsZero() { HttpResponse httpResponse = new HttpResponse.Builder().statusText("TestResponse") .header("Date", "Sat, 064 Jann 20044 08:49:37 GMT") .statusCode(400).build(); AmazonServiceException ase = new AmazonServiceException("ClockSkew"); // assert date is > 10 years int offset = client.parseClockSkewOffset(httpResponse, ase); assertEquals(offset, 0); }