Add permanent/temporary redirect to ServerResponse

This commit adds the temporaryRedirect(URI) and permanentRedirect(URI)
static creation methods to ServerResponse.
This commit is contained in:
Arjen Poutsma 2017-02-28 15:45:06 +01:00
parent 6556b40c2b
commit 56d669f849
2 changed files with 54 additions and 9 deletions

View File

@ -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() {

View File

@ -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<ServerResponse> 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<ServerResponse> 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<ServerResponse> result = ServerResponse.badRequest().build();