Polish use of LookupPath
This commit is contained in:
		
							parent
							
								
									90df7dd279
								
							
						
					
					
						commit
						a7020e419a
					
				|  | @ -80,7 +80,9 @@ public class UrlBasedCorsConfigurationSource implements CorsConfigurationSource | ||||||
| 
 | 
 | ||||||
| 	@Override | 	@Override | ||||||
| 	public CorsConfiguration getCorsConfiguration(ServerWebExchange exchange) { | 	public CorsConfiguration getCorsConfiguration(ServerWebExchange exchange) { | ||||||
| 		String lookupPath = exchange.<LookupPath>getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE).get().getPath(); | 		String lookupPath = exchange.<LookupPath>getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE) | ||||||
|  | 				.map(LookupPath::getPath) | ||||||
|  | 				.orElseThrow(() -> new IllegalStateException("No LookupPath attribute.")); | ||||||
| 		for (Map.Entry<String, CorsConfiguration> entry : this.corsConfigurations.entrySet()) { | 		for (Map.Entry<String, CorsConfiguration> entry : this.corsConfigurations.entrySet()) { | ||||||
| 			if (this.pathMatcher.match(entry.getKey(), lookupPath)) { | 			if (this.pathMatcher.match(entry.getKey(), lookupPath)) { | ||||||
| 				return entry.getValue(); | 				return entry.getValue(); | ||||||
|  |  | ||||||
|  | @ -29,18 +29,21 @@ public final class LookupPath { | ||||||
| 
 | 
 | ||||||
| 	public static final String LOOKUP_PATH_ATTRIBUTE = LookupPath.class.getName(); | 	public static final String LOOKUP_PATH_ATTRIBUTE = LookupPath.class.getName(); | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
| 	private final String path; | 	private final String path; | ||||||
| 
 | 
 | ||||||
| 	private final int fileExtStartIndex; | 	private final int fileExtStartIndex; | ||||||
| 
 | 
 | ||||||
| 	private final int fileExtEndIndex; | 	private final int fileExtEndIndex; | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
| 	public LookupPath(String path, int fileExtStartIndex, int fileExtEndIndex) { | 	public LookupPath(String path, int fileExtStartIndex, int fileExtEndIndex) { | ||||||
| 		this.path = path; | 		this.path = path; | ||||||
| 		this.fileExtStartIndex = fileExtStartIndex; | 		this.fileExtStartIndex = fileExtStartIndex; | ||||||
| 		this.fileExtEndIndex = fileExtEndIndex; | 		this.fileExtEndIndex = fileExtEndIndex; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
| 	public String getPath() { | 	public String getPath() { | ||||||
| 			return this.path; | 			return this.path; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | @ -41,7 +41,7 @@ public class UrlBasedCorsConfigurationSourceTests { | ||||||
| 	@Test | 	@Test | ||||||
| 	public void empty() { | 	public void empty() { | ||||||
| 		ServerWebExchange exchange = MockServerHttpRequest.get("/bar/test.html").toExchange(); | 		ServerWebExchange exchange = MockServerHttpRequest.get("/bar/test.html").toExchange(); | ||||||
| 		setLookupPathAttribute(exchange); | 		initLookupPath(exchange); | ||||||
| 		assertNull(this.configSource.getCorsConfiguration(exchange)); | 		assertNull(this.configSource.getCorsConfiguration(exchange)); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | @ -51,11 +51,11 @@ public class UrlBasedCorsConfigurationSourceTests { | ||||||
| 		this.configSource.registerCorsConfiguration("/bar/**", config); | 		this.configSource.registerCorsConfiguration("/bar/**", config); | ||||||
| 
 | 
 | ||||||
| 		ServerWebExchange exchange = MockServerHttpRequest.get("/foo/test.html").toExchange(); | 		ServerWebExchange exchange = MockServerHttpRequest.get("/foo/test.html").toExchange(); | ||||||
| 		setLookupPathAttribute(exchange); | 		initLookupPath(exchange); | ||||||
| 		assertNull(this.configSource.getCorsConfiguration(exchange)); | 		assertNull(this.configSource.getCorsConfiguration(exchange)); | ||||||
| 
 | 
 | ||||||
| 		exchange = MockServerHttpRequest.get("/bar/test.html").toExchange(); | 		exchange = MockServerHttpRequest.get("/bar/test.html").toExchange(); | ||||||
| 		setLookupPathAttribute(exchange); | 		initLookupPath(exchange); | ||||||
| 		assertEquals(config, this.configSource.getCorsConfiguration(exchange)); | 		assertEquals(config, this.configSource.getCorsConfiguration(exchange)); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | @ -64,10 +64,9 @@ public class UrlBasedCorsConfigurationSourceTests { | ||||||
| 		this.configSource.getCorsConfigurations().put("/**", new CorsConfiguration()); | 		this.configSource.getCorsConfigurations().put("/**", new CorsConfiguration()); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	public void setLookupPathAttribute(ServerWebExchange exchange) { | 	private void initLookupPath(ServerWebExchange exchange) { | ||||||
| 		HttpRequestPathHelper helper = new HttpRequestPathHelper(); |  | ||||||
| 		exchange.getAttributes().put(LookupPath.LOOKUP_PATH_ATTRIBUTE, | 		exchange.getAttributes().put(LookupPath.LOOKUP_PATH_ATTRIBUTE, | ||||||
| 				helper.getLookupPathForRequest(exchange)); | 				new HttpRequestPathHelper().getLookupPathForRequest(exchange)); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -175,12 +175,12 @@ public abstract class AbstractHandlerMapping extends ApplicationObjectSupport im | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	protected LookupPath getLookupPath(ServerWebExchange exchange) { | 	protected LookupPath getLookupPath(ServerWebExchange exchange) { | ||||||
| 		Optional<LookupPath> attribute = exchange.getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE); | 		return exchange.<LookupPath>getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE) | ||||||
| 		return attribute.orElseGet(() -> { | 				.orElseGet(() -> { | ||||||
| 			LookupPath lookupPath = createLookupPath(exchange); | 					LookupPath lookupPath = createLookupPath(exchange); | ||||||
| 			exchange.getAttributes().put(LookupPath.LOOKUP_PATH_ATTRIBUTE, lookupPath); | 					exchange.getAttributes().put(LookupPath.LOOKUP_PATH_ATTRIBUTE, lookupPath); | ||||||
| 			return lookupPath; | 					return lookupPath; | ||||||
| 		}); | 				}); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	protected LookupPath createLookupPath(ServerWebExchange exchange) { | 	protected LookupPath createLookupPath(ServerWebExchange exchange) { | ||||||
|  |  | ||||||
|  | @ -185,7 +185,9 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed | ||||||
| 	private int getLookupPathIndex(ServerWebExchange exchange) { | 	private int getLookupPathIndex(ServerWebExchange exchange) { | ||||||
| 		ServerHttpRequest request = exchange.getRequest(); | 		ServerHttpRequest request = exchange.getRequest(); | ||||||
| 		String requestPath = request.getURI().getPath(); | 		String requestPath = request.getURI().getPath(); | ||||||
| 		LookupPath lookupPath = getPathHelper().getLookupPathForRequest(exchange); | 		LookupPath lookupPath = exchange | ||||||
|  | 				.<LookupPath>getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE) | ||||||
|  | 				.orElseGet(() -> getPathHelper().getLookupPathForRequest(exchange)); | ||||||
| 		return requestPath.indexOf(lookupPath.getPath()); | 		return requestPath.indexOf(lookupPath.getPath()); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -29,8 +29,8 @@ import java.util.Set; | ||||||
| 
 | 
 | ||||||
| import org.springframework.util.PathMatcher; | import org.springframework.util.PathMatcher; | ||||||
| import org.springframework.util.StringUtils; | import org.springframework.util.StringUtils; | ||||||
| import org.springframework.web.server.support.LookupPath; |  | ||||||
| import org.springframework.web.server.ServerWebExchange; | import org.springframework.web.server.ServerWebExchange; | ||||||
|  | import org.springframework.web.server.support.LookupPath; | ||||||
| import org.springframework.web.util.pattern.ParsingPathMatcher; | import org.springframework.web.util.pattern.ParsingPathMatcher; | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  | @ -187,8 +187,7 @@ public final class PatternsRequestCondition extends AbstractRequestCondition<Pat | ||||||
| 			return this; | 			return this; | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		LookupPath lookupPath = exchange | 		LookupPath lookupPath = getLookupPath(exchange); | ||||||
| 				.<LookupPath>getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE).get(); |  | ||||||
| 		List<String> matches = getMatchingPatterns(lookupPath); | 		List<String> matches = getMatchingPatterns(lookupPath); | ||||||
| 
 | 
 | ||||||
| 		return matches.isEmpty() ? null : | 		return matches.isEmpty() ? null : | ||||||
|  | @ -196,6 +195,11 @@ public final class PatternsRequestCondition extends AbstractRequestCondition<Pat | ||||||
| 						this.useTrailingSlashMatch, this.fileExtensions); | 						this.useTrailingSlashMatch, this.fileExtensions); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	private LookupPath getLookupPath(ServerWebExchange exchange) { | ||||||
|  | 		return exchange.<LookupPath>getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE) | ||||||
|  | 				.orElseThrow(() -> new IllegalStateException("No LookupPath attribute.")); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	/** | 	/** | ||||||
| 	 * Find the patterns matching the given lookup path. Invoking this method should | 	 * Find the patterns matching the given lookup path. Invoking this method should | ||||||
| 	 * yield results equivalent to those of calling | 	 * yield results equivalent to those of calling | ||||||
|  | @ -259,8 +263,7 @@ public final class PatternsRequestCondition extends AbstractRequestCondition<Pat | ||||||
| 	 */ | 	 */ | ||||||
| 	@Override | 	@Override | ||||||
| 	public int compareTo(PatternsRequestCondition other, ServerWebExchange exchange) { | 	public int compareTo(PatternsRequestCondition other, ServerWebExchange exchange) { | ||||||
| 		LookupPath lookupPath = exchange | 		LookupPath lookupPath = getLookupPath(exchange); | ||||||
| 				.<LookupPath>getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE).get(); |  | ||||||
| 		Comparator<String> patternComparator = this.pathMatcher.getPatternComparator(lookupPath.getPath()); | 		Comparator<String> patternComparator = this.pathMatcher.getPatternComparator(lookupPath.getPath()); | ||||||
| 		Iterator<String> iterator = this.patterns.iterator(); | 		Iterator<String> iterator = this.patterns.iterator(); | ||||||
| 		Iterator<String> iteratorOther = other.patterns.iterator(); | 		Iterator<String> iteratorOther = other.patterns.iterator(); | ||||||
|  |  | ||||||
|  | @ -32,13 +32,11 @@ import org.springframework.web.bind.annotation.RequestMapping; | ||||||
| import org.springframework.web.bind.annotation.RequestMethod; | import org.springframework.web.bind.annotation.RequestMethod; | ||||||
| import org.springframework.web.cors.CorsConfiguration; | import org.springframework.web.cors.CorsConfiguration; | ||||||
| import org.springframework.web.method.HandlerMethod; | import org.springframework.web.method.HandlerMethod; | ||||||
| import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder; |  | ||||||
| import org.springframework.web.reactive.accept.RequestedContentTypeResolver; | import org.springframework.web.reactive.accept.RequestedContentTypeResolver; | ||||||
|  | import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder; | ||||||
| import org.springframework.web.reactive.result.condition.RequestCondition; | import org.springframework.web.reactive.result.condition.RequestCondition; | ||||||
| import org.springframework.web.reactive.result.method.RequestMappingInfo; | import org.springframework.web.reactive.result.method.RequestMappingInfo; | ||||||
| import org.springframework.web.reactive.result.method.RequestMappingInfoHandlerMapping; | import org.springframework.web.reactive.result.method.RequestMappingInfoHandlerMapping; | ||||||
| import org.springframework.web.server.support.LookupPath; |  | ||||||
| import org.springframework.web.server.ServerWebExchange; |  | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * An extension of {@link RequestMappingInfoHandlerMapping} that creates |  * An extension of {@link RequestMappingInfoHandlerMapping} that creates | ||||||
|  | @ -169,11 +167,6 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi | ||||||
| 				AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class)); | 				AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class)); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	@Override |  | ||||||
| 	protected LookupPath createLookupPath(ServerWebExchange exchange) { |  | ||||||
| 		return getPathHelper().getLookupPathForRequest(exchange); |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	/** | 	/** | ||||||
| 	 * Uses method and type-level @{@link RequestMapping} annotations to create | 	 * Uses method and type-level @{@link RequestMapping} annotations to create | ||||||
| 	 * the RequestMappingInfo. | 	 * the RequestMappingInfo. | ||||||
|  |  | ||||||
|  | @ -257,7 +257,9 @@ public class ViewResolutionResultHandler extends HandlerResultHandlerSupport | ||||||
| 	 * Use the request path the leading and trailing slash stripped. | 	 * Use the request path the leading and trailing slash stripped. | ||||||
| 	 */ | 	 */ | ||||||
| 	private String getDefaultViewName(ServerWebExchange exchange) { | 	private String getDefaultViewName(ServerWebExchange exchange) { | ||||||
| 		String path = exchange.<LookupPath>getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE).get().getPath(); | 		String path = exchange.<LookupPath>getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE) | ||||||
|  | 				.map(LookupPath::getPath) | ||||||
|  | 				.orElseThrow(() -> new IllegalStateException("No LookupPath attribute.")); | ||||||
| 		if (path.startsWith("/")) { | 		if (path.startsWith("/")) { | ||||||
| 			path = path.substring(1); | 			path = path.substring(1); | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
|  | @ -22,9 +22,9 @@ import java.util.Set; | ||||||
| import org.junit.Test; | import org.junit.Test; | ||||||
| 
 | 
 | ||||||
| import org.springframework.mock.http.server.reactive.test.MockServerWebExchange; | import org.springframework.mock.http.server.reactive.test.MockServerWebExchange; | ||||||
|  | import org.springframework.web.server.ServerWebExchange; | ||||||
| import org.springframework.web.server.support.HttpRequestPathHelper; | import org.springframework.web.server.support.HttpRequestPathHelper; | ||||||
| import org.springframework.web.server.support.LookupPath; | import org.springframework.web.server.support.LookupPath; | ||||||
| import org.springframework.web.server.ServerWebExchange; |  | ||||||
| 
 | 
 | ||||||
| import static org.junit.Assert.assertEquals; | import static org.junit.Assert.assertEquals; | ||||||
| import static org.junit.Assert.assertNotNull; | import static org.junit.Assert.assertNotNull; | ||||||
|  | @ -82,7 +82,7 @@ public class PatternsRequestConditionTests { | ||||||
| 	@Test | 	@Test | ||||||
| 	public void matchDirectPath() throws Exception { | 	public void matchDirectPath() throws Exception { | ||||||
| 		PatternsRequestCondition condition = new PatternsRequestCondition("/foo"); | 		PatternsRequestCondition condition = new PatternsRequestCondition("/foo"); | ||||||
| 		PatternsRequestCondition match = condition.getMatchingCondition(createExchange("/foo")); | 		PatternsRequestCondition match = condition.getMatchingCondition(initExchange("/foo")); | ||||||
| 
 | 
 | ||||||
| 		assertNotNull(match); | 		assertNotNull(match); | ||||||
| 	} | 	} | ||||||
|  | @ -90,7 +90,7 @@ public class PatternsRequestConditionTests { | ||||||
| 	@Test | 	@Test | ||||||
| 	public void matchPattern() throws Exception { | 	public void matchPattern() throws Exception { | ||||||
| 		PatternsRequestCondition condition = new PatternsRequestCondition("/foo/*"); | 		PatternsRequestCondition condition = new PatternsRequestCondition("/foo/*"); | ||||||
| 		PatternsRequestCondition match = condition.getMatchingCondition(createExchange("/foo/bar")); | 		PatternsRequestCondition match = condition.getMatchingCondition(initExchange("/foo/bar")); | ||||||
| 
 | 
 | ||||||
| 		assertNotNull(match); | 		assertNotNull(match); | ||||||
| 	} | 	} | ||||||
|  | @ -98,7 +98,7 @@ public class PatternsRequestConditionTests { | ||||||
| 	@Test | 	@Test | ||||||
| 	public void matchSortPatterns() throws Exception { | 	public void matchSortPatterns() throws Exception { | ||||||
| 		PatternsRequestCondition condition = new PatternsRequestCondition("/*/*", "/foo/bar", "/foo/*"); | 		PatternsRequestCondition condition = new PatternsRequestCondition("/*/*", "/foo/bar", "/foo/*"); | ||||||
| 		PatternsRequestCondition match = condition.getMatchingCondition(createExchange("/foo/bar")); | 		PatternsRequestCondition match = condition.getMatchingCondition(initExchange("/foo/bar")); | ||||||
| 		PatternsRequestCondition expected = new PatternsRequestCondition("/foo/bar", "/foo/*", "/*/*"); | 		PatternsRequestCondition expected = new PatternsRequestCondition("/foo/bar", "/foo/*", "/*/*"); | ||||||
| 
 | 
 | ||||||
| 		assertEquals(expected, match); | 		assertEquals(expected, match); | ||||||
|  | @ -106,7 +106,7 @@ public class PatternsRequestConditionTests { | ||||||
| 
 | 
 | ||||||
| 	@Test | 	@Test | ||||||
| 	public void matchSuffixPattern() throws Exception { | 	public void matchSuffixPattern() throws Exception { | ||||||
| 		ServerWebExchange exchange = createExchange("/foo.html"); | 		ServerWebExchange exchange = initExchange("/foo.html"); | ||||||
| 
 | 
 | ||||||
| 		PatternsRequestCondition condition = new PatternsRequestCondition("/{foo}"); | 		PatternsRequestCondition condition = new PatternsRequestCondition("/{foo}"); | ||||||
| 		PatternsRequestCondition match = condition.getMatchingCondition(exchange); | 		PatternsRequestCondition match = condition.getMatchingCondition(exchange); | ||||||
|  | @ -129,13 +129,13 @@ public class PatternsRequestConditionTests { | ||||||
| 		Set<String> extensions = Collections.singleton("json"); | 		Set<String> extensions = Collections.singleton("json"); | ||||||
| 		PatternsRequestCondition condition = new PatternsRequestCondition(patterns, null, true, false, extensions); | 		PatternsRequestCondition condition = new PatternsRequestCondition(patterns, null, true, false, extensions); | ||||||
| 
 | 
 | ||||||
| 		MockServerWebExchange exchange = createExchange("/jobs/my.job"); | 		MockServerWebExchange exchange = initExchange("/jobs/my.job"); | ||||||
| 		PatternsRequestCondition match = condition.getMatchingCondition(exchange); | 		PatternsRequestCondition match = condition.getMatchingCondition(exchange); | ||||||
| 
 | 
 | ||||||
| 		assertNotNull(match); | 		assertNotNull(match); | ||||||
| 		assertEquals("/jobs/{jobName}", match.getPatterns().iterator().next()); | 		assertEquals("/jobs/{jobName}", match.getPatterns().iterator().next()); | ||||||
| 
 | 
 | ||||||
| 		exchange = createExchange("/jobs/my.job.json"); | 		exchange = initExchange("/jobs/my.job.json"); | ||||||
| 		match = condition.getMatchingCondition(exchange); | 		match = condition.getMatchingCondition(exchange); | ||||||
| 
 | 
 | ||||||
| 		assertNotNull(match); | 		assertNotNull(match); | ||||||
|  | @ -152,7 +152,7 @@ public class PatternsRequestConditionTests { | ||||||
| 
 | 
 | ||||||
| 		PatternsRequestCondition combined = condition1.combine(condition2); | 		PatternsRequestCondition combined = condition1.combine(condition2); | ||||||
| 
 | 
 | ||||||
| 		MockServerWebExchange exchange = createExchange("/prefix/suffix.json"); | 		MockServerWebExchange exchange = initExchange("/prefix/suffix.json"); | ||||||
| 		PatternsRequestCondition match = combined.getMatchingCondition(exchange); | 		PatternsRequestCondition match = combined.getMatchingCondition(exchange); | ||||||
| 
 | 
 | ||||||
| 		assertNotNull(match); | 		assertNotNull(match); | ||||||
|  | @ -160,7 +160,7 @@ public class PatternsRequestConditionTests { | ||||||
| 
 | 
 | ||||||
| 	@Test | 	@Test | ||||||
| 	public void matchTrailingSlash() throws Exception { | 	public void matchTrailingSlash() throws Exception { | ||||||
| 		MockServerWebExchange exchange = createExchange("/foo/"); | 		MockServerWebExchange exchange = initExchange("/foo/"); | ||||||
| 
 | 
 | ||||||
| 		PatternsRequestCondition condition = new PatternsRequestCondition("/foo"); | 		PatternsRequestCondition condition = new PatternsRequestCondition("/foo"); | ||||||
| 		PatternsRequestCondition match = condition.getMatchingCondition(exchange); | 		PatternsRequestCondition match = condition.getMatchingCondition(exchange); | ||||||
|  | @ -175,7 +175,7 @@ public class PatternsRequestConditionTests { | ||||||
| 		assertEquals("Trailing slash should be insensitive to useSuffixPatternMatch settings (SPR-6164, SPR-5636)", | 		assertEquals("Trailing slash should be insensitive to useSuffixPatternMatch settings (SPR-6164, SPR-5636)", | ||||||
| 				"/foo/", match.getPatterns().iterator().next()); | 				"/foo/", match.getPatterns().iterator().next()); | ||||||
| 
 | 
 | ||||||
| 		exchange = createExchange("/foo/"); | 		exchange = initExchange("/foo/"); | ||||||
| 		condition = new PatternsRequestCondition(new String[] {"/foo"}, null, false, false, null); | 		condition = new PatternsRequestCondition(new String[] {"/foo"}, null, false, false, null); | ||||||
| 		match = condition.getMatchingCondition(exchange); | 		match = condition.getMatchingCondition(exchange); | ||||||
| 
 | 
 | ||||||
|  | @ -185,7 +185,7 @@ public class PatternsRequestConditionTests { | ||||||
| 	@Test | 	@Test | ||||||
| 	public void matchPatternContainsExtension() throws Exception { | 	public void matchPatternContainsExtension() throws Exception { | ||||||
| 		PatternsRequestCondition condition = new PatternsRequestCondition("/foo.jpg"); | 		PatternsRequestCondition condition = new PatternsRequestCondition("/foo.jpg"); | ||||||
| 		PatternsRequestCondition match = condition.getMatchingCondition(createExchange("/foo.html")); | 		PatternsRequestCondition match = condition.getMatchingCondition(initExchange("/foo.html")); | ||||||
| 
 | 
 | ||||||
| 		assertNull(match); | 		assertNull(match); | ||||||
| 	} | 	} | ||||||
|  | @ -195,7 +195,7 @@ public class PatternsRequestConditionTests { | ||||||
| 		PatternsRequestCondition c1 = new PatternsRequestCondition("/foo*"); | 		PatternsRequestCondition c1 = new PatternsRequestCondition("/foo*"); | ||||||
| 		PatternsRequestCondition c2 = new PatternsRequestCondition("/foo*"); | 		PatternsRequestCondition c2 = new PatternsRequestCondition("/foo*"); | ||||||
| 
 | 
 | ||||||
| 		assertEquals(0, c1.compareTo(c2, createExchange("/foo"))); | 		assertEquals(0, c1.compareTo(c2, initExchange("/foo"))); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	@Test | 	@Test | ||||||
|  | @ -203,12 +203,12 @@ public class PatternsRequestConditionTests { | ||||||
| 		PatternsRequestCondition c1 = new PatternsRequestCondition("/fo*"); | 		PatternsRequestCondition c1 = new PatternsRequestCondition("/fo*"); | ||||||
| 		PatternsRequestCondition c2 = new PatternsRequestCondition("/foo"); | 		PatternsRequestCondition c2 = new PatternsRequestCondition("/foo"); | ||||||
| 
 | 
 | ||||||
| 		assertEquals(1, c1.compareTo(c2, createExchange("/foo"))); | 		assertEquals(1, c1.compareTo(c2, initExchange("/foo"))); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	@Test | 	@Test | ||||||
| 	public void compareNumberOfMatchingPatterns() throws Exception { | 	public void compareNumberOfMatchingPatterns() throws Exception { | ||||||
| 		ServerWebExchange exchange = createExchange("/foo.html"); | 		ServerWebExchange exchange = initExchange("/foo.html"); | ||||||
| 
 | 
 | ||||||
| 		PatternsRequestCondition c1 = new PatternsRequestCondition("/foo", "*.jpeg"); | 		PatternsRequestCondition c1 = new PatternsRequestCondition("/foo", "*.jpeg"); | ||||||
| 		PatternsRequestCondition c2 = new PatternsRequestCondition("/foo", "*.html"); | 		PatternsRequestCondition c2 = new PatternsRequestCondition("/foo", "*.html"); | ||||||
|  | @ -220,10 +220,10 @@ public class PatternsRequestConditionTests { | ||||||
| 		assertEquals(1, match1.compareTo(match2, exchange)); | 		assertEquals(1, match1.compareTo(match2, exchange)); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	private MockServerWebExchange createExchange(String path) { | 	private MockServerWebExchange initExchange(String path) { | ||||||
| 		MockServerWebExchange exchange = get(path).toExchange(); | 		MockServerWebExchange exchange = get(path).toExchange(); | ||||||
| 		HttpRequestPathHelper helper = new HttpRequestPathHelper(); | 		exchange.getAttributes().put(LookupPath.LOOKUP_PATH_ATTRIBUTE, | ||||||
| 		exchange.getAttributes().put(LookupPath.LOOKUP_PATH_ATTRIBUTE, helper.getLookupPathForRequest(exchange)); | 				new HttpRequestPathHelper().getLookupPathForRequest(exchange)); | ||||||
| 		return exchange; | 		return exchange; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -67,7 +67,7 @@ public class RequestMappingInfoTests { | ||||||
| 	@Test | 	@Test | ||||||
| 	public void matchPatternsCondition() { | 	public void matchPatternsCondition() { | ||||||
| 		MockServerWebExchange exchange = MockServerHttpRequest.get("/foo").toExchange(); | 		MockServerWebExchange exchange = MockServerHttpRequest.get("/foo").toExchange(); | ||||||
| 		setLookupPathAttribute(exchange); | 		initLookupPath(exchange); | ||||||
| 
 | 
 | ||||||
| 		RequestMappingInfo info = paths("/foo*", "/bar").build(); | 		RequestMappingInfo info = paths("/foo*", "/bar").build(); | ||||||
| 		RequestMappingInfo expected = paths("/foo*").build(); | 		RequestMappingInfo expected = paths("/foo*").build(); | ||||||
|  | @ -83,7 +83,7 @@ public class RequestMappingInfoTests { | ||||||
| 	@Test | 	@Test | ||||||
| 	public void matchParamsCondition() { | 	public void matchParamsCondition() { | ||||||
| 		ServerWebExchange exchange = MockServerHttpRequest.get("/foo?foo=bar").toExchange(); | 		ServerWebExchange exchange = MockServerHttpRequest.get("/foo?foo=bar").toExchange(); | ||||||
| 		setLookupPathAttribute(exchange); | 		initLookupPath(exchange); | ||||||
| 
 | 
 | ||||||
| 		RequestMappingInfo info = paths("/foo").params("foo=bar").build(); | 		RequestMappingInfo info = paths("/foo").params("foo=bar").build(); | ||||||
| 		RequestMappingInfo match = info.getMatchingCondition(exchange); | 		RequestMappingInfo match = info.getMatchingCondition(exchange); | ||||||
|  | @ -99,7 +99,7 @@ public class RequestMappingInfoTests { | ||||||
| 	@Test | 	@Test | ||||||
| 	public void matchHeadersCondition() { | 	public void matchHeadersCondition() { | ||||||
| 		ServerWebExchange exchange = MockServerHttpRequest.get("/foo").header("foo", "bar").toExchange(); | 		ServerWebExchange exchange = MockServerHttpRequest.get("/foo").header("foo", "bar").toExchange(); | ||||||
| 		setLookupPathAttribute(exchange); | 		initLookupPath(exchange); | ||||||
| 
 | 
 | ||||||
| 		RequestMappingInfo info = paths("/foo").headers("foo=bar").build(); | 		RequestMappingInfo info = paths("/foo").headers("foo=bar").build(); | ||||||
| 		RequestMappingInfo match = info.getMatchingCondition(exchange); | 		RequestMappingInfo match = info.getMatchingCondition(exchange); | ||||||
|  | @ -115,7 +115,7 @@ public class RequestMappingInfoTests { | ||||||
| 	@Test | 	@Test | ||||||
| 	public void matchConsumesCondition() { | 	public void matchConsumesCondition() { | ||||||
| 		ServerWebExchange exchange = MockServerHttpRequest.post("/foo").contentType(MediaType.TEXT_PLAIN).toExchange(); | 		ServerWebExchange exchange = MockServerHttpRequest.post("/foo").contentType(MediaType.TEXT_PLAIN).toExchange(); | ||||||
| 		setLookupPathAttribute(exchange); | 		initLookupPath(exchange); | ||||||
| 
 | 
 | ||||||
| 		RequestMappingInfo info = paths("/foo").consumes("text/plain").build(); | 		RequestMappingInfo info = paths("/foo").consumes("text/plain").build(); | ||||||
| 		RequestMappingInfo match = info.getMatchingCondition(exchange); | 		RequestMappingInfo match = info.getMatchingCondition(exchange); | ||||||
|  | @ -131,7 +131,7 @@ public class RequestMappingInfoTests { | ||||||
| 	@Test | 	@Test | ||||||
| 	public void matchProducesCondition() { | 	public void matchProducesCondition() { | ||||||
| 		ServerWebExchange exchange = MockServerHttpRequest.get("/foo").accept(MediaType.TEXT_PLAIN).toExchange(); | 		ServerWebExchange exchange = MockServerHttpRequest.get("/foo").accept(MediaType.TEXT_PLAIN).toExchange(); | ||||||
| 		setLookupPathAttribute(exchange); | 		initLookupPath(exchange); | ||||||
| 
 | 
 | ||||||
| 		RequestMappingInfo info = paths("/foo").produces("text/plain").build(); | 		RequestMappingInfo info = paths("/foo").produces("text/plain").build(); | ||||||
| 		RequestMappingInfo match = info.getMatchingCondition(exchange); | 		RequestMappingInfo match = info.getMatchingCondition(exchange); | ||||||
|  | @ -147,7 +147,7 @@ public class RequestMappingInfoTests { | ||||||
| 	@Test | 	@Test | ||||||
| 	public void matchCustomCondition() { | 	public void matchCustomCondition() { | ||||||
| 		ServerWebExchange exchange = MockServerHttpRequest.get("/foo?foo=bar").toExchange(); | 		ServerWebExchange exchange = MockServerHttpRequest.get("/foo?foo=bar").toExchange(); | ||||||
| 		setLookupPathAttribute(exchange); | 		initLookupPath(exchange); | ||||||
| 		 | 		 | ||||||
| 		RequestMappingInfo info = paths("/foo").params("foo=bar").build(); | 		RequestMappingInfo info = paths("/foo").params("foo=bar").build(); | ||||||
| 		RequestMappingInfo match = info.getMatchingCondition(exchange); | 		RequestMappingInfo match = info.getMatchingCondition(exchange); | ||||||
|  | @ -169,7 +169,7 @@ public class RequestMappingInfoTests { | ||||||
| 		RequestMappingInfo oneMethodOneParam = paths().methods(RequestMethod.GET).params("foo").build(); | 		RequestMappingInfo oneMethodOneParam = paths().methods(RequestMethod.GET).params("foo").build(); | ||||||
| 
 | 
 | ||||||
| 		ServerWebExchange exchange = MockServerHttpRequest.get("/foo").toExchange(); | 		ServerWebExchange exchange = MockServerHttpRequest.get("/foo").toExchange(); | ||||||
| 		setLookupPathAttribute(exchange); | 		initLookupPath(exchange); | ||||||
| 		Comparator<RequestMappingInfo> comparator = (info, otherInfo) -> info.compareTo(otherInfo, exchange); | 		Comparator<RequestMappingInfo> comparator = (info, otherInfo) -> info.compareTo(otherInfo, exchange); | ||||||
| 
 | 
 | ||||||
| 		List<RequestMappingInfo> list = asList(none, oneMethod, oneMethodOneParam); | 		List<RequestMappingInfo> list = asList(none, oneMethod, oneMethodOneParam); | ||||||
|  | @ -279,9 +279,9 @@ public class RequestMappingInfoTests { | ||||||
| 		assertNull("Pre-flight should match the ACCESS_CONTROL_REQUEST_METHOD", match); | 		assertNull("Pre-flight should match the ACCESS_CONTROL_REQUEST_METHOD", match); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	public void setLookupPathAttribute(ServerWebExchange exchange) { | 	private void initLookupPath(ServerWebExchange exchange) { | ||||||
| 		HttpRequestPathHelper helper = new HttpRequestPathHelper(); | 		exchange.getAttributes().put(LookupPath.LOOKUP_PATH_ATTRIBUTE, | ||||||
| 		exchange.getAttributes().put(LookupPath.LOOKUP_PATH_ATTRIBUTE, helper.getLookupPathForRequest(exchange)); | 				new HttpRequestPathHelper().getLookupPathForRequest(exchange)); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -219,17 +219,17 @@ public class ViewResolutionResultHandlerTests { | ||||||
| 		ViewResolutionResultHandler handler = resultHandler(new TestViewResolver("account")); | 		ViewResolutionResultHandler handler = resultHandler(new TestViewResolver("account")); | ||||||
| 
 | 
 | ||||||
| 		MockServerWebExchange exchange = get("/account").toExchange(); | 		MockServerWebExchange exchange = get("/account").toExchange(); | ||||||
| 		addLookupPathAttribute(exchange); | 		initLookupPath(exchange); | ||||||
| 		handler.handleResult(exchange, result).block(Duration.ofMillis(5000)); | 		handler.handleResult(exchange, result).block(Duration.ofMillis(5000)); | ||||||
| 		assertResponseBody(exchange, "account: {id=123}"); | 		assertResponseBody(exchange, "account: {id=123}"); | ||||||
| 
 | 
 | ||||||
| 		exchange = get("/account/").toExchange(); | 		exchange = get("/account/").toExchange(); | ||||||
| 		addLookupPathAttribute(exchange); | 		initLookupPath(exchange); | ||||||
| 		handler.handleResult(exchange, result).block(Duration.ofMillis(5000)); | 		handler.handleResult(exchange, result).block(Duration.ofMillis(5000)); | ||||||
| 		assertResponseBody(exchange, "account: {id=123}"); | 		assertResponseBody(exchange, "account: {id=123}"); | ||||||
| 
 | 
 | ||||||
| 		exchange = get("/account.123").toExchange(); | 		exchange = get("/account.123").toExchange(); | ||||||
| 		addLookupPathAttribute(exchange); | 		initLookupPath(exchange); | ||||||
| 		handler.handleResult(exchange, result).block(Duration.ofMillis(5000)); | 		handler.handleResult(exchange, result).block(Duration.ofMillis(5000)); | ||||||
| 		assertResponseBody(exchange, "account: {id=123}"); | 		assertResponseBody(exchange, "account: {id=123}"); | ||||||
| 	} | 	} | ||||||
|  | @ -256,7 +256,7 @@ public class ViewResolutionResultHandlerTests { | ||||||
| 		HandlerResult handlerResult = new HandlerResult(new Object(), value, returnType, this.bindingContext); | 		HandlerResult handlerResult = new HandlerResult(new Object(), value, returnType, this.bindingContext); | ||||||
| 
 | 
 | ||||||
| 		MockServerWebExchange exchange = get("/account").accept(APPLICATION_JSON).toExchange(); | 		MockServerWebExchange exchange = get("/account").accept(APPLICATION_JSON).toExchange(); | ||||||
| 		addLookupPathAttribute(exchange); | 		initLookupPath(exchange); | ||||||
| 		 | 		 | ||||||
| 		TestView defaultView = new TestView("jsonView", APPLICATION_JSON); | 		TestView defaultView = new TestView("jsonView", APPLICATION_JSON); | ||||||
| 
 | 
 | ||||||
|  | @ -307,9 +307,9 @@ public class ViewResolutionResultHandlerTests { | ||||||
| 		assertEquals("/", response.getHeaders().getLocation().toString()); | 		assertEquals("/", response.getHeaders().getLocation().toString()); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	private void addLookupPathAttribute(ServerWebExchange exchange) { | 	private void initLookupPath(ServerWebExchange exchange) { | ||||||
| 		HttpRequestPathHelper helper = new HttpRequestPathHelper(); | 		exchange.getAttributes().put(LookupPath.LOOKUP_PATH_ATTRIBUTE, | ||||||
| 		exchange.getAttributes().put(LookupPath.LOOKUP_PATH_ATTRIBUTE, helper.getLookupPathForRequest(exchange)); | 				new HttpRequestPathHelper().getLookupPathForRequest(exchange)); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | @ -333,7 +333,7 @@ public class ViewResolutionResultHandlerTests { | ||||||
| 		model.addAttribute("id", "123"); | 		model.addAttribute("id", "123"); | ||||||
| 		HandlerResult result = new HandlerResult(new Object(), returnValue, returnType, this.bindingContext); | 		HandlerResult result = new HandlerResult(new Object(), returnValue, returnType, this.bindingContext); | ||||||
| 		MockServerWebExchange exchange = get(path).toExchange(); | 		MockServerWebExchange exchange = get(path).toExchange(); | ||||||
| 		addLookupPathAttribute(exchange); | 		initLookupPath(exchange); | ||||||
| 		resultHandler(resolvers).handleResult(exchange, result).block(Duration.ofSeconds(5)); | 		resultHandler(resolvers).handleResult(exchange, result).block(Duration.ofSeconds(5)); | ||||||
| 		assertResponseBody(exchange, responseBody); | 		assertResponseBody(exchange, responseBody); | ||||||
| 		return exchange; | 		return exchange; | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue