SPR-8809 - RestTemplate headers not sent when bufferRequestBody is false

This commit is contained in:
Arjen Poutsma 2011-11-30 11:07:38 +00:00
parent 951514a576
commit 91c14bd1fe
2 changed files with 37 additions and 4 deletions

View File

@ -85,9 +85,8 @@ class InterceptingClientHttpRequest extends AbstractBufferingClientHttpRequest {
delegate.getHeaders().putAll(request.getHeaders()); delegate.getHeaders().putAll(request.getHeaders());
if (body.length > 0) {
FileCopyUtils.copy(body, delegate.getBody()); FileCopyUtils.copy(body, delegate.getBody());
}
return delegate.execute(); return delegate.execute();
} }
} }

View File

@ -16,14 +16,18 @@
package org.springframework.http.client; package org.springframework.http.client;
import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.URI; import java.net.URI;
import java.util.Collections;
import java.util.Random; import java.util.Random;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -37,6 +41,36 @@ public class StreamingSimpleHttpRequestFactoryTests extends AbstractHttpRequestF
return factory; return factory;
} }
// SPR-8809
@Test
public void interceptor() throws Exception {
final String headerName = "MyHeader";
final String headerValue = "MyValue";
ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() {
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
throws IOException {
request.getHeaders().add(headerName, headerValue);
return execution.execute(request, body);
}
};
InterceptingClientHttpRequestFactory factory = new InterceptingClientHttpRequestFactory(createRequestFactory(),
Collections.singletonList(interceptor));
ClientHttpResponse response = null;
try {
ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/echo"), HttpMethod.POST);
response = request.execute();
assertEquals("Invalid response status", HttpStatus.OK, response.getStatusCode());
HttpHeaders responseHeaders = response.getHeaders();
assertEquals("Custom header invalid", headerValue, responseHeaders.getFirst(headerName));
}
finally {
if (response != null) {
response.close();
}
}
}
@Test @Test
@Ignore @Ignore
public void largeFileUpload() throws Exception { public void largeFileUpload() throws Exception {
@ -49,7 +83,7 @@ public class StreamingSimpleHttpRequestFactoryTests extends AbstractHttpRequestF
final int contentLength = ITERATIONS * BUF_SIZE; final int contentLength = ITERATIONS * BUF_SIZE;
// request.getHeaders().setContentLength(contentLength); // request.getHeaders().setContentLength(contentLength);
OutputStream body = request.getBody(); OutputStream body = request.getBody();
for (int i=0; i < ITERATIONS; i++) { for (int i = 0; i < ITERATIONS; i++) {
byte[] buffer = new byte[BUF_SIZE]; byte[] buffer = new byte[BUF_SIZE];
rnd.nextBytes(buffer); rnd.nextBytes(buffer);
body.write(buffer); body.write(buffer);