use of org.springframework.web.util.pattern.PathPattern in project spring-framework by spring-projects.
the class RouterFunctionMappingTests method mappedRequestShouldHoldAttributes.
@Test
void mappedRequestShouldHoldAttributes() {
HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().build();
RouterFunction<ServerResponse> routerFunction = RouterFunctions.route().GET("/match", handlerFunction).build();
RouterFunctionMapping mapping = new RouterFunctionMapping(routerFunction);
mapping.setMessageReaders(this.codecConfigurer.getReaders());
ServerWebExchange exchange = createExchange("https://example.com/match");
Mono<Object> result = mapping.getHandler(exchange);
StepVerifier.create(result).expectNext(handlerFunction).expectComplete().verify();
PathPattern matchingPattern = exchange.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
assertThat(matchingPattern).isNotNull();
assertThat(matchingPattern.getPatternString()).isEqualTo("/match");
ServerRequest serverRequest = exchange.getAttribute(RouterFunctions.REQUEST_ATTRIBUTE);
assertThat(serverRequest).isNotNull();
HandlerFunction<?> handler = exchange.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE);
assertThat(handler).isEqualTo(handlerFunction);
}
use of org.springframework.web.util.pattern.PathPattern in project spring-framework by spring-projects.
the class RequestMappingInfoTests method createEmpty.
// TODO: CORS pre-flight (see @Disabled)
@Test
public void createEmpty() {
RequestMappingInfo info = paths().build();
PathPattern emptyPattern = (new PathPatternParser()).parse("");
assertThat(info.getPatternsCondition().getPatterns()).isEqualTo(Collections.singleton(emptyPattern));
assertThat(info.getMethodsCondition().getMethods().size()).isEqualTo(0);
assertThat(info.getConsumesCondition().isEmpty()).isTrue();
assertThat(info.getProducesCondition().isEmpty()).isTrue();
assertThat(info.getParamsCondition()).isNotNull();
assertThat(info.getHeadersCondition()).isNotNull();
assertThat(info.getCustomCondition()).isNull();
RequestMappingInfo anotherInfo = paths().build();
assertThat(info.getPatternsCondition()).isSameAs(anotherInfo.getPatternsCondition());
assertThat(info.getMethodsCondition()).isSameAs(anotherInfo.getMethodsCondition());
assertThat(info.getParamsCondition()).isSameAs(anotherInfo.getParamsCondition());
assertThat(info.getHeadersCondition()).isSameAs(anotherInfo.getHeadersCondition());
assertThat(info.getConsumesCondition()).isSameAs(anotherInfo.getConsumesCondition());
assertThat(info.getProducesCondition()).isSameAs(anotherInfo.getProducesCondition());
assertThat(info.getCustomCondition()).isSameAs(anotherInfo.getCustomCondition());
RequestMappingInfo result = info.combine(anotherInfo);
assertThat(info.getPatternsCondition()).isSameAs(result.getPatternsCondition());
assertThat(info.getMethodsCondition()).isSameAs(result.getMethodsCondition());
assertThat(info.getParamsCondition()).isSameAs(result.getParamsCondition());
assertThat(info.getHeadersCondition()).isSameAs(result.getHeadersCondition());
assertThat(info.getConsumesCondition()).isSameAs(result.getConsumesCondition());
assertThat(info.getProducesCondition()).isSameAs(result.getProducesCondition());
assertThat(info.getCustomCondition()).isSameAs(result.getCustomCondition());
}
use of org.springframework.web.util.pattern.PathPattern in project spring-framework by spring-projects.
the class RouterFunctionMapping method setAttributes.
@SuppressWarnings("unchecked")
private void setAttributes(Map<String, Object> attributes, ServerRequest serverRequest, HandlerFunction<?> handlerFunction) {
attributes.put(RouterFunctions.REQUEST_ATTRIBUTE, serverRequest);
attributes.put(BEST_MATCHING_HANDLER_ATTRIBUTE, handlerFunction);
PathPattern matchingPattern = (PathPattern) attributes.get(RouterFunctions.MATCHING_PATTERN_ATTRIBUTE);
if (matchingPattern != null) {
attributes.put(BEST_MATCHING_PATTERN_ATTRIBUTE, matchingPattern);
}
Map<String, String> uriVariables = (Map<String, String>) attributes.get(RouterFunctions.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
if (uriVariables != null) {
attributes.put(URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriVariables);
}
}
use of org.springframework.web.util.pattern.PathPattern in project spring-framework by spring-projects.
the class RequestMappingInfoHandlerMappingTests method handleMatchBestMatchingPatternAttribute.
@Test
public void handleMatchBestMatchingPatternAttribute() {
RequestMappingInfo key = paths("/{path1}/2", "/**").build();
ServerWebExchange exchange = MockServerWebExchange.from(get("/1/2"));
this.handlerMapping.handleMatch(key, handlerMethod, exchange);
PathPattern bestMatch = (PathPattern) exchange.getAttributes().get(BEST_MATCHING_PATTERN_ATTRIBUTE);
assertThat(bestMatch.getPatternString()).isEqualTo("/{path1}/2");
HandlerMethod mapped = (HandlerMethod) exchange.getAttributes().get(BEST_MATCHING_HANDLER_ATTRIBUTE);
assertThat(mapped).isSameAs(handlerMethod);
}
use of org.springframework.web.util.pattern.PathPattern in project spring-framework by spring-projects.
the class RouterFunctionMapping method setAttributes.
private void setAttributes(HttpServletRequest servletRequest, ServerRequest request, @Nullable HandlerFunction<?> handlerFunction) {
PathPattern matchingPattern = (PathPattern) servletRequest.getAttribute(RouterFunctions.MATCHING_PATTERN_ATTRIBUTE);
if (matchingPattern != null) {
servletRequest.removeAttribute(RouterFunctions.MATCHING_PATTERN_ATTRIBUTE);
servletRequest.setAttribute(BEST_MATCHING_PATTERN_ATTRIBUTE, matchingPattern.getPatternString());
}
servletRequest.setAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE, handlerFunction);
servletRequest.setAttribute(RouterFunctions.REQUEST_ATTRIBUTE, request);
}
Aggregations