Add resource redirection to WebFlux functional router
See gh-27257
This commit is contained in:
@@ -39,6 +39,7 @@ import org.springframework.web.testfixture.server.MockServerWebExchange;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.web.reactive.function.server.RequestPredicates.HEAD;
|
||||
import static org.springframework.web.reactive.function.server.RequestPredicates.path;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
@@ -102,6 +103,27 @@ class RouterFunctionBuilderTests {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void resource() {
|
||||
Resource resource = new ClassPathResource("/org/springframework/web/reactive/function/server/response.txt");
|
||||
assertThat(resource.exists()).isTrue();
|
||||
|
||||
RouterFunction<ServerResponse> route = RouterFunctions.route()
|
||||
.resource(path("/test"), resource)
|
||||
.build();
|
||||
|
||||
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://localhost/test").build();
|
||||
ServerRequest resourceRequest = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
|
||||
|
||||
Mono<HttpStatusCode> responseMono = route.route(resourceRequest)
|
||||
.flatMap(handlerFunction -> handlerFunction.handle(resourceRequest))
|
||||
.map(ServerResponse::statusCode);
|
||||
|
||||
StepVerifier.create(responseMono)
|
||||
.expectNext(HttpStatus.OK)
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
void resources() {
|
||||
Resource resource = new ClassPathResource("/org/springframework/web/reactive/function/server/");
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
* Copyright 2002-2024 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.
|
||||
@@ -93,16 +93,6 @@ class CoRouterFunctionDslTests {
|
||||
.verifyComplete()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun resourceByPath() {
|
||||
val mockRequest = get("https://example.com/org/springframework/web/reactive/function/response.txt")
|
||||
.build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(sampleRouter().route(request))
|
||||
.expectNextCount(1)
|
||||
.verifyComplete()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun method() {
|
||||
val mockRequest = patch("https://example.com/")
|
||||
@@ -124,6 +114,24 @@ class CoRouterFunctionDslTests {
|
||||
|
||||
@Test
|
||||
fun resource() {
|
||||
val mockRequest = get("https://example.com/response2.txt").build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(sampleRouter().route(request))
|
||||
.expectNextCount(1)
|
||||
.verifyComplete()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun resources() {
|
||||
val mockRequest = get("https://example.com/resources/response.txt").build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(sampleRouter().route(request))
|
||||
.expectNextCount(1)
|
||||
.verifyComplete()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun resourcesLookupFunction() {
|
||||
val mockRequest = get("https://example.com/response.txt").build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(sampleRouter().route(request))
|
||||
@@ -305,8 +313,9 @@ class CoRouterFunctionDslTests {
|
||||
GET("/api/foo/", ::handle)
|
||||
}
|
||||
headers({ it.header("bar").isNotEmpty() }, ::handle)
|
||||
resources("/org/springframework/web/reactive/function/**",
|
||||
ClassPathResource("/org/springframework/web/reactive/function/response.txt"))
|
||||
resource(path("/response2.txt"), ClassPathResource("/org/springframework/web/reactive/function/response.txt"))
|
||||
resources("/resources/**",
|
||||
ClassPathResource("/org/springframework/web/reactive/function/"))
|
||||
resources {
|
||||
if (it.path() == "/response.txt") {
|
||||
ClassPathResource("/org/springframework/web/reactive/function/response.txt")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
* Copyright 2002-2024 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.
|
||||
@@ -91,16 +91,6 @@ class RouterFunctionDslTests {
|
||||
.verifyComplete()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun resourceByPath() {
|
||||
val mockRequest = get("https://example.com/org/springframework/web/reactive/function/response.txt")
|
||||
.build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(sampleRouter().route(request))
|
||||
.expectNextCount(1)
|
||||
.verifyComplete()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun method() {
|
||||
val mockRequest = patch("https://example.com/")
|
||||
@@ -122,6 +112,24 @@ class RouterFunctionDslTests {
|
||||
|
||||
@Test
|
||||
fun resource() {
|
||||
val mockRequest = get("https://example.com/response2.txt").build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(sampleRouter().route(request))
|
||||
.expectNextCount(1)
|
||||
.verifyComplete()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun resources() {
|
||||
val mockRequest = get("https://example.com/resources/response.txt").build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(sampleRouter().route(request))
|
||||
.expectNextCount(1)
|
||||
.verifyComplete()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun resourcesLookupFunction() {
|
||||
val mockRequest = get("https://example.com/response.txt").build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(sampleRouter().route(request))
|
||||
@@ -237,8 +245,9 @@ class RouterFunctionDslTests {
|
||||
GET("/api/foo/", ::handle)
|
||||
}
|
||||
headers({ it.header("bar").isNotEmpty() }, ::handle)
|
||||
resources("/org/springframework/web/reactive/function/**",
|
||||
ClassPathResource("/org/springframework/web/reactive/function/response.txt"))
|
||||
resource(path("/response2.txt"), ClassPathResource("/org/springframework/web/reactive/function/response.txt"))
|
||||
resources("/resources/**",
|
||||
ClassPathResource("/org/springframework/web/reactive/function/"))
|
||||
resources {
|
||||
if (it.path() == "/response.txt") {
|
||||
Mono.just(ClassPathResource("/org/springframework/web/reactive/function/response.txt"))
|
||||
|
||||
Reference in New Issue
Block a user