SPR-6188 - UriTemplate: Insufficient handling of characters that need to be escaped.
This commit is contained in:
@@ -51,6 +51,8 @@ public class RestTemplateIntegrationTests {
|
||||
|
||||
private static String helloWorld = "H\u00e9llo W\u00f6rld";
|
||||
|
||||
private static final String URI = "http://localhost:8889";
|
||||
|
||||
@BeforeClass
|
||||
public static void startJettyServer() throws Exception {
|
||||
jettyServer = new Server(8889);
|
||||
@@ -60,10 +62,11 @@ public class RestTemplateIntegrationTests {
|
||||
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 PostServlet(helloWorld, "http://localhost:8889/post/1", bytes, contentType)),
|
||||
new ServletHolder(new PostServlet(helloWorld, URI + "/post/1", bytes, contentType)),
|
||||
"/post");
|
||||
jettyContext.addServlet(new ServletHolder(new ErrorServlet(404)), "/errors/notfound");
|
||||
jettyContext.addServlet(new ServletHolder(new ErrorServlet(500)), "/errors/server");
|
||||
jettyContext.addServlet(new ServletHolder(new UriServlet()), "/uri/*");
|
||||
jettyServer.start();
|
||||
}
|
||||
|
||||
@@ -81,45 +84,51 @@ public class RestTemplateIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void getString() {
|
||||
String s = template.getForObject("http://localhost:8889/{method}", String.class, "get");
|
||||
String s = template.getForObject(URI + "/{method}", String.class, "get");
|
||||
assertEquals("Invalid content", helloWorld, s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getNoResponse() {
|
||||
String s = template.getForObject("http://localhost:8889/get/nothing", String.class);
|
||||
String s = template.getForObject(URI + "/get/nothing", String.class);
|
||||
assertEquals("Invalid content", "", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postForLocation() throws URISyntaxException {
|
||||
URI location = template.postForLocation("http://localhost:8889/{method}", helloWorld, "post");
|
||||
assertEquals("Invalid location", new URI("http://localhost:8889/post/1"), location);
|
||||
URI location = template.postForLocation(URI + "/{method}", helloWorld, "post");
|
||||
assertEquals("Invalid location", new URI(URI + "/post/1"), location);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postForObject() throws URISyntaxException {
|
||||
String s = template.postForObject("http://localhost:8889/{method}", helloWorld, String.class, "post");
|
||||
String s = template.postForObject(URI + "/{method}", helloWorld, String.class, "post");
|
||||
assertEquals("Invalid content", helloWorld, s);
|
||||
}
|
||||
|
||||
@Test(expected = HttpClientErrorException.class)
|
||||
public void notFound() {
|
||||
template.execute("http://localhost:8889/errors/notfound", HttpMethod.GET, null, null);
|
||||
template.execute(URI + "/errors/notfound", HttpMethod.GET, null, null);
|
||||
}
|
||||
|
||||
@Test(expected = HttpServerErrorException.class)
|
||||
public void serverError() {
|
||||
template.execute("http://localhost:8889/errors/server", HttpMethod.GET, null, null);
|
||||
template.execute(URI + "/errors/server", HttpMethod.GET, null, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void optionsForAllow() throws URISyntaxException {
|
||||
Set<HttpMethod> allowed = template.optionsForAllow(new URI("http://localhost:8889/get"));
|
||||
Set<HttpMethod> allowed = template.optionsForAllow(new URI(URI + "/get"));
|
||||
assertEquals("Invalid response",
|
||||
EnumSet.of(HttpMethod.GET, HttpMethod.OPTIONS, HttpMethod.HEAD, HttpMethod.TRACE), allowed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void uri() throws InterruptedException, URISyntaxException {
|
||||
String result = template.getForObject(URI + "/uri/{query}", String.class, "Z<EFBFBD>rich");
|
||||
assertEquals("Invalid request URI", "/uri/Z%FCrich", result);
|
||||
}
|
||||
|
||||
/** Servlet that returns and error message for a given status code. */
|
||||
private static class ErrorServlet extends GenericServlet {
|
||||
|
||||
@@ -187,4 +196,14 @@ public class RestTemplateIntegrationTests {
|
||||
}
|
||||
}
|
||||
|
||||
private static class UriServlet extends HttpServlet {
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
resp.setContentType("text/plain");
|
||||
resp.setCharacterEncoding("UTF-8");
|
||||
resp.getWriter().write(req.getRequestURI());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -84,12 +84,9 @@ public class UriTemplateTests {
|
||||
|
||||
@Test
|
||||
public void expandEncoded() throws Exception {
|
||||
UriTemplate template = new UriTemplate("http://example.com/hotel list/{hotel}");
|
||||
URI result = template.expand(Collections.singletonMap("hotel", "foo bar \u20AC"));
|
||||
assertEquals("Invalid expanded template", new URI("http", "//example.com/hotel list/foo bar \u20AC", null),
|
||||
result);
|
||||
assertEquals("Invalid expanded template", "http://example.com/hotel%20list/foo%20bar%20%E2%82%AC",
|
||||
result.toASCIIString());
|
||||
UriTemplate template = new UriTemplate("http://example.com//hotel list/{hotel}");
|
||||
URI result = template.expand("Z<EFBFBD>rich");
|
||||
assertEquals("Invalid expanded template", new URI("http://example.com//hotel%20list/Z%FCrich"), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -16,35 +16,108 @@
|
||||
|
||||
package org.springframework.web.util;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.Matcher;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/** @author Arjen Poutsma */
|
||||
public class UriUtilsTest {
|
||||
|
||||
private static final String ENC = "UTF-8";
|
||||
|
||||
@Test
|
||||
public void encode() throws UnsupportedEncodingException {
|
||||
assertEquals("Invalid encoded URI", "foobar", UriUtils.encode("foobar", "UTF-8"));
|
||||
assertEquals("Invalid encoded URI", "foo%20bar", UriUtils.encode("foo bar", "UTF-8"));
|
||||
assertEquals("Invalid encoded URI", "foo%2Bbar", UriUtils.encode("foo+bar", "UTF-8"));
|
||||
public void encodeScheme() throws UnsupportedEncodingException {
|
||||
assertEquals("Invalid encoded result", "foobar+-.", UriUtils.encodeScheme("foobar+-.", ENC));
|
||||
assertEquals("Invalid encoded result", "foo%20bar", UriUtils.encodeScheme("foo bar", ENC));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeUserInfo() throws UnsupportedEncodingException {
|
||||
assertEquals("Invalid encoded result", "foobar:", UriUtils.encodeUserInfo("foobar:", ENC));
|
||||
assertEquals("Invalid encoded result", "foo%20bar", UriUtils.encodeUserInfo("foo bar", ENC));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeHost() throws UnsupportedEncodingException {
|
||||
assertEquals("Invalid encoded result", "foobar", UriUtils.encodeHost("foobar", ENC));
|
||||
assertEquals("Invalid encoded result", "foo%20bar", UriUtils.encodeHost("foo bar", ENC));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodePort() throws UnsupportedEncodingException {
|
||||
assertEquals("Invalid encoded result", "80", UriUtils.encodePort("80", ENC));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodePath() throws UnsupportedEncodingException {
|
||||
assertEquals("Invalid encoded result", "/foo/bar", UriUtils.encodePath("/foo/bar", ENC));
|
||||
assertEquals("Invalid encoded result", "/foo%20bar", UriUtils.encodePath("/foo bar", ENC));
|
||||
assertEquals("Invalid encoded result", "/Z%FCrich", UriUtils.encodePath("/Z\u00fcrich", ENC));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodePathSegment() throws UnsupportedEncodingException {
|
||||
assertEquals("Invalid encoded result", "foobar", UriUtils.encodePathSegment("foobar", ENC));
|
||||
assertEquals("Invalid encoded result", "%2Ffoo%2Fbar", UriUtils.encodePathSegment("/foo/bar", ENC));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeQuery() throws UnsupportedEncodingException {
|
||||
assertEquals("Invalid encoded result", "foobar", UriUtils.encodeQuery("foobar", ENC));
|
||||
assertEquals("Invalid encoded result", "foo%20bar", UriUtils.encodeQuery("foo bar", ENC));
|
||||
assertEquals("Invalid encoded result", "foobar/+", UriUtils.encodeQuery("foobar/+", ENC));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeQueryParam() throws UnsupportedEncodingException {
|
||||
assertEquals("Invalid encoded result", "foobar", UriUtils.encodeQueryParam("foobar", ENC));
|
||||
assertEquals("Invalid encoded result", "foo%20bar", UriUtils.encodeQueryParam("foo bar", ENC));
|
||||
assertEquals("Invalid encoded result", "foo%26bar", UriUtils.encodeQueryParam("foo&bar", ENC));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeFragment() throws UnsupportedEncodingException {
|
||||
assertEquals("Invalid encoded result", "foobar", UriUtils.encodeFragment("foobar", ENC));
|
||||
assertEquals("Invalid encoded result", "foo%20bar", UriUtils.encodeFragment("foo bar", ENC));
|
||||
assertEquals("Invalid encoded result", "foobar/", UriUtils.encodeFragment("foobar/", ENC));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decode() throws UnsupportedEncodingException {
|
||||
assertEquals("Invalid encoded URI", "foobar", UriUtils.decode("foobar", "UTF-8"));
|
||||
assertEquals("Invalid encoded URI", "foo bar", UriUtils.decode("foo%20bar", "UTF-8"));
|
||||
assertEquals("Invalid encoded URI", "foo+bar", UriUtils.decode("foo%2bbar", "UTF-8"));
|
||||
assertEquals("Invalid encoded URI", "foobar", UriUtils.decode("foobar", ENC));
|
||||
assertEquals("Invalid encoded URI", "foo bar", UriUtils.decode("foo%20bar", ENC));
|
||||
assertEquals("Invalid encoded URI", "foo+bar", UriUtils.decode("foo%2bbar", ENC));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void decodeInvalidSequence() throws UnsupportedEncodingException {
|
||||
UriUtils.decode("foo%2", "UTF-8");
|
||||
UriUtils.decode("foo%2", ENC);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeUri() throws UnsupportedEncodingException {
|
||||
assertEquals("Invalid encoded URI", "http://www.ietf.org/rfc/rfc3986.txt",
|
||||
UriUtils.encodeUri("http://www.ietf.org/rfc/rfc3986.txt", ENC));
|
||||
assertEquals("Invalid encoded URI", "http://www.google.com/?q=z%FCrich",
|
||||
UriUtils.encodeUri("http://www.google.com/?q=z<>rich", ENC));
|
||||
assertEquals("Invalid encoded URI",
|
||||
"http://arjen:foobar@java.sun.com:80/javase/6/docs/api/java/util/BitSet.html?foo=bar#and(java.util.BitSet)",
|
||||
UriUtils.encodeUri(
|
||||
"http://arjen:foobar@java.sun.com:80/javase/6/docs/api/java/util/BitSet.html?foo=bar#and(java.util.BitSet)",
|
||||
ENC));
|
||||
assertEquals("Invalid encoded URI", "mailto:java-net@java.sun.com",
|
||||
UriUtils.encodeUri("mailto:java-net@java.sun.com", ENC));
|
||||
assertEquals("Invalid encoded URI", "news:comp.lang.java", UriUtils.encodeUri("news:comp.lang.java", ENC));
|
||||
assertEquals("Invalid encoded URI", "urn:isbn:096139210x", UriUtils.encodeUri("urn:isbn:096139210x", ENC));
|
||||
assertEquals("Invalid encoded URI", "http://java.sun.com/j2se/1.3/",
|
||||
UriUtils.encodeUri("http://java.sun.com/j2se/1.3/", ENC));
|
||||
assertEquals("Invalid encoded URI", "docs/guide/collections/designfaq.html#28",
|
||||
UriUtils.encodeUri("docs/guide/collections/designfaq.html#28", ENC));
|
||||
assertEquals("Invalid encoded URI", "../../../demo/jfc/SwingSet2/src/SwingSet2.java",
|
||||
UriUtils.encodeUri("../../../demo/jfc/SwingSet2/src/SwingSet2.java", ENC));
|
||||
assertEquals("Invalid encoded URI", "file:///~/calendar", UriUtils.encodeUri("file:///~/calendar", ENC));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user