usedParams)
throws JspException {
+ String encoding = pageContext.getResponse().getCharacterEncoding();
+
for (Param param : params) {
String template = URL_TEMPLATE_DELIMITER_PREFIX + param.getName() + URL_TEMPLATE_DELIMITER_SUFFIX;
if (uri.contains(template)) {
usedParams.add(param.getName());
- uri = uri.replace(template, urlEncode(param.getValue()));
+ try {
+ uri = uri.replace(template, UriUtils.encodePath(param.getValue(), encoding));
+ }
+ catch (UnsupportedEncodingException ex) {
+ throw new JspException(ex);
+ }
}
}
return uri;
}
- /**
- * URL-encode the provided String using the character encoding for the response.
- * This method will not URL-encode according to the
- * application/x-www-form-urlencoded MIME format. Spaces will
- * encoded as regular character instead of +. In UTF-8
- * a space encodes to %20.
- * @param value the value to encode
- * @return the URL encoded value
- * @throws JspException if the character encoding is invalid
- */
- protected String urlEncode(String value) throws JspException {
- if (value == null) {
- return null;
- }
- try {
- String encoding = pageContext.getResponse().getCharacterEncoding();
- return UriUtils.encode(value, encoding);
- }
- catch (UnsupportedEncodingException ex) {
- throw new JspException(ex);
- }
- }
-
/**
* Internal enum that classifies URLs by type.
*/
diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/UrlTagTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/UrlTagTests.java
index 9ba86bb6131..02e2db821ab 100644
--- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/UrlTagTests.java
+++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/UrlTagTests.java
@@ -550,26 +550,6 @@ public class UrlTagTests extends AbstractTagTests {
assertEquals("url/path?foo=bar&name=value", uri);
}
- public void testUrlEncode() throws JspException {
- assertEquals("my%20name%2Bis", tag.urlEncode("my name+is"));
- }
-
- public void testUrlEncodeNull() throws JspException {
- assertNull(tag.urlEncode(null));
- }
-
- public void testUrlEncodeBadEncoding() {
- context.getResponse().setCharacterEncoding("bad encoding");
-
- try {
- tag.urlEncode("my name");
- fail("expected JspException");
- }
- catch (JspException e) {
- // we want this
- }
- }
-
public void testJspWriterOutput() {
// TODO assert that the output to the JspWriter is the expected output
}
diff --git a/org.springframework.web/src/main/java/org/springframework/web/util/UriTemplate.java b/org.springframework.web/src/main/java/org/springframework/web/util/UriTemplate.java
index 2bc3521e088..a532fd763b8 100644
--- a/org.springframework.web/src/main/java/org/springframework/web/util/UriTemplate.java
+++ b/org.springframework.web/src/main/java/org/springframework/web/util/UriTemplate.java
@@ -25,6 +25,7 @@ import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import java.io.UnsupportedEncodingException;
import org.springframework.util.Assert;
@@ -175,48 +176,12 @@ public class UriTemplate {
private static URI encodeUri(String uri) {
try {
- int schemeIdx = uri.indexOf(':');
- if (schemeIdx == -1) {
- int queryIdx = uri.indexOf('?');
- if (queryIdx == -1) {
- int fragmentIdx = uri.indexOf('#');
- if (fragmentIdx == -1) {
- return new URI(null, null, uri, null);
- }
- else {
- String path = uri.substring(0, fragmentIdx);
- String fragment = uri.substring(fragmentIdx + 1);
- return new URI(null, null, path, fragment);
- }
- }
- else {
- int fragmentIdx = uri.indexOf('#', queryIdx + 1);
- if (fragmentIdx == -1) {
- String path = uri.substring(0, queryIdx);
- String query = uri.substring(queryIdx + 1);
- return new URI(null, null, path, query, null);
- }
- else {
- String path = uri.substring(0, queryIdx);
- String query = uri.substring(queryIdx + 1, fragmentIdx);
- String fragment = uri.substring(fragmentIdx + 1);
- return new URI(null, null, path, query, fragment);
- }
- }
- }
- else {
- int fragmentIdx = uri.indexOf('#', schemeIdx + 1);
- String scheme = uri.substring(0, schemeIdx);
- if (fragmentIdx == -1) {
- String ssp = uri.substring(schemeIdx + 1);
- return new URI(scheme, ssp, null);
- }
- else {
- String ssp = uri.substring(schemeIdx + 1, fragmentIdx);
- String fragment = uri.substring(fragmentIdx + 1);
- return new URI(scheme, ssp, fragment);
- }
- }
+ String encoded = UriUtils.encodeUri(uri, "UTF-8");
+ return new URI(encoded);
+ }
+ catch (UnsupportedEncodingException ex) {
+ // should not happen, UTF-8 is always supported
+ throw new IllegalStateException(ex);
}
catch (URISyntaxException ex) {
throw new IllegalArgumentException("Could not create URI from [" + uri + "]: " + ex);
diff --git a/org.springframework.web/src/main/java/org/springframework/web/util/UriUtils.java b/org.springframework.web/src/main/java/org/springframework/web/util/UriUtils.java
index c7e837b06d8..4fabdb2aca3 100644
--- a/org.springframework.web/src/main/java/org/springframework/web/util/UriUtils.java
+++ b/org.springframework.web/src/main/java/org/springframework/web/util/UriUtils.java
@@ -19,63 +19,340 @@ package org.springframework.web.util;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.BitSet;
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
import org.springframework.util.Assert;
/**
- * Utility class for URI encoding. Based on RFC 2396.
+ * Utility class for URI encoding and decoding based on RFC 3986. Offers encoding methods for
+ * the various URI components.
*
- *
Effectively, the encoding and decoding methods in this class
- * are similar to those found in {@link java.net.URLEncoder} and
- * {@link java.net.URLDecoder}, except that the space character
- * is encoded as {@code %20}, not {@code +}.
+ *
All {@code encode*(String, String} methods in this class operate in a similar way:
+ *
+ * - Valid characters for the specific URI component as defined in RFC 3986 stay the same.
+ *
- All other characters are converted into one or more bytes in the given encoding scheme.
+ * Each of the resulting bytes is written as a hexadecimal string in the "
%xy" format.
+ *
*
* @author Arjen Poutsma
* @since 3.0
- * @see RFC 2396
+ * @see RFC 3986
*/
public abstract class UriUtils {
- private static final BitSet notEncoded = new BitSet(256);
+ private static final BitSet SCHEME;
+
+ private static final BitSet USER_INFO;
+
+ private static final BitSet HOST;
+
+ private static final BitSet PORT;
+
+ private static final BitSet PATH;
+
+ private static final BitSet SEGMENT;
+
+ private static final BitSet QUERY;
+
+ private static final BitSet QUERY_PARAM;
+
+ private static final BitSet FRAGMENT;
+
+ private static final String SCHEME_PATTERN = "([^:/?#]+):";
+
+ private static final String USERINFO_PATTERN = "([^@]*)";
+
+ private static final String HOST_PATTERN = "([^/?#:]*)";
+
+ private static final String PORT_PATTERN = "(\\d*)";
+
+ private static final String PATH_PATTERN = "([^?#]*)";
+
+ private static final String QUERY_PATTERN = "([^#]*)";
+
+ private static final String FRAGMENT_PATTERN = "(.*)";
+
+ // Regex patterns that matches URIs. See RFC 3986, appendix B
+ private static final Pattern URI_PATTERN =
+ Pattern.compile("^(" + SCHEME_PATTERN + ")?" +
+ "(//(" + USERINFO_PATTERN + "@)?" + HOST_PATTERN + "(:" + PORT_PATTERN + ")?" + ")?"
+ + PATH_PATTERN +
+ "(\\?" + QUERY_PATTERN + ")?" +
+ "(#" + FRAGMENT_PATTERN + ")?");
static {
+ // variable names refer to RFC 3986, appendix A
+ BitSet alpha = new BitSet(256);
for (int i = 'a'; i <= 'z'; i++) {
- notEncoded.set(i);
+ alpha.set(i);
}
for (int i = 'A'; i <= 'Z'; i++) {
- notEncoded.set(i);
+ alpha.set(i);
}
+ BitSet digit = new BitSet(256);
for (int i = '0'; i <= '9'; i++) {
- notEncoded.set(i);
+ digit.set(i);
}
- notEncoded.set('-');
- notEncoded.set('_');
- notEncoded.set('.');
- notEncoded.set('*');
+ BitSet gendelims = new BitSet(256);
+ gendelims.set(':');
+ gendelims.set('/');
+ gendelims.set('?');
+ gendelims.set('#');
+ gendelims.set('[');
+ gendelims.set(']');
+ gendelims.set('@');
+
+ BitSet subdelims = new BitSet(256);
+ subdelims.set('!');
+ subdelims.set('$');
+ subdelims.set('&');
+ subdelims.set('\'');
+ subdelims.set('(');
+ subdelims.set(')');
+ subdelims.set('*');
+ subdelims.set('+');
+ subdelims.set(',');
+ subdelims.set(';');
+ subdelims.set('=');
+
+ BitSet reserved = new BitSet(256);
+ reserved.or(gendelims);
+ reserved.or(subdelims);
+
+ BitSet unreserved = new BitSet(256);
+ unreserved.or(alpha);
+ unreserved.or(digit);
+ unreserved.set('-');
+ unreserved.set('.');
+ unreserved.set('_');
+ unreserved.set('~');
+
+ SCHEME = new BitSet(256);
+ SCHEME.or(alpha);
+ SCHEME.or(digit);
+ SCHEME.set('+');
+ SCHEME.set('-');
+ SCHEME.set('.');
+
+ USER_INFO = new BitSet(256);
+ USER_INFO.or(unreserved);
+ USER_INFO.or(subdelims);
+ USER_INFO.set(':');
+
+ HOST = new BitSet(256);
+ HOST.or(unreserved);
+ HOST.or(subdelims);
+
+ PORT = new BitSet(256);
+ PORT.or(digit);
+
+ BitSet pchar = new BitSet(256);
+ pchar.or(unreserved);
+ pchar.or(subdelims);
+ pchar.set(':');
+ pchar.set('@');
+
+ SEGMENT = new BitSet(256);
+ SEGMENT.or(pchar);
+
+ PATH = new BitSet(256);
+ PATH.or(SEGMENT);
+ PATH.set('/');
+
+ QUERY = new BitSet(256);
+ QUERY.or(pchar);
+ QUERY.set('/');
+ QUERY.set('?');
+
+ QUERY_PARAM = new BitSet(256);
+ QUERY_PARAM.or(pchar);
+ QUERY_PARAM.set('/');
+ QUERY_PARAM.set('?');
+ QUERY_PARAM.clear('=');
+ QUERY_PARAM.clear('+');
+ QUERY_PARAM.clear('&');
+
+ FRAGMENT = new BitSet(256);
+ FRAGMENT.or(pchar);
+ FRAGMENT.set('/');
+ FRAGMENT.set('?');
}
/**
- * Encodes the given source URI into an encoded String. Based on the following
- * rules:
- *
- * - Alphanumeric characters {@code "a"} through {@code "z"},
- * {@code "A"} through {@code "Z"}, and {@code "0"} through {@code "9"}
- * stay the same.
- *
- Special characters {@code "-"}, {@code "_"}, {@code "."}, and
- * {@code "*"} stay the same.
- *
- All other characters are converted into one or more bytes using the
- * given encoding scheme. Each of the resulting bytes is written as a
- * hexadecimal string in the "
%xy" format.
- *
+ * Encodes the given source URI into an encoded String. All various URI components are encoded according to
+ * their respective valid character sets.
*
- * @param source the String to be encoded
- * @param encoding the encoding to use
- * @return the encoded string
+ * @param uri the URI to be encoded
+ * @param encoding the character encoding to encode to
+ * @return the encoded URI
+ * @throws IllegalArgumentException when the given uri parameter is not a valid URI
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
- * @see java.net.URLEncoder#encode(String, String)
*/
- public static String encode(String source, String encoding) throws UnsupportedEncodingException {
+ public static String encodeUri(String uri, String encoding) throws UnsupportedEncodingException {
+ Assert.notNull(uri, "'uri' must not be null");
+ Assert.hasLength(encoding, "'encoding' must not be empty");
+ Matcher m = URI_PATTERN.matcher(uri);
+ if (m.matches()) {
+ String scheme = m.group(2);
+ String authority = m.group(3);
+ String userinfo = m.group(5);
+ String host = m.group(6);
+ String port = m.group(8);
+ String path = m.group(9);
+ String query = m.group(11);
+ String fragment = m.group(13);
+
+ StringBuilder sb = new StringBuilder();
+
+ if (scheme != null) {
+ sb.append(encodeScheme(scheme, encoding));
+ sb.append(':');
+ }
+
+ if (authority != null) {
+ sb.append("//");
+ if (userinfo != null) {
+ sb.append(encodeUserInfo(userinfo, encoding));
+ sb.append('@');
+ }
+ if (host != null) {
+ sb.append(encodeHost(host, encoding));
+ }
+ if (port != null) {
+ sb.append(':');
+ sb.append(encodePort(port, encoding));
+ }
+ }
+
+ sb.append(encodePath(path, encoding));
+
+ if (query != null) {
+ sb.append('?');
+ sb.append(encodeQuery(query, encoding));
+ }
+
+ if (fragment != null) {
+ sb.append('#');
+ sb.append(encodeFragment(fragment, encoding));
+ }
+
+ return sb.toString();
+ } else {
+ throw new IllegalArgumentException("[" + uri + "] is not a valid URI");
+ }
+ }
+
+ /**
+ * Encodes the given URI scheme.
+ *
+ * @param scheme the scheme to be encoded
+ * @param encoding the character encoding to encode to
+ * @return the encoded scheme
+ * @throws UnsupportedEncodingException when the given encoding parameter is not supported
+ */
+ public static String encodeScheme(String scheme, String encoding) throws UnsupportedEncodingException {
+ return encode(scheme, encoding, SCHEME);
+ }
+
+ /**
+ * Encodes the given URI user info.
+ *
+ * @param userInfo the user info to be encoded
+ * @param encoding the character encoding to encode to
+ * @return the encoded user info
+ * @throws UnsupportedEncodingException when the given encoding parameter is not supported
+ */
+ public static String encodeUserInfo(String userInfo, String encoding) throws UnsupportedEncodingException {
+ return encode(userInfo, encoding, USER_INFO);
+ }
+
+ /**
+ * Encodes the given URI host.
+ *
+ * @param host the host to be encoded
+ * @param encoding the character encoding to encode to
+ * @return the encoded host
+ * @throws UnsupportedEncodingException when the given encoding parameter is not supported
+ */
+ public static String encodeHost(String host, String encoding) throws UnsupportedEncodingException {
+ return encode(host, encoding, HOST);
+ }
+
+ /**
+ * Encodes the given URI port.
+ *
+ * @param port the port to be encoded
+ * @param encoding the character encoding to encode to
+ * @return the encoded port
+ * @throws UnsupportedEncodingException when the given encoding parameter is not supported
+ */
+ public static String encodePort(String port, String encoding) throws UnsupportedEncodingException {
+ return encode(port, encoding, PORT);
+ }
+
+ /**
+ * Encodes the given URI path.
+ *
+ * @param path the path to be encoded
+ * @param encoding the character encoding to encode to
+ * @return the encoded path
+ * @throws UnsupportedEncodingException when the given encoding parameter is not supported
+ */
+ public static String encodePath(String path, String encoding) throws UnsupportedEncodingException {
+ return encode(path, encoding, PATH);
+ }
+
+ /**
+ * Encodes the given URI path segment.
+ *
+ * @param segment the segment to be encoded
+ * @param encoding the character encoding to encode to
+ * @return the encoded segment
+ * @throws UnsupportedEncodingException when the given encoding parameter is not supported
+ */
+ public static String encodePathSegment(String segment, String encoding) throws UnsupportedEncodingException {
+ return encode(segment, encoding, SEGMENT);
+ }
+
+ /**
+ * Encodes the given URI query.
+ *
+ * @param query the query to be encoded
+ * @param encoding the character encoding to encode to
+ * @return the encoded query
+ * @throws UnsupportedEncodingException when the given encoding parameter is not supported
+ */
+ public static String encodeQuery(String query, String encoding) throws UnsupportedEncodingException {
+ return encode(query, encoding, QUERY);
+ }
+
+ /**
+ * Encodes the given URI query parameter.
+ *
+ * @param query the query parameter to be encoded
+ * @param encoding the character encoding to encode to
+ * @return the encoded query parameter
+ * @throws UnsupportedEncodingException when the given encoding parameter is not supported
+ */
+ public static String encodeQueryParam(String queryParam, String encoding) throws UnsupportedEncodingException {
+ return encode(queryParam, encoding, QUERY_PARAM);
+ }
+
+ /**
+ * Encodes the given URI fragment.
+ *
+ * @param fragment the fragment to be encoded
+ * @param encoding the character encoding to encode to
+ * @return the encoded fragment
+ * @throws UnsupportedEncodingException when the given encoding parameter is not supported
+ */
+ public static String encodeFragment(String fragment, String encoding) throws UnsupportedEncodingException {
+ return encode(fragment, encoding, FRAGMENT);
+ }
+
+ private static String encode(String source, String encoding, BitSet notEncoded) throws UnsupportedEncodingException {
Assert.notNull(source, "'source' must not be null");
Assert.hasLength(encoding, "'encoding' must not be empty");
ByteArrayOutputStream bos = new ByteArrayOutputStream(source.length() * 2);
diff --git a/org.springframework.web/src/test/java/org/springframework/web/client/RestTemplateIntegrationTests.java b/org.springframework.web/src/test/java/org/springframework/web/client/RestTemplateIntegrationTests.java
index 446249a9b64..466cc91c8a2 100644
--- a/org.springframework.web/src/test/java/org/springframework/web/client/RestTemplateIntegrationTests.java
+++ b/org.springframework.web/src/test/java/org/springframework/web/client/RestTemplateIntegrationTests.java
@@ -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 allowed = template.optionsForAllow(new URI("http://localhost:8889/get"));
+ Set 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Ÿ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());
+ }
+ }
+
}
diff --git a/org.springframework.web/src/test/java/org/springframework/web/util/UriTemplateTests.java b/org.springframework.web/src/test/java/org/springframework/web/util/UriTemplateTests.java
index 10c2c4a20aa..80743e4da52 100644
--- a/org.springframework.web/src/test/java/org/springframework/web/util/UriTemplateTests.java
+++ b/org.springframework.web/src/test/java/org/springframework/web/util/UriTemplateTests.java
@@ -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Ÿrich");
+ assertEquals("Invalid expanded template", new URI("http://example.com//hotel%20list/Z%FCrich"), result);
}
@Test
diff --git a/org.springframework.web/src/test/java/org/springframework/web/util/UriUtilsTest.java b/org.springframework.web/src/test/java/org/springframework/web/util/UriUtilsTest.java
index 3fd5a9e4d04..65d72ab0211 100644
--- a/org.springframework.web/src/test/java/org/springframework/web/util/UriUtilsTest.java
+++ b/org.springframework.web/src/test/java/org/springframework/web/util/UriUtilsTest.java
@@ -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));
+
}
}