Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
}