Skip to content
Open
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 @@ -463,7 +463,7 @@ private HttpRequest buildHttpRequest(boolean usingHead) throws IOException {
httpRequest.setContent(new EmptyContent());
}
httpRequest.getHeaders().putAll(requestHeaders);
if (!disableGZipContent) {
if (!disableGZipContent && !(httpRequest.getContent() instanceof EmptyContent)) {
httpRequest.setEncoding(new GZipEncoding());
}
httpRequest.setResponseReturnRawInputStream(returnRawInputStream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import com.google.api.client.googleapis.services.AbstractGoogleClientRequest.ApiClientVersion;
import com.google.api.client.googleapis.testing.services.MockGoogleClient;
import com.google.api.client.googleapis.testing.services.MockGoogleClientRequest;
import com.google.api.client.http.ByteArrayContent;
import com.google.api.client.http.EmptyContent;
import com.google.api.client.http.GZipEncoding;
import com.google.api.client.http.HttpMethods;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpResponseException;
Expand Down Expand Up @@ -163,6 +165,87 @@ private void subtestBuildHttpRequest_emptyContent(String method, boolean expectE
}
}

public void testBuildHttpRequest_emptyContentDisablesGZip() throws Exception {
for (String method : Arrays.asList("POST", "PUT", "PATCH")) {
subtestBuildHttpRequest_emptyContentDisablesGZip(method);
}
}

private void subtestBuildHttpRequest_emptyContentDisablesGZip(String method) throws Exception {
HttpTransport transport =
new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) {
return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() {
// empty content is sent without Content-Encoding and with Content-Length: 0,
// otherwise some servers reject the request (see GitHub issue 1548)
assertNull(getContentEncoding());
assertEquals(0, getContentLength());
return new MockLowLevelHttpResponse();
}
};
}
};
MockGoogleClient client =
new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null)
.setApplicationName("Test Application")
.build();
MockGoogleClientRequest<String> request =
new MockGoogleClientRequest<String>(client, method, URI_TEMPLATE, null, String.class);
HttpRequest httpRequest = request.buildHttpRequest();
assertNull(httpRequest.getEncoding());
request.executeUnparsed();
}

public void testBuildHttpRequest_nonEmptyContentGZipEncoded() throws Exception {
HttpTransport transport = new MockHttpTransport();
MockGoogleClient client =
new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null)
.setApplicationName("Test Application")
.build();
MockGoogleClientRequest<String> request =
new MockGoogleClientRequest<String>(
client,
HttpMethods.POST,
URI_TEMPLATE,
new ByteArrayContent("application/json", new byte[10]),
String.class);
// non-empty content is GZip encoded by default
assertTrue(request.buildHttpRequest().getEncoding() instanceof GZipEncoding);
// ... unless GZip compression is disabled
request.setDisableGZipContent(true);
assertNull(request.buildHttpRequest().getEncoding());
// ... and content explicitly set to EmptyContent is never GZip encoded
request =
new MockGoogleClientRequest<String>(
client, HttpMethods.POST, URI_TEMPLATE, new EmptyContent(), String.class);
assertNull(request.buildHttpRequest().getEncoding());
}

public void testBuildHttpRequest_methodOverrideEmptyContent() throws Exception {
HttpTransport transport =
new MockHttpTransport() {
@Override
public boolean supportsMethod(String method) throws IOException {
// the transport does not support PATCH, so the request is overridden with POST
return false;
}
};
MockGoogleClient client =
new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null)
.setApplicationName("Test Application")
.build();
MockGoogleClientRequest<String> request =
new MockGoogleClientRequest<String>(
client, HttpMethods.PATCH, URI_TEMPLATE, null, String.class);
HttpRequest httpRequest = request.buildHttpRequest();
assertEquals("POST", httpRequest.getRequestMethod());
assertTrue(httpRequest.getContent() instanceof EmptyContent);
assertNull(httpRequest.getEncoding());
}

public void testCheckRequiredParameter() throws Exception {
HttpTransport transport = new MockHttpTransport();
MockGoogleClient client =
Expand Down
Loading