Search in sources :

Example 1 with PathPatternParser

use of org.springframework.web.util.pattern.PathPatternParser in project spring-framework by spring-projects.

the class RequestPredicatesTests method pathPredicates.

@Test
public void pathPredicates() {
    PathPatternParser parser = new PathPatternParser();
    parser.setCaseSensitive(false);
    Function<String, RequestPredicate> pathPredicates = RequestPredicates.pathPredicates(parser);
    RequestPredicate predicate = pathPredicates.apply("/P*");
    MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com/path").build();
    ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
    assertThat(predicate.test(request)).isTrue();
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) Test(org.junit.jupiter.api.Test)

Example 2 with PathPatternParser

use of org.springframework.web.util.pattern.PathPatternParser in project spring-framework by spring-projects.

the class RequestMappingInfoTests method pathPatternsArguments.

@SuppressWarnings("unused")
static Stream<RequestMappingInfo.Builder> pathPatternsArguments() {
    RequestMappingInfo.BuilderConfiguration config = new RequestMappingInfo.BuilderConfiguration();
    config.setPatternParser(new PathPatternParser());
    return Stream.of(RequestMappingInfo.paths().options(config), RequestMappingInfo.paths());
}
Also used : PathPatternParser(org.springframework.web.util.pattern.PathPatternParser)

Example 3 with PathPatternParser

use of org.springframework.web.util.pattern.PathPatternParser in project spring-framework by spring-projects.

the class RequestPredicatesTests method pathPredicates.

@Test
void pathPredicates() {
    PathPatternParser parser = new PathPatternParser();
    parser.setCaseSensitive(false);
    Function<String, RequestPredicate> pathPredicates = RequestPredicates.pathPredicates(parser);
    RequestPredicate predicate = pathPredicates.apply("/P*");
    assertThat(predicate.test(initRequest("GET", "/path"))).isTrue();
}
Also used : PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) Test(org.junit.jupiter.api.Test)

Example 4 with PathPatternParser

use of org.springframework.web.util.pattern.PathPatternParser 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 5 with PathPatternParser

use of org.springframework.web.util.pattern.PathPatternParser in project spring-framework by spring-projects.

the class PatternsRequestConditionTests method matchTrailingSlash.

@Test
public void matchTrailingSlash() {
    MockServerWebExchange exchange = MockServerWebExchange.from(get("/foo/"));
    PatternsRequestCondition condition = createPatternsCondition("/foo");
    PatternsRequestCondition match = condition.getMatchingCondition(exchange);
    assertThat(match).isNotNull();
    assertThat(match.getPatterns().iterator().next().getPatternString()).as("Should match by default").isEqualTo("/foo");
    condition = createPatternsCondition("/foo");
    match = condition.getMatchingCondition(exchange);
    assertThat(match).isNotNull();
    assertThat(match.getPatterns().iterator().next().getPatternString()).as("Trailing slash should be insensitive to useSuffixPatternMatch settings (SPR-6164, SPR-5636)").isEqualTo("/foo");
    PathPatternParser parser = new PathPatternParser();
    parser.setMatchOptionalTrailingSeparator(false);
    condition = new PatternsRequestCondition(parser.parse("/foo"));
    match = condition.getMatchingCondition(MockServerWebExchange.from(get("/foo/")));
    assertThat(match).isNull();
}
Also used : PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Aggregations

PathPatternParser (org.springframework.web.util.pattern.PathPatternParser)22 Test (org.junit.jupiter.api.Test)13 RequestMappingInfo (org.springframework.web.reactive.result.method.RequestMappingInfo)4 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)3 Field (java.lang.reflect.Field)2 Collections (java.util.Collections)2 List (java.util.List)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 ApplicationContext (org.springframework.context.ApplicationContext)2 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)2 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)2 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)2 PathPattern (org.springframework.web.util.pattern.PathPattern)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Tag (io.micrometer.core.instrument.Tag)1 Method (java.lang.reflect.Method)1 Optional (java.util.Optional)1 Properties (java.util.Properties)1 BiConsumer (java.util.function.BiConsumer)1