Revisit empty body response support in HTTP client
Prior to this commit, HTTP responses without body (response status 204 or 304, Content-Length: 0) were handled properly by RestTemplates. But some other cases were not properly managed, throwing exceptions for valid HTTP responses. This commit better handles HTTP responses, using a response wrapper that can tell if a response: * has no message body (HTTP status 1XX, 204, 304 or Content-Length:0) * has an empty message body This covers rfc7230 Section 3.3.3. Issue: SPR-8016
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.web.client;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
@@ -26,6 +27,7 @@ import org.junit.Test;
|
||||
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpInputMessage;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
@@ -73,6 +75,17 @@ public class HttpMessageConverterExtractorTests {
|
||||
assertNull(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void informational() throws IOException {
|
||||
HttpMessageConverter<?> converter = mock(HttpMessageConverter.class);
|
||||
extractor = new HttpMessageConverterExtractor<String>(String.class, createConverterList(converter));
|
||||
given(response.getStatusCode()).willReturn(HttpStatus.CONTINUE);
|
||||
|
||||
Object result = extractor.extractData(response);
|
||||
|
||||
assertNull(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zeroContentLength() throws IOException {
|
||||
HttpMessageConverter<?> converter = mock(HttpMessageConverter.class);
|
||||
@@ -87,6 +100,22 @@ public class HttpMessageConverterExtractorTests {
|
||||
assertNull(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void emptyMessageBody() throws IOException {
|
||||
HttpMessageConverter<String> converter = mock(HttpMessageConverter.class);
|
||||
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
|
||||
converters.add(converter);
|
||||
HttpHeaders responseHeaders = new HttpHeaders();
|
||||
extractor = new HttpMessageConverterExtractor<String>(String.class, createConverterList(converter));
|
||||
given(response.getStatusCode()).willReturn(HttpStatus.OK);
|
||||
given(response.getHeaders()).willReturn(responseHeaders);
|
||||
given(response.getBody()).willReturn(new ByteArrayInputStream("".getBytes()));
|
||||
|
||||
Object result = extractor.extractData(response);
|
||||
assertNull(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void normal() throws IOException {
|
||||
@@ -100,8 +129,9 @@ public class HttpMessageConverterExtractorTests {
|
||||
extractor = new HttpMessageConverterExtractor<String>(String.class, converters);
|
||||
given(response.getStatusCode()).willReturn(HttpStatus.OK);
|
||||
given(response.getHeaders()).willReturn(responseHeaders);
|
||||
given(response.getBody()).willReturn(new ByteArrayInputStream(expected.getBytes()));
|
||||
given(converter.canRead(String.class, contentType)).willReturn(true);
|
||||
given(converter.read(String.class, response)).willReturn(expected);
|
||||
given(converter.read(eq(String.class), any(HttpInputMessage.class))).willReturn(expected);
|
||||
|
||||
Object result = extractor.extractData(response);
|
||||
|
||||
@@ -120,27 +150,12 @@ public class HttpMessageConverterExtractorTests {
|
||||
extractor = new HttpMessageConverterExtractor<String>(String.class, converters);
|
||||
given(response.getStatusCode()).willReturn(HttpStatus.OK);
|
||||
given(response.getHeaders()).willReturn(responseHeaders);
|
||||
given(response.getBody()).willReturn(new ByteArrayInputStream("Foobar".getBytes()));
|
||||
given(converter.canRead(String.class, contentType)).willReturn(false);
|
||||
|
||||
extractor.extractData(response);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void connectionClose() throws IOException {
|
||||
HttpMessageConverter<String> converter = mock(HttpMessageConverter.class);
|
||||
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
|
||||
converters.add(converter);
|
||||
HttpHeaders responseHeaders = new HttpHeaders();
|
||||
responseHeaders.setConnection("close");
|
||||
extractor = new HttpMessageConverterExtractor<String>(String.class, createConverterList(converter));
|
||||
given(response.getStatusCode()).willReturn(HttpStatus.OK);
|
||||
given(response.getHeaders()).willReturn(responseHeaders);
|
||||
|
||||
Object result = extractor.extractData(response);
|
||||
assertNull(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void generics() throws IOException {
|
||||
@@ -155,8 +170,9 @@ public class HttpMessageConverterExtractorTests {
|
||||
extractor = new HttpMessageConverterExtractor<List<String>>(type, converters);
|
||||
given(response.getStatusCode()).willReturn(HttpStatus.OK);
|
||||
given(response.getHeaders()).willReturn(responseHeaders);
|
||||
given(response.getBody()).willReturn(new ByteArrayInputStream(expected.getBytes()));
|
||||
given(converter.canRead(type, null, contentType)).willReturn(true);
|
||||
given(converter.read(type, null, response)).willReturn(expected);
|
||||
given(converter.read(eq(type), eq(null), any(HttpInputMessage.class))).willReturn(expected);
|
||||
|
||||
Object result = extractor.extractData(response);
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.web.client;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.Collections;
|
||||
@@ -31,6 +32,7 @@ import org.junit.Test;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpInputMessage;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
@@ -170,14 +172,15 @@ public class RestTemplateTests {
|
||||
given(request.getHeaders()).willReturn(requestHeaders);
|
||||
given(request.execute()).willReturn(response);
|
||||
given(errorHandler.hasError(response)).willReturn(false);
|
||||
String expected = "Hello World";
|
||||
HttpHeaders responseHeaders = new HttpHeaders();
|
||||
responseHeaders.setContentType(textPlain);
|
||||
responseHeaders.setContentLength(10);
|
||||
given(response.getStatusCode()).willReturn(HttpStatus.OK);
|
||||
given(response.getHeaders()).willReturn(responseHeaders);
|
||||
given(response.getBody()).willReturn(new ByteArrayInputStream(expected.getBytes()));
|
||||
given(converter.canRead(String.class, textPlain)).willReturn(true);
|
||||
String expected = "Hello World";
|
||||
given(converter.read(String.class, response)).willReturn(expected);
|
||||
given(converter.read(eq(String.class), any(HttpInputMessage.class))).willReturn(expected);
|
||||
HttpStatus status = HttpStatus.OK;
|
||||
given(response.getStatusCode()).willReturn(status);
|
||||
given(response.getStatusText()).willReturn(status.getReasonPhrase());
|
||||
@@ -205,6 +208,7 @@ public class RestTemplateTests {
|
||||
responseHeaders.setContentLength(10);
|
||||
given(response.getStatusCode()).willReturn(HttpStatus.OK);
|
||||
given(response.getHeaders()).willReturn(responseHeaders);
|
||||
given(response.getBody()).willReturn(new ByteArrayInputStream("Foo".getBytes()));
|
||||
given(converter.canRead(String.class, contentType)).willReturn(false);
|
||||
HttpStatus status = HttpStatus.OK;
|
||||
given(response.getStatusCode()).willReturn(status);
|
||||
@@ -232,14 +236,15 @@ public class RestTemplateTests {
|
||||
given(request.getHeaders()).willReturn(requestHeaders);
|
||||
given(request.execute()).willReturn(response);
|
||||
given(errorHandler.hasError(response)).willReturn(false);
|
||||
String expected = "Hello World";
|
||||
HttpHeaders responseHeaders = new HttpHeaders();
|
||||
responseHeaders.setContentType(textPlain);
|
||||
responseHeaders.setContentLength(10);
|
||||
given(response.getStatusCode()).willReturn(HttpStatus.OK);
|
||||
given(response.getHeaders()).willReturn(responseHeaders);
|
||||
given(response.getBody()).willReturn(new ByteArrayInputStream(expected.getBytes()));
|
||||
given(converter.canRead(String.class, textPlain)).willReturn(true);
|
||||
String expected = "Hello World";
|
||||
given(converter.read(String.class, response)).willReturn(expected);
|
||||
given(converter.read(eq(String.class), any(HttpInputMessage.class))).willReturn(expected);
|
||||
given(response.getStatusCode()).willReturn(HttpStatus.OK);
|
||||
HttpStatus status = HttpStatus.OK;
|
||||
given(response.getStatusCode()).willReturn(status);
|
||||
@@ -405,14 +410,15 @@ public class RestTemplateTests {
|
||||
converter.write(request, null, this.request);
|
||||
given(this.request.execute()).willReturn(response);
|
||||
given(errorHandler.hasError(response)).willReturn(false);
|
||||
Integer expected = 42;
|
||||
HttpHeaders responseHeaders = new HttpHeaders();
|
||||
responseHeaders.setContentType(textPlain);
|
||||
responseHeaders.setContentLength(10);
|
||||
given(response.getStatusCode()).willReturn(HttpStatus.OK);
|
||||
given(response.getHeaders()).willReturn(responseHeaders);
|
||||
Integer expected = 42;
|
||||
given(response.getBody()).willReturn(new ByteArrayInputStream(expected.toString().getBytes()));
|
||||
given(converter.canRead(Integer.class, textPlain)).willReturn(true);
|
||||
given(converter.read(Integer.class, response)).willReturn(expected);
|
||||
given(converter.read(eq(Integer.class), any(HttpInputMessage.class))).willReturn(expected);
|
||||
HttpStatus status = HttpStatus.OK;
|
||||
given(response.getStatusCode()).willReturn(status);
|
||||
given(response.getStatusText()).willReturn(status.getReasonPhrase());
|
||||
@@ -437,14 +443,15 @@ public class RestTemplateTests {
|
||||
converter.write(request, null, this.request);
|
||||
given(this.request.execute()).willReturn(response);
|
||||
given(errorHandler.hasError(response)).willReturn(false);
|
||||
Integer expected = 42;
|
||||
HttpHeaders responseHeaders = new HttpHeaders();
|
||||
responseHeaders.setContentType(textPlain);
|
||||
responseHeaders.setContentLength(10);
|
||||
given(response.getStatusCode()).willReturn(HttpStatus.OK);
|
||||
given(response.getHeaders()).willReturn(responseHeaders);
|
||||
Integer expected = 42;
|
||||
given(response.getBody()).willReturn(new ByteArrayInputStream(expected.toString().getBytes()));
|
||||
given(converter.canRead(Integer.class, textPlain)).willReturn(true);
|
||||
given(converter.read(Integer.class, response)).willReturn(expected);
|
||||
given(converter.read(eq(Integer.class), any(HttpInputMessage.class))).willReturn(expected);
|
||||
given(response.getStatusCode()).willReturn(HttpStatus.OK);
|
||||
HttpStatus status = HttpStatus.OK;
|
||||
given(response.getStatusCode()).willReturn(status);
|
||||
@@ -615,14 +622,16 @@ public class RestTemplateTests {
|
||||
converter.write(body, null, this.request);
|
||||
given(this.request.execute()).willReturn(response);
|
||||
given(errorHandler.hasError(response)).willReturn(false);
|
||||
Integer expected = 42;
|
||||
HttpHeaders responseHeaders = new HttpHeaders();
|
||||
responseHeaders.setContentType(MediaType.TEXT_PLAIN);
|
||||
responseHeaders.setContentLength(10);
|
||||
given(response.getStatusCode()).willReturn(HttpStatus.OK);
|
||||
given(response.getHeaders()).willReturn(responseHeaders);
|
||||
Integer expected = 42;
|
||||
given(response.getBody()).willReturn(new ByteArrayInputStream(expected.toString().getBytes()));
|
||||
given(converter.canRead(Integer.class, MediaType.TEXT_PLAIN)).willReturn(true);
|
||||
given(converter.read(Integer.class, response)).willReturn(expected);
|
||||
given(converter.read(eq(Integer.class), any(HttpInputMessage.class))).willReturn(expected);
|
||||
given(response.getStatusCode()).willReturn(HttpStatus.OK);
|
||||
HttpStatus status = HttpStatus.OK;
|
||||
given(response.getStatusCode()).willReturn(status);
|
||||
@@ -658,14 +667,15 @@ public class RestTemplateTests {
|
||||
converter.write(requestBody, null, this.request);
|
||||
given(this.request.execute()).willReturn(response);
|
||||
given(errorHandler.hasError(response)).willReturn(false);
|
||||
List<Integer> expected = Collections.singletonList(42);
|
||||
HttpHeaders responseHeaders = new HttpHeaders();
|
||||
responseHeaders.setContentType(MediaType.TEXT_PLAIN);
|
||||
responseHeaders.setContentLength(10);
|
||||
given(response.getStatusCode()).willReturn(HttpStatus.OK);
|
||||
given(response.getHeaders()).willReturn(responseHeaders);
|
||||
List<Integer> expected = Collections.singletonList(42);
|
||||
given(response.getBody()).willReturn(new ByteArrayInputStream(new Integer(42).toString().getBytes()));
|
||||
given(converter.canRead(intList.getType(), null, MediaType.TEXT_PLAIN)).willReturn(true);
|
||||
given(converter.read(intList.getType(), null, response)).willReturn(expected);
|
||||
given(converter.read(eq(intList.getType()), eq(null), any(HttpInputMessage.class))).willReturn(expected);
|
||||
given(response.getStatusCode()).willReturn(HttpStatus.OK);
|
||||
HttpStatus status = HttpStatus.OK;
|
||||
given(response.getStatusCode()).willReturn(status);
|
||||
|
||||
Reference in New Issue
Block a user