Polishing contribution

Closes gh-27280
This commit is contained in:
rstoyanchev
2022-09-27 17:08:52 +01:00
parent d14477eb84
commit b1ee44f12e
3 changed files with 57 additions and 74 deletions

View File

@@ -20,10 +20,6 @@ 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;
@@ -84,6 +80,7 @@ public class DefaultResponseCreator implements ResponseCreator {
/**
* Set the body from a string using the given character set.
* @since 6.0
*/
public DefaultResponseCreator body(String content, Charset charset) {
this.content = content.getBytes(charset);
@@ -123,22 +120,13 @@ public class DefaultResponseCreator implements ResponseCreator {
}
/**
* Add a single header.
* Add a response header with one or more values.
* @since 6.0
*/
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);
public DefaultResponseCreator header(String name, String ... headerValues) {
for (String headerValue : headerValues) {
this.headers.add(name, headerValue);
}
return this;
}
@@ -150,34 +138,23 @@ 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.
* @since 6.0
*/
public DefaultResponseCreator cookies(ResponseCookie... cookies) {
for (ResponseCookie cookie : cookies) {
this.headers.add(HttpHeaders.SET_COOKIE, cookie.toString());
}
return this;
}
/**
* Copy all given cookies.
* Copy all cookies from the given {@link MultiValueMap}.
* @since 6.0
*/
public DefaultResponseCreator cookies(MultiValueMap<String, ResponseCookie> cookies) {
cookies.values()
.stream()
.flatMap(List::stream)
.forEach(cookie -> this.headers.add(HttpHeaders.SET_COOKIE, cookie.toString()));
public DefaultResponseCreator cookies(MultiValueMap<String, ResponseCookie> multiValueMap) {
multiValueMap.values().forEach(cookies -> cookies.forEach(this::cookies));
return this;
}

View File

@@ -85,6 +85,7 @@ public abstract class MockRestResponseCreators {
/**
* {@code ResponseCreator} for a 202 response (ACCEPTED).
* @since 6.0
*/
public static DefaultResponseCreator withAccepted() {
return new DefaultResponseCreator(HttpStatus.ACCEPTED);
@@ -113,6 +114,7 @@ public abstract class MockRestResponseCreators {
/**
* {@code ResponseCreator} for a 403 response (FORBIDDEN).
* @since 6.0
*/
public static DefaultResponseCreator withForbiddenRequest() {
return new DefaultResponseCreator(HttpStatus.FORBIDDEN);
@@ -120,6 +122,7 @@ public abstract class MockRestResponseCreators {
/**
* {@code ResponseCreator} for a 404 response (NOT_FOUND).
* @since 6.0
*/
public static DefaultResponseCreator withResourceNotFound() {
return new DefaultResponseCreator(HttpStatus.NOT_FOUND);
@@ -127,6 +130,7 @@ public abstract class MockRestResponseCreators {
/**
* {@code ResponseCreator} for a 409 response (CONFLICT).
* @since 6.0
*/
public static DefaultResponseCreator withRequestConflict() {
return new DefaultResponseCreator(HttpStatus.CONFLICT);
@@ -134,14 +138,16 @@ public abstract class MockRestResponseCreators {
/**
* {@code ResponseCreator} for a 429 ratelimited response (TOO_MANY_REQUESTS).
* @since 6.0
*/
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.
* {@code ResponseCreator} for a 429 rate-limited response (TOO_MANY_REQUESTS)
* with a {@code Retry-After} header in seconds.
* @since 6.0
*/
public static DefaultResponseCreator withTooManyRequests(int retryAfter) {
return new DefaultResponseCreator(HttpStatus.TOO_MANY_REQUESTS)
@@ -157,6 +163,7 @@ public abstract class MockRestResponseCreators {
/**
* {@code ResponseCreator} for a 502 response (BAD_GATEWAY).
* @since 6.0
*/
public static DefaultResponseCreator withBadGateway() {
return new DefaultResponseCreator(HttpStatus.BAD_GATEWAY);
@@ -164,6 +171,7 @@ public abstract class MockRestResponseCreators {
/**
* {@code ResponseCreator} for a 503 response (SERVICE_UNAVAILABLE).
* @since 6.0
*/
public static DefaultResponseCreator withServiceUnavailable() {
return new DefaultResponseCreator(HttpStatus.SERVICE_UNAVAILABLE);
@@ -171,6 +179,7 @@ public abstract class MockRestResponseCreators {
/**
* {@code ResponseCreator} for a 504 response (GATEWAY_TIMEOUT).
* @since 6.0
*/
public static DefaultResponseCreator withGatewayTimeout() {
return new DefaultResponseCreator(HttpStatus.GATEWAY_TIMEOUT);