Skip to content

Commit 28ac0d3

Browse files
committed
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
1 parent ca4b6e8 commit 28ac0d3

File tree

143 files changed

+1052
-938
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

143 files changed

+1052
-938
lines changed

spring-test/src/main/java/org/springframework/mock/http/client/MockClientHttpResponse.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.InputStream;
2121

2222
import org.springframework.http.HttpStatus;
23+
import org.springframework.http.HttpStatusCode;
2324
import org.springframework.http.client.ClientHttpResponse;
2425
import org.springframework.mock.http.MockHttpInputMessage;
2526
import org.springframework.util.Assert;
@@ -32,62 +33,65 @@
3233
*/
3334
public class MockClientHttpResponse extends MockHttpInputMessage implements ClientHttpResponse {
3435

35-
private final int statusCode;
36+
private final HttpStatusCode statusCode;
3637

3738

3839
/**
3940
* Constructor with response body as a byte array.
4041
*/
41-
public MockClientHttpResponse(byte[] body, HttpStatus statusCode) {
42+
public MockClientHttpResponse(byte[] body, HttpStatusCode statusCode) {
4243
super(body);
43-
Assert.notNull(statusCode, "HttpStatus is required");
44-
this.statusCode = statusCode.value();
44+
Assert.notNull(statusCode, "HttpStatusCode is required");
45+
this.statusCode = statusCode;
4546
}
4647

4748
/**
48-
* Variant of {@link #MockClientHttpResponse(byte[], HttpStatus)} with a
49+
* Variant of {@link #MockClientHttpResponse(byte[], HttpStatusCode)} with a
4950
* custom HTTP status code.
5051
* @since 5.3.17
5152
*/
5253
public MockClientHttpResponse(byte[] body, int statusCode) {
53-
super(body);
54-
this.statusCode = statusCode;
54+
this(body, HttpStatusCode.valueOf(statusCode));
5555
}
5656

5757
/**
5858
* Constructor with response body as InputStream.
5959
*/
60-
public MockClientHttpResponse(InputStream body, HttpStatus statusCode) {
60+
public MockClientHttpResponse(InputStream body, HttpStatusCode statusCode) {
6161
super(body);
6262
Assert.notNull(statusCode, "HttpStatus is required");
63-
this.statusCode = statusCode.value();
63+
this.statusCode = statusCode;
6464
}
6565

6666
/**
67-
* Variant of {@link #MockClientHttpResponse(InputStream, HttpStatus)} with a
67+
* Variant of {@link #MockClientHttpResponse(InputStream, HttpStatusCode)} with a
6868
* custom HTTP status code.
6969
* @since 5.3.17
7070
*/
7171
public MockClientHttpResponse(InputStream body, int statusCode) {
72-
super(body);
73-
this.statusCode = statusCode;
72+
this(body, HttpStatusCode.valueOf(statusCode));
7473
}
7574

7675

7776
@Override
78-
public HttpStatus getStatusCode() {
79-
return HttpStatus.valueOf(this.statusCode);
77+
public HttpStatusCode getStatusCode() {
78+
return this.statusCode;
8079
}
8180

8281
@Override
82+
@Deprecated
8383
public int getRawStatusCode() {
84-
return this.statusCode;
84+
return this.statusCode.value();
8585
}
8686

8787
@Override
8888
public String getStatusText() {
89-
HttpStatus status = HttpStatus.resolve(this.statusCode);
90-
return (status != null ? status.getReasonPhrase() : "");
89+
if (this.statusCode instanceof HttpStatus status) {
90+
return status.getReasonPhrase();
91+
}
92+
else {
93+
return "";
94+
}
9195
}
9296

9397
@Override

spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpResponse.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
3131
import org.springframework.http.HttpHeaders;
3232
import org.springframework.http.HttpStatus;
33+
import org.springframework.http.HttpStatusCode;
3334
import org.springframework.http.MediaType;
3435
import org.springframework.http.ResponseCookie;
3536
import org.springframework.http.client.reactive.ClientHttpResponse;
@@ -46,7 +47,7 @@
4647
*/
4748
public class MockClientHttpResponse implements ClientHttpResponse {
4849

49-
private final int status;
50+
private final HttpStatusCode statusCode;
5051

5152
private final HttpHeaders headers = new HttpHeaders();
5253

@@ -55,25 +56,25 @@ public class MockClientHttpResponse implements ClientHttpResponse {
5556
private Flux<DataBuffer> body = Flux.empty();
5657

5758

58-
public MockClientHttpResponse(HttpStatus status) {
59-
Assert.notNull(status, "HttpStatus is required");
60-
this.status = status.value();
59+
public MockClientHttpResponse(int status) {
60+
this(HttpStatusCode.valueOf(status));
6161
}
6262

63-
public MockClientHttpResponse(int status) {
64-
Assert.isTrue(status > 99 && status < 1000, "Status must be between 100 and 999");
65-
this.status = status;
63+
public MockClientHttpResponse(HttpStatusCode status) {
64+
Assert.notNull(status, "HttpStatusCode is required");
65+
this.statusCode = status;
6666
}
6767

6868

6969
@Override
70-
public HttpStatus getStatusCode() {
71-
return HttpStatus.valueOf(this.status);
70+
public HttpStatusCode getStatusCode() {
71+
return this.statusCode;
7272
}
7373

7474
@Override
75+
@Deprecated
7576
public int getRawStatusCode() {
76-
return this.status;
77+
return this.statusCode.value();
7778
}
7879

7980
@Override
@@ -140,7 +141,11 @@ private Charset getCharset() {
140141

141142
@Override
142143
public String toString() {
143-
HttpStatus code = HttpStatus.resolve(this.status);
144-
return (code != null ? code.name() + "(" + this.status + ")" : "Status (" + this.status + ")") + this.headers;
144+
if (this.statusCode instanceof HttpStatus status) {
145+
return status.name() + "(" + this.statusCode + ")" + this.headers;
146+
}
147+
else {
148+
return "Status (" + this.statusCode + ")" + this.headers;
149+
}
145150
}
146151
}

spring-test/src/main/java/org/springframework/test/web/client/MockMvcClientHttpRequestFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.http.HttpHeaders;
2424
import org.springframework.http.HttpMethod;
2525
import org.springframework.http.HttpStatus;
26+
import org.springframework.http.HttpStatusCode;
2627
import org.springframework.http.client.ClientHttpRequest;
2728
import org.springframework.http.client.ClientHttpRequestFactory;
2829
import org.springframework.http.client.ClientHttpResponse;
@@ -71,7 +72,7 @@ private ClientHttpResponse getClientHttpResponse(
7172
.andReturn()
7273
.getResponse();
7374

74-
HttpStatus status = HttpStatus.valueOf(servletResponse.getStatus());
75+
HttpStatusCode status = HttpStatusCode.valueOf(servletResponse.getStatus());
7576
byte[] body = servletResponse.getContentAsByteArray();
7677
MockClientHttpResponse clientResponse = new MockClientHttpResponse(body, status);
7778
clientResponse.getHeaders().putAll(getResponseHeaders(servletResponse));

spring-test/src/main/java/org/springframework/test/web/client/response/DefaultResponseCreator.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import org.springframework.core.io.Resource;
2424
import org.springframework.http.HttpHeaders;
25-
import org.springframework.http.HttpStatus;
25+
import org.springframework.http.HttpStatusCode;
2626
import org.springframework.http.MediaType;
2727
import org.springframework.http.client.ClientHttpRequest;
2828
import org.springframework.http.client.ClientHttpResponse;
@@ -39,7 +39,7 @@
3939
*/
4040
public class DefaultResponseCreator implements ResponseCreator {
4141

42-
private final int statusCode;
42+
private final HttpStatusCode statusCode;
4343

4444
private byte[] content = new byte[0];
4545

@@ -52,18 +52,18 @@ public class DefaultResponseCreator implements ResponseCreator {
5252
/**
5353
* Protected constructor.
5454
* Use static factory methods in {@link MockRestResponseCreators}.
55+
* @since 5.3.17
5556
*/
56-
protected DefaultResponseCreator(HttpStatus statusCode) {
57-
Assert.notNull(statusCode, "HttpStatus must not be null");
58-
this.statusCode = statusCode.value();
57+
protected DefaultResponseCreator(int statusCode) {
58+
this(HttpStatusCode.valueOf(statusCode));
5959
}
6060

6161
/**
6262
* Protected constructor.
6363
* Use static factory methods in {@link MockRestResponseCreators}.
64-
* @since 5.3.17
6564
*/
66-
protected DefaultResponseCreator(int statusCode) {
65+
protected DefaultResponseCreator(HttpStatusCode statusCode) {
66+
Assert.notNull(statusCode, "HttpStatusCode must not be null");
6767
this.statusCode = statusCode;
6868
}
6969

spring-test/src/main/java/org/springframework/test/web/client/response/MockRestResponseCreators.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.springframework.core.io.Resource;
2323
import org.springframework.http.HttpStatus;
24+
import org.springframework.http.HttpStatusCode;
2425
import org.springframework.http.MediaType;
2526
import org.springframework.lang.Nullable;
2627
import org.springframework.test.web.client.ResponseCreator;
@@ -113,12 +114,12 @@ public static DefaultResponseCreator withServerError() {
113114
* {@code ResponseCreator} with a specific HTTP status.
114115
* @param status the response status
115116
*/
116-
public static DefaultResponseCreator withStatus(HttpStatus status) {
117+
public static DefaultResponseCreator withStatus(HttpStatusCode status) {
117118
return new DefaultResponseCreator(status);
118119
}
119120

120121
/**
121-
* Variant of {@link #withStatus(HttpStatus)} for a custom HTTP status code.
122+
* Variant of {@link #withStatus(HttpStatusCode)} with an integer.
122123
* @param status the response status
123124
* @since 5.3.17
124125
*/

spring-test/src/main/java/org/springframework/test/web/reactive/server/ExchangeResult.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.http.HttpHeaders;
3232
import org.springframework.http.HttpMethod;
3333
import org.springframework.http.HttpStatus;
34+
import org.springframework.http.HttpStatusCode;
3435
import org.springframework.http.MediaType;
3536
import org.springframework.http.ResponseCookie;
3637
import org.springframework.http.client.reactive.ClientHttpRequest;
@@ -170,17 +171,18 @@ public byte[] getRequestBodyContent() {
170171

171172

172173
/**
173-
* Return the HTTP status code as an {@link HttpStatus} enum value.
174+
* Return the HTTP status code as an {@link HttpStatusCode} value.
174175
*/
175-
public HttpStatus getStatus() {
176+
public HttpStatusCode getStatus() {
176177
return this.response.getStatusCode();
177178
}
178179

179180
/**
180-
* Return the HTTP status code (potentially non-standard and not resolvable
181-
* through the {@link HttpStatus} enum) as an integer.
181+
* Return the HTTP status code as an integer.
182182
* @since 5.1.10
183+
* @deprecated as of 6.0, in favor of {@link #getStatus()}
183184
*/
185+
@Deprecated
184186
public int getRawStatusCode() {
185187
return this.response.getRawStatusCode();
186188
}
@@ -248,13 +250,22 @@ public String toString() {
248250
"\n" +
249251
formatBody(getRequestHeaders().getContentType(), this.requestBody) + "\n" +
250252
"\n" +
251-
"< " + getStatus() + " " + getStatus().getReasonPhrase() + "\n" +
253+
"< " + getStatus() + " " + getReasonPhrase(getStatus()) + "\n" +
252254
"< " + formatHeaders(getResponseHeaders(), "\n< ") + "\n" +
253255
"\n" +
254256
formatBody(getResponseHeaders().getContentType(), this.responseBody) +"\n" +
255257
formatMockServerResult();
256258
}
257259

260+
private static String getReasonPhrase(HttpStatusCode statusCode) {
261+
if (statusCode instanceof HttpStatus status) {
262+
return status.getReasonPhrase();
263+
}
264+
else {
265+
return "";
266+
}
267+
}
268+
258269
private String formatHeaders(HttpHeaders headers, String delimiter) {
259270
return headers.entrySet().stream()
260271
.map(entry -> entry.getKey() + ": " + entry.getValue())

spring-test/src/main/java/org/springframework/test/web/reactive/server/HttpHandlerConnector.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import org.springframework.http.HttpCookie;
3232
import org.springframework.http.HttpHeaders;
3333
import org.springframework.http.HttpMethod;
34+
import org.springframework.http.HttpStatus;
35+
import org.springframework.http.HttpStatusCode;
3436
import org.springframework.http.client.reactive.ClientHttpConnector;
3537
import org.springframework.http.client.reactive.ClientHttpRequest;
3638
import org.springframework.http.client.reactive.ClientHttpResponse;
@@ -142,8 +144,8 @@ private ServerHttpResponse prepareResponse(ServerHttpResponse response, ServerHt
142144
}
143145

144146
private ClientHttpResponse adaptResponse(MockServerHttpResponse response, Flux<DataBuffer> body) {
145-
Integer status = response.getRawStatusCode();
146-
MockClientHttpResponse clientResponse = new MockClientHttpResponse((status != null) ? status : 200);
147+
HttpStatusCode status = response.getStatusCode();
148+
MockClientHttpResponse clientResponse = new MockClientHttpResponse((status != null) ? status : HttpStatus.OK);
147149
clientResponse.getHeaders().putAll(response.getHeaders());
148150
clientResponse.getCookies().putAll(response.getCookies());
149151
clientResponse.setBody(body);

0 commit comments

Comments
 (0)