Skip to content

Commit

Permalink
Introduce additional constructors in MockClientHttp[Request|Response]
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sbrannen committed Dec 9, 2022
1 parent 9b38e43 commit 83eb8ac
Show file tree
Hide file tree
Showing 31 changed files with 270 additions and 218 deletions.
@@ -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.
Expand Down Expand Up @@ -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;
Expand Down
@@ -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.
Expand Down Expand Up @@ -34,31 +34,23 @@
*/
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();
Expand All @@ -68,12 +60,12 @@ public byte[] getBodyAsBytes() {
* 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);
Expand Down
Expand Up @@ -25,11 +25,13 @@
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
*/
Expand All @@ -46,22 +48,35 @@ 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;
this.uri = uri;
}


/**
* Set the HTTP method of the request.
*/
public void setMethod(HttpMethod httpMethod) {
this.httpMethod = httpMethod;
}
Expand All @@ -71,6 +86,9 @@ public HttpMethod getMethod() {
return this.httpMethod;
}

/**
* Set the URI of the request.
*/
public void setURI(URI uri) {
this.uri = uri;
}
Expand All @@ -80,10 +98,19 @@ public URI getURI() {
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;
}
Expand Down
Expand Up @@ -29,6 +29,7 @@
* Mock implementation of {@link ClientHttpResponse}.
*
* @author Rossen Stoyanchev
* @author Sam Brannen
* @since 3.2
*/
public class MockClientHttpResponse extends MockHttpInputMessage implements ClientHttpResponse {
Expand All @@ -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);
Expand All @@ -46,16 +57,17 @@ public MockClientHttpResponse(byte[] body, HttpStatusCode statusCode) {
}

/**
* 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) {
this(body, HttpStatusCode.valueOf(statusCode));
}

/**
* 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);
Expand All @@ -64,8 +76,8 @@ public MockClientHttpResponse(InputStream body, HttpStatusCode statusCode) {
}

/**
* 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) {
Expand All @@ -86,12 +98,7 @@ public int getRawStatusCode() {

@Override
public String getStatusText() {
if (this.statusCode instanceof HttpStatus status) {
return status.getReasonPhrase();
}
else {
return "";
}
return (this.statusCode instanceof HttpStatus status ? status.getReasonPhrase() : "");
}

@Override
Expand Down
Expand Up @@ -22,79 +22,73 @@

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);
assertThat(result.getWidth()).as("Invalid width").isEqualTo(750);
}

@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);
assertThat(result.getWidth()).as("Invalid width").isEqualTo(750);
}

@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);
Expand Down
@@ -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.
Expand All @@ -22,8 +22,8 @@
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;

Expand Down
Expand Up @@ -41,12 +41,12 @@
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;
Expand Down
@@ -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.
Expand Down Expand Up @@ -29,9 +29,9 @@
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;
Expand Down

0 comments on commit 83eb8ac

Please sign in to comment.