|
|
|
@ -39,7 +39,6 @@ import org.apache.commons.fileupload.FileUploadException; |
|
|
|
import org.apache.commons.fileupload.disk.DiskFileItemFactory; |
|
|
|
import org.apache.commons.fileupload.disk.DiskFileItemFactory; |
|
|
|
import org.apache.commons.fileupload.servlet.ServletFileUpload; |
|
|
|
import org.apache.commons.fileupload.servlet.ServletFileUpload; |
|
|
|
import org.junit.AfterClass; |
|
|
|
import org.junit.AfterClass; |
|
|
|
import static org.junit.Assert.*; |
|
|
|
|
|
|
|
import org.junit.Before; |
|
|
|
import org.junit.Before; |
|
|
|
import org.junit.BeforeClass; |
|
|
|
import org.junit.BeforeClass; |
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
@ -56,10 +55,13 @@ import org.springframework.http.HttpStatus; |
|
|
|
import org.springframework.http.MediaType; |
|
|
|
import org.springframework.http.MediaType; |
|
|
|
import org.springframework.http.ResponseEntity; |
|
|
|
import org.springframework.http.ResponseEntity; |
|
|
|
import org.springframework.http.client.CommonsClientHttpRequestFactory; |
|
|
|
import org.springframework.http.client.CommonsClientHttpRequestFactory; |
|
|
|
|
|
|
|
import org.springframework.http.client.FreePortScanner; |
|
|
|
import org.springframework.util.FileCopyUtils; |
|
|
|
import org.springframework.util.FileCopyUtils; |
|
|
|
import org.springframework.util.LinkedMultiValueMap; |
|
|
|
import org.springframework.util.LinkedMultiValueMap; |
|
|
|
import org.springframework.util.MultiValueMap; |
|
|
|
import org.springframework.util.MultiValueMap; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.*; |
|
|
|
|
|
|
|
|
|
|
|
/** @author Arjen Poutsma */ |
|
|
|
/** @author Arjen Poutsma */ |
|
|
|
public class RestTemplateIntegrationTests { |
|
|
|
public class RestTemplateIntegrationTests { |
|
|
|
|
|
|
|
|
|
|
|
@ -69,20 +71,22 @@ public class RestTemplateIntegrationTests { |
|
|
|
|
|
|
|
|
|
|
|
private static String helloWorld = "H\u00e9llo W\u00f6rld"; |
|
|
|
private static String helloWorld = "H\u00e9llo W\u00f6rld"; |
|
|
|
|
|
|
|
|
|
|
|
private static final String URI = "http://localhost:8889"; |
|
|
|
private static String baseUrl; |
|
|
|
|
|
|
|
|
|
|
|
private static MediaType contentType; |
|
|
|
private static MediaType contentType; |
|
|
|
|
|
|
|
|
|
|
|
@BeforeClass |
|
|
|
@BeforeClass |
|
|
|
public static void startJettyServer() throws Exception { |
|
|
|
public static void startJettyServer() throws Exception { |
|
|
|
jettyServer = new Server(8889); |
|
|
|
int port = FreePortScanner.getFreePort(); |
|
|
|
|
|
|
|
jettyServer = new Server(port); |
|
|
|
|
|
|
|
baseUrl = "http://localhost:" + port; |
|
|
|
Context jettyContext = new Context(jettyServer, "/"); |
|
|
|
Context jettyContext = new Context(jettyServer, "/"); |
|
|
|
byte[] bytes = helloWorld.getBytes("UTF-8"); |
|
|
|
byte[] bytes = helloWorld.getBytes("UTF-8"); |
|
|
|
contentType = new MediaType("text", "plain", Collections.singletonMap("charset", "utf-8")); |
|
|
|
contentType = new MediaType("text", "plain", Collections.singletonMap("charset", "utf-8")); |
|
|
|
jettyContext.addServlet(new ServletHolder(new GetServlet(bytes, contentType)), "/get"); |
|
|
|
jettyContext.addServlet(new ServletHolder(new GetServlet(bytes, contentType)), "/get"); |
|
|
|
jettyContext.addServlet(new ServletHolder(new GetServlet(new byte[0], contentType)), "/get/nothing"); |
|
|
|
jettyContext.addServlet(new ServletHolder(new GetServlet(new byte[0], contentType)), "/get/nothing"); |
|
|
|
jettyContext.addServlet( |
|
|
|
jettyContext.addServlet( |
|
|
|
new ServletHolder(new PostServlet(helloWorld, URI + "/post/1", bytes, contentType)), |
|
|
|
new ServletHolder(new PostServlet(helloWorld, baseUrl + "/post/1", bytes, contentType)), |
|
|
|
"/post"); |
|
|
|
"/post"); |
|
|
|
jettyContext.addServlet(new ServletHolder(new ErrorServlet(404)), "/errors/notfound"); |
|
|
|
jettyContext.addServlet(new ServletHolder(new ErrorServlet(404)), "/errors/notfound"); |
|
|
|
jettyContext.addServlet(new ServletHolder(new ErrorServlet(500)), "/errors/server"); |
|
|
|
jettyContext.addServlet(new ServletHolder(new ErrorServlet(500)), "/errors/server"); |
|
|
|
@ -105,13 +109,13 @@ public class RestTemplateIntegrationTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void getString() { |
|
|
|
public void getString() { |
|
|
|
String s = template.getForObject(URI + "/{method}", String.class, "get"); |
|
|
|
String s = template.getForObject(baseUrl + "/{method}", String.class, "get"); |
|
|
|
assertEquals("Invalid content", helloWorld, s); |
|
|
|
assertEquals("Invalid content", helloWorld, s); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void getEntity() { |
|
|
|
public void getEntity() { |
|
|
|
ResponseEntity<String> entity = template.getForEntity(URI + "/{method}", String.class, "get"); |
|
|
|
ResponseEntity<String> entity = template.getForEntity(baseUrl + "/{method}", String.class, "get"); |
|
|
|
assertEquals("Invalid content", helloWorld, entity.getBody()); |
|
|
|
assertEquals("Invalid content", helloWorld, entity.getBody()); |
|
|
|
assertFalse("No headers", entity.getHeaders().isEmpty()); |
|
|
|
assertFalse("No headers", entity.getHeaders().isEmpty()); |
|
|
|
assertEquals("Invalid content-type", contentType, entity.getHeaders().getContentType()); |
|
|
|
assertEquals("Invalid content-type", contentType, entity.getHeaders().getContentType()); |
|
|
|
@ -120,14 +124,14 @@ public class RestTemplateIntegrationTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void getNoResponse() { |
|
|
|
public void getNoResponse() { |
|
|
|
String s = template.getForObject(URI + "/get/nothing", String.class); |
|
|
|
String s = template.getForObject(baseUrl + "/get/nothing", String.class); |
|
|
|
assertEquals("Invalid content", "", s); |
|
|
|
assertEquals("Invalid content", "", s); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void postForLocation() throws URISyntaxException { |
|
|
|
public void postForLocation() throws URISyntaxException { |
|
|
|
URI location = template.postForLocation(URI + "/{method}", helloWorld, "post"); |
|
|
|
URI location = template.postForLocation(baseUrl + "/{method}", helloWorld, "post"); |
|
|
|
assertEquals("Invalid location", new URI(URI + "/post/1"), location); |
|
|
|
assertEquals("Invalid location", new URI(baseUrl + "/post/1"), location); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@ -135,20 +139,20 @@ public class RestTemplateIntegrationTests { |
|
|
|
HttpHeaders entityHeaders = new HttpHeaders(); |
|
|
|
HttpHeaders entityHeaders = new HttpHeaders(); |
|
|
|
entityHeaders.setContentType(new MediaType("text", "plain", Charset.forName("ISO-8859-15"))); |
|
|
|
entityHeaders.setContentType(new MediaType("text", "plain", Charset.forName("ISO-8859-15"))); |
|
|
|
HttpEntity<String> entity = new HttpEntity<String>(helloWorld, entityHeaders); |
|
|
|
HttpEntity<String> entity = new HttpEntity<String>(helloWorld, entityHeaders); |
|
|
|
URI location = template.postForLocation(URI + "/{method}", entity, "post"); |
|
|
|
URI location = template.postForLocation(baseUrl + "/{method}", entity, "post"); |
|
|
|
assertEquals("Invalid location", new URI(URI + "/post/1"), location); |
|
|
|
assertEquals("Invalid location", new URI(baseUrl + "/post/1"), location); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void postForObject() throws URISyntaxException { |
|
|
|
public void postForObject() throws URISyntaxException { |
|
|
|
String s = template.postForObject(URI + "/{method}", helloWorld, String.class, "post"); |
|
|
|
String s = template.postForObject(baseUrl + "/{method}", helloWorld, String.class, "post"); |
|
|
|
assertEquals("Invalid content", helloWorld, s); |
|
|
|
assertEquals("Invalid content", helloWorld, s); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void notFound() { |
|
|
|
public void notFound() { |
|
|
|
try { |
|
|
|
try { |
|
|
|
template.execute(URI + "/errors/notfound", HttpMethod.GET, null, null); |
|
|
|
template.execute(baseUrl + "/errors/notfound", HttpMethod.GET, null, null); |
|
|
|
fail("HttpClientErrorException expected"); |
|
|
|
fail("HttpClientErrorException expected"); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (HttpClientErrorException ex) { |
|
|
|
catch (HttpClientErrorException ex) { |
|
|
|
@ -161,7 +165,7 @@ public class RestTemplateIntegrationTests { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void serverError() { |
|
|
|
public void serverError() { |
|
|
|
try { |
|
|
|
try { |
|
|
|
template.execute(URI + "/errors/server", HttpMethod.GET, null, null); |
|
|
|
template.execute(baseUrl + "/errors/server", HttpMethod.GET, null, null); |
|
|
|
fail("HttpServerErrorException expected"); |
|
|
|
fail("HttpServerErrorException expected"); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (HttpServerErrorException ex) { |
|
|
|
catch (HttpServerErrorException ex) { |
|
|
|
@ -173,20 +177,20 @@ public class RestTemplateIntegrationTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void optionsForAllow() throws URISyntaxException { |
|
|
|
public void optionsForAllow() throws URISyntaxException { |
|
|
|
Set<HttpMethod> allowed = template.optionsForAllow(new URI(URI + "/get")); |
|
|
|
Set<HttpMethod> allowed = template.optionsForAllow(new URI(baseUrl + "/get")); |
|
|
|
assertEquals("Invalid response", |
|
|
|
assertEquals("Invalid response", |
|
|
|
EnumSet.of(HttpMethod.GET, HttpMethod.OPTIONS, HttpMethod.HEAD, HttpMethod.TRACE), allowed); |
|
|
|
EnumSet.of(HttpMethod.GET, HttpMethod.OPTIONS, HttpMethod.HEAD, HttpMethod.TRACE), allowed); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void uri() throws InterruptedException, URISyntaxException { |
|
|
|
public void uri() throws InterruptedException, URISyntaxException { |
|
|
|
String result = template.getForObject(URI + "/uri/{query}", String.class, "Z\u00fcrich"); |
|
|
|
String result = template.getForObject(baseUrl + "/uri/{query}", String.class, "Z\u00fcrich"); |
|
|
|
assertEquals("Invalid request URI", "/uri/Z%C3%BCrich", result); |
|
|
|
assertEquals("Invalid request URI", "/uri/Z%C3%BCrich", result); |
|
|
|
|
|
|
|
|
|
|
|
result = template.getForObject(URI + "/uri/query={query}", String.class, "foo@bar"); |
|
|
|
result = template.getForObject(baseUrl + "/uri/query={query}", String.class, "foo@bar"); |
|
|
|
assertEquals("Invalid request URI", "/uri/query=foo@bar", result); |
|
|
|
assertEquals("Invalid request URI", "/uri/query=foo@bar", result); |
|
|
|
|
|
|
|
|
|
|
|
result = template.getForObject(URI + "/uri/query={query}", String.class, "T\u014dky\u014d"); |
|
|
|
result = template.getForObject(baseUrl + "/uri/query={query}", String.class, "T\u014dky\u014d"); |
|
|
|
assertEquals("Invalid request URI", "/uri/query=T%C5%8Dky%C5%8D", result); |
|
|
|
assertEquals("Invalid request URI", "/uri/query=T%C5%8Dky%C5%8D", result); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -199,7 +203,7 @@ public class RestTemplateIntegrationTests { |
|
|
|
Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg"); |
|
|
|
Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg"); |
|
|
|
parts.add("logo", logo); |
|
|
|
parts.add("logo", logo); |
|
|
|
|
|
|
|
|
|
|
|
template.postForLocation(URI + "/multipart", parts); |
|
|
|
template.postForLocation(baseUrl + "/multipart", parts); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@ -208,7 +212,7 @@ public class RestTemplateIntegrationTests { |
|
|
|
requestHeaders.set("MyHeader", "MyValue"); |
|
|
|
requestHeaders.set("MyHeader", "MyValue"); |
|
|
|
HttpEntity<?> requestEntity = new HttpEntity(requestHeaders); |
|
|
|
HttpEntity<?> requestEntity = new HttpEntity(requestHeaders); |
|
|
|
ResponseEntity<String> response = |
|
|
|
ResponseEntity<String> response = |
|
|
|
template.exchange(URI + "/{method}", HttpMethod.GET, requestEntity, String.class, "get"); |
|
|
|
template.exchange(baseUrl + "/{method}", HttpMethod.GET, requestEntity, String.class, "get"); |
|
|
|
assertEquals("Invalid content", helloWorld, response.getBody()); |
|
|
|
assertEquals("Invalid content", helloWorld, response.getBody()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -218,8 +222,8 @@ public class RestTemplateIntegrationTests { |
|
|
|
requestHeaders.set("MyHeader", "MyValue"); |
|
|
|
requestHeaders.set("MyHeader", "MyValue"); |
|
|
|
requestHeaders.setContentType(MediaType.TEXT_PLAIN); |
|
|
|
requestHeaders.setContentType(MediaType.TEXT_PLAIN); |
|
|
|
HttpEntity<String> requestEntity = new HttpEntity<String>(helloWorld, requestHeaders); |
|
|
|
HttpEntity<String> requestEntity = new HttpEntity<String>(helloWorld, requestHeaders); |
|
|
|
HttpEntity<?> result = template.exchange(URI + "/{method}", HttpMethod.POST, requestEntity, null, "post"); |
|
|
|
HttpEntity<?> result = template.exchange(baseUrl + "/{method}", HttpMethod.POST, requestEntity, null, "post"); |
|
|
|
assertEquals("Invalid location", new URI(URI + "/post/1"), result.getHeaders().getLocation()); |
|
|
|
assertEquals("Invalid location", new URI(baseUrl + "/post/1"), result.getHeaders().getLocation()); |
|
|
|
assertFalse(result.hasBody()); |
|
|
|
assertFalse(result.hasBody()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|