JCLOUDS-1322: Remove Expect header for requests with empty body

Signed-off-by: Chaithanya Ganta <ganta@adobe.com>
This commit is contained in:
Chaithanya Ganta 2017-07-17 14:50:16 +05:30 committed by Andrew Gaul
parent 7cbc16d99c
commit e331a000d1
3 changed files with 29 additions and 1 deletions

View File

@ -20,6 +20,7 @@ import static com.google.common.net.HttpHeaders.CONTENT_LENGTH;
import static com.google.common.net.HttpHeaders.ETAG;
import static com.google.common.net.HttpHeaders.EXPECT;
import static com.google.common.util.concurrent.MoreExecutors.sameThreadExecutor;
import static org.assertj.core.api.Assertions.assertThat;
import static org.jclouds.Constants.PROPERTY_MAX_RETRIES;
import static org.testng.Assert.assertEquals;
@ -73,7 +74,7 @@ public class S3ClientMockTest {
RecordedRequest request = server.takeRequest();
assertEquals(request.getRequestLine(), "PUT /bucket/object HTTP/1.1");
assertEquals(request.getHeaders(CONTENT_LENGTH), ImmutableList.of("0"));
assertEquals(request.getHeaders(EXPECT), ImmutableList.of("100-continue"));
assertThat(request.getHeaders(EXPECT)).isEmpty();
server.shutdown();
}

View File

@ -357,6 +357,9 @@ public class RestAnnotationProcessor implements Function<Invocation, HttpRequest
if (request.getPayload() != null) {
contentMetadataCodec.fromHeaders(request.getPayload().getContentMetadata(), headers);
}
request = stripExpectHeaderIfContentZero(request);
utils.checkRequestHasRequiredProperties(request);
return request;
}
@ -779,6 +782,18 @@ public class RestAnnotationProcessor implements Function<Invocation, HttpRequest
return parts.build();
}
private static GeneratedHttpRequest stripExpectHeaderIfContentZero(GeneratedHttpRequest request) {
boolean isBodyEmpty = true;
if (request.getPayload() != null) {
Long length = request.getPayload().getContentMetadata().getContentLength();
isBodyEmpty = length != null && length == 0;
}
if (isBodyEmpty) {
request = request.toBuilder().removeHeader("Expect").build();
}
return request;
}
private boolean isEncodedUsed(Invocation invocation) {
return !parametersWithAnnotation(invocation.getInvokable(), Encoded.class).isEmpty();
}

View File

@ -21,6 +21,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.MediaType.APPLICATION_XML;
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
import static org.assertj.core.api.Assertions.assertThat;
import static org.jclouds.io.Payloads.newInputStreamPayload;
import static org.jclouds.io.Payloads.newStringPayload;
import static org.jclouds.providers.AnonymousProviderMetadata.forApiOnEndpoint;
@ -1559,6 +1560,17 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest {
assertEquals(request.getFilters().get(1).getClass(), ConnectionCloseHeader.class);
}
@Test
public void testZeroContentStripExpectHeader() {
Invokable<?, ?> method = method(TestRequestFilter.class, "post");
Invocation invocation = Invocation.create(method,
ImmutableList.<Object>of(HttpRequest.builder().method("POST").endpoint("http://localhost")
.payload(new byte[0]).addHeader(HttpHeaders.EXPECT, "100-Continue")
.build()));
GeneratedHttpRequest request = processor.apply(invocation);
assertThat(request.getFirstHeaderOrNull(HttpHeaders.EXPECT)).isNull();
}
public static class TestEncoding {
@GET
@Path("/{path1}/{path2}")