Support custom HTTP status in MockClientHttpResponse
See gh-28105
This commit is contained in:
parent
21d61316be
commit
f1fdc11fab
|
@ -32,7 +32,7 @@ import org.springframework.util.Assert;
|
|||
*/
|
||||
public class MockClientHttpResponse extends MockHttpInputMessage implements ClientHttpResponse {
|
||||
|
||||
private final HttpStatus status;
|
||||
private final Object status;
|
||||
|
||||
|
||||
/**
|
||||
|
@ -44,6 +44,14 @@ public class MockClientHttpResponse extends MockHttpInputMessage implements Clie
|
|||
this.status = statusCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor with response body as a byte array.
|
||||
*/
|
||||
public MockClientHttpResponse(byte[] body, int statusCode) {
|
||||
super(body);
|
||||
this.status = statusCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor with response body as InputStream.
|
||||
*/
|
||||
|
@ -53,20 +61,43 @@ public class MockClientHttpResponse extends MockHttpInputMessage implements Clie
|
|||
this.status = statusCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor with response body as InputStream.
|
||||
*/
|
||||
public MockClientHttpResponse(InputStream body, int statusCode) {
|
||||
super(body);
|
||||
this.status = statusCode;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public HttpStatus getStatusCode() throws IOException {
|
||||
return this.status;
|
||||
if (this.status instanceof HttpStatus) {
|
||||
return (HttpStatus) this.status;
|
||||
}
|
||||
else {
|
||||
return HttpStatus.valueOf((Integer) this.status);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRawStatusCode() throws IOException {
|
||||
return this.status.value();
|
||||
if (this.status instanceof HttpStatus) {
|
||||
return ((HttpStatus) this.status).value();
|
||||
}
|
||||
else {
|
||||
return (Integer) this.status;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getStatusText() throws IOException {
|
||||
return this.status.getReasonPhrase();
|
||||
if (this.status instanceof HttpStatus) {
|
||||
return ((HttpStatus) this.status).getReasonPhrase();
|
||||
}
|
||||
else {
|
||||
return "Custom http status";
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -40,7 +40,7 @@ import org.springframework.util.Assert;
|
|||
*/
|
||||
public class DefaultResponseCreator implements ResponseCreator {
|
||||
|
||||
private HttpStatus statusCode;
|
||||
private final Object statusCode;
|
||||
|
||||
private byte[] content = new byte[0];
|
||||
|
||||
|
@ -59,6 +59,14 @@ public class DefaultResponseCreator implements ResponseCreator {
|
|||
this.statusCode = statusCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected constructor.
|
||||
* Use static factory methods in {@link MockRestResponseCreators}.
|
||||
*/
|
||||
protected DefaultResponseCreator(int statusCode) {
|
||||
this.statusCode = statusCode;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the body as a UTF-8 String.
|
||||
|
@ -114,10 +122,20 @@ public class DefaultResponseCreator implements ResponseCreator {
|
|||
MockClientHttpResponse response;
|
||||
if (this.contentResource != null) {
|
||||
InputStream stream = this.contentResource.getInputStream();
|
||||
response = new MockClientHttpResponse(stream, this.statusCode);
|
||||
if (this.statusCode instanceof HttpStatus) {
|
||||
response = new MockClientHttpResponse(stream, (HttpStatus) this.statusCode);
|
||||
}
|
||||
else {
|
||||
response = new MockClientHttpResponse(stream, (Integer) this.statusCode);
|
||||
}
|
||||
}
|
||||
else {
|
||||
response = new MockClientHttpResponse(this.content, this.statusCode);
|
||||
if (this.statusCode instanceof HttpStatus) {
|
||||
response = new MockClientHttpResponse(this.content, (HttpStatus) this.statusCode);
|
||||
}
|
||||
else {
|
||||
response = new MockClientHttpResponse(this.content, (Integer) this.statusCode);
|
||||
}
|
||||
}
|
||||
response.getHeaders().putAll(this.headers);
|
||||
return response;
|
||||
|
|
|
@ -117,6 +117,14 @@ public abstract class MockRestResponseCreators {
|
|||
return new DefaultResponseCreator(status);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code ResponseCreator} with a specific HTTP status.
|
||||
* @param status the response status
|
||||
*/
|
||||
public static DefaultResponseCreator withStatus(int status) {
|
||||
return new DefaultResponseCreator(status);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code ResponseCreator} with an internal application {@code IOException}.
|
||||
* <p>For example, one could use this to simulate a {@code SocketTimeoutException}.
|
||||
|
|
|
@ -128,6 +128,15 @@ class ResponseCreatorsTests {
|
|||
assertThat(StreamUtils.copyToByteArray(response.getBody()).length).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void withCustomStatus() throws Exception {
|
||||
DefaultResponseCreator responseCreator = MockRestResponseCreators.withStatus(454);
|
||||
MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);
|
||||
|
||||
assertThat(response.getRawStatusCode()).isEqualTo(454);
|
||||
assertThat(response.getStatusText()).isEqualTo("Custom http status");
|
||||
}
|
||||
|
||||
@Test
|
||||
void withException() {
|
||||
ResponseCreator responseCreator = MockRestResponseCreators.withException(new SocketTimeoutException());
|
||||
|
|
Loading…
Reference in New Issue