Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Always add Content-Length header #648

Merged
merged 2 commits into from
Jan 31, 2019
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 @@ -80,6 +80,10 @@ static StatusCode.Code httpStatusToStatusCode(int httpStatus, String errorMessag
} else {
return Code.ABORTED;
}
case 411:
throw new IllegalStateException(
"411 status code received (Content-Length header not given.) Please file a bug against https://github.com/googleapis/gax-java/\n"
+ httpStatus);
case 429:
return Code.RESOURCE_EXHAUSTED;
case 499:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
*/
package com.google.api.gax.httpjson;

import com.google.api.client.http.EmptyContent;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpContent;
import com.google.api.client.http.HttpMediaType;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
Expand Down Expand Up @@ -88,12 +90,16 @@ HttpRequest createHttpRequest() throws IOException {

// Create HTTP request body.
String requestBody = requestFormatter.getRequestBody(getRequest());
JsonHttpContent jsonHttpContent = null;
HttpContent jsonHttpContent;
if (!Strings.isNullOrEmpty(requestBody)) {
getJsonFactory().createJsonParser(requestBody).parse(tokenRequest);
jsonHttpContent =
new JsonHttpContent(getJsonFactory(), tokenRequest)
.setMediaType((new HttpMediaType("application/json")));
} else {
// Force underlying HTTP lib to set Content-Length header to avoid 411s.
// See EmptyContent.java.
jsonHttpContent = new EmptyContent();
}

// Populate URL path and query parameters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*/
package com.google.api.gax.httpjson;

import com.google.api.client.http.EmptyContent;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.testing.http.MockHttpTransport;
Expand Down Expand Up @@ -161,6 +162,7 @@ public String serialize(Void response) {
@Test
public void testRequestUrl() throws IOException {
HttpRequest httpRequest = httpRequestRunnable.createHttpRequest();
Truth.assertThat(httpRequest.getContent()).isInstanceOf(EmptyContent.class);
String expectedUrl = ENDPOINT + "name/feline" + "?food=bird&food=mouse&size=small";
Truth.assertThat(httpRequest.getUrl().toString()).isEqualTo(expectedUrl);
}
Expand Down