WebMvc respects RouterFunction beans ordering
Closes gh-28595
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
* Copyright 2002-2022 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.
|
||||
@@ -21,7 +21,10 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.web.servlet.HandlerExecutionChain;
|
||||
import org.springframework.web.servlet.HandlerMapping;
|
||||
@@ -41,7 +44,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*/
|
||||
class RouterFunctionMappingTests {
|
||||
|
||||
private List<HttpMessageConverter<?>> messageConverters = Collections.emptyList();
|
||||
private final List<HttpMessageConverter<?>> messageConverters = Collections.emptyList();
|
||||
|
||||
@Test
|
||||
void normal() throws Exception {
|
||||
@@ -71,6 +74,65 @@ class RouterFunctionMappingTests {
|
||||
assertThat(result).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void empty() throws Exception {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
context.refresh();
|
||||
|
||||
RouterFunctionMapping mapping = new RouterFunctionMapping();
|
||||
mapping.setMessageConverters(this.messageConverters);
|
||||
mapping.setApplicationContext(context);
|
||||
mapping.afterPropertiesSet();
|
||||
|
||||
MockHttpServletRequest request = createTestRequest("/match");
|
||||
HandlerExecutionChain result = mapping.getHandler(request);
|
||||
|
||||
assertThat(result).isNull();
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {true, false})
|
||||
void detectHandlerFunctionsInAncestorContexts(boolean detect) throws Exception {
|
||||
HandlerFunction<ServerResponse> function1 = request -> ServerResponse.ok().build();
|
||||
HandlerFunction<ServerResponse> function2 = request -> ServerResponse.ok().build();
|
||||
HandlerFunction<ServerResponse> function3 = request -> ServerResponse.ok().build();
|
||||
|
||||
AnnotationConfigApplicationContext context1 = new AnnotationConfigApplicationContext();
|
||||
context1.registerBean(RouterFunction.class, () -> RouterFunctions.route().GET("/fn1", function1).build());
|
||||
context1.refresh();
|
||||
|
||||
AnnotationConfigApplicationContext context2 = new AnnotationConfigApplicationContext();
|
||||
context2.registerBean(RouterFunction.class, () -> RouterFunctions.route().GET("/fn2", function2).build());
|
||||
context2.setParent(context1);
|
||||
context2.refresh();
|
||||
|
||||
AnnotationConfigApplicationContext context3 = new AnnotationConfigApplicationContext();
|
||||
context3.registerBean(RouterFunction.class, () -> RouterFunctions.route().GET("/fn3", function3).build());
|
||||
context3.setParent(context2);
|
||||
context3.refresh();
|
||||
|
||||
RouterFunctionMapping mapping = new RouterFunctionMapping();
|
||||
mapping.setDetectHandlerFunctionsInAncestorContexts(detect);
|
||||
mapping.setMessageConverters(this.messageConverters);
|
||||
mapping.setApplicationContext(context3);
|
||||
mapping.afterPropertiesSet();
|
||||
|
||||
HandlerExecutionChain chain1 = mapping.getHandler(createTestRequest("/fn1"));
|
||||
HandlerExecutionChain chain2 = mapping.getHandler(createTestRequest("/fn2"));
|
||||
if (detect) {
|
||||
assertThat(chain1).isNotNull().extracting(HandlerExecutionChain::getHandler).isSameAs(function1);
|
||||
assertThat(chain2).isNotNull().extracting(HandlerExecutionChain::getHandler).isSameAs(function2);
|
||||
}
|
||||
else {
|
||||
assertThat(chain1).isNull();
|
||||
assertThat(chain2).isNull();
|
||||
}
|
||||
|
||||
HandlerExecutionChain chain3 = mapping.getHandler(createTestRequest("/fn3"));
|
||||
assertThat(chain3).isNotNull().extracting(HandlerExecutionChain::getHandler).isSameAs(function3);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void changeParser() throws Exception {
|
||||
HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().build();
|
||||
|
||||
Reference in New Issue
Block a user