Skip to content

Commit

Permalink
UriComponentsBuilder.cloneBuilder copies uriVariables
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Mar 26, 2020
1 parent 818d18a commit ef013f7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ protected UriComponentsBuilder(UriComponentsBuilder other) {
this.host = other.host;
this.port = other.port;
this.pathBuilder = other.pathBuilder.cloneBuilder();
this.uriVariables.putAll(other.uriVariables);
this.queryParams.putAll(other.queryParams);
this.fragment = other.fragment;
this.encodeTemplate = other.encodeTemplate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ public void parsesEmptyUri() {
}

@Test
public void testClone() {
public void testCloneAndMerge() {
UriComponentsBuilder builder1 = UriComponentsBuilder.newInstance();
builder1.scheme("http").host("e1.com").path("/p1").pathSegment("ps1").queryParam("q1").fragment("f1").encode();

Expand All @@ -775,6 +775,37 @@ public void testClone() {
assertEquals("f2", result2.getFragment());
}

@Test // gh-24772
public void testDeepClone() {
HashMap<String, Object> vars = new HashMap<>();
vars.put("ps1", "foo");
vars.put("ps2", "bar");

UriComponentsBuilder builder1 = UriComponentsBuilder.newInstance();
builder1.scheme("http").host("e1.com").userInfo("user:pwd").path("/p1").pathSegment("{ps1}")
.pathSegment("{ps2}").queryParam("q1").fragment("f1").uriVariables(vars).encode();

UriComponentsBuilder builder2 = (UriComponentsBuilder) builder1.clone();

UriComponents result1 = builder1.build();
assertEquals("http", result1.getScheme());
assertEquals("user:pwd", result1.getUserInfo());
assertEquals("e1.com", result1.getHost());
assertEquals("/p1/foo/bar", result1.getPath());
assertEquals("q1", result1.getQuery());
assertEquals("f1", result1.getFragment());
assertNull(result1.getSchemeSpecificPart());

UriComponents result2 = builder2.build();
assertEquals("http", result2.getScheme());
assertEquals("user:pwd", result2.getUserInfo());
assertEquals("e1.com", result2.getHost());
assertEquals("/p1/foo/bar", result2.getPath());
assertEquals("q1", result2.getQuery());
assertEquals("f1", result2.getFragment());
assertNull(result1.getSchemeSpecificPart());
}

@Test // SPR-11856
public void fromHttpRequestForwardedHeader() {
MockHttpServletRequest request = new MockHttpServletRequest();
Expand Down

0 comments on commit ef013f7

Please sign in to comment.