parent
d14477eb84
commit
b1ee44f12e
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -52,23 +52,25 @@ import static org.mockito.Mockito.mock;
|
|||
* Tests for the {@link DefaultResponseCreator} factory methods.
|
||||
*
|
||||
* @author Ashley Scopes
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
class DefaultResponseCreatorTests {
|
||||
|
||||
@ParameterizedTest(name = "expect status to be set [{0}]")
|
||||
@ValueSource(ints = {200, 401, 429})
|
||||
void expectStatus(int statusValue) throws IOException {
|
||||
HttpStatus status = HttpStatus.valueOf(statusValue);
|
||||
ClientHttpResponse response = createResponse(new DefaultResponseCreator(status));
|
||||
assertThat(response.getStatusCode()).isEqualTo(status);
|
||||
DefaultResponseCreator creator = new DefaultResponseCreator(status);
|
||||
|
||||
assertThat(createResponse(creator).getStatusCode()).isEqualTo(status);
|
||||
}
|
||||
|
||||
@Test
|
||||
void setBodyFromString() throws IOException {
|
||||
// Use unicode codepoint for "thinking" emoji to help verify correct encoding is used internally.
|
||||
ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK)
|
||||
.body("hello, world! \uD83E\uDD14"));
|
||||
DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK).body("hello, world! \uD83E\uDD14");
|
||||
|
||||
assertThat(IOUtils.toByteArray(response.getBody()))
|
||||
assertThat(IOUtils.toByteArray(createResponse(creator).getBody()))
|
||||
.isEqualTo("hello, world! \uD83E\uDD14".getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
|
@ -81,68 +83,62 @@ class DefaultResponseCreatorTests {
|
|||
.isTrue();
|
||||
|
||||
Charset charsetObj = Charset.forName(charset);
|
||||
|
||||
String content = "hello! €½$~@><·─";
|
||||
|
||||
ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK)
|
||||
.body(content, charsetObj));
|
||||
DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK).body(content, charsetObj);
|
||||
|
||||
ByteBuffer expectBuff = charsetObj.encode(content);
|
||||
byte[] expect = new byte[expectBuff.remaining()];
|
||||
expectBuff.get(expect);
|
||||
|
||||
assertThat(IOUtils.toByteArray(response.getBody())).isEqualTo(expect);
|
||||
assertThat(IOUtils.toByteArray(createResponse(creator).getBody())).isEqualTo(expect);
|
||||
}
|
||||
|
||||
@Test
|
||||
void setBodyFromByteArray() throws IOException {
|
||||
byte[] body = { 0, 9, 18, 27, 36, 45, 54, 63, 72, 81, 90 };
|
||||
ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK).body(body));
|
||||
assertThat(IOUtils.toByteArray(response.getBody())).isEqualTo(body);
|
||||
DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK).body(body);
|
||||
|
||||
assertThat(IOUtils.toByteArray(createResponse(creator).getBody())).isEqualTo(body);
|
||||
}
|
||||
|
||||
@Test
|
||||
void setBodyFromResource() throws IOException {
|
||||
byte[] resourceContent = {7, 14, 21, 28, 35};
|
||||
|
||||
Resource resource = mock(Resource.class);
|
||||
given(resource.getInputStream()).willReturn(new ByteArrayInputStream(resourceContent));
|
||||
|
||||
ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK).body(resource));
|
||||
|
||||
then(resource).should().getInputStream();
|
||||
|
||||
assertThat(IOUtils.toByteArray(response.getBody())).isEqualTo(resourceContent);
|
||||
}
|
||||
|
||||
@Test
|
||||
void setContentType() throws IOException {
|
||||
ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK)
|
||||
.contentType(MediaType.APPLICATION_JSON));
|
||||
MediaType mediaType = MediaType.APPLICATION_JSON;
|
||||
DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK).contentType(mediaType);
|
||||
|
||||
assertThat(response.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
|
||||
assertThat(createResponse(creator).getHeaders().getContentType()).isEqualTo(mediaType);
|
||||
}
|
||||
|
||||
@Test
|
||||
void setLocation() throws IOException {
|
||||
URI uri = UriComponentsBuilder
|
||||
.fromUriString("https://docs.spring.io/spring-framework/docs/current/reference/html/testing.html")
|
||||
.build()
|
||||
.toUri();
|
||||
URI uri = URI.create("https://docs.spring.io/spring-framework/docs/current/reference/html/testing.html");
|
||||
DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK).location(uri);
|
||||
|
||||
ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK).location(uri));
|
||||
assertThat(response.getHeaders().getLocation()).isEqualTo(uri);
|
||||
assertThat(createResponse(creator).getHeaders().getLocation()).isEqualTo(uri);
|
||||
}
|
||||
|
||||
@Test
|
||||
void setHeader() throws IOException {
|
||||
|
||||
ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK)
|
||||
DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK)
|
||||
.header("foo", "bar")
|
||||
.header("baz", "bork")
|
||||
.headers("lorem", "ipsum", "dolor", "sit", "amet"));
|
||||
.header("lorem", "ipsum", "dolor", "sit", "amet");
|
||||
|
||||
HttpHeaders headers = response.getHeaders();
|
||||
HttpHeaders headers = createResponse(creator).getHeaders();
|
||||
assertThat(headers.get("foo")).isNotNull().isEqualTo(Collections.singletonList("bar"));
|
||||
assertThat(headers.get("baz")).isNotNull().isEqualTo(Collections.singletonList("bork"));
|
||||
assertThat(headers.get("lorem")).isNotNull().isEqualTo(Arrays.asList("ipsum", "dolor", "sit", "amet"));
|
||||
|
@ -158,11 +154,11 @@ class DefaultResponseCreatorTests {
|
|||
HttpHeaders secondHeaders = new HttpHeaders();
|
||||
secondHeaders.setAllow(Collections.singleton(HttpMethod.PUT));
|
||||
|
||||
ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK)
|
||||
DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK)
|
||||
.headers(firstHeaders)
|
||||
.headers(secondHeaders));
|
||||
.headers(secondHeaders);
|
||||
|
||||
HttpHeaders responseHeaders = response.getHeaders();
|
||||
HttpHeaders responseHeaders = createResponse(creator).getHeaders();
|
||||
|
||||
assertThat(responseHeaders.getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
|
||||
assertThat(responseHeaders.getOrigin()).isEqualTo("https://github.com");
|
||||
|
@ -176,12 +172,12 @@ class DefaultResponseCreatorTests {
|
|||
ResponseCookie thirdCookie = ResponseCookie.from("cookie-cookie", "cookies").build();
|
||||
ResponseCookie fourthCookie = ResponseCookie.from("foobar", "bazbork").build();
|
||||
|
||||
ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK)
|
||||
.cookie(firstCookie)
|
||||
.cookie(secondCookie)
|
||||
.cookies(thirdCookie, fourthCookie));
|
||||
DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK)
|
||||
.cookies(firstCookie)
|
||||
.cookies(secondCookie)
|
||||
.cookies(thirdCookie, fourthCookie);
|
||||
|
||||
HttpHeaders responseHeaders = response.getHeaders();
|
||||
HttpHeaders responseHeaders = createResponse(creator).getHeaders();
|
||||
|
||||
assertThat(responseHeaders.get(HttpHeaders.SET_COOKIE))
|
||||
.isNotNull()
|
||||
|
@ -207,11 +203,11 @@ class DefaultResponseCreatorTests {
|
|||
firstCookies.add(thirdCookie.getName(), thirdCookie);
|
||||
firstCookies.add(fourthCookie.getName(), fourthCookie);
|
||||
|
||||
ClientHttpResponse response = createResponse(new DefaultResponseCreator(HttpStatus.OK)
|
||||
DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK)
|
||||
.cookies(firstCookies)
|
||||
.cookies(secondCookies));
|
||||
.cookies(secondCookies);
|
||||
|
||||
HttpHeaders responseHeaders = response.getHeaders();
|
||||
HttpHeaders responseHeaders = createResponse(creator).getHeaders();
|
||||
|
||||
assertThat(responseHeaders.get(HttpHeaders.SET_COOKIE))
|
||||
.isNotNull()
|
||||
|
@ -227,4 +223,5 @@ class DefaultResponseCreatorTests {
|
|||
URI uri = UriComponentsBuilder.fromUriString("/foo/bar").build().toUri();
|
||||
return creator.createResponse(new MockClientHttpRequest(HttpMethod.POST, uri));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue