From 2ca75386f1e9a1c9f12c71ce56e47c3dee998703 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 11 Feb 2013 11:17:23 -0800 Subject: [PATCH] Align UriComponents.toUri() with toUriString() 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 --- .../web/util/HierarchicalUriComponents.java | 5 ++++- .../web/util/UriComponentsBuilderTests.java | 16 +++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java b/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java index 9c33379422..dce503962c 100644 --- a/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java +++ b/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java @@ -407,7 +407,10 @@ final class HierarchicalUriComponents extends UriComponents { 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()); diff --git a/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java b/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java index afa1691008..3040ee4aa9 100644 --- a/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java @@ -57,7 +57,9 @@ public class UriComponentsBuilderTests { 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(); @@ -332,4 +334,16 @@ public class UriComponentsBuilderTests { 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")); + } }