Add methods to DefaultResponseCreator & MockRestResponseCreators
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 gh-27280
This commit is contained in:
committed by
rstoyanchev
parent
badba7c3e9
commit
d14477eb84
@@ -18,18 +18,25 @@ package org.springframework.test.web.client.response;
|
||||
|
||||
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.
|
||||
@@ -67,7 +74,6 @@ public class DefaultResponseCreator implements ResponseCreator {
|
||||
this.statusCode = statusCode;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the body as a UTF-8 String.
|
||||
*/
|
||||
@@ -76,6 +82,14 @@ public class DefaultResponseCreator implements ResponseCreator {
|
||||
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.
|
||||
*/
|
||||
@@ -85,7 +99,7 @@ public class DefaultResponseCreator implements ResponseCreator {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the body as a {@link Resource}.
|
||||
* Set the body from a {@link Resource}.
|
||||
*/
|
||||
public DefaultResponseCreator body(Resource resource) {
|
||||
this.contentResource = resource;
|
||||
@@ -108,6 +122,26 @@ public class DefaultResponseCreator implements ResponseCreator {
|
||||
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.
|
||||
*/
|
||||
@@ -116,6 +150,36 @@ public class DefaultResponseCreator implements ResponseCreator {
|
||||
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 {
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.io.IOException;
|
||||
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;
|
||||
@@ -82,6 +83,13 @@ public abstract class MockRestResponseCreators {
|
||||
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).
|
||||
*/
|
||||
@@ -103,6 +111,43 @@ public abstract class MockRestResponseCreators {
|
||||
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).
|
||||
*/
|
||||
@@ -110,6 +155,27 @@ public abstract class MockRestResponseCreators {
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user