Merge two DispatcherHandlerIntegrationTests

This commit is contained in:
Arjen Poutsma
2022-11-02 14:48:12 +01:00
parent 9e306151c7
commit 2042cb68a5
2 changed files with 23 additions and 100 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 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.
@@ -28,6 +28,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.stereotype.Controller;
@@ -44,6 +45,7 @@ import org.springframework.web.util.pattern.PathPattern;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.web.reactive.function.BodyInserters.fromPublisher;
import static org.springframework.web.reactive.function.server.RequestPredicates.accept;
import static org.springframework.web.reactive.function.server.RouterFunctions.nest;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
@@ -122,6 +124,15 @@ class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegrationTe
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
}
@ParameterizedHttpServerTest
void nested(HttpServer httpServer) throws Exception {
startServer(httpServer);
ResponseEntity<String> result = this.restTemplate
.getForEntity("http://localhost:" + this.port + "/foo/bar", String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
}
@EnableWebFlux
@Configuration
@@ -157,6 +168,17 @@ class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegrationTe
return nest(RequestPredicates.GET("/attributes"),
route(RequestPredicates.GET("/{foo}"), attributesHandler::attributes));
}
@Bean
public RouterFunction<ServerResponse> nested() {
return route()
.path("/foo", () -> route()
.nest(accept(MediaType.APPLICATION_JSON), builder -> builder
.GET("/bar", request -> ServerResponse.ok().build()))
.build())
.build();
}
}

View File

@@ -1,99 +0,0 @@
/*
* Copyright 2002-2019 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.reactive.function.server.support;
import reactor.core.publisher.Mono;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.DispatcherHandler;
import org.springframework.web.reactive.config.EnableWebFlux;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
import org.springframework.web.testfixture.http.server.reactive.bootstrap.AbstractHttpHandlerIntegrationTests;
import org.springframework.web.testfixture.http.server.reactive.bootstrap.HttpServer;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.web.reactive.function.server.RequestPredicates.accept;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
/**
* @author Arjen Poutsma
*/
class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegrationTests {
private final RestTemplate restTemplate = new RestTemplate();
@Override
protected HttpHandler createHttpHandler() {
AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext();
wac.register(TestConfiguration.class);
wac.refresh();
return WebHttpHandlerBuilder.webHandler(new DispatcherHandler(wac)).build();
}
@ParameterizedHttpServerTest
void nested(HttpServer httpServer) throws Exception {
startServer(httpServer);
ResponseEntity<String> result = this.restTemplate
.getForEntity("http://localhost:" + this.port + "/foo/bar", String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
}
@Configuration
@EnableWebFlux
static class TestConfiguration {
@Bean
public RouterFunction<ServerResponse> router(Handler handler) {
return route()
.path("/foo", () -> route()
.nest(accept(MediaType.APPLICATION_JSON), builder -> builder
.GET("/bar", handler::handle))
.build())
.build();
}
@Bean
public Handler handler() {
return new Handler();
}
}
static class Handler {
public Mono<ServerResponse> handle(ServerRequest request) {
return ServerResponse.ok().build();
}
}
}