Use HttpStatusCode interface

This commit contains changes made because of the introduction of
HttpStatusCode. In general, methods that used to return a HttpStatus
now return HttpStatusCode instead, and methods that returned raw status
codes are now deprecated.

See gh-28214
This commit is contained in:
Arjen Poutsma
2022-03-17 14:21:04 +01:00
parent ca4b6e86a4
commit 28ac0d3883
143 changed files with 1064 additions and 950 deletions

View File

@@ -20,6 +20,7 @@ import java.io.IOException;
import java.io.InputStream;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.mock.http.MockHttpInputMessage;
import org.springframework.util.Assert;
@@ -32,62 +33,65 @@ import org.springframework.util.Assert;
*/
public class MockClientHttpResponse extends MockHttpInputMessage implements ClientHttpResponse {
private final int statusCode;
private final HttpStatusCode statusCode;
/**
* Constructor with response body as a byte array.
*/
public MockClientHttpResponse(byte[] body, HttpStatus statusCode) {
public MockClientHttpResponse(byte[] body, HttpStatusCode statusCode) {
super(body);
Assert.notNull(statusCode, "HttpStatus is required");
this.statusCode = statusCode.value();
Assert.notNull(statusCode, "HttpStatusCode is required");
this.statusCode = statusCode;
}
/**
* Variant of {@link #MockClientHttpResponse(byte[], HttpStatus)} with a
* Variant of {@link #MockClientHttpResponse(byte[], HttpStatusCode)} with a
* custom HTTP status code.
* @since 5.3.17
*/
public MockClientHttpResponse(byte[] body, int statusCode) {
super(body);
this.statusCode = statusCode;
this(body, HttpStatusCode.valueOf(statusCode));
}
/**
* Constructor with response body as InputStream.
*/
public MockClientHttpResponse(InputStream body, HttpStatus statusCode) {
public MockClientHttpResponse(InputStream body, HttpStatusCode statusCode) {
super(body);
Assert.notNull(statusCode, "HttpStatus is required");
this.statusCode = statusCode.value();
this.statusCode = statusCode;
}
/**
* Variant of {@link #MockClientHttpResponse(InputStream, HttpStatus)} with a
* Variant of {@link #MockClientHttpResponse(InputStream, HttpStatusCode)} with a
* custom HTTP status code.
* @since 5.3.17
*/
public MockClientHttpResponse(InputStream body, int statusCode) {
super(body);
this.statusCode = statusCode;
this(body, HttpStatusCode.valueOf(statusCode));
}
@Override
public HttpStatus getStatusCode() {
return HttpStatus.valueOf(this.statusCode);
}
@Override
public int getRawStatusCode() {
public HttpStatusCode getStatusCode() {
return this.statusCode;
}
@Override
@Deprecated
public int getRawStatusCode() {
return this.statusCode.value();
}
@Override
public String getStatusText() {
HttpStatus status = HttpStatus.resolve(this.statusCode);
return (status != null ? status.getReasonPhrase() : "");
if (this.statusCode instanceof HttpStatus status) {
return status.getReasonPhrase();
}
else {
return "";
}
}
@Override

View File

@@ -30,6 +30,7 @@ import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseCookie;
import org.springframework.http.client.reactive.ClientHttpResponse;
@@ -46,7 +47,7 @@ import org.springframework.util.MultiValueMap;
*/
public class MockClientHttpResponse implements ClientHttpResponse {
private final int status;
private final HttpStatusCode statusCode;
private final HttpHeaders headers = new HttpHeaders();
@@ -55,25 +56,25 @@ public class MockClientHttpResponse implements ClientHttpResponse {
private Flux<DataBuffer> body = Flux.empty();
public MockClientHttpResponse(HttpStatus status) {
Assert.notNull(status, "HttpStatus is required");
this.status = status.value();
}
public MockClientHttpResponse(int status) {
Assert.isTrue(status > 99 && status < 1000, "Status must be between 100 and 999");
this.status = status;
this(HttpStatusCode.valueOf(status));
}
public MockClientHttpResponse(HttpStatusCode status) {
Assert.notNull(status, "HttpStatusCode is required");
this.statusCode = status;
}
@Override
public HttpStatus getStatusCode() {
return HttpStatus.valueOf(this.status);
public HttpStatusCode getStatusCode() {
return this.statusCode;
}
@Override
@Deprecated
public int getRawStatusCode() {
return this.status;
return this.statusCode.value();
}
@Override
@@ -140,7 +141,11 @@ public class MockClientHttpResponse implements ClientHttpResponse {
@Override
public String toString() {
HttpStatus code = HttpStatus.resolve(this.status);
return (code != null ? code.name() + "(" + this.status + ")" : "Status (" + this.status + ")") + this.headers;
if (this.statusCode instanceof HttpStatus status) {
return status.name() + "(" + this.statusCode + ")" + this.headers;
}
else {
return "Status (" + this.statusCode + ")" + this.headers;
}
}
}

View File

@@ -23,6 +23,7 @@ import java.util.List;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpResponse;
@@ -71,7 +72,7 @@ public class MockMvcClientHttpRequestFactory implements ClientHttpRequestFactory
.andReturn()
.getResponse();
HttpStatus status = HttpStatus.valueOf(servletResponse.getStatus());
HttpStatusCode status = HttpStatusCode.valueOf(servletResponse.getStatus());
byte[] body = servletResponse.getContentAsByteArray();
MockClientHttpResponse clientResponse = new MockClientHttpResponse(body, status);
clientResponse.getHeaders().putAll(getResponseHeaders(servletResponse));

View File

@@ -22,7 +22,7 @@ import java.nio.charset.StandardCharsets;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpResponse;
@@ -39,7 +39,7 @@ import org.springframework.util.Assert;
*/
public class DefaultResponseCreator implements ResponseCreator {
private final int statusCode;
private final HttpStatusCode statusCode;
private byte[] content = new byte[0];
@@ -52,18 +52,18 @@ public class DefaultResponseCreator implements ResponseCreator {
/**
* Protected constructor.
* Use static factory methods in {@link MockRestResponseCreators}.
* @since 5.3.17
*/
protected DefaultResponseCreator(HttpStatus statusCode) {
Assert.notNull(statusCode, "HttpStatus must not be null");
this.statusCode = statusCode.value();
protected DefaultResponseCreator(int statusCode) {
this(HttpStatusCode.valueOf(statusCode));
}
/**
* Protected constructor.
* Use static factory methods in {@link MockRestResponseCreators}.
* @since 5.3.17
*/
protected DefaultResponseCreator(int statusCode) {
protected DefaultResponseCreator(HttpStatusCode statusCode) {
Assert.notNull(statusCode, "HttpStatusCode must not be null");
this.statusCode = statusCode;
}

View File

@@ -21,6 +21,7 @@ import java.net.URI;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.lang.Nullable;
import org.springframework.test.web.client.ResponseCreator;
@@ -113,12 +114,12 @@ public abstract class MockRestResponseCreators {
* {@code ResponseCreator} with a specific HTTP status.
* @param status the response status
*/
public static DefaultResponseCreator withStatus(HttpStatus status) {
public static DefaultResponseCreator withStatus(HttpStatusCode status) {
return new DefaultResponseCreator(status);
}
/**
* Variant of {@link #withStatus(HttpStatus)} for a custom HTTP status code.
* Variant of {@link #withStatus(HttpStatusCode)} with an integer.
* @param status the response status
* @since 5.3.17
*/

View File

@@ -31,6 +31,7 @@ import reactor.core.publisher.Mono;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseCookie;
import org.springframework.http.client.reactive.ClientHttpRequest;
@@ -170,17 +171,18 @@ public class ExchangeResult {
/**
* Return the HTTP status code as an {@link HttpStatus} enum value.
* Return the HTTP status code as an {@link HttpStatusCode} value.
*/
public HttpStatus getStatus() {
public HttpStatusCode getStatus() {
return this.response.getStatusCode();
}
/**
* Return the HTTP status code (potentially non-standard and not resolvable
* through the {@link HttpStatus} enum) as an integer.
* Return the HTTP status code as an integer.
* @since 5.1.10
* @deprecated as of 6.0, in favor of {@link #getStatus()}
*/
@Deprecated
public int getRawStatusCode() {
return this.response.getRawStatusCode();
}
@@ -248,13 +250,22 @@ public class ExchangeResult {
"\n" +
formatBody(getRequestHeaders().getContentType(), this.requestBody) + "\n" +
"\n" +
"< " + getStatus() + " " + getStatus().getReasonPhrase() + "\n" +
"< " + getStatus() + " " + getReasonPhrase(getStatus()) + "\n" +
"< " + formatHeaders(getResponseHeaders(), "\n< ") + "\n" +
"\n" +
formatBody(getResponseHeaders().getContentType(), this.responseBody) +"\n" +
formatMockServerResult();
}
private static String getReasonPhrase(HttpStatusCode statusCode) {
if (statusCode instanceof HttpStatus status) {
return status.getReasonPhrase();
}
else {
return "";
}
}
private String formatHeaders(HttpHeaders headers, String delimiter) {
return headers.entrySet().stream()
.map(entry -> entry.getKey() + ": " + entry.getValue())

View File

@@ -31,6 +31,8 @@ import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.client.reactive.ClientHttpConnector;
import org.springframework.http.client.reactive.ClientHttpRequest;
import org.springframework.http.client.reactive.ClientHttpResponse;
@@ -142,8 +144,8 @@ public class HttpHandlerConnector implements ClientHttpConnector {
}
private ClientHttpResponse adaptResponse(MockServerHttpResponse response, Flux<DataBuffer> body) {
Integer status = response.getRawStatusCode();
MockClientHttpResponse clientResponse = new MockClientHttpResponse((status != null) ? status : 200);
HttpStatusCode status = response.getStatusCode();
MockClientHttpResponse clientResponse = new MockClientHttpResponse((status != null) ? status : HttpStatus.OK);
clientResponse.getHeaders().putAll(response.getHeaders());
clientResponse.getCookies().putAll(response.getCookies());
clientResponse.setBody(body);

View File

@@ -22,6 +22,7 @@ import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.test.util.AssertionErrors;
/**
@@ -45,19 +46,19 @@ public class StatusAssertions {
/**
* Assert the response status as an {@link HttpStatus}.
* Assert the response status as an {@link HttpStatusCode}.
*/
public WebTestClient.ResponseSpec isEqualTo(HttpStatus status) {
return isEqualTo(status.value());
public WebTestClient.ResponseSpec isEqualTo(HttpStatusCode status) {
HttpStatusCode actual = this.exchangeResult.getStatus();
this.exchangeResult.assertWithDiagnostics(() -> AssertionErrors.assertEquals("Status", status, actual));
return this.responseSpec;
}
/**
* Assert the response status as an integer.
*/
public WebTestClient.ResponseSpec isEqualTo(int status) {
int actual = this.exchangeResult.getRawStatusCode();
this.exchangeResult.assertWithDiagnostics(() -> AssertionErrors.assertEquals("Status", status, actual));
return this.responseSpec;
return isEqualTo(HttpStatusCode.valueOf(status));
}
/**
@@ -156,12 +157,22 @@ public class StatusAssertions {
* Assert the response error message.
*/
public WebTestClient.ResponseSpec reasonEquals(String reason) {
String actual = this.exchangeResult.getStatus().getReasonPhrase();
String actual = getReasonPhrase(this.exchangeResult.getStatus());
this.exchangeResult.assertWithDiagnostics(() ->
AssertionErrors.assertEquals("Response status reason", reason, actual));
return this.responseSpec;
}
private static String getReasonPhrase(HttpStatusCode statusCode) {
if (statusCode instanceof HttpStatus status) {
return status.getReasonPhrase();
}
else {
return "";
}
}
/**
* Assert the response status code is in the 1xx range.
*/
@@ -203,7 +214,7 @@ public class StatusAssertions {
* @since 5.1
*/
public WebTestClient.ResponseSpec value(Matcher<? super Integer> matcher) {
int actual = this.exchangeResult.getRawStatusCode();
int actual = this.exchangeResult.getStatus().value();
this.exchangeResult.assertWithDiagnostics(() -> MatcherAssert.assertThat("Response status", actual, matcher));
return this.responseSpec;
}
@@ -214,22 +225,23 @@ public class StatusAssertions {
* @since 5.1
*/
public WebTestClient.ResponseSpec value(Consumer<Integer> consumer) {
int actual = this.exchangeResult.getRawStatusCode();
int actual = this.exchangeResult.getStatus().value();
this.exchangeResult.assertWithDiagnostics(() -> consumer.accept(actual));
return this.responseSpec;
}
private WebTestClient.ResponseSpec assertStatusAndReturn(HttpStatus expected) {
HttpStatus actual = this.exchangeResult.getStatus();
private WebTestClient.ResponseSpec assertStatusAndReturn(HttpStatusCode expected) {
HttpStatusCode actual = this.exchangeResult.getStatus();
this.exchangeResult.assertWithDiagnostics(() -> AssertionErrors.assertEquals("Status", expected, actual));
return this.responseSpec;
}
private WebTestClient.ResponseSpec assertSeriesAndReturn(HttpStatus.Series expected) {
HttpStatus status = this.exchangeResult.getStatus();
HttpStatusCode status = this.exchangeResult.getStatus();
HttpStatus.Series series = HttpStatus.Series.resolve(status.value());
this.exchangeResult.assertWithDiagnostics(() ->
AssertionErrors.assertEquals("Range for response status value " + status, expected, status.series()));
AssertionErrors.assertEquals("Range for response status value " + status, expected, series));
return this.responseSpec;
}

View File

@@ -19,6 +19,7 @@ package org.springframework.test.web.servlet.result;
import org.hamcrest.Matcher;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultMatcher;
@@ -104,9 +105,7 @@ public class StatusResultMatchers {
}
private HttpStatus.Series getHttpStatusSeries(MvcResult result) {
int statusValue = result.getResponse().getStatus();
HttpStatus status = HttpStatus.valueOf(statusValue);
return status.series();
return HttpStatus.Series.resolve(result.getResponse().getStatus());
}
/**
@@ -623,7 +622,7 @@ public class StatusResultMatchers {
/**
* Match the expected response status to that of the HttpServletResponse.
*/
private ResultMatcher matcher(HttpStatus status) {
private ResultMatcher matcher(HttpStatusCode status) {
return result -> assertEquals("Status", status.value(), result.getResponse().getStatus());
}