Introduced ResponseEntity, for access to the response status code
This commit is contained in:
@@ -16,9 +16,6 @@
|
||||
|
||||
package org.springframework.http;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -39,10 +36,13 @@ public class HttpEntityTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contentType() {
|
||||
MediaType contentType = MediaType.TEXT_PLAIN;
|
||||
HttpEntity<String> entity = new HttpEntity<String>("foo", contentType);
|
||||
assertEquals(contentType, entity.getHeaders().getContentType());
|
||||
public void httpHeaders() {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.TEXT_PLAIN);
|
||||
String body = "foo";
|
||||
HttpEntity<String> entity = new HttpEntity<String>(body, headers);
|
||||
assertEquals(body, entity.getBody());
|
||||
assertEquals(MediaType.TEXT_PLAIN, entity.getHeaders().getContentType());
|
||||
assertEquals("text/plain", entity.getHeaders().getFirst("Content-Type"));
|
||||
}
|
||||
|
||||
@@ -50,18 +50,24 @@ public class HttpEntityTests {
|
||||
public void multiValueMap() {
|
||||
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
|
||||
map.set("Content-Type", "text/plain");
|
||||
HttpEntity<String> entity = new HttpEntity<String>("foo", map);
|
||||
String body = "foo";
|
||||
HttpEntity<String> entity = new HttpEntity<String>(body, map);
|
||||
assertEquals(body, entity.getBody());
|
||||
assertEquals(MediaType.TEXT_PLAIN, entity.getHeaders().getContentType());
|
||||
assertEquals("text/plain", entity.getHeaders().getFirst("Content-Type"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void map() {
|
||||
Map<String, String> map = new LinkedHashMap<String, String>();
|
||||
map.put("Content-Type", "text/plain");
|
||||
HttpEntity<String> entity = new HttpEntity<String>("foo", map);
|
||||
public void responseEntity() {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.TEXT_PLAIN);
|
||||
String body = "foo";
|
||||
ResponseEntity<String> entity = new ResponseEntity<String>(body, headers, HttpStatus.OK);
|
||||
assertEquals(body, entity.getBody());
|
||||
assertEquals(MediaType.TEXT_PLAIN, entity.getHeaders().getContentType());
|
||||
assertEquals("text/plain", entity.getHeaders().getFirst("Content-Type"));
|
||||
assertEquals("text/plain", entity.getHeaders().getFirst("Content-Type"));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -37,6 +37,7 @@ import org.junit.Test;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.MockHttpInputMessage;
|
||||
import org.springframework.http.MockHttpOutputMessage;
|
||||
@@ -109,7 +110,9 @@ public class FormHttpMessageConverterTests {
|
||||
Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
|
||||
parts.add("logo", logo);
|
||||
Source xml = new StreamSource(new StringReader("<root><child/></root>"));
|
||||
HttpEntity<Source> entity = new HttpEntity<Source>(xml, MediaType.TEXT_XML);
|
||||
HttpHeaders entityHeaders = new HttpHeaders();
|
||||
entityHeaders.setContentType(MediaType.TEXT_XML);
|
||||
HttpEntity<Source> entity = new HttpEntity<Source>(xml, entityHeaders);
|
||||
parts.add("xml", entity);
|
||||
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
|
||||
@@ -52,7 +52,9 @@ import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.client.CommonsClientHttpRequestFactory;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
@@ -109,10 +111,11 @@ public class RestTemplateIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void getEntity() {
|
||||
HttpEntity<String> entity = template.getForEntity(URI + "/{method}", String.class, "get");
|
||||
ResponseEntity<String> entity = template.getForEntity(URI + "/{method}", String.class, "get");
|
||||
assertEquals("Invalid content", helloWorld, entity.getBody());
|
||||
assertFalse("No headers", entity.getHeaders().isEmpty());
|
||||
assertEquals("Invalid content-type", contentType, entity.getHeaders().getContentType());
|
||||
assertEquals("Invalid status code", HttpStatus.OK, entity.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -129,7 +132,9 @@ public class RestTemplateIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void postForLocationEntity() throws URISyntaxException {
|
||||
HttpEntity<String> entity = new HttpEntity<String>(helloWorld, new MediaType("text", "plain", Charset.forName("ISO-8859-15")));
|
||||
HttpHeaders entityHeaders = new HttpHeaders();
|
||||
entityHeaders.setContentType(new MediaType("text", "plain", Charset.forName("ISO-8859-15")));
|
||||
HttpEntity<String> entity = new HttpEntity<String>(helloWorld, entityHeaders);
|
||||
URI location = template.postForLocation(URI + "/{method}", entity, "post");
|
||||
assertEquals("Invalid location", new URI(URI + "/post/1"), location);
|
||||
}
|
||||
@@ -183,7 +188,7 @@ public class RestTemplateIntegrationTests {
|
||||
HttpHeaders requestHeaders = new HttpHeaders();
|
||||
requestHeaders.set("MyHeader", "MyValue");
|
||||
HttpEntity<?> requestEntity = new HttpEntity(requestHeaders);
|
||||
HttpEntity<String> response =
|
||||
ResponseEntity<String> response =
|
||||
template.exchange(URI + "/{method}", HttpMethod.GET, requestEntity, String.class, "get");
|
||||
assertEquals("Invalid content", helloWorld, response.getBody());
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.client.ClientHttpRequest;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
@@ -194,14 +195,16 @@ public class RestTemplateTests {
|
||||
expect(converter.canRead(String.class, textPlain)).andReturn(true);
|
||||
String expected = "Hello World";
|
||||
expect(converter.read(String.class, response)).andReturn(expected);
|
||||
expect(response.getStatusCode()).andReturn(HttpStatus.OK);
|
||||
response.close();
|
||||
|
||||
replayMocks();
|
||||
|
||||
HttpEntity<String> result = template.getForEntity("http://example.com", String.class);
|
||||
ResponseEntity<String> result = template.getForEntity("http://example.com", String.class);
|
||||
assertEquals("Invalid GET result", expected, result.getBody());
|
||||
assertEquals("Invalid Accept header", textPlain.toString(), requestHeaders.getFirst("Accept"));
|
||||
assertEquals("Invalid Content-Type header", textPlain, result.getHeaders().getContentType());
|
||||
assertEquals("Invalid status code", HttpStatus.OK, result.getStatusCode());
|
||||
|
||||
verifyMocks();
|
||||
}
|
||||
@@ -265,7 +268,9 @@ public class RestTemplateTests {
|
||||
|
||||
replayMocks();
|
||||
|
||||
HttpEntity<String> entity = new HttpEntity<String>(helloWorld, contentType);
|
||||
HttpHeaders entityHeaders = new HttpHeaders();
|
||||
entityHeaders.setContentType(contentType);
|
||||
HttpEntity<String> entity = new HttpEntity<String>(helloWorld, entityHeaders);
|
||||
|
||||
URI result = template.postForLocation("http://example.com", entity);
|
||||
assertEquals("Invalid POST result", expected, result);
|
||||
@@ -291,7 +296,9 @@ public class RestTemplateTests {
|
||||
|
||||
replayMocks();
|
||||
|
||||
HttpEntity<String> entity = new HttpEntity<String>(helloWorld, Collections.singletonMap("MyHeader", "MyValue"));
|
||||
HttpHeaders entityHeaders = new HttpHeaders();
|
||||
entityHeaders.set("MyHeader", "MyValue");
|
||||
HttpEntity<String> entity = new HttpEntity<String>(helloWorld, entityHeaders);
|
||||
|
||||
URI result = template.postForLocation("http://example.com", entity);
|
||||
assertEquals("Invalid POST result", expected, result);
|
||||
@@ -387,14 +394,16 @@ public class RestTemplateTests {
|
||||
Integer expected = 42;
|
||||
expect(converter.canRead(Integer.class, textPlain)).andReturn(true);
|
||||
expect(converter.read(Integer.class, response)).andReturn(expected);
|
||||
expect(response.getStatusCode()).andReturn(HttpStatus.OK);
|
||||
response.close();
|
||||
|
||||
replayMocks();
|
||||
|
||||
HttpEntity<Integer> result = template.postForEntity("http://example.com", request, Integer.class);
|
||||
ResponseEntity<Integer> result = template.postForEntity("http://example.com", request, Integer.class);
|
||||
assertEquals("Invalid POST result", expected, result.getBody());
|
||||
assertEquals("Invalid Content-Type", textPlain, result.getHeaders().getContentType());
|
||||
assertEquals("Invalid Accept header", textPlain.toString(), requestHeaders.getFirst("Accept"));
|
||||
assertEquals("Invalid status code", HttpStatus.OK, result.getStatusCode());
|
||||
|
||||
verifyMocks();
|
||||
}
|
||||
@@ -439,13 +448,15 @@ public class RestTemplateTests {
|
||||
expect(response.getHeaders()).andReturn(responseHeaders).times(2);
|
||||
expect(converter.canRead(Integer.class, textPlain)).andReturn(true);
|
||||
expect(converter.read(Integer.class, response)).andReturn(null);
|
||||
expect(response.getStatusCode()).andReturn(HttpStatus.OK);
|
||||
response.close();
|
||||
|
||||
replayMocks();
|
||||
HttpEntity<Integer> result = template.postForEntity("http://example.com", null, Integer.class);
|
||||
ResponseEntity<Integer> result = template.postForEntity("http://example.com", null, Integer.class);
|
||||
assertFalse("Invalid POST result", result.hasBody());
|
||||
assertEquals("Invalid Content-Type", textPlain, result.getHeaders().getContentType());
|
||||
assertEquals("Invalid content length", 0, requestHeaders.getContentLength());
|
||||
assertEquals("Invalid status code", HttpStatus.OK, result.getStatusCode());
|
||||
|
||||
verifyMocks();
|
||||
}
|
||||
@@ -557,16 +568,20 @@ public class RestTemplateTests {
|
||||
Integer expected = 42;
|
||||
expect(converter.canRead(Integer.class, textPlain)).andReturn(true);
|
||||
expect(converter.read(Integer.class, response)).andReturn(expected);
|
||||
expect(response.getStatusCode()).andReturn(HttpStatus.OK);
|
||||
response.close();
|
||||
|
||||
replayMocks();
|
||||
|
||||
HttpEntity<String> requestEntity = new HttpEntity<String>(body, Collections.singletonMap("MyHeader", "MyValue"));
|
||||
HttpEntity<Integer> result = template.exchange("http://example.com", HttpMethod.POST, requestEntity, Integer.class);
|
||||
HttpHeaders entityHeaders = new HttpHeaders();
|
||||
entityHeaders.set("MyHeader", "MyValue");
|
||||
HttpEntity<String> requestEntity = new HttpEntity<String>(body, entityHeaders);
|
||||
ResponseEntity<Integer> result = template.exchange("http://example.com", HttpMethod.POST, requestEntity, Integer.class);
|
||||
assertEquals("Invalid POST result", expected, result.getBody());
|
||||
assertEquals("Invalid Content-Type", textPlain, result.getHeaders().getContentType());
|
||||
assertEquals("Invalid Accept header", textPlain.toString(), requestHeaders.getFirst("Accept"));
|
||||
assertEquals("Invalid custom header", "MyValue", requestHeaders.getFirst("MyHeader"));
|
||||
assertEquals("Invalid status code", HttpStatus.OK, result.getStatusCode());
|
||||
|
||||
verifyMocks();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user