/* * Copyright 2010-2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * http://aws.amazon.com/apache2.0 * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ package com.amazonaws.http; import com.amazonaws.transform.StaxUnmarshallerContext; import com.amazonaws.transform.Unmarshaller; import java.io.IOException; import java.io.InputStream; import java.net.SocketTimeoutException; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; public class StaxResponseHandlerTest { @Mock private Unmarshaller unmarshaller; private StaxResponseHandler responseHandler; @Before public void setup() { responseHandler = new StaxResponseHandler(unmarshaller); } /** * This throws a socket timeout exception immediately. When creating an XML event reader, * it may read ahead a few bytes so we have to handle the {@link javax.xml.stream.XMLStreamException} in * two places. */ @Test(expected = IOException.class) public void socketTimeoutThrownFromInputStream_ThrowsIoException() throws Exception { HttpResponse response = new HttpResponse(null, null); response.setContent(new InputStream() { @Override public int read() throws IOException { throw new SocketTimeoutException("socket timeout"); } }); responseHandler.handle(response); } /** * The XML reader throws an {@link javax.xml.stream.XMLStreamException} which wraps the IO Exception. The handler * must unwrap it so that it's handled by retry policies correctly. */ @Test(expected = IOException.class) public void socketTimeoutThrownAfternInitialContent_ThrowsIoException() throws Exception { HttpResponse response = new HttpResponse(null, null); response.setContent(new InputStream() { String content = "myname123falsetrue1.21.3200a2015-01-25T08:00:00Zrequest-id"; int read = 0; @Override public int read() throws IOException { // Let it read some content first to get past creating an event reader. if (read > 50) { throw new SocketTimeoutException("socket timeout"); } return content.charAt(read++); } }); responseHandler.handle(response); } }