Skip to content

Commit f1fdc11

Browse files
AzZuremanrstoyanchev
authored andcommitted
Support custom HTTP status in MockClientHttpResponse
See gh-28105
1 parent 21d6131 commit f1fdc11

File tree

4 files changed

+73
-7
lines changed

4 files changed

+73
-7
lines changed

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

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*/
3333
public class MockClientHttpResponse extends MockHttpInputMessage implements ClientHttpResponse {
3434

35-
private final HttpStatus status;
35+
private final Object status;
3636

3737

3838
/**
@@ -44,6 +44,14 @@ public MockClientHttpResponse(byte[] body, HttpStatus statusCode) {
4444
this.status = statusCode;
4545
}
4646

47+
/**
48+
* Constructor with response body as a byte array.
49+
*/
50+
public MockClientHttpResponse(byte[] body, int statusCode) {
51+
super(body);
52+
this.status = statusCode;
53+
}
54+
4755
/**
4856
* Constructor with response body as InputStream.
4957
*/
@@ -53,20 +61,43 @@ public MockClientHttpResponse(InputStream body, HttpStatus statusCode) {
5361
this.status = statusCode;
5462
}
5563

64+
/**
65+
* Constructor with response body as InputStream.
66+
*/
67+
public MockClientHttpResponse(InputStream body, int statusCode) {
68+
super(body);
69+
this.status = statusCode;
70+
}
71+
5672

5773
@Override
5874
public HttpStatus getStatusCode() throws IOException {
59-
return this.status;
75+
if (this.status instanceof HttpStatus) {
76+
return (HttpStatus) this.status;
77+
}
78+
else {
79+
return HttpStatus.valueOf((Integer) this.status);
80+
}
6081
}
6182

6283
@Override
6384
public int getRawStatusCode() throws IOException {
64-
return this.status.value();
85+
if (this.status instanceof HttpStatus) {
86+
return ((HttpStatus) this.status).value();
87+
}
88+
else {
89+
return (Integer) this.status;
90+
}
6591
}
6692

6793
@Override
6894
public String getStatusText() throws IOException {
69-
return this.status.getReasonPhrase();
95+
if (this.status instanceof HttpStatus) {
96+
return ((HttpStatus) this.status).getReasonPhrase();
97+
}
98+
else {
99+
return "Custom http status";
100+
}
70101
}
71102

72103
@Override

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
*/
4141
public class DefaultResponseCreator implements ResponseCreator {
4242

43-
private HttpStatus statusCode;
43+
private final Object statusCode;
4444

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

@@ -59,6 +59,14 @@ protected DefaultResponseCreator(HttpStatus statusCode) {
5959
this.statusCode = statusCode;
6060
}
6161

62+
/**
63+
* Protected constructor.
64+
* Use static factory methods in {@link MockRestResponseCreators}.
65+
*/
66+
protected DefaultResponseCreator(int statusCode) {
67+
this.statusCode = statusCode;
68+
}
69+
6270

6371
/**
6472
* Set the body as a UTF-8 String.
@@ -114,10 +122,20 @@ public ClientHttpResponse createResponse(@Nullable ClientHttpRequest request) th
114122
MockClientHttpResponse response;
115123
if (this.contentResource != null) {
116124
InputStream stream = this.contentResource.getInputStream();
117-
response = new MockClientHttpResponse(stream, this.statusCode);
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+
}
118131
}
119132
else {
120-
response = new MockClientHttpResponse(this.content, this.statusCode);
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+
}
121139
}
122140
response.getHeaders().putAll(this.headers);
123141
return response;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@ public static DefaultResponseCreator withStatus(HttpStatus status) {
117117
return new DefaultResponseCreator(status);
118118
}
119119

120+
/**
121+
* {@code ResponseCreator} with a specific HTTP status.
122+
* @param status the response status
123+
*/
124+
public static DefaultResponseCreator withStatus(int status) {
125+
return new DefaultResponseCreator(status);
126+
}
127+
120128
/**
121129
* {@code ResponseCreator} with an internal application {@code IOException}.
122130
* <p>For example, one could use this to simulate a {@code SocketTimeoutException}.

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,15 @@ void withStatus() throws Exception {
128128
assertThat(StreamUtils.copyToByteArray(response.getBody()).length).isEqualTo(0);
129129
}
130130

131+
@Test
132+
void withCustomStatus() throws Exception {
133+
DefaultResponseCreator responseCreator = MockRestResponseCreators.withStatus(454);
134+
MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);
135+
136+
assertThat(response.getRawStatusCode()).isEqualTo(454);
137+
assertThat(response.getStatusText()).isEqualTo("Custom http status");
138+
}
139+
131140
@Test
132141
void withException() {
133142
ResponseCreator responseCreator = MockRestResponseCreators.withException(new SocketTimeoutException());

0 commit comments

Comments
 (0)