RequestPath improvements

Static parse methods on PathSegmentContainer and PathSegment plus:

isEmpty() on PathSegmentContainer and PathSegment
isAbsolute() and hasTrailingSlash() on PathSegmentContainer
char[] alternative for valueDecoded() on PathSegment
This commit is contained in:
Rossen Stoyanchev
2017-06-14 15:58:44 -04:00
parent 7b5f96c804
commit 97a97f9bba
5 changed files with 201 additions and 63 deletions

View File

@@ -16,7 +16,6 @@
package org.springframework.http.server.reactive;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@@ -27,6 +26,7 @@ import org.junit.Test;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
/**
@@ -38,97 +38,118 @@ public class DefaultRequestPathTests {
@Test
public void pathSegment() throws Exception {
// basic
testPathSegment("cars", "", "cars", "cars", new LinkedMultiValueMap<>());
testPathSegment("cars", "", "cars", "cars", false, new LinkedMultiValueMap<>());
// empty
testPathSegment("", "", "", "", new LinkedMultiValueMap<>());
testPathSegment("", "", "", "", true, new LinkedMultiValueMap<>());
// spaces
testPathSegment("%20", "", "%20", " ", new LinkedMultiValueMap<>());
testPathSegment("%20a%20", "", "%20a%20", " a ", new LinkedMultiValueMap<>());
testPathSegment("%20%20", "", "%20%20", " ", true, new LinkedMultiValueMap<>());
testPathSegment("%20a%20", "", "%20a%20", " a ", false, new LinkedMultiValueMap<>());
}
@Test
public void pathSegmentWithParams() throws Exception {
public void pathSegmentParams() throws Exception {
// basic
LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("colors", "red");
params.add("colors", "blue");
params.add("colors", "green");
params.add("year", "2012");
testPathSegment("cars", ";colors=red,blue,green;year=2012", "cars", "cars", params);
testPathSegment("cars", ";colors=red,blue,green;year=2012", "cars", "cars", false, params);
// trailing semicolon
params = new LinkedMultiValueMap<>();
params.add("p", "1");
testPathSegment("path", ";p=1;", "path", "path", params);
testPathSegment("path", ";p=1;", "path", "path", false, params);
// params with spaces
params = new LinkedMultiValueMap<>();
params.add("param name", "param value");
testPathSegment("path", ";param%20name=param%20value;%20", "path", "path", params);
testPathSegment("path", ";param%20name=param%20value;%20", "path", "path", false, params);
// empty params
params = new LinkedMultiValueMap<>();
params.add("p", "1");
testPathSegment("path", ";;;%20;%20;p=1;%20", "path", "path", params);
testPathSegment("path", ";;;%20;%20;p=1;%20", "path", "path", false, params);
}
private void testPathSegment(String pathSegment, String semicolonContent,
String value, String valueDecoded, boolean empty, MultiValueMap<String, String> params) {
PathSegment segment = PathSegment.parse(pathSegment + semicolonContent, UTF_8);
assertEquals("value: '" + pathSegment + "'", value, segment.value());
assertEquals("valueDecoded: '" + pathSegment + "'", valueDecoded, segment.valueDecoded());
assertEquals("isEmpty: '" + pathSegment + "'", empty, segment.isEmpty());
assertEquals("semicolonContent: '" + pathSegment + "'", semicolonContent, segment.semicolonContent());
assertEquals("params: '" + pathSegment + "'", params, segment.parameters());
}
@Test
public void path() throws Exception {
// basic
testPath("/a/b/c", "/a/b/c", Arrays.asList("a", "b", "c"));
testPath("/a/b/c", "/a/b/c", false, true, Arrays.asList("a", "b", "c"), false);
// root path
testPath("/%20", "/%20", Collections.singletonList("%20"));
testPath("", "", Collections.emptyList());
testPath("%20", "", Collections.emptyList());
testPath("/", "/", false, true, Collections.singletonList(""), false);
// empty path
testPath("", "", true, false, Collections.emptyList(), false);
testPath("%20%20", "%20%20", true, false, Collections.singletonList("%20%20"), false);
// trailing slash
testPath("/a/b/", "/a/b/", Arrays.asList("a", "b", ""));
testPath("/a/b//", "/a/b//", Arrays.asList("a", "b", "", ""));
testPath("/a/b/", "/a/b/", false, true, Arrays.asList("a", "b"), true);
testPath("/a/b//", "/a/b//", false, true, Arrays.asList("a", "b", ""), true);
// extra slashes ande spaces
testPath("//%20/%20", "//%20/%20", Arrays.asList("", "%20", "%20"));
// extra slashes and spaces
testPath("/%20", "/%20", false, true, Collections.singletonList("%20"), false);
testPath("//%20/%20", "//%20/%20", false, true, Arrays.asList("", "%20", "%20"), false);
}
private void testPath(String input, String value, boolean empty, boolean absolute,
List<String> segments, boolean trailingSlash) {
PathSegmentContainer path = PathSegmentContainer.parse(input, UTF_8);
List<String> segmentValues = path.pathSegments().stream().map(PathSegment::value)
.collect(Collectors.toList());
assertEquals("value: '" + input + "'", value, path.value());
assertEquals("empty: '" + input + "'", empty, path.isEmpty());
assertEquals("isAbsolute: '" + input + "'", absolute, path.isAbsolute());
assertEquals("pathSegments: " + input, segments, segmentValues);
assertEquals("hasTrailingSlash: '" + input + "'", trailingSlash, path.hasTrailingSlash());
}
@Test
public void contextPath() throws Exception {
URI uri = URI.create("http://localhost:8080/app/a/b/c");
RequestPath path = new DefaultRequestPath(uri, "/app", StandardCharsets.UTF_8);
public void requestPath() throws Exception {
// basic
testRequestPath("/app/a/b/c", "/app", "/a/b/c", false, true, false);
PathSegmentContainer contextPath = path.contextPath();
assertEquals("/app", contextPath.value());
assertEquals(Collections.singletonList("app"), pathSegmentValues(contextPath));
// no context path
testRequestPath("/a/b/c", "", "/a/b/c", false, true, false);
PathSegmentContainer pathWithinApplication = path.pathWithinApplication();
assertEquals("/a/b/c", pathWithinApplication.value());
assertEquals(Arrays.asList("a", "b", "c"), pathSegmentValues(pathWithinApplication));
// empty path
testRequestPath("", "", "", true, false, false);
testRequestPath("", "/", "", true, false, false);
// trailing slash
testRequestPath("/app/a/", "/app", "/a/", false, true, true);
testRequestPath("/app/a//", "/app", "/a//", false, true, true);
}
private void testRequestPath(String fullPath, String contextPath, String pathWithinApplication,
boolean empty, boolean absolute, boolean trailingSlash) {
private void testPathSegment(String pathSegment, String semicolonContent,
String value, String valueDecoded, MultiValueMap<String, String> parameters) {
URI uri = URI.create("http://localhost:8080" + fullPath);
RequestPath requestPath = new DefaultRequestPath(uri, contextPath, UTF_8);
URI uri = URI.create("http://localhost:8080/" + pathSegment + semicolonContent);
PathSegment segment = new DefaultRequestPath(uri, "", StandardCharsets.UTF_8).pathSegments().get(0);
assertEquals(value, segment.value());
assertEquals(valueDecoded, segment.valueDecoded());
assertEquals(semicolonContent, segment.semicolonContent());
assertEquals(parameters, segment.parameters());
}
private void testPath(String input, String value, List<String> segments) {
URI uri = URI.create("http://localhost:8080" + input);
RequestPath path = new DefaultRequestPath(uri, "", StandardCharsets.UTF_8);
assertEquals(value, path.value());
assertEquals(segments, pathSegmentValues(path));
}
private static List<String> pathSegmentValues(PathSegmentContainer path) {
return path.pathSegments().stream().map(PathSegment::value).collect(Collectors.toList());
assertEquals(empty, requestPath.isEmpty());
assertEquals(absolute, requestPath.isAbsolute());
assertEquals(trailingSlash, requestPath.hasTrailingSlash());
assertEquals(contextPath.equals("/") ? "" : contextPath, requestPath.contextPath().value());
assertEquals(pathWithinApplication, requestPath.pathWithinApplication().value());
}
}