@Test public void testHandle() throws Exception { S3StringResponseHandler handler = new S3StringResponseHandler(); ByteArrayInputStream bais = new ByteArrayInputStream("content".getBytes(StringUtils.UTF8)); HttpResponse response = new HttpResponse.Builder().content(bais).build(); AmazonWebServiceResponse<String> awsr = handler.handle(response); assertEquals(awsr.getResult(), "content"); } }
@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 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 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 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 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 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 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 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 testParseResponseMetadata() { HttpResponse response = new HttpResponse.Builder() .header(Headers.REQUEST_ID, "testId") .header(Headers.EXTENDED_REQUEST_ID, "extendedId") .build(); TClass handler = new TClass(); AmazonWebServiceResponse<String> result = handler.parseResponseMetadata(response); assertEquals(result.getResponseMetadata().getRequestId(), "testId"); assertEquals(((S3ResponseMetadata) result.getResponseMetadata()).getHostId(), "extendedId"); }
@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); }
@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 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 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 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(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); }
@Override public HttpResponse execute(HttpRequest request) throws IOException { return HttpResponse.builder() .statusCode(statusCode) .statusText(reasonPhrase) .build(); }