Skip to content

Commit

Permalink
Add methods to DefaultResponseCreator & MockRestResponseCreators
Browse files Browse the repository at this point in the history
New methods in DefaultResponseCreator for adding headers and cookies,
for specifying the character encoding when setting a string body on
a response, which is useful when working in environments that do not
automatically assume UTF-8, such as integrating with legacy
applications from a new Spring one.

New methods in MockRestResponseCreators support extra commonly used
HTTP status codes, including some that occur when working in AWS,
CloudFlare, or when using gateways such as Kong, where resilient
applications should be able to respond to ratelimits, gateway errors,
and gateway timeouts that may occur if a remote service is down.

Added test cases for any changes made.

See spring-projectsgh-27280
  • Loading branch information
ascopes authored and rstoyanchev committed Sep 27, 2022
1 parent badba7c commit d14477e
Show file tree
Hide file tree
Showing 4 changed files with 446 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,25 @@

import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseCookie;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.lang.Nullable;
import org.springframework.mock.http.client.MockClientHttpResponse;
import org.springframework.test.web.client.ResponseCreator;
import org.springframework.util.Assert;
import org.springframework.util.MultiValueMap;

/**
* A {@code ResponseCreator} with builder-style methods for adding response details.
Expand Down Expand Up @@ -67,7 +74,6 @@ protected DefaultResponseCreator(HttpStatusCode statusCode) {
this.statusCode = statusCode;
}


/**
* Set the body as a UTF-8 String.
*/
Expand All @@ -76,6 +82,14 @@ public DefaultResponseCreator body(String content) {
return this;
}

/**
* Set the body from a string using the given character set.
*/
public DefaultResponseCreator body(String content, Charset charset) {
this.content = content.getBytes(charset);
return this;
}

/**
* Set the body as a byte array.
*/
Expand All @@ -85,7 +99,7 @@ public DefaultResponseCreator body(byte[] content) {
}

/**
* Set the body as a {@link Resource}.
* Set the body from a {@link Resource}.
*/
public DefaultResponseCreator body(Resource resource) {
this.contentResource = resource;
Expand All @@ -108,6 +122,26 @@ public DefaultResponseCreator location(URI location) {
return this;
}

/**
* Add a single header.
*/
public DefaultResponseCreator header(String name, String value) {
// This is really just an alias, but it makes the interface more fluent.
return headers(name, value);
}

/**
* Add one or more headers.
*/
public DefaultResponseCreator headers(String name, String ... value) {
List<String> valueList = Stream.of(value)
.filter(Objects::nonNull)
.collect(Collectors.toList());

this.headers.addAll(name, valueList);
return this;
}

/**
* Copy all given headers.
*/
Expand All @@ -116,6 +150,36 @@ public DefaultResponseCreator headers(HttpHeaders headers) {
return this;
}

/**
* Add a single cookie.
*/
public DefaultResponseCreator cookie(ResponseCookie cookie) {
// This is really just an alias, but it makes the interface more fluent.
return cookies(cookie);
}

/**
* Add one or more cookies.
*/
public DefaultResponseCreator cookies(ResponseCookie... cookies) {
for (ResponseCookie cookie : cookies) {
this.headers.add(HttpHeaders.SET_COOKIE, cookie.toString());
}

return this;
}

/**
* Copy all given cookies.
*/
public DefaultResponseCreator cookies(MultiValueMap<String, ResponseCookie> cookies) {
cookies.values()
.stream()
.flatMap(List::stream)
.forEach(cookie -> this.headers.add(HttpHeaders.SET_COOKIE, cookie.toString()));

return this;
}

@Override
public ClientHttpResponse createResponse(@Nullable ClientHttpRequest request) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.net.URI;

import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -82,6 +83,13 @@ public static DefaultResponseCreator withCreatedEntity(URI location) {
return new DefaultResponseCreator(HttpStatus.CREATED).location(location);
}

/**
* {@code ResponseCreator} for a 202 response (ACCEPTED).
*/
public static DefaultResponseCreator withAccepted() {
return new DefaultResponseCreator(HttpStatus.ACCEPTED);
}

/**
* {@code ResponseCreator} for a 204 response (NO_CONTENT).
*/
Expand All @@ -103,13 +111,71 @@ public static DefaultResponseCreator withUnauthorizedRequest() {
return new DefaultResponseCreator(HttpStatus.UNAUTHORIZED);
}

/**
* {@code ResponseCreator} for a 403 response (FORBIDDEN).
*/
public static DefaultResponseCreator withForbiddenRequest() {
return new DefaultResponseCreator(HttpStatus.FORBIDDEN);
}

/**
* {@code ResponseCreator} for a 404 response (NOT_FOUND).
*/
public static DefaultResponseCreator withResourceNotFound() {
return new DefaultResponseCreator(HttpStatus.NOT_FOUND);
}

/**
* {@code ResponseCreator} for a 409 response (CONFLICT).
*/
public static DefaultResponseCreator withRequestConflict() {
return new DefaultResponseCreator(HttpStatus.CONFLICT);
}

/**
* {@code ResponseCreator} for a 429 ratelimited response (TOO_MANY_REQUESTS).
*/
public static DefaultResponseCreator withTooManyRequests() {
return new DefaultResponseCreator(HttpStatus.TOO_MANY_REQUESTS);
}

/**
* {@code ResponseCreator} for a 429 ratelimited response (TOO_MANY_REQUESTS) with a {@code Retry-After} header
* in seconds.
*/
public static DefaultResponseCreator withTooManyRequests(int retryAfter) {
return new DefaultResponseCreator(HttpStatus.TOO_MANY_REQUESTS)
.header(HttpHeaders.RETRY_AFTER, Integer.toString(retryAfter));
}

/**
* {@code ResponseCreator} for a 500 response (SERVER_ERROR).
*/
public static DefaultResponseCreator withServerError() {
return new DefaultResponseCreator(HttpStatus.INTERNAL_SERVER_ERROR);
}

/**
* {@code ResponseCreator} for a 502 response (BAD_GATEWAY).
*/
public static DefaultResponseCreator withBadGateway() {
return new DefaultResponseCreator(HttpStatus.BAD_GATEWAY);
}

/**
* {@code ResponseCreator} for a 503 response (SERVICE_UNAVAILABLE).
*/
public static DefaultResponseCreator withServiceUnavailable() {
return new DefaultResponseCreator(HttpStatus.SERVICE_UNAVAILABLE);
}

/**
* {@code ResponseCreator} for a 504 response (GATEWAY_TIMEOUT).
*/
public static DefaultResponseCreator withGatewayTimeout() {
return new DefaultResponseCreator(HttpStatus.GATEWAY_TIMEOUT);
}

/**
* {@code ResponseCreator} with a specific HTTP status.
* @param status the response status
Expand Down
Loading

0 comments on commit d14477e

Please sign in to comment.