Add PathPatternRegistry
This commit adds the new `PathPatternRegistry`, which holds a sorted set of `PathPattern`s and allows for searching/adding patterns This registry is being used in `HandlerMapping` implementations and separates path pattern parsing/matching logic from the rest. Directly using `PathPattern` instances should improve the performance of those `HandlerMapping` implementations, since the parsing and generation of pattern variants (trailing slash, suffix patterns, etc) is done only once. Issue: SPR-14544
This commit is contained in:
@@ -25,9 +25,11 @@ import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse
|
||||
import org.springframework.web.cors.CorsConfiguration;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.adapter.DefaultServerWebExchange;
|
||||
import org.springframework.web.util.patterns.PathPattern;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link UrlBasedCorsConfigurationSource}.
|
||||
@@ -60,7 +62,7 @@ public class UrlBasedCorsConfigurationSourceTests {
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void unmodifiableConfigurationsMap() {
|
||||
this.configSource.getCorsConfigurations().put("/**", new CorsConfiguration());
|
||||
this.configSource.getCorsConfigurations().put(mock(PathPattern.class), new CorsConfiguration());
|
||||
}
|
||||
|
||||
private ServerWebExchange createExchange(HttpMethod httpMethod, String url) {
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
/*
|
||||
* Copyright 2002-2017 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.util.patterns;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link PathPatternRegistry}
|
||||
* @author Brian Clozel
|
||||
*/
|
||||
public class PathPatternRegistryTests {
|
||||
|
||||
private PathPatternRegistry registry;
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
this.registry = new PathPatternRegistry();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotRegisterInvalidPatterns() {
|
||||
this.thrown.expect(PatternParseException.class);
|
||||
this.thrown.expectMessage(Matchers.containsString("Expected close capture character after variable name"));
|
||||
this.registry.register("/{invalid");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotRegisterPatternVariants() {
|
||||
List<PathPattern> patterns = this.registry.register("/foo/{bar}");
|
||||
assertPathPatternListContains(patterns, "/foo/{bar}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRegisterTrailingSlashVariants() {
|
||||
this.registry.setUseTrailingSlashMatch(true);
|
||||
List<PathPattern> patterns = this.registry.register("/foo/{bar}");
|
||||
assertPathPatternListContains(patterns, "/foo/{bar}", "/foo/{bar}/");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRegisterSuffixVariants() {
|
||||
this.registry.setUseSuffixPatternMatch(true);
|
||||
List<PathPattern> patterns = this.registry.register("/foo/{bar}");
|
||||
assertPathPatternListContains(patterns, "/foo/{bar}", "/foo/{bar}.*");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRegisterExtensionsVariants() {
|
||||
Set<String> fileExtensions = new HashSet<>();
|
||||
fileExtensions.add("json");
|
||||
fileExtensions.add("xml");
|
||||
this.registry.setUseSuffixPatternMatch(true);
|
||||
this.registry.setFileExtensions(fileExtensions);
|
||||
List<PathPattern> patterns = this.registry.register("/foo/{bar}");
|
||||
assertPathPatternListContains(patterns, "/foo/{bar}", "/foo/{bar}.xml", "/foo/{bar}.json");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRegisterAllVariants() {
|
||||
Set<String> fileExtensions = new HashSet<>();
|
||||
fileExtensions.add("json");
|
||||
fileExtensions.add("xml");
|
||||
this.registry.setUseSuffixPatternMatch(true);
|
||||
this.registry.setUseTrailingSlashMatch(true);
|
||||
this.registry.setFileExtensions(fileExtensions);
|
||||
List<PathPattern> patterns = this.registry.register("/foo/{bar}");
|
||||
assertPathPatternListContains(patterns, "/foo/{bar}",
|
||||
"/foo/{bar}.xml", "/foo/{bar}.json", "/foo/{bar}/");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void combineEmptyRegistries() {
|
||||
PathPatternRegistry result = this.registry.combine(new PathPatternRegistry());
|
||||
assertPathPatternListContains(result.getPatterns(), "");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void combineWithEmptyRegistry() {
|
||||
this.registry.register("/foo");
|
||||
PathPatternRegistry result = this.registry.combine(new PathPatternRegistry());
|
||||
assertPathPatternListContains(result.getPatterns(), "/foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void combineRegistries() {
|
||||
this.registry.register("/foo");
|
||||
PathPatternRegistry other = new PathPatternRegistry();
|
||||
other.register("/bar");
|
||||
other.register("/baz");
|
||||
PathPatternRegistry result = this.registry.combine(other);
|
||||
assertPathPatternListContains(result.getPatterns(), "/foo/bar", "/foo/baz");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void registerPatternsWithSameSpecificity() {
|
||||
PathPattern fooOne = this.registry.parsePattern("/fo?");
|
||||
PathPattern fooTwo = this.registry.parsePattern("/f?o");
|
||||
assertThat(fooOne.compareTo(fooTwo), is(0));
|
||||
|
||||
this.registry.add(fooOne);
|
||||
this.registry.add(fooTwo);
|
||||
Set<PathPattern> matches = this.registry.findMatches("/foo");
|
||||
assertPathPatternListContains(matches, "/f?o", "/fo?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findNoMatch() {
|
||||
this.registry.register("/foo/{bar}");
|
||||
assertThat(this.registry.findMatches("/other"), hasSize(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void orderMatchesBySpecificity() {
|
||||
this.registry.register("/foo/{*baz}");
|
||||
this.registry.register("/foo/bar/baz");
|
||||
this.registry.register("/foo/bar/{baz}");
|
||||
Set<PathPattern> matches = this.registry.findMatches("/foo/bar/baz");
|
||||
assertPathPatternListContains(matches, "/foo/bar/baz", "/foo/bar/{baz}",
|
||||
"/foo/{*baz}");
|
||||
}
|
||||
|
||||
|
||||
private void assertPathPatternListContains(Collection<PathPattern> parsedPatterns, String... pathPatterns) {
|
||||
List<String> patternList = parsedPatterns.
|
||||
stream().map(pattern -> pattern.getPatternString()).collect(Collectors.toList());
|
||||
assertThat(patternList, Matchers.contains(pathPatterns));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user