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

View File

@@ -22,50 +22,44 @@ import java.io.IOException;
import javax.imageio.ImageIO;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.testfixture.http.MockHttpInputMessage;
import org.springframework.web.testfixture.http.MockHttpOutputMessage;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Unit tests for BufferedImageHttpMessageConverter.
*
* @author Arjen Poutsma
* @author Rossen Stoyanchev
*/
public class BufferedImageHttpMessageConverterTests {
class BufferedImageHttpMessageConverterTests {
private BufferedImageHttpMessageConverter converter;
private final BufferedImageHttpMessageConverter converter = new BufferedImageHttpMessageConverter();
@BeforeEach
public void setUp() {
converter = new BufferedImageHttpMessageConverter();
}
@Test
public void canRead() {
void canRead() {
assertThat(converter.canRead(BufferedImage.class, null)).as("Image not supported").isTrue();
assertThat(converter.canRead(BufferedImage.class, new MediaType("image", "png"))).as("Image not supported").isTrue();
}
@Test
public void canWrite() {
void canWrite() {
assertThat(converter.canWrite(BufferedImage.class, null)).as("Image not supported").isTrue();
assertThat(converter.canWrite(BufferedImage.class, new MediaType("image", "png"))).as("Image not supported").isTrue();
assertThat(converter.canWrite(BufferedImage.class, new MediaType("*", "*"))).as("Image not supported").isTrue();
}
@Test
public void read() throws IOException {
void read() throws IOException {
Resource logo = new ClassPathResource("logo.jpg", BufferedImageHttpMessageConverterTests.class);
byte[] body = FileCopyUtils.copyToByteArray(logo.getInputStream());
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
MockHttpInputMessage inputMessage = new MockHttpInputMessage(logo.getInputStream());
inputMessage.getHeaders().setContentType(new MediaType("image", "jpeg"));
BufferedImage result = converter.read(BufferedImage.class, inputMessage);
assertThat(result.getHeight()).as("Invalid height").isEqualTo(500);
@@ -73,13 +67,13 @@ public class BufferedImageHttpMessageConverterTests {
}
@Test
public void write() throws IOException {
void write() throws IOException {
Resource logo = new ClassPathResource("logo.jpg", BufferedImageHttpMessageConverterTests.class);
BufferedImage body = ImageIO.read(logo.getFile());
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
MediaType contentType = new MediaType("image", "png");
converter.write(body, contentType, outputMessage);
assertThat(outputMessage.getWrittenHeaders().getContentType()).as("Invalid content type").isEqualTo(contentType);
assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content type").isEqualTo(contentType);
assertThat(outputMessage.getBodyAsBytes().length > 0).as("Invalid size").isTrue();
BufferedImage result = ImageIO.read(new ByteArrayInputStream(outputMessage.getBodyAsBytes()));
assertThat(result.getHeight()).as("Invalid height").isEqualTo(500);
@@ -87,14 +81,14 @@ public class BufferedImageHttpMessageConverterTests {
}
@Test
public void writeDefaultContentType() throws IOException {
void writeDefaultContentType() throws IOException {
Resource logo = new ClassPathResource("logo.jpg", BufferedImageHttpMessageConverterTests.class);
MediaType contentType = new MediaType("image", "png");
converter.setDefaultContentType(contentType);
BufferedImage body = ImageIO.read(logo.getFile());
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
converter.write(body, new MediaType("*", "*"), outputMessage);
assertThat(outputMessage.getWrittenHeaders().getContentType()).as("Invalid content type").isEqualTo(contentType);
assertThat(outputMessage.getHeaders().getContentType()).as("Invalid content type").isEqualTo(contentType);
assertThat(outputMessage.getBodyAsBytes().length > 0).as("Invalid size").isTrue();
BufferedImage result = ImageIO.read(new ByteArrayInputStream(outputMessage.getBodyAsBytes()));
assertThat(result.getHeight()).as("Invalid height").isEqualTo(500);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 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.
@@ -22,8 +22,8 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.web.testfixture.http.MockHttpInputMessage;
import org.springframework.web.testfixture.http.MockHttpOutputMessage;
import static org.assertj.core.api.Assertions.assertThat;

View File

@@ -41,12 +41,12 @@ import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
import org.springframework.http.converter.xml.SourceHttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.testfixture.http.MockHttpInputMessage;
import org.springframework.web.testfixture.http.MockHttpOutputMessage;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED;

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.
@@ -29,9 +29,9 @@ import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.ContentDisposition;
import org.springframework.http.MediaType;
import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.testfixture.http.MockHttpInputMessage;
import org.springframework.web.testfixture.http.MockHttpOutputMessage;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 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,8 +34,8 @@ import org.springframework.core.io.support.ResourceRegion;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRange;
import org.springframework.http.MediaType;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.util.StringUtils;
import org.springframework.web.testfixture.http.MockHttpOutputMessage;
import static org.assertj.core.api.Assertions.assertThat;

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.
@@ -24,8 +24,8 @@ import org.junit.jupiter.api.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.web.testfixture.http.MockHttpInputMessage;
import org.springframework.web.testfixture.http.MockHttpOutputMessage;
import static org.assertj.core.api.Assertions.assertThat;

View File

@@ -33,8 +33,8 @@ import org.xmlunit.diff.NodeMatcher;
import org.springframework.core.testfixture.xml.XmlContent;
import org.springframework.http.MediaType;
import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.web.testfixture.http.MockHttpInputMessage;
import org.springframework.web.testfixture.http.MockHttpOutputMessage;
import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThat;

View File

@@ -29,8 +29,8 @@ import org.junit.jupiter.api.Test;
import org.springframework.core.testfixture.xml.XmlContent;
import org.springframework.http.MediaType;
import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.web.testfixture.http.MockHttpInputMessage;
import org.springframework.web.testfixture.http.MockHttpOutputMessage;
import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThat;

View File

@@ -31,9 +31,9 @@ import org.skyscreamer.jsonassert.JSONAssert;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.MediaType;
import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.testfixture.http.MockHttpInputMessage;
import org.springframework.web.testfixture.http.MockHttpOutputMessage;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

View File

@@ -31,9 +31,9 @@ import org.skyscreamer.jsonassert.JSONAssert;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.MediaType;
import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.testfixture.http.MockHttpInputMessage;
import org.springframework.web.testfixture.http.MockHttpOutputMessage;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

View File

@@ -41,11 +41,11 @@ import org.skyscreamer.jsonassert.JSONAssert;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.MediaType;
import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.lang.Nullable;
import org.springframework.web.testfixture.http.MockHttpInputMessage;
import org.springframework.web.testfixture.http.MockHttpOutputMessage;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

View File

@@ -26,10 +26,10 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.protobuf.Msg;
import org.springframework.protobuf.SecondMsg;
import org.springframework.web.testfixture.http.MockHttpInputMessage;
import org.springframework.web.testfixture.http.MockHttpOutputMessage;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

View File

@@ -23,10 +23,10 @@ import com.google.protobuf.util.JsonFormat;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.protobuf.Msg;
import org.springframework.protobuf.SecondMsg;
import org.springframework.web.testfixture.http.MockHttpInputMessage;
import org.springframework.web.testfixture.http.MockHttpOutputMessage;
import static org.assertj.core.api.Assertions.assertThat;

View File

@@ -23,8 +23,8 @@ import com.fasterxml.jackson.dataformat.smile.SmileFactory;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.web.testfixture.http.MockHttpInputMessage;
import org.springframework.web.testfixture.http.MockHttpOutputMessage;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.within;

View File

@@ -34,8 +34,8 @@ import org.junit.jupiter.api.Test;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.testfixture.http.MockHttpInputMessage;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

View File

@@ -37,9 +37,9 @@ import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.testfixture.xml.XmlContent;
import org.springframework.http.MediaType;
import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.testfixture.http.MockHttpInputMessage;
import org.springframework.web.testfixture.http.MockHttpOutputMessage;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

View File

@@ -26,10 +26,10 @@ import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.MediaType;
import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.web.testfixture.http.MockHttpInputMessage;
import org.springframework.web.testfixture.http.MockHttpOutputMessage;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

View File

@@ -25,14 +25,14 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.MediaType;
import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.MarshallingFailureException;
import org.springframework.oxm.Unmarshaller;
import org.springframework.oxm.UnmarshallingFailureException;
import org.springframework.web.testfixture.http.MockHttpInputMessage;
import org.springframework.web.testfixture.http.MockHttpOutputMessage;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

View File

@@ -43,10 +43,10 @@ import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.testfixture.xml.XmlContent;
import org.springframework.http.MediaType;
import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.testfixture.http.MockHttpInputMessage;
import org.springframework.web.testfixture.http.MockHttpOutputMessage;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 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.
@@ -21,11 +21,11 @@ import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.testfixture.http.MockHttpOutputMessage;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import org.springframework.web.testfixture.servlet.MockPart;

View File

@@ -32,9 +32,9 @@ import org.junit.jupiter.api.Test
import org.springframework.core.Ordered
import org.springframework.http.MediaType
import org.springframework.http.MockHttpInputMessage
import org.springframework.http.MockHttpOutputMessage
import org.springframework.http.converter.HttpMessageNotReadableException
import org.springframework.web.testfixture.http.MockHttpInputMessage
import org.springframework.web.testfixture.http.MockHttpOutputMessage
/**
* Tests for the CBOR conversion using kotlinx.serialization.

View File

@@ -30,9 +30,9 @@ import kotlin.reflect.typeOf
import org.springframework.core.Ordered
import org.springframework.core.ResolvableType
import org.springframework.http.MediaType
import org.springframework.http.MockHttpInputMessage
import org.springframework.http.MockHttpOutputMessage
import org.springframework.http.converter.HttpMessageNotReadableException
import org.springframework.web.testfixture.http.MockHttpInputMessage
import org.springframework.web.testfixture.http.MockHttpOutputMessage
/**
* Tests for the JSON conversion using kotlinx.serialization.

View File

@@ -25,9 +25,9 @@ import org.assertj.core.api.Assertions.assertThatExceptionOfType
import org.junit.jupiter.api.Test
import org.springframework.core.Ordered
import org.springframework.http.MediaType
import org.springframework.http.MockHttpInputMessage
import org.springframework.http.MockHttpOutputMessage
import org.springframework.http.converter.HttpMessageNotReadableException
import org.springframework.web.testfixture.http.MockHttpInputMessage
import org.springframework.web.testfixture.http.MockHttpOutputMessage
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
import java.nio.charset.StandardCharsets

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 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.
@@ -14,12 +14,14 @@
* limitations under the License.
*/
package org.springframework.http;
package org.springframework.web.testfixture.http;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.util.Assert;
/**
@@ -35,25 +37,31 @@ public class MockHttpInputMessage implements HttpInputMessage {
private final InputStream body;
public MockHttpInputMessage(byte[] contents) {
Assert.notNull(contents, "'contents' must not be null");
this.body = new ByteArrayInputStream(contents);
/**
* 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, "'body' must not be null");
Assert.notNull(body, "InputStream must not be null");
this.body = body;
}
@Override
public HttpHeaders getHeaders() {
return headers;
return this.headers;
}
@Override
public InputStream getBody() throws IOException {
return body;
return this.body;
}
}

View File

@@ -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.
@@ -14,64 +14,61 @@
* limitations under the License.
*/
package org.springframework.http;
package org.springframework.web.testfixture.http;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpOutputMessage;
import org.springframework.util.StreamUtils;
/**
* @author Arjen Poutsma
* Mock implementation of {@link HttpOutputMessage}.
*
* @author Rossen Stoyanchev
* @since 3.2
*/
public class MockHttpOutputMessage implements HttpOutputMessage {
private final HttpHeaders headers = new HttpHeaders();
private final ByteArrayOutputStream body = new ByteArrayOutputStream();
private boolean headersWritten = false;
private final HttpHeaders writtenHeaders = new HttpHeaders();
private final ByteArrayOutputStream body = new ByteArrayOutputStream(1024);
@Override
public HttpHeaders getHeaders() {
return (this.headersWritten ? HttpHeaders.readOnlyHttpHeaders(this.headers) : this.headers);
}
/**
* Return a copy of the actual headers written at the time of the call to
* getResponseBody, i.e. ignoring any further changes that may have been made to
* the underlying headers, e.g. via a previously obtained instance.
*/
public HttpHeaders getWrittenHeaders() {
return writtenHeaders;
return this.headers;
}
@Override
public OutputStream getBody() throws IOException {
writeHeaders();
return body;
return this.body;
}
/**
* Return the body content as a byte array.
*/
public byte[] getBodyAsBytes() {
writeHeaders();
return body.toByteArray();
return this.body.toByteArray();
}
/**
* Return the body content interpreted as a UTF-8 string.
*/
public String getBodyAsString() {
return getBodyAsString(StandardCharsets.UTF_8);
}
/**
* 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) {
byte[] bytes = getBodyAsBytes();
return new String(bytes, charset);
}
private void writeHeaders() {
if (this.headersWritten) {
return;
}
this.headersWritten = true;
this.writtenHeaders.putAll(this.headers);
return StreamUtils.copyToString(this.body, charset);
}
}

View File

@@ -16,78 +16,67 @@
package org.springframework.web.testfixture.http.client;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StreamUtils;
import org.springframework.web.testfixture.http.MockHttpOutputMessage;
import org.springframework.web.util.UriComponentsBuilder;
/**
* Mock implementation of {@link ClientHttpRequest}.
*
* @author Brian Clozel
* @author Rossen Stoyanchev
* @author Brian Clozel
* @author Sam Brannen
* @since 3.2
*/
public class MockClientHttpRequest implements ClientHttpRequest {
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
private final HttpHeaders headers = new HttpHeaders();
public class MockClientHttpRequest extends MockHttpOutputMessage implements ClientHttpRequest {
private HttpMethod httpMethod;
private URI uri;
private final ByteArrayOutputStream body = new ByteArrayOutputStream(1024);
@Nullable
private ClientHttpResponse clientHttpResponse;
private boolean executed = false;
/**
* 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("/"));
}
public MockClientHttpRequest(HttpMethod httpMethod, String urlTemplate, Object... vars) {
/**
* 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;
this.uri = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(vars).encode().toUri();
this.uri = uri;
}
@Override
public HttpHeaders getHeaders() {
return this.headers;
}
@Override
public OutputStream getBody() throws IOException {
return this.body;
}
public byte[] getBodyAsBytes() {
return this.body.toByteArray();
}
public String getBodyAsString() {
return getBodyAsString(DEFAULT_CHARSET);
}
public String getBodyAsString(Charset charset) {
return StreamUtils.copyToString(this.body, charset);
}
/**
* Set the HTTP method of the request.
*/
public void setMethod(HttpMethod httpMethod) {
this.httpMethod = httpMethod;
}
@@ -97,13 +86,9 @@ public class MockClientHttpRequest implements ClientHttpRequest {
return this.httpMethod;
}
@SuppressWarnings("removal")
@Override
@Deprecated
public String getMethodValue() {
return this.httpMethod.name();
}
/**
* Set the URI of the request.
*/
public void setURI(URI uri) {
this.uri = uri;
}
@@ -113,25 +98,46 @@ public class MockClientHttpRequest implements ClientHttpRequest {
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;
}
/**
* Set the {@link #isExecuted() executed} flag to {@code true} and return the
* configured {@link #setResponse(ClientHttpResponse) response}.
* @see #executeInternal()
*/
@Override
public final ClientHttpResponse execute() throws IOException {
this.executed = true;
return executeInternal();
}
/**
* The default implementation returns the configured
* {@link #setResponse(ClientHttpResponse) response}.
* <p>Override this method to execute the request and provide a response,
* potentially different from the configured response.
*/
protected ClientHttpResponse executeInternal() throws IOException {
Assert.state(this.clientHttpResponse != null, "No ClientHttpResponse");
return this.clientHttpResponse;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();

View File

@@ -16,74 +16,89 @@
package org.springframework.web.testfixture.http.client;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.Assert;
import org.springframework.web.testfixture.http.MockHttpInputMessage;
/**
* Mock implementation of {@link ClientHttpResponse}.
*
* @author Rossen Stoyanchev
* @author Brian Clozel
* @author Sam Brannen
* @since 3.2
*/
public class MockClientHttpResponse implements ClientHttpResponse {
public class MockClientHttpResponse extends MockHttpInputMessage implements ClientHttpResponse {
private final HttpHeaders headers = new HttpHeaders();
private final HttpStatus status;
private InputStream body;
private final HttpStatusCode statusCode;
/**
* Create a {@code MockClientHttpResponse} with an empty response body and
* HTTP status code {@link HttpStatus#OK OK}.
* @since 6.0.3
*/
public MockClientHttpResponse() {
this.status = HttpStatus.OK;
this(new byte[0], HttpStatus.OK);
}
public MockClientHttpResponse(HttpStatus statusCode) {
Assert.notNull(statusCode, "HttpStatus is required");
this.status = statusCode;
/**
* 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);
Assert.notNull(statusCode, "HttpStatusCode must not be null");
this.statusCode = statusCode;
}
/**
* 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) {
this(body, HttpStatusCode.valueOf(statusCode));
}
/**
* Create a {@code MockClientHttpResponse} with response body as {@link InputStream}
* and the supplied HTTP status code.
*/
public MockClientHttpResponse(InputStream body, HttpStatusCode statusCode) {
super(body);
Assert.notNull(statusCode, "HttpStatusCode must not be null");
this.statusCode = statusCode;
}
/**
* 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) {
this(body, HttpStatusCode.valueOf(statusCode));
}
@Override
public HttpStatusCode getStatusCode() {
return this.statusCode;
}
@Override
public HttpStatus getStatusCode() throws IOException {
return this.status;
@Deprecated
public int getRawStatusCode() {
return this.statusCode.value();
}
@Override
@SuppressWarnings("deprecation")
public int getRawStatusCode() throws IOException {
return this.status.value();
}
@Override
public String getStatusText() throws IOException {
return this.status.getReasonPhrase();
}
@Override
public HttpHeaders getHeaders() {
return this.headers;
}
@Override
public InputStream getBody() throws IOException {
return this.body;
}
public void setBody(byte[] body) {
Assert.notNull(body, "body is required");
this.body = new ByteArrayInputStream(body);
}
public void setBody(String body) {
Assert.notNull(body, "body is required");
this.body = new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8));
public String getStatusText() {
return (this.statusCode instanceof HttpStatus status ? status.getReasonPhrase() : "");
}
@Override