Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update NetHttpRequest to prevent silent retry of DELETE requests #1472

Merged
merged 1 commit into from Oct 7, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -141,6 +141,9 @@ LowLevelHttpResponse execute(final OutputWriter outputWriter) throws IOException
Preconditions.checkArgument(
contentLength == 0, "%s with non-zero content length is not supported", requestMethod);
}
} else if ("DELETE".equals(connection.getRequestMethod())) {
connection.setDoOutput(true);
connection.setFixedLengthStreamingMode(0L);
}
// connect
boolean successfulConnection = false;
Expand Down
Expand Up @@ -42,6 +42,10 @@ public class MockHttpURLConnection extends HttpURLConnection {

/** Whether {@link #doOutput} was called. */
private boolean doOutputCalled;
/** Whether {@link #setFixedLengthStreamingMode(int)} was called. */
private boolean setFixedLengthStreamingModeIntCalled = false;
/** Whether {@link #setFixedLengthStreamingMode(long)} was called. */
private boolean setFixedLengthStreamingModeLongCalled = false;

/**
* Output stream or {@code null} to throw an {@link UnknownServiceException} when {@link
Expand Down Expand Up @@ -205,4 +209,24 @@ public String getHeaderField(String name) {
public int getChunkLength() {
return chunkLength;
}

@Override
public void setFixedLengthStreamingMode(int contentLength) {
this.setFixedLengthStreamingModeIntCalled = true;
super.setFixedLengthStreamingMode(contentLength);
}

@Override
public void setFixedLengthStreamingMode(long contentLength) {
this.setFixedLengthStreamingModeLongCalled = true;
super.setFixedLengthStreamingMode(contentLength);
}

public boolean isSetFixedLengthStreamingModeIntCalled() {
return setFixedLengthStreamingModeIntCalled;
}

public boolean isSetFixedLengthStreamingModeLongCalled() {
return setFixedLengthStreamingModeLongCalled;
}
}
Expand Up @@ -233,4 +233,17 @@ public void testChunkedLengthNotSet() throws Exception {
assertEquals(connection.getChunkLength(), -1);
assertEquals("6", request.getRequestProperty("Content-Length"));
}

// see https://github.com/googleapis/google-http-java-client/issues/1471 for more details
@Test
public void testDeleteSetsContentLengthToZeroWithoutContent() throws Exception {
MockHttpURLConnection connection = new MockHttpURLConnection(new URL(HttpTesting.SIMPLE_URL));
connection.setRequestMethod("DELETE");
NetHttpRequest request = new NetHttpRequest(connection);
request.execute();

assertTrue(connection.doOutputCalled());
assertTrue(connection.isSetFixedLengthStreamingModeLongCalled());
assertFalse(connection.isSetFixedLengthStreamingModeIntCalled());
}
}