Skip to content

Commit ee7f600

Browse files
committed
Polishing contribution
Closes gh-28105
1 parent f1fdc11 commit ee7f600

File tree

4 files changed

+33
-57
lines changed

4 files changed

+33
-57
lines changed

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

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,7 +32,7 @@
3232
*/
3333
public class MockClientHttpResponse extends MockHttpInputMessage implements ClientHttpResponse {
3434

35-
private final Object status;
35+
private final int statusCode;
3636

3737

3838
/**
@@ -41,15 +41,17 @@ public class MockClientHttpResponse extends MockHttpInputMessage implements Clie
4141
public MockClientHttpResponse(byte[] body, HttpStatus statusCode) {
4242
super(body);
4343
Assert.notNull(statusCode, "HttpStatus is required");
44-
this.status = statusCode;
44+
this.statusCode = statusCode.value();
4545
}
4646

4747
/**
48-
* Constructor with response body as a byte array.
48+
* Variant of {@link #MockClientHttpResponse(byte[], HttpStatus)} with a
49+
* custom HTTP status code.
50+
* @since 5.3.17
4951
*/
5052
public MockClientHttpResponse(byte[] body, int statusCode) {
5153
super(body);
52-
this.status = statusCode;
54+
this.statusCode = statusCode;
5355
}
5456

5557
/**
@@ -58,46 +60,34 @@ public MockClientHttpResponse(byte[] body, int statusCode) {
5860
public MockClientHttpResponse(InputStream body, HttpStatus statusCode) {
5961
super(body);
6062
Assert.notNull(statusCode, "HttpStatus is required");
61-
this.status = statusCode;
63+
this.statusCode = statusCode.value();
6264
}
6365

6466
/**
65-
* Constructor with response body as InputStream.
67+
* Variant of {@link #MockClientHttpResponse(InputStream, HttpStatus)} with a
68+
* custom HTTP status code.
69+
* @since 5.3.17
6670
*/
6771
public MockClientHttpResponse(InputStream body, int statusCode) {
6872
super(body);
69-
this.status = statusCode;
73+
this.statusCode = statusCode;
7074
}
7175

7276

7377
@Override
74-
public HttpStatus getStatusCode() throws IOException {
75-
if (this.status instanceof HttpStatus) {
76-
return (HttpStatus) this.status;
77-
}
78-
else {
79-
return HttpStatus.valueOf((Integer) this.status);
80-
}
78+
public HttpStatus getStatusCode() {
79+
return HttpStatus.valueOf(this.statusCode);
8180
}
8281

8382
@Override
84-
public int getRawStatusCode() throws IOException {
85-
if (this.status instanceof HttpStatus) {
86-
return ((HttpStatus) this.status).value();
87-
}
88-
else {
89-
return (Integer) this.status;
90-
}
83+
public int getRawStatusCode() {
84+
return this.statusCode;
9185
}
9286

9387
@Override
94-
public String getStatusText() throws IOException {
95-
if (this.status instanceof HttpStatus) {
96-
return ((HttpStatus) this.status).getReasonPhrase();
97-
}
98-
else {
99-
return "Custom http status";
100-
}
88+
public String getStatusText() {
89+
HttpStatus status = HttpStatus.resolve(this.statusCode);
90+
return (status != null ? status.getReasonPhrase() : "");
10191
}
10292

10393
@Override

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

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
1717
package org.springframework.test.web.client.response;
1818

1919
import java.io.IOException;
20-
import java.io.InputStream;
2120
import java.net.URI;
2221
import java.nio.charset.StandardCharsets;
2322

@@ -40,7 +39,7 @@
4039
*/
4140
public class DefaultResponseCreator implements ResponseCreator {
4241

43-
private final Object statusCode;
42+
private final int statusCode;
4443

4544
private byte[] content = new byte[0];
4645

@@ -56,12 +55,13 @@ public class DefaultResponseCreator implements ResponseCreator {
5655
*/
5756
protected DefaultResponseCreator(HttpStatus statusCode) {
5857
Assert.notNull(statusCode, "HttpStatus must not be null");
59-
this.statusCode = statusCode;
58+
this.statusCode = statusCode.value();
6059
}
6160

6261
/**
6362
* Protected constructor.
6463
* Use static factory methods in {@link MockRestResponseCreators}.
64+
* @since 5.3.17
6565
*/
6666
protected DefaultResponseCreator(int statusCode) {
6767
this.statusCode = statusCode;
@@ -119,24 +119,9 @@ public DefaultResponseCreator headers(HttpHeaders headers) {
119119

120120
@Override
121121
public ClientHttpResponse createResponse(@Nullable ClientHttpRequest request) throws IOException {
122-
MockClientHttpResponse response;
123-
if (this.contentResource != null) {
124-
InputStream stream = this.contentResource.getInputStream();
125-
if (this.statusCode instanceof HttpStatus) {
126-
response = new MockClientHttpResponse(stream, (HttpStatus) this.statusCode);
127-
}
128-
else {
129-
response = new MockClientHttpResponse(stream, (Integer) this.statusCode);
130-
}
131-
}
132-
else {
133-
if (this.statusCode instanceof HttpStatus) {
134-
response = new MockClientHttpResponse(this.content, (HttpStatus) this.statusCode);
135-
}
136-
else {
137-
response = new MockClientHttpResponse(this.content, (Integer) this.statusCode);
138-
}
139-
}
122+
MockClientHttpResponse response = (this.contentResource != null ?
123+
new MockClientHttpResponse(this.contentResource.getInputStream(), this.statusCode) :
124+
new MockClientHttpResponse(this.content, this.statusCode));
140125
response.getHeaders().putAll(this.headers);
141126
return response;
142127
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -118,10 +118,11 @@ public static DefaultResponseCreator withStatus(HttpStatus status) {
118118
}
119119

120120
/**
121-
* {@code ResponseCreator} with a specific HTTP status.
121+
* Variant of {@link #withStatus(HttpStatus)} for a custom HTTP status code.
122122
* @param status the response status
123+
* @since 5.3.17
123124
*/
124-
public static DefaultResponseCreator withStatus(int status) {
125+
public static DefaultResponseCreator withRawStatus(int status) {
125126
return new DefaultResponseCreator(status);
126127
}
127128

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -130,11 +130,11 @@ void withStatus() throws Exception {
130130

131131
@Test
132132
void withCustomStatus() throws Exception {
133-
DefaultResponseCreator responseCreator = MockRestResponseCreators.withStatus(454);
133+
DefaultResponseCreator responseCreator = MockRestResponseCreators.withRawStatus(454);
134134
MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);
135135

136136
assertThat(response.getRawStatusCode()).isEqualTo(454);
137-
assertThat(response.getStatusText()).isEqualTo("Custom http status");
137+
assertThat(response.getStatusText()).isEmpty();
138138
}
139139

140140
@Test

0 commit comments

Comments
 (0)