Support opaque URIs in UriComponentsBuilder
Before this commit, UriComponentsBuilder did not handle opaque URIs at all. After this commit it does. Support is introduced by making UriComponents an abstract base class, and having two concrete subclasses: HierarchicalUriComponents and OpaqueUriComponents. The former is more or less the same as the old UriComponents class. Issue: SPR-9798
This commit is contained in:
committed by
Rossen Stoyanchev
parent
b2037c8316
commit
6e45a79ecb
@@ -64,7 +64,7 @@ public class UriComponentsBuilderTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromUri() throws URISyntaxException {
|
||||
public void fromHierarchicalUri() throws URISyntaxException {
|
||||
URI uri = new URI("http://example.com/foo?bar#baz");
|
||||
UriComponents result = UriComponentsBuilder.fromUri(uri).build();
|
||||
assertEquals("http", result.getScheme());
|
||||
@@ -76,6 +76,17 @@ public class UriComponentsBuilderTests {
|
||||
assertEquals("Invalid result URI", uri, result.toUri());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromOpaqueUri() throws URISyntaxException {
|
||||
URI uri = new URI("mailto:foo@bar.com#baz");
|
||||
UriComponents result = UriComponentsBuilder.fromUri(uri).build();
|
||||
assertEquals("mailto", result.getScheme());
|
||||
assertEquals("foo@bar.com", result.getSchemeSpecificPart());
|
||||
assertEquals("baz", result.getFragment());
|
||||
|
||||
assertEquals("Invalid result URI", uri, result.toUri());
|
||||
}
|
||||
|
||||
// SPR-9317
|
||||
|
||||
@Test
|
||||
@@ -113,14 +124,15 @@ public class UriComponentsBuilderTests {
|
||||
assertEquals(expectedQueryParams, result.getQueryParams());
|
||||
assertEquals("and(java.util.BitSet)", result.getFragment());
|
||||
|
||||
result = UriComponentsBuilder.fromUriString("mailto:java-net@java.sun.com").build();
|
||||
result = UriComponentsBuilder.fromUriString("mailto:java-net@java.sun.com#baz").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));
|
||||
assertEquals("java-net@java.sun.com", result.getSchemeSpecificPart());
|
||||
assertNull(result.getPath());
|
||||
assertNull(result.getQuery());
|
||||
assertNull(result.getFragment());
|
||||
assertEquals("baz", result.getFragment());
|
||||
|
||||
result = UriComponentsBuilder.fromUriString("docs/guide/collections/designfaq.html#28").build();
|
||||
assertNull(result.getScheme());
|
||||
@@ -265,7 +277,7 @@ public class UriComponentsBuilderTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildAndExpand() {
|
||||
public void buildAndExpandHierarchical() {
|
||||
UriComponents result = UriComponentsBuilder.fromPath("/{foo}").buildAndExpand("fooValue");
|
||||
assertEquals("/fooValue", result.toUriString());
|
||||
|
||||
@@ -275,4 +287,17 @@ public class UriComponentsBuilderTests {
|
||||
result = UriComponentsBuilder.fromPath("/{foo}/{bar}").buildAndExpand(values);
|
||||
assertEquals("/fooValue/barValue", result.toUriString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildAndExpandOpaque() {
|
||||
UriComponents result = UriComponentsBuilder.fromUriString("mailto:{user}@{domain}").buildAndExpand("foo", "example.com");
|
||||
assertEquals("mailto:foo@example.com", result.toUriString());
|
||||
|
||||
Map<String, String> values = new HashMap<String, String>();
|
||||
values.put("user", "foo");
|
||||
values.put("domain", "example.com");
|
||||
UriComponentsBuilder.fromUriString("mailto:{user}@{domain}").buildAndExpand(values);
|
||||
assertEquals("mailto:foo@example.com", result.toUriString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user