Introduce additional constructors in MockClientHttp[Request|Response]

This commit introduces additional constructors in MockClientHttpRequest
and MockClientHttpResponse that were previously only present in the
internal "test fixtures" in spring-web.

This commit also aligns the mocks in spring-test with the test fixtures
in spring-web to simplify continued maintenance of the mocks and test
fixtures.

Closes gh-29670
This commit is contained in:
Sam Brannen
2022-12-09 15:46:08 -05:00
parent 9b38e43c17
commit 83eb8ac0ea
31 changed files with 280 additions and 228 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 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.
@@ -37,11 +37,17 @@ public class MockHttpInputMessage implements HttpInputMessage {
private final InputStream body;
public MockHttpInputMessage(byte[] content) {
Assert.notNull(content, "Byte array must not be null");
this.body = new ByteArrayInputStream(content);
/**
* Create a {@code MockHttpInputMessage} with the supplied body.
*/
public MockHttpInputMessage(byte[] body) {
Assert.notNull(body, "Byte array must not be null");
this.body = new ByteArrayInputStream(body);
}
/**
* Create a {@code MockHttpInputMessage} with the supplied body.
*/
public MockHttpInputMessage(InputStream body) {
Assert.notNull(body, "InputStream must not be null");
this.body = body;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 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.
@@ -34,31 +34,23 @@ import org.springframework.util.StreamUtils;
*/
public class MockHttpOutputMessage implements HttpOutputMessage {
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
private final HttpHeaders headers = new HttpHeaders();
private final ByteArrayOutputStream body = new ByteArrayOutputStream(1024);
/**
* Return the headers.
*/
@Override
public HttpHeaders getHeaders() {
return this.headers;
}
/**
* Return the body content.
*/
@Override
public OutputStream getBody() throws IOException {
return this.body;
}
/**
* Return body content as a byte array.
* Return the body content as a byte array.
*/
public byte[] getBodyAsBytes() {
return this.body.toByteArray();
@@ -68,12 +60,12 @@ public class MockHttpOutputMessage implements HttpOutputMessage {
* Return the body content interpreted as a UTF-8 string.
*/
public String getBodyAsString() {
return getBodyAsString(DEFAULT_CHARSET);
return getBodyAsString(StandardCharsets.UTF_8);
}
/**
* Return the body content as a string.
* @param charset the charset to use to turn the body content to a String
* Return the body content interpreted as a string using the supplied character set.
* @param charset the charset to use to turn the body content into a String
*/
public String getBodyAsString(Charset charset) {
return StreamUtils.copyToString(this.body, charset);

View File

@@ -25,11 +25,13 @@ import org.springframework.http.client.ClientHttpResponse;
import org.springframework.lang.Nullable;
import org.springframework.mock.http.MockHttpOutputMessage;
import org.springframework.util.Assert;
import org.springframework.web.util.UriComponentsBuilder;
/**
* Mock implementation of {@link ClientHttpRequest}.
*
* @author Rossen Stoyanchev
* @author Brian Clozel
* @author Sam Brannen
* @since 3.2
*/
@@ -46,15 +48,25 @@ public class MockClientHttpRequest extends MockHttpOutputMessage implements Clie
/**
* Default constructor.
* Create a {@code MockClientHttpRequest} with {@link HttpMethod#GET GET} as
* the HTTP request method and {@code "/"} as the {@link URI}.
*/
public MockClientHttpRequest() {
this.httpMethod = HttpMethod.GET;
this.uri = URI.create("/");
this(HttpMethod.GET, URI.create("/"));
}
/**
* Create an instance with the given HttpMethod and URI.
* Create a {@code MockClientHttpRequest} with the given {@link HttpMethod},
* URI template, and URI template variable values.
* @since 6.0.3
*/
public MockClientHttpRequest(HttpMethod httpMethod, String uriTemplate, Object... vars) {
this(httpMethod, UriComponentsBuilder.fromUriString(uriTemplate).buildAndExpand(vars).encode().toUri());
}
/**
* Create a {@code MockClientHttpRequest} with the given {@link HttpMethod}
* and {@link URI}.
*/
public MockClientHttpRequest(HttpMethod httpMethod, URI uri) {
this.httpMethod = httpMethod;
@@ -62,6 +74,9 @@ public class MockClientHttpRequest extends MockHttpOutputMessage implements Clie
}
/**
* Set the HTTP method of the request.
*/
public void setMethod(HttpMethod httpMethod) {
this.httpMethod = httpMethod;
}
@@ -71,6 +86,9 @@ public class MockClientHttpRequest extends MockHttpOutputMessage implements Clie
return this.httpMethod;
}
/**
* Set the URI of the request.
*/
public void setURI(URI uri) {
this.uri = uri;
}
@@ -80,10 +98,19 @@ public class MockClientHttpRequest extends MockHttpOutputMessage implements Clie
return this.uri;
}
/**
* Set the {@link ClientHttpResponse} to be used as the result of executing
* the this request.
* @see #execute()
*/
public void setResponse(ClientHttpResponse clientHttpResponse) {
this.clientHttpResponse = clientHttpResponse;
}
/**
* Get the {@link #isExecuted() executed} flag.
* @see #execute()
*/
public boolean isExecuted() {
return this.executed;
}

View File

@@ -29,6 +29,7 @@ import org.springframework.util.Assert;
* Mock implementation of {@link ClientHttpResponse}.
*
* @author Rossen Stoyanchev
* @author Sam Brannen
* @since 3.2
*/
public class MockClientHttpResponse extends MockHttpInputMessage implements ClientHttpResponse {
@@ -37,7 +38,17 @@ public class MockClientHttpResponse extends MockHttpInputMessage implements Clie
/**
* Constructor with response body as a byte array.
* Create a {@code MockClientHttpResponse} with an empty response body and
* HTTP status code {@link HttpStatus#OK OK}.
* @since 6.0.3
*/
public MockClientHttpResponse() {
this(new byte[0], HttpStatus.OK);
}
/**
* Create a {@code MockClientHttpResponse} with response body as a byte array
* and the supplied HTTP status code.
*/
public MockClientHttpResponse(byte[] body, HttpStatusCode statusCode) {
super(body);
@@ -46,8 +57,8 @@ public class MockClientHttpResponse extends MockHttpInputMessage implements Clie
}
/**
* Variant of {@link #MockClientHttpResponse(byte[], HttpStatusCode)} with a
* custom HTTP status code.
* Create a {@code MockClientHttpResponse} with response body as a byte array
* and a custom HTTP status code.
* @since 5.3.17
*/
public MockClientHttpResponse(byte[] body, int statusCode) {
@@ -55,7 +66,8 @@ public class MockClientHttpResponse extends MockHttpInputMessage implements Clie
}
/**
* Constructor with response body as InputStream.
* Create a {@code MockClientHttpResponse} with response body as {@link InputStream}
* and the supplied HTTP status code.
*/
public MockClientHttpResponse(InputStream body, HttpStatusCode statusCode) {
super(body);
@@ -64,8 +76,8 @@ public class MockClientHttpResponse extends MockHttpInputMessage implements Clie
}
/**
* Variant of {@link #MockClientHttpResponse(InputStream, HttpStatusCode)} with a
* custom HTTP status code.
* Create a {@code MockClientHttpResponse} with response body as {@link InputStream}
* and a custom HTTP status code.
* @since 5.3.17
*/
public MockClientHttpResponse(InputStream body, int statusCode) {
@@ -86,12 +98,7 @@ public class MockClientHttpResponse extends MockHttpInputMessage implements Clie
@Override
public String getStatusText() {
if (this.statusCode instanceof HttpStatus status) {
return status.getReasonPhrase();
}
else {
return "";
}
return (this.statusCode instanceof HttpStatus status ? status.getReasonPhrase() : "");
}
@Override