diff --git a/google-http-client/src/main/java/com/google/api/client/http/javanet/NetHttpRequest.java b/google-http-client/src/main/java/com/google/api/client/http/javanet/NetHttpRequest.java index cd92cac11..58850b794 100644 --- a/google-http-client/src/main/java/com/google/api/client/http/javanet/NetHttpRequest.java +++ b/google-http-client/src/main/java/com/google/api/client/http/javanet/NetHttpRequest.java @@ -117,6 +117,12 @@ LowLevelHttpResponse execute(final OutputWriter outputWriter) throws IOException writeContentToOutputStream(outputWriter, out); threw = false; + } catch (IOException e) { + // If we've gotten a response back, continue on and try to parse the response. Otherwise, + // re-throw the IOException + if (!hasReponse(connection)) { + throw e; + } } finally { try { out.close(); @@ -150,6 +156,15 @@ LowLevelHttpResponse execute(final OutputWriter outputWriter) throws IOException } } + private boolean hasReponse(HttpURLConnection connection) { + try { + return connection.getResponseCode() > 0; + } catch (IOException e) { + // There's some exception trying to parse the response + return false; + } + } + private void writeContentToOutputStream(final OutputWriter outputWriter, final OutputStream out) throws IOException { if (writeTimeout == 0) { diff --git a/google-http-client/src/test/java/com/google/api/client/http/javanet/NetHttpRequestTest.java b/google-http-client/src/test/java/com/google/api/client/http/javanet/NetHttpRequestTest.java index 6839dfd76..d1a3e03cc 100644 --- a/google-http-client/src/test/java/com/google/api/client/http/javanet/NetHttpRequestTest.java +++ b/google-http-client/src/test/java/com/google/api/client/http/javanet/NetHttpRequestTest.java @@ -2,6 +2,7 @@ import com.google.api.client.http.HttpContent; import com.google.api.client.http.InputStreamContent; +import com.google.api.client.http.LowLevelHttpResponse; import com.google.api.client.http.javanet.NetHttpRequest.OutputWriter; import com.google.api.client.testing.http.HttpTesting; import com.google.api.client.testing.http.javanet.MockHttpURLConnection; @@ -82,4 +83,118 @@ private static void postWithTimeout(int timeout) throws Exception { request.execute(new SleepingOutputWriter(5000L)); } + @Test + public void testInterruptedWriteWithResponse() throws Exception { + MockHttpURLConnection connection = new MockHttpURLConnection(new URL(HttpTesting.SIMPLE_URL)) { + @Override + public OutputStream getOutputStream() throws IOException { + return new OutputStream() { + @Override + public void write(int b) throws IOException { + throw new IOException("Error writing request body to server"); + } + }; + } + }; + connection.setResponseCode(401); + connection.setRequestMethod("POST"); + NetHttpRequest request = new NetHttpRequest(connection); + InputStream is = NetHttpRequestTest.class.getClassLoader().getResourceAsStream("file.txt"); + HttpContent content = new InputStreamContent("text/plain", is); + request.setStreamingContent(content); + + LowLevelHttpResponse response = request.execute(); + assertEquals(401, response.getStatusCode()); + } + + @Test + public void testInterruptedWriteWithoutResponse() throws Exception { + MockHttpURLConnection connection = new MockHttpURLConnection(new URL(HttpTesting.SIMPLE_URL)) { + @Override + public OutputStream getOutputStream() throws IOException { + return new OutputStream() { + @Override + public void write(int b) throws IOException { + throw new IOException("Error writing request body to server"); + } + }; + } + }; + connection.setRequestMethod("POST"); + NetHttpRequest request = new NetHttpRequest(connection); + InputStream is = NetHttpRequestTest.class.getClassLoader().getResourceAsStream("file.txt"); + HttpContent content = new InputStreamContent("text/plain", is); + request.setStreamingContent(content); + + try { + request.execute(); + fail("Expected to throw an IOException"); + } catch (IOException e) { + assertEquals("Error writing request body to server", e.getMessage()); + } + } + + @Test + public void testInterruptedWriteErrorOnResponse() throws Exception { + MockHttpURLConnection connection = new MockHttpURLConnection(new URL(HttpTesting.SIMPLE_URL)) { + @Override + public OutputStream getOutputStream() throws IOException { + return new OutputStream() { + @Override + public void write(int b) throws IOException { + throw new IOException("Error writing request body to server"); + } + }; + } + + @Override + public int getResponseCode() throws IOException { + throw new IOException("Error parsing response code"); + } + }; + connection.setRequestMethod("POST"); + NetHttpRequest request = new NetHttpRequest(connection); + InputStream is = NetHttpRequestTest.class.getClassLoader().getResourceAsStream("file.txt"); + HttpContent content = new InputStreamContent("text/plain", is); + request.setStreamingContent(content); + + try { + request.execute(); + fail("Expected to throw an IOException"); + } catch (IOException e) { + assertEquals("Error writing request body to server", e.getMessage()); + } + } + + @Test + public void testErrorOnClose() throws Exception { + MockHttpURLConnection connection = new MockHttpURLConnection(new URL(HttpTesting.SIMPLE_URL)) { + @Override + public OutputStream getOutputStream() throws IOException { + return new OutputStream() { + @Override + public void write(int b) throws IOException { + return; + } + + @Override + public void close() throws IOException { + throw new IOException("Error during close"); + } + }; + } + }; + connection.setRequestMethod("POST"); + NetHttpRequest request = new NetHttpRequest(connection); + InputStream is = NetHttpRequestTest.class.getClassLoader().getResourceAsStream("file.txt"); + HttpContent content = new InputStreamContent("text/plain", is); + request.setStreamingContent(content); + + try { + request.execute(); + fail("Expected to throw an IOException"); + } catch (IOException e) { + assertEquals("Error during close", e.getMessage()); + } + } }