SPR-5973: UriComponents no longer a Map, moved all static methods from UriComponents to builder, added expand method to UriComponents
This commit is contained in:
@@ -18,10 +18,14 @@ package org.springframework.web.util;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/** @author Arjen Poutsma */
|
||||
public class UriComponentsBuilderTests {
|
||||
@@ -71,40 +75,90 @@ public class UriComponentsBuilderTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pathSegments() throws URISyntaxException {
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.newInstance();
|
||||
URI result = builder.pathSegment("foo").pathSegment("bar").build().toUri();
|
||||
public void fromUriString() {
|
||||
UriComponents result = UriComponentsBuilder.fromUriString("http://www.ietf.org/rfc/rfc3986.txt").build();
|
||||
assertEquals("http", result.getScheme());
|
||||
assertNull(result.getUserInfo());
|
||||
assertEquals("www.ietf.org", result.getHost());
|
||||
assertEquals(-1, result.getPort());
|
||||
assertEquals("/rfc/rfc3986.txt", result.getPath());
|
||||
assertEquals(Arrays.asList("rfc", "rfc3986.txt"), result.getPathSegments());
|
||||
assertNull(result.getQuery());
|
||||
assertNull(result.getFragment());
|
||||
|
||||
URI expected = new URI("/foo/bar");
|
||||
assertEquals("Invalid result URI", expected, result);
|
||||
result = UriComponentsBuilder.fromUriString(
|
||||
"http://arjen:foobar@java.sun.com:80/javase/6/docs/api/java/util/BitSet.html?foo=bar#and(java.util.BitSet)")
|
||||
.build();
|
||||
assertEquals("http", result.getScheme());
|
||||
assertEquals("arjen:foobar", result.getUserInfo());
|
||||
assertEquals("java.sun.com", result.getHost());
|
||||
assertEquals(80, result.getPort());
|
||||
assertEquals("/javase/6/docs/api/java/util/BitSet.html", result.getPath());
|
||||
assertEquals("foo=bar", result.getQuery());
|
||||
MultiValueMap<String, String> expectedQueryParams = new LinkedMultiValueMap<String, String>(1);
|
||||
expectedQueryParams.add("foo", "bar");
|
||||
assertEquals(expectedQueryParams, result.getQueryParams());
|
||||
assertEquals("and(java.util.BitSet)", result.getFragment());
|
||||
|
||||
result = UriComponentsBuilder.fromUriString("mailto:java-net@java.sun.com").build();
|
||||
assertEquals("mailto", result.getScheme());
|
||||
assertNull(result.getUserInfo());
|
||||
assertNull(result.getHost());
|
||||
assertEquals(-1, result.getPort());
|
||||
assertEquals("java-net@java.sun.com", result.getPathSegments().get(0));
|
||||
assertNull(result.getQuery());
|
||||
assertNull(result.getFragment());
|
||||
|
||||
result = UriComponentsBuilder.fromUriString("docs/guide/collections/designfaq.html#28").build();
|
||||
assertNull(result.getScheme());
|
||||
assertNull(result.getUserInfo());
|
||||
assertNull(result.getHost());
|
||||
assertEquals(-1, result.getPort());
|
||||
assertEquals("/docs/guide/collections/designfaq.html", result.getPath());
|
||||
assertNull(result.getQuery());
|
||||
assertEquals("28", result.getFragment());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void path() throws URISyntaxException {
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/foo/bar");
|
||||
UriComponents result = builder.build();
|
||||
|
||||
assertEquals("/foo/bar", result.getPath());
|
||||
assertEquals(Arrays.asList("foo", "bar"), result.getPathSegments());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryParam() throws URISyntaxException {
|
||||
public void pathSegments() throws URISyntaxException {
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.newInstance();
|
||||
URI result = builder.queryParam("baz", "qux", 42).build().toUri();
|
||||
UriComponents result = builder.pathSegment("foo").pathSegment("bar").build();
|
||||
|
||||
URI expected = new URI("?baz=qux&baz=42");
|
||||
assertEquals("Invalid result URI", expected, result);
|
||||
assertEquals("/foo/bar", result.getPath());
|
||||
assertEquals(Arrays.asList("foo", "bar"), result.getPathSegments());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryParams() throws URISyntaxException {
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.newInstance();
|
||||
UriComponents result = builder.queryParam("baz", "qux", 42).build();
|
||||
|
||||
assertEquals("baz=qux&baz=42", result.getQuery());
|
||||
MultiValueMap<String, String> expectedQueryParams = new LinkedMultiValueMap<String, String>(2);
|
||||
expectedQueryParams.add("baz", "qux");
|
||||
expectedQueryParams.add("baz", "42");
|
||||
assertEquals(expectedQueryParams, result.getQueryParams());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void emptyQueryParam() throws URISyntaxException {
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.newInstance();
|
||||
URI result = builder.queryParam("baz").build().toUri();
|
||||
UriComponents result = builder.queryParam("baz").build();
|
||||
|
||||
URI expected = new URI("?baz");
|
||||
assertEquals("Invalid result URI", expected, result);
|
||||
assertEquals("baz", result.getQuery());
|
||||
MultiValueMap<String, String> expectedQueryParams = new LinkedMultiValueMap<String, String>(2);
|
||||
expectedQueryParams.add("baz", null);
|
||||
assertEquals(expectedQueryParams, result.getQueryParams());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void combineWithUriTemplate() throws URISyntaxException {
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/{foo}");
|
||||
UriComponents components = builder.build();
|
||||
UriTemplate template = new UriTemplate(components);
|
||||
URI uri = template.expand("bar baz");
|
||||
assertEquals(new URI("/bar%20baz"), uri);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -18,102 +18,32 @@ package org.springframework.web.util;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/** @author Arjen Poutsma */
|
||||
public class UriComponentsTests {
|
||||
|
||||
@Test
|
||||
public void fromUri() {
|
||||
Map<UriComponents.Type, String> result = UriComponents.fromUriString("http://www.ietf.org/rfc/rfc3986.txt");
|
||||
assertEquals("http", result.get(UriComponents.Type.SCHEME));
|
||||
assertNull(result.get(UriComponents.Type.USER_INFO));
|
||||
assertEquals("www.ietf.org", result.get(UriComponents.Type.HOST));
|
||||
assertNull(result.get(UriComponents.Type.PORT));
|
||||
assertEquals("/rfc/rfc3986.txt", result.get(UriComponents.Type.PATH));
|
||||
assertNull(result.get(UriComponents.Type.QUERY));
|
||||
assertNull(result.get(UriComponents.Type.FRAGMENT));
|
||||
|
||||
result = UriComponents.fromUriString(
|
||||
"http://arjen:foobar@java.sun.com:80/javase/6/docs/api/java/util/BitSet.html?foo=bar#and(java.util.BitSet)");
|
||||
assertEquals("http", result.get(UriComponents.Type.SCHEME));
|
||||
assertEquals("arjen:foobar", result.get(UriComponents.Type.USER_INFO));
|
||||
assertEquals("java.sun.com", result.get(UriComponents.Type.HOST));
|
||||
assertEquals("80", result.get(UriComponents.Type.PORT));
|
||||
assertEquals("/javase/6/docs/api/java/util/BitSet.html", result.get(UriComponents.Type.PATH));
|
||||
assertEquals("foo=bar", result.get(UriComponents.Type.QUERY));
|
||||
assertEquals("and(java.util.BitSet)", result.get(UriComponents.Type.FRAGMENT));
|
||||
|
||||
result = UriComponents.fromUriString("mailto:java-net@java.sun.com");
|
||||
assertEquals("mailto", result.get(UriComponents.Type.SCHEME));
|
||||
assertNull(result.get(UriComponents.Type.USER_INFO));
|
||||
assertNull(result.get(UriComponents.Type.HOST));
|
||||
assertNull(result.get(UriComponents.Type.PORT));
|
||||
assertEquals("java-net@java.sun.com", result.get(UriComponents.Type.PATH));
|
||||
assertNull(result.get(UriComponents.Type.QUERY));
|
||||
assertNull(result.get(UriComponents.Type.FRAGMENT));
|
||||
|
||||
result = UriComponents.fromUriString("docs/guide/collections/designfaq.html#28");
|
||||
assertNull(result.get(UriComponents.Type.SCHEME));
|
||||
assertNull(result.get(UriComponents.Type.USER_INFO));
|
||||
assertNull(result.get(UriComponents.Type.HOST));
|
||||
assertNull(result.get(UriComponents.Type.PORT));
|
||||
assertEquals("docs/guide/collections/designfaq.html", result.get(UriComponents.Type.PATH));
|
||||
assertNull(result.get(UriComponents.Type.QUERY));
|
||||
assertEquals("28", result.get(UriComponents.Type.FRAGMENT));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pathSegments() {
|
||||
String path = "/foo/bar";
|
||||
UriComponents components = UriComponents.fromUriComponentMap(Collections.singletonMap(UriComponents.Type.PATH, path));
|
||||
List<String> expected = Arrays.asList("foo", "bar");
|
||||
|
||||
List<String> pathSegments = components.getPathSegments();
|
||||
assertEquals(expected, pathSegments);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryParams() {
|
||||
String query = "foo=bar&foo=baz&qux";
|
||||
UriComponents components = UriComponents.fromUriComponentMap(
|
||||
Collections.singletonMap(UriComponents.Type.QUERY, query));
|
||||
MultiValueMap<String, String> expected = new LinkedMultiValueMap<String, String>(1);
|
||||
expected.put("foo", Arrays.asList("bar", "baz"));
|
||||
expected.set("qux", null);
|
||||
|
||||
MultiValueMap<String, String> result = components.getQueryParams();
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encode() {
|
||||
UriComponents uriComponents = UriComponents.fromUriString("http://example.com/hotel list");
|
||||
UriComponents uriComponents = UriComponentsBuilder.fromPath("/hotel list").build();
|
||||
UriComponents encoded = uriComponents.encode();
|
||||
assertEquals("/hotel%20list", encoded.getPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toUriEncoded() throws URISyntaxException {
|
||||
UriComponents uriComponents = UriComponents.fromUriString("http://example.com/hotel list/Z\u00fcrich");
|
||||
UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://example.com/hotel list/Z\u00fcrich").build();
|
||||
UriComponents encoded = uriComponents.encode();
|
||||
assertEquals(new URI("http://example.com/hotel%20list/Z%C3%BCrich"), encoded.toUri());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void toUriNotEncoded() throws URISyntaxException {
|
||||
UriComponents uriComponents = UriComponents.fromUriString("http://example.com/hotel list/Z\u00fcrich");
|
||||
UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://example.com/hotel list/Z\u00fcrich").build();
|
||||
assertEquals(new URI("http://example.com/hotel%20list/Z\u00fcrich"), uriComponents.toUri());
|
||||
}
|
||||
|
||||
|
||||
@@ -89,22 +89,6 @@ public class UriTemplateTests {
|
||||
assertEquals("Invalid expanded template", new URI("http://example.com/hotel%20list/Z%C3%BCrich"), result);
|
||||
}
|
||||
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void expandMapInvalidAmountVariables() throws Exception {
|
||||
UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
|
||||
template.expand(Collections.singletonMap("hotel", "1"));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void expandMapUnboundVariables() throws Exception {
|
||||
Map<String, String> uriVariables = new HashMap<String, String>(2);
|
||||
uriVariables.put("booking", "42");
|
||||
uriVariables.put("bar", "1");
|
||||
UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
|
||||
template.expand(uriVariables);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expandEncoded() throws Exception {
|
||||
UriTemplate template = new UriTemplate("http://example.com/hotel list/{hotel}");
|
||||
|
||||
@@ -20,7 +20,7 @@ import java.io.UnsupportedEncodingException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
@@ -111,23 +111,19 @@ public class UriUtilsTests {
|
||||
UriUtils.encodeUri("http://www.ietf.org/rfc/rfc3986.txt", ENC));
|
||||
assertEquals("Invalid encoded URI", "https://www.ietf.org/rfc/rfc3986.txt",
|
||||
UriUtils.encodeUri("https://www.ietf.org/rfc/rfc3986.txt", ENC));
|
||||
assertEquals("Invalid encoded URI", "http://www.google.com/?q=Z%C3%BCrich",
|
||||
UriUtils.encodeUri("http://www.google.com/?q=Z\u00fcrich", ENC));
|
||||
assertEquals("Invalid encoded URI", "http://www.google.com?q=Z%C3%BCrich",
|
||||
UriUtils.encodeUri("http://www.google.com?q=Z\u00fcrich", 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", "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));
|
||||
assertEquals("Invalid encoded URI", "http://example.com/query=foo@bar",
|
||||
UriUtils.encodeUri("http://example.com/query=foo@bar", ENC));
|
||||
@@ -140,8 +136,8 @@ public class UriUtilsTests {
|
||||
UriUtils.encodeHttpUrl("http://www.ietf.org/rfc/rfc3986.txt", ENC));
|
||||
assertEquals("Invalid encoded URI", "https://www.ietf.org/rfc/rfc3986.txt",
|
||||
UriUtils.encodeHttpUrl("https://www.ietf.org/rfc/rfc3986.txt", ENC));
|
||||
assertEquals("Invalid encoded HTTP URL", "http://www.google.com/?q=Z%C3%BCrich",
|
||||
UriUtils.encodeHttpUrl("http://www.google.com/?q=Z\u00fcrich", ENC));
|
||||
assertEquals("Invalid encoded HTTP URL", "http://www.google.com?q=Z%C3%BCrich",
|
||||
UriUtils.encodeHttpUrl("http://www.google.com?q=Z\u00fcrich", ENC));
|
||||
assertEquals("Invalid encoded HTTP URL", "http://ws.geonames.org/searchJSON?q=T%C5%8Dky%C5%8D&style=FULL&maxRows=300",
|
||||
UriUtils.encodeHttpUrl("http://ws.geonames.org/searchJSON?q=T\u014dky\u014d&style=FULL&maxRows=300", ENC));
|
||||
assertEquals("Invalid encoded HTTP URL",
|
||||
@@ -150,8 +146,8 @@ public class UriUtilsTests {
|
||||
"http://arjen:foobar@java.sun.com:80/javase/6/docs/api/java/util/BitSet.html?foo=bar", ENC));
|
||||
assertEquals("Invalid encoded HTTP URL", "http://search.twitter.com/search.atom?q=%23avatar",
|
||||
UriUtils.encodeHttpUrl("http://search.twitter.com/search.atom?q=#avatar", ENC));
|
||||
assertEquals("Invalid encoded HTTP URL", "http://java.sun.com/j2se/1.3/",
|
||||
UriUtils.encodeHttpUrl("http://java.sun.com/j2se/1.3/", ENC));
|
||||
assertEquals("Invalid encoded HTTP URL", "http://java.sun.com/j2se/1.3",
|
||||
UriUtils.encodeHttpUrl("http://java.sun.com/j2se/1.3", ENC));
|
||||
assertEquals("Invalid encoded HTTP URL", "http://example.com/query=foo@bar",
|
||||
UriUtils.encodeHttpUrl("http://example.com/query=foo@bar", ENC));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user