Skip to content

Commit

Permalink
Align UriComponents.toUri() with toUriString()
Browse files Browse the repository at this point in the history
Update HierarchicalUriComponents.toUri() to only prepend a missing '/'
when the scheme, user info, host or port are specified. This makes
the toUri() method behave in the same way as .toUriString() and allows
relative URIs to be created.

Issue: SPR-10231
  • Loading branch information
philwebb committed Feb 11, 2013
1 parent 203b22b commit 2ca7538
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,10 @@ public URI toUri() {
else {
String path = getPath();
if (StringUtils.hasLength(path) && path.charAt(0) != PATH_DELIMITER) {
path = PATH_DELIMITER + path;
// Only prefix the path delimiter if something exists before it
if(getScheme() != null || getUserInfo() != null || getHost() != null || getPort() != -1) {
path = PATH_DELIMITER + path;
}
}
return new URI(getScheme(), getUserInfo(), getHost(), getPort(), path, getQuery(),
getFragment());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ public void fromPath() throws URISyntaxException {
assertEquals("bar", result.getQuery());
assertEquals("baz", result.getFragment());

URI expected = new URI("/foo?bar#baz");
assertEquals("Invalid result URI String", "foo?bar#baz", result.toUriString());

URI expected = new URI("foo?bar#baz");
assertEquals("Invalid result URI", expected, result.toUri());

result = UriComponentsBuilder.fromPath("/foo").build();
Expand Down Expand Up @@ -332,4 +334,16 @@ public void queryParamWithoutValueWithoutEquals() throws Exception {
assertThat(uriComponents.toUriString(), equalTo("http://example.com/foo?bar"));
}

@Test
public void relativeUrls() throws Exception {
assertThat(UriComponentsBuilder.fromUriString("http://example.com/foo/../bar").build().toString(), equalTo("http://example.com/foo/../bar"));
assertThat(UriComponentsBuilder.fromUriString("http://example.com/foo/../bar").build().toUriString(), equalTo("http://example.com/foo/../bar"));
assertThat(UriComponentsBuilder.fromUriString("http://example.com/foo/../bar").build().toUri().getPath(), equalTo("/foo/../bar"));
assertThat(UriComponentsBuilder.fromUriString("../../").build().toString(), equalTo("../../"));
assertThat(UriComponentsBuilder.fromUriString("../../").build().toUriString(), equalTo("../../"));
assertThat(UriComponentsBuilder.fromUriString("../../").build().toUri().getPath(), equalTo("../../"));
assertThat(UriComponentsBuilder.fromUriString("http://example.com").path("foo/../bar").build().toString(), equalTo("http://example.com/foo/../bar"));
assertThat(UriComponentsBuilder.fromUriString("http://example.com").path("foo/../bar").build().toUriString(), equalTo("http://example.com/foo/../bar"));
assertThat(UriComponentsBuilder.fromUriString("http://example.com").path("foo/../bar").build().toUri().getPath(), equalTo("/foo/../bar"));
}
}

0 comments on commit 2ca7538

Please sign in to comment.