@Test public void testS3XmlResponseHandlerTest() throws Exception { ByteArrayInputStream bais = new ByteArrayInputStream("content".getBytes(StringUtils.UTF8)); HttpResponse response = new HttpResponse.Builder().content(bais) .header("testKey", "testValue").build(); final List<Boolean> handleCalled = new ArrayList<Boolean>(); S3XmlResponseHandler<String> handler = new S3XmlResponseHandler<String>( new Unmarshaller<String, InputStream>() { @Override public String unmarshall(InputStream in) throws Exception { assertEquals("content", IOUtils.toString(in)); handleCalled.add(true); return null; } }); handler.handle(response); assertEquals(handleCalled.size(), 1); assertTrue(handleCalled.get(0)); }
@Test public void testHandle() throws Exception { ByteArrayInputStream bais = new ByteArrayInputStream("content".getBytes(StringUtils.UTF8)); HttpResponse response = new HttpResponse.Builder().content(bais) .header(Headers.REDIRECT_LOCATION, "redirect") .header(Headers.REQUESTER_CHARGED_HEADER, "true").build(); S3ObjectResponseHandler handler = new S3ObjectResponseHandler(); AmazonWebServiceResponse<S3Object> object = handler.handle(response); S3Object content = object.getResult(); assertEquals(content.getRedirectLocation(), "redirect"); assertTrue(content.isRequesterCharged()); S3ObjectInputStream is = content.getObjectContent(); int curr = -1; ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((curr = is.read()) != -1) { baos.write(curr); } assertArrayEquals(baos.toByteArray(), "content".getBytes(StringUtils.UTF8)); }
@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 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 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 = 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(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 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 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 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 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"); } }
@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 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 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); }
/** * Creates a builder for {@link HttpResponse}. * * @return a builder */ public static Builder builder() { return new Builder(); }