@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(); }
@Override public HttpResponse execute(HttpRequest request) throws IOException { return HttpResponse.builder() .statusCode(statusCode) .statusText(reasonPhrase) .build(); }
@Test public void testJsonErrorResponseReturnsNullIfExceptionIsNull() throws Exception { List<JsonErrorUnmarshaller> exceptionUnmarshallers = new ArrayList<JsonErrorUnmarshaller>(); exceptionUnmarshallers.add(new JsonErrorUnmarshaller() { @Override public AmazonServiceException unmarshall(JsonErrorResponse error) throws Exception { return null; } }); handler = new JsonErrorResponseHandler(exceptionUnmarshallers); response = HttpResponse.builder() .statusCode(403) .content(new ByteArrayInputStream("{}".getBytes(StringUtils.UTF8))) .build(); assertNull(handler.handle(response)); }
@Test public void testJsonErrorResponse() throws Exception { String body = "{\"__type\":\"InvalidSignatureException\",\"message\":\"Clock is skewed\"}"; ByteArrayInputStream content = new ByteArrayInputStream(body.getBytes(StringUtils.UTF8)); response = HttpResponse.builder() .content(content) .statusCode(403) .build(); AmazonServiceException ase = handler.handle(response); assertEquals(ase.getErrorCode(), "InvalidSignatureException"); }
@Test public void testRestJsonErrorResponse() throws Exception { String body = "{\"message\":\"Clock is skewed\"}"; ByteArrayInputStream content = new ByteArrayInputStream(body.getBytes(StringUtils.UTF8)); response = HttpResponse.builder() .content(content) .statusCode(403) .header("x-amzn-ErrorType", "InvalidSignatureException") .build(); AmazonServiceException ase = handler.handle(response); assertEquals(ase.getErrorCode(), "InvalidSignatureException"); }
@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 testJsonErrorResponseReturnsServiceErrorTypeIfErrorStatus5XX() throws Exception { List<JsonErrorUnmarshaller> exceptionUnmarshallers = new ArrayList<JsonErrorUnmarshaller>(); exceptionUnmarshallers.add(new JsonErrorUnmarshaller() { @Override public AmazonServiceException unmarshall(JsonErrorResponse error) throws Exception { return new AmazonServiceException("TestException"); } }); handler = new JsonErrorResponseHandler(exceptionUnmarshallers); response = HttpResponse.builder() .statusCode(500) .content(new ByteArrayInputStream("{}".getBytes(StringUtils.UTF8))) .build(); AmazonServiceException returnedException = handler.handle(response); assertEquals(returnedException.getErrorType(), ErrorType.Service); }
@Test public void testJsonErrorResponseReturnsXAmzRequestId() throws Exception { List<JsonErrorUnmarshaller> exceptionUnmarshallers = new ArrayList<JsonErrorUnmarshaller>(); exceptionUnmarshallers.add(new JsonErrorUnmarshaller() { @Override public AmazonServiceException unmarshall(JsonErrorResponse error) throws Exception { return new AmazonServiceException("TestException"); } }); handler = new JsonErrorResponseHandler(exceptionUnmarshallers); response = HttpResponse.builder() .statusCode(500) .header("X-Amzn-RequestId", "55") .content(new ByteArrayInputStream("{}".getBytes(StringUtils.UTF8))) .build(); AmazonServiceException returnedException = handler.handle(response); assertEquals(returnedException.getRequestId(), "55"); } }
@Test public void testParseExpirationHeader2() { MockObjectExpirationResult result = new MockObjectExpirationResult(); ObjectExpirationHeaderHandler<MockObjectExpirationResult> handler = new ObjectExpirationHeaderHandler<MockObjectExpirationResult>(); HttpResponse response = HttpResponse.builder() .header("x-amz-expiration", "rule-id=\"Test\", expiry-date=\"Tue, 01 Jan 2013 00:00:00 GMT\"") .build(); handler.handle(result, response); Assert.assertEquals(1356998400000L, result.getExpirationTime().getTime()); Assert.assertEquals("Test", result.getExpirationTimeRuleId()); }
@Test public void testContentEncodingGZIPRaw() throws Exception { String str = "content to be zipped"; InputStream zippedContent = getGzippedInputStream(str); builder = HttpResponse.builder() .header("Content-Encoding", "gzip") .content(zippedContent); response = builder.build(); InputStream rawContent = response.getRawContent(); assertFalse("Not gzip", rawContent instanceof GZIPInputStream); GZIPInputStream gis = new GZIPInputStream(rawContent); String result = IOUtils.toString(gis); assertEquals("unzip correctly", str, result); }
@Test public void testParseExpirationHeader1() { MockObjectExpirationResult result = new MockObjectExpirationResult(); ObjectExpirationHeaderHandler<MockObjectExpirationResult> handler = new ObjectExpirationHeaderHandler<MockObjectExpirationResult>(); HttpResponse response = HttpResponse.builder() .header("x-amz-expiration", "expiry-date=\"Tue, 01 Jan 2013 00:00:00 GMT\", rule-id=\"Test\"") .build(); handler.handle(result, response); Assert.assertEquals(1356998400000L, result.getExpirationTime().getTime()); Assert.assertEquals("Test", result.getExpirationTimeRuleId()); }
@Test public void testParseRestoreHeader2() { MockObjectRestoreResult result = new MockObjectRestoreResult(); ObjectRestoreHeaderHandler<MockObjectRestoreResult> handler = new ObjectRestoreHeaderHandler<MockObjectRestoreResult>(); HttpResponse response = HttpResponse.builder() .header("x-amz-restore", "ongoing-request=\"false\", " + "expiry-date=\"Tue, 01 Jan 2013 00:00:00 GMT\"") .build(); handler.handle(result, response); Assert.assertFalse(result.getOngoingRestore()); Assert.assertEquals(1356998400000L, result.getRestoreExpirationTime().getTime()); }
@Test public void testParseRestoreHeader3() { MockObjectRestoreResult result = new MockObjectRestoreResult(); ObjectRestoreHeaderHandler<MockObjectRestoreResult> handler = new ObjectRestoreHeaderHandler<MockObjectRestoreResult>(); HttpResponse response = HttpResponse.builder() .header("x-amz-restore", "expiry-date=\"Tue, 01 Jan 2013 00:00:00 GMT\", " + "ongoing-request=\"false\"") .build(); handler.handle(result, response); Assert.assertFalse(result.getOngoingRestore()); Assert.assertEquals(1356998400000L, result.getRestoreExpirationTime().getTime()); }
@Test public void testParseRestoreHeader1() { MockObjectRestoreResult result = new MockObjectRestoreResult(); ObjectRestoreHeaderHandler<MockObjectRestoreResult> handler = new ObjectRestoreHeaderHandler<MockObjectRestoreResult>(); HttpResponse response = HttpResponse.builder() .header("x-amz-restore", "ongoing-request=\"true\"") .build(); handler.handle(result, response); Assert.assertTrue(result.getOngoingRestore()); }
@Test public void testParseEmptyRestoreHeader() { MockObjectRestoreResult result = new MockObjectRestoreResult(); ObjectRestoreHeaderHandler<MockObjectRestoreResult> handler = new ObjectRestoreHeaderHandler<MockObjectRestoreResult>(); HttpResponse response = HttpResponse.builder().build(); handler.handle(result, response); Assert.assertNull(result.getRestoreExpirationTime()); Assert.assertNull(result.getOngoingRestore()); }
@Test public void testContentEncodingIdentity() throws Exception { builder = HttpResponse.builder() .header("Content-Encoding", "identity") .content(content); response = builder.build(); assertFalse("Not gzip", response.getContent() instanceof GZIPInputStream); assertEquals("same content", "content", IOUtils.toString(response.getContent())); }
@Test public void testParseEmptyExpirationHeader() { MockObjectExpirationResult result = new MockObjectExpirationResult(); ObjectExpirationHeaderHandler<MockObjectExpirationResult> handler = new ObjectExpirationHeaderHandler<MockObjectExpirationResult>(); HttpResponse response = HttpResponse.builder().build(); handler.handle(result, response); Assert.assertNull(result.getExpirationTime()); Assert.assertNull(result.getExpirationTimeRuleId()); }
@Test public void testJsonExceptionUnmarshallerWithAdditionalFields() throws Exception { HttpResponse response = HttpResponse.builder() .content(new ByteArrayInputStream(errorResponse.getBytes("UTF-8"))) .build(); JsonErrorResponse error = JsonErrorResponse.fromResponse(response); AmazonServiceException ase = new InternalServerErrorExceptionUnmarshaller() .unmarshall(error); assertTrue(ase instanceof InternalServerErrorException); assertEquals("value1", ((InternalServerErrorException) ase).getField1()); assertEquals("value2", ((InternalServerErrorException) ase).getField2()); assertEquals("InternalServerError", ase.getErrorCode()); assertEquals("Requested resource not found " + "(Service: null; " + "Status Code: 0; " + "Error Code: InternalServerError; " + "Request ID: null)", ase.getMessage()); }
@Test public void testContentEncodingGZIP() throws Exception { String str = "content to be zipped"; InputStream zippedContent = getGzippedInputStream(str); builder = HttpResponse.builder() .header("Content-Encoding", "gzip") .content(zippedContent); response = builder.build(); InputStream unzippedContent = response.getContent(); assertTrue(unzippedContent instanceof GZIPInputStream); String result = IOUtils.toString(unzippedContent); assertEquals("unzip correctly", str, result); }
@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()); }