From 56d669f849d4b4caaef97b93385c873ac810ecd9 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Tue, 28 Feb 2017 15:45:06 +0100 Subject: [PATCH] Add permanent/temporary redirect to ServerResponse This commit adds the temporaryRedirect(URI) and permanentRedirect(URI) static creation methods to ServerResponse. --- .../function/server/ServerResponse.java | 36 +++++++++++++++---- .../DefaultServerResponseBuilderTests.java | 27 ++++++++++++-- 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerResponse.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerResponse.java index 6c742da31b..0fe59a7d95 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerResponse.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ServerResponse.java @@ -94,7 +94,7 @@ public interface ServerResponse { } /** - * Create a builder with the status set to {@linkplain HttpStatus#OK OK}. + * Create a builder with the status set to {@linkplain HttpStatus#OK 200 OK}. * @return the created builder */ static BodyBuilder ok() { @@ -102,7 +102,7 @@ public interface ServerResponse { } /** - * Create a new builder with a {@linkplain HttpStatus#CREATED CREATED} status + * Create a new builder with a {@linkplain HttpStatus#CREATED 201 Created} status * and a location header set to the given URI. * @param location the location URI * @return the created builder @@ -113,7 +113,7 @@ public interface ServerResponse { } /** - * Create a builder with an {@linkplain HttpStatus#ACCEPTED ACCEPTED} status. + * Create a builder with an {@linkplain HttpStatus#ACCEPTED 202 Accepted} status. * @return the created builder */ static BodyBuilder accepted() { @@ -121,7 +121,7 @@ public interface ServerResponse { } /** - * Create a builder with a {@linkplain HttpStatus#NO_CONTENT NO_CONTENT} status. + * Create a builder with a {@linkplain HttpStatus#NO_CONTENT 204 No Content} status. * @return the created builder */ static HeadersBuilder noContent() { @@ -129,7 +129,29 @@ public interface ServerResponse { } /** - * Create a builder with a {@linkplain HttpStatus#BAD_REQUEST BAD_REQUEST} status. + * Create a builder with a {@linkplain HttpStatus#TEMPORARY_REDIRECT 307 Temporary Redirect} + * status and a location header set to the given URI. + * @param location the location URI + * @return the created builder + */ + static BodyBuilder temporaryRedirect(URI location) { + BodyBuilder builder = status(HttpStatus.TEMPORARY_REDIRECT); + return builder.location(location); + } + + /** + * Create a builder with a {@linkplain HttpStatus#PERMANENT_REDIRECT 308 Permanent Redirect} + * status and a location header set to the given URI. + * @param location the location URI + * @return the created builder + */ + static BodyBuilder permanentRedirect(URI location) { + BodyBuilder builder = status(HttpStatus.PERMANENT_REDIRECT); + return builder.location(location); + } + + /** + * Create a builder with a {@linkplain HttpStatus#BAD_REQUEST 400 Bad Request} status. * @return the created builder */ static BodyBuilder badRequest() { @@ -137,7 +159,7 @@ public interface ServerResponse { } /** - * Create a builder with a {@linkplain HttpStatus#NOT_FOUND NOT_FOUND} status. + * Create a builder with a {@linkplain HttpStatus#NOT_FOUND 404 Not Found} status. * * @return the created builder */ @@ -147,7 +169,7 @@ public interface ServerResponse { /** * Create a builder with an - * {@linkplain HttpStatus#UNPROCESSABLE_ENTITY UNPROCESSABLE_ENTITY} status. + * {@linkplain HttpStatus#UNPROCESSABLE_ENTITY 422 Unprocessable Entity} status. * @return the created builder */ static BodyBuilder unprocessableEntity() { diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilderTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilderTests.java index 89b250ddf1..37d6230d09 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilderTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilderTests.java @@ -35,8 +35,9 @@ import org.springframework.http.MediaType; import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse; import org.springframework.web.server.ServerWebExchange; -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** * @author Arjen Poutsma @@ -104,6 +105,28 @@ public class DefaultServerResponseBuilderTests { } + @Test + public void temporaryRedirect() throws Exception { + URI location = URI.create("http://example.com"); + Mono result = ServerResponse.temporaryRedirect(location).build(); + StepVerifier.create(result) + .expectNextMatches(response -> HttpStatus.TEMPORARY_REDIRECT.equals(response.statusCode()) && + location.equals(response.headers().getLocation())) + .expectComplete() + .verify(); + } + + @Test + public void permanentRedirect() throws Exception { + URI location = URI.create("http://example.com"); + Mono result = ServerResponse.permanentRedirect(location).build(); + StepVerifier.create(result) + .expectNextMatches(response -> HttpStatus.PERMANENT_REDIRECT.equals(response.statusCode()) && + location.equals(response.headers().getLocation())) + .expectComplete() + .verify(); + } + @Test public void badRequest() throws Exception { Mono result = ServerResponse.badRequest().build();