Skip to content

Commit

Permalink
Support custom HTTP status in MockClientHttpResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
AzZureman authored and rstoyanchev committed Mar 16, 2022
1 parent 21d6131 commit f1fdc11
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 7 deletions.
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 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

0 comments on commit f1fdc11

Please sign in to comment.