Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom HTTP status in client-side REST testing support #28105

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -32,7 +32,7 @@
*/
public class MockClientHttpResponse extends MockHttpInputMessage implements ClientHttpResponse {

private final HttpStatus status;
private final Object status;


/**
Expand All @@ -44,6 +44,14 @@ public MockClientHttpResponse(byte[] body, HttpStatus statusCode) {
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.
*/
Expand All @@ -53,20 +61,43 @@ public MockClientHttpResponse(InputStream body, HttpStatus statusCode) {
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
Expand Down
Expand Up @@ -40,7 +40,7 @@
*/
public class DefaultResponseCreator implements ResponseCreator {

private final HttpStatus statusCode;
private final Object statusCode;

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

Expand All @@ -59,6 +59,14 @@ protected DefaultResponseCreator(HttpStatus statusCode) {
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.
Expand Down Expand Up @@ -114,10 +122,20 @@ public ClientHttpResponse createResponse(@Nullable ClientHttpRequest request) th
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;
Expand Down
Expand Up @@ -117,6 +117,14 @@ public static DefaultResponseCreator withStatus(HttpStatus status) {
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}.
Expand Down
Expand Up @@ -128,6 +128,15 @@ void withStatus() throws Exception {
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());
Expand Down