Store PathPattern instead of String in attributes
This commit changes the attributes stored under RouterFunctions.MATCHING_PATTERN_ATTRIBUTE and HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE from a String to a PathPattern, similar to what annotated controllers set. Issue: SPR-17395
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.web.reactive.function.server;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
@@ -36,12 +37,15 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.reactive.DispatcherHandler;
|
||||
import org.springframework.web.reactive.HandlerMapping;
|
||||
import org.springframework.web.reactive.config.EnableWebFlux;
|
||||
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
|
||||
import org.springframework.web.util.pattern.PathPattern;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.web.reactive.function.BodyInserters.*;
|
||||
import static org.springframework.web.reactive.function.server.RouterFunctions.*;
|
||||
import static org.springframework.web.reactive.function.BodyInserters.fromPublisher;
|
||||
import static org.springframework.web.reactive.function.server.RouterFunctions.nest;
|
||||
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
|
||||
|
||||
/**
|
||||
* Tests the use of {@link HandlerFunction} and {@link RouterFunction} in a
|
||||
@@ -101,6 +105,15 @@ public class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegr
|
||||
assertEquals("John", result.getBody().getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void attributes() {
|
||||
ResponseEntity<String> result =
|
||||
this.restTemplate
|
||||
.getForEntity("http://localhost:" + this.port + "/attributes/bar", String.class);
|
||||
|
||||
assertEquals(HttpStatus.OK, result.getStatusCode());
|
||||
}
|
||||
|
||||
|
||||
@EnableWebFlux
|
||||
@Configuration
|
||||
@@ -116,6 +129,12 @@ public class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegr
|
||||
return new PersonController();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AttributesHandler attributesHandler() {
|
||||
return new AttributesHandler();
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public RouterFunction<EntityResponse<Person>> monoRouterFunction(PersonHandler personHandler) {
|
||||
return route(RequestPredicates.GET("/mono"), personHandler::mono);
|
||||
@@ -126,6 +145,12 @@ public class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegr
|
||||
return route(RequestPredicates.GET("/flux"), personHandler::flux);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RouterFunction<ServerResponse> attributesRouterFunction(AttributesHandler attributesHandler) {
|
||||
return nest(RequestPredicates.GET("/attributes"),
|
||||
route(RequestPredicates.GET("/{foo}"), attributesHandler::attributes));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -145,8 +170,44 @@ public class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegr
|
||||
|
||||
}
|
||||
|
||||
private static class AttributesHandler {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Mono<ServerResponse> attributes(ServerRequest request) {
|
||||
assertTrue(request.attributes().containsKey(RouterFunctions.REQUEST_ATTRIBUTE));
|
||||
assertTrue(request.attributes()
|
||||
.containsKey(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE));
|
||||
|
||||
Map<String, String> pathVariables =
|
||||
(Map<String, String>) request.attributes().get(RouterFunctions.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
|
||||
assertNotNull(pathVariables);
|
||||
assertEquals(1, pathVariables.size());
|
||||
assertEquals("bar", pathVariables.get("foo"));
|
||||
|
||||
pathVariables =
|
||||
(Map<String, String>) request.attributes().get(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
|
||||
assertNotNull(pathVariables);
|
||||
assertEquals(1, pathVariables.size());
|
||||
assertEquals("bar", pathVariables.get("foo"));
|
||||
|
||||
|
||||
PathPattern pattern =
|
||||
(PathPattern) request.attributes().get(RouterFunctions.MATCHING_PATTERN_ATTRIBUTE);
|
||||
assertNotNull(pattern);
|
||||
assertEquals("/attributes/{foo}", pattern.getPatternString());
|
||||
|
||||
pattern = (PathPattern) request.attributes()
|
||||
.get(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
|
||||
assertNotNull(pattern);
|
||||
assertEquals("/attributes/{foo}", pattern.getPatternString());
|
||||
|
||||
return ServerResponse.ok().build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Controller
|
||||
private static class PersonController {
|
||||
public static class PersonController {
|
||||
|
||||
@RequestMapping("/controller")
|
||||
@ResponseBody
|
||||
|
||||
@@ -25,7 +25,9 @@ import reactor.core.publisher.Mono;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.util.pattern.PathPattern;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
|
||||
@@ -122,7 +124,7 @@ public class NestedRouteIntegrationTests extends AbstractRouterFunctionIntegrati
|
||||
private static class NestedHandler {
|
||||
|
||||
public Mono<ServerResponse> pattern(ServerRequest request) {
|
||||
String pattern = matchingPattern(request);
|
||||
String pattern = matchingPattern(request).getPatternString();
|
||||
return ServerResponse.ok().syncBody(pattern);
|
||||
}
|
||||
|
||||
@@ -134,7 +136,8 @@ public class NestedRouteIntegrationTests extends AbstractRouterFunctionIntegrati
|
||||
assertTrue( (pathVariables.equals(attributePathVariables))
|
||||
|| (pathVariables.isEmpty() && (attributePathVariables == null)));
|
||||
|
||||
String pattern = matchingPattern(request);
|
||||
PathPattern pathPattern = matchingPattern(request);
|
||||
String pattern = pathPattern != null ? pathPattern.getPatternString() : "";
|
||||
Flux<String> responseBody;
|
||||
if (!pattern.isEmpty()) {
|
||||
responseBody = Flux.just(pattern, "\n", pathVariables.toString());
|
||||
@@ -144,8 +147,9 @@ public class NestedRouteIntegrationTests extends AbstractRouterFunctionIntegrati
|
||||
return ServerResponse.ok().body(responseBody, String.class);
|
||||
}
|
||||
|
||||
private String matchingPattern(ServerRequest request) {
|
||||
return (String) request.attributes().getOrDefault(RouterFunctions.MATCHING_PATTERN_ATTRIBUTE, "");
|
||||
@Nullable
|
||||
private PathPattern matchingPattern(ServerRequest request) {
|
||||
return (PathPattern) request.attributes().get(RouterFunctions.MATCHING_PATTERN_ATTRIBUTE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user