Add copyToUriComponentsBuilder method

After this change UriComponentsBuilder#uriComponents method no longer
no longer copies from the given UriComponents but rather lets the
UriComponents instance copy itself to the UriComponentsBuilder.

This avoids the need for instanceof checks and also makes it possible
to distinguish between path and path segments, which otherwise is
internal knowledge of UriComponentsBuilder.

Issue: SPR-12742
This commit is contained in:
Rossen Stoyanchev
2015-03-20 12:11:42 -04:00
parent 8e4bfa9cc1
commit 0e7eecfe34
6 changed files with 86 additions and 37 deletions

View File

@@ -436,6 +436,15 @@ public class UriComponentsBuilderTests {
assertEquals("https://a.example.org/mvc-showcase", result.toString());
}
// SPR-12742
@Test
public void fromHttpRequestWithTrailingSlash() throws Exception {
UriComponents before = UriComponentsBuilder.fromPath("/foo/").build();
UriComponents after = UriComponentsBuilder.newInstance().uriComponents(before).build();
assertEquals("/foo/", after.getPath());
}
@Test
public void path() throws URISyntaxException {
UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/foo/bar");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@ import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import org.junit.Test;
@@ -132,6 +133,16 @@ public class UriComponentsTests {
assertThat(uriComponents.toString(), equalTo(readObject.toString()));
}
@Test
public void copyToUriComponentsBuilder() {
UriComponents source = UriComponentsBuilder.fromPath("/foo/bar").pathSegment("ba/z").build();
UriComponentsBuilder targetBuilder = UriComponentsBuilder.newInstance();
source.copyToUriComponentsBuilder(targetBuilder);
UriComponents result = targetBuilder.build().encode();
assertEquals("/foo/bar/ba%2Fz", result.getPath());
assertEquals(Arrays.asList("foo", "bar", "ba%2Fz"), result.getPathSegments());
}
@Test
public void equalsHierarchicalUriComponents() throws Exception {
UriComponents uriComponents1 = UriComponentsBuilder.fromUriString("http://example.com").path("/{foo}").query("bar={baz}").build();