Search in sources :

Example 1 with PathPattern

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);
}
Also used : ServerResponse(org.springframework.web.reactive.function.server.ServerResponse) StepVerifier(reactor.test.StepVerifier) RouterFunctions(org.springframework.web.reactive.function.server.RouterFunctions) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Mono(reactor.core.publisher.Mono) ServerCodecConfigurer(org.springframework.http.codec.ServerCodecConfigurer) ServerRequest(org.springframework.web.reactive.function.server.ServerRequest) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.jupiter.api.Test) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) RouterFunction(org.springframework.web.reactive.function.server.RouterFunction) ServerResponse(org.springframework.web.reactive.function.server.ServerResponse) PathPattern(org.springframework.web.util.pattern.PathPattern) HandlerFunction(org.springframework.web.reactive.function.server.HandlerFunction) HandlerMapping(org.springframework.web.reactive.HandlerMapping) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) PathPattern(org.springframework.web.util.pattern.PathPattern) ServerRequest(org.springframework.web.reactive.function.server.ServerRequest) Test(org.junit.jupiter.api.Test)

Example 2 with PathPattern

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());
}
Also used : PathPattern(org.springframework.web.util.pattern.PathPattern) RequestMappingInfo(org.springframework.web.reactive.result.method.RequestMappingInfo) PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) Test(org.junit.jupiter.api.Test)

Example 3 with PathPattern

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);
    }
}
Also used : PathPattern(org.springframework.web.util.pattern.PathPattern) Map(java.util.Map)

Example 4 with PathPattern

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);
}
Also used : MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) PathPattern(org.springframework.web.util.pattern.PathPattern) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Example 5 with PathPattern

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);
}
Also used : PathPattern(org.springframework.web.util.pattern.PathPattern)

Aggregations

PathPattern (org.springframework.web.util.pattern.PathPattern)19 PathContainer (org.springframework.http.server.PathContainer)6 Test (org.junit.jupiter.api.Test)5 Nullable (org.springframework.lang.Nullable)4 RequestMappingInfo (org.springframework.web.reactive.result.method.RequestMappingInfo)3 ServerWebExchange (org.springframework.web.server.ServerWebExchange)3 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)3 PathPatternParser (org.springframework.web.util.pattern.PathPatternParser)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 ServiceBrokerApiVersionErrorMessage (org.springframework.cloud.servicebroker.exception.ServiceBrokerApiVersionErrorMessage)1 ErrorMessage (org.springframework.cloud.servicebroker.model.error.ErrorMessage)1 DataBuffer (org.springframework.core.io.buffer.DataBuffer)1 HttpStatus (org.springframework.http.HttpStatus)1 MediaType (org.springframework.http.MediaType)1 ServerCodecConfigurer (org.springframework.http.codec.ServerCodecConfigurer)1