Skip to content

Commit

Permalink
Polishing contribution
Browse files Browse the repository at this point in the history
Closes gh-28105
  • Loading branch information
rstoyanchev committed Mar 16, 2022
1 parent f1fdc11 commit ee7f600
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 57 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,7 +32,7 @@
*/
public class MockClientHttpResponse extends MockHttpInputMessage implements ClientHttpResponse {

private final Object status;
private final int statusCode;


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

/**
* Constructor with response body as a byte array.
* Variant of {@link #MockClientHttpResponse(byte[], HttpStatus)} with a
* custom HTTP status code.
* @since 5.3.17
*/
public MockClientHttpResponse(byte[] body, int statusCode) {
super(body);
this.status = statusCode;
this.statusCode = statusCode;
}

/**
Expand All @@ -58,46 +60,34 @@ public MockClientHttpResponse(byte[] body, int statusCode) {
public MockClientHttpResponse(InputStream body, HttpStatus statusCode) {
super(body);
Assert.notNull(statusCode, "HttpStatus is required");
this.status = statusCode;
this.statusCode = statusCode.value();
}

/**
* Constructor with response body as InputStream.
* Variant of {@link #MockClientHttpResponse(InputStream, HttpStatus)} with a
* custom HTTP status code.
* @since 5.3.17
*/
public MockClientHttpResponse(InputStream body, int statusCode) {
super(body);
this.status = statusCode;
this.statusCode = statusCode;
}


@Override
public HttpStatus getStatusCode() throws IOException {
if (this.status instanceof HttpStatus) {
return (HttpStatus) this.status;
}
else {
return HttpStatus.valueOf((Integer) this.status);
}
public HttpStatus getStatusCode() {
return HttpStatus.valueOf(this.statusCode);
}

@Override
public int getRawStatusCode() throws IOException {
if (this.status instanceof HttpStatus) {
return ((HttpStatus) this.status).value();
}
else {
return (Integer) this.status;
}
public int getRawStatusCode() {
return this.statusCode;
}

@Override
public String getStatusText() throws IOException {
if (this.status instanceof HttpStatus) {
return ((HttpStatus) this.status).getReasonPhrase();
}
else {
return "Custom http status";
}
public String getStatusText() {
HttpStatus status = HttpStatus.resolve(this.statusCode);
return (status != null ? status.getReasonPhrase() : "");
}

@Override
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,7 +17,6 @@
package org.springframework.test.web.client.response;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;

Expand All @@ -40,7 +39,7 @@
*/
public class DefaultResponseCreator implements ResponseCreator {

private final Object statusCode;
private final int statusCode;

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

Expand All @@ -56,12 +55,13 @@ public class DefaultResponseCreator implements ResponseCreator {
*/
protected DefaultResponseCreator(HttpStatus statusCode) {
Assert.notNull(statusCode, "HttpStatus must not be null");
this.statusCode = statusCode;
this.statusCode = statusCode.value();
}

/**
* Protected constructor.
* Use static factory methods in {@link MockRestResponseCreators}.
* @since 5.3.17
*/
protected DefaultResponseCreator(int statusCode) {
this.statusCode = statusCode;
Expand Down Expand Up @@ -119,24 +119,9 @@ public DefaultResponseCreator headers(HttpHeaders headers) {

@Override
public ClientHttpResponse createResponse(@Nullable ClientHttpRequest request) throws IOException {
MockClientHttpResponse response;
if (this.contentResource != null) {
InputStream stream = this.contentResource.getInputStream();
if (this.statusCode instanceof HttpStatus) {
response = new MockClientHttpResponse(stream, (HttpStatus) this.statusCode);
}
else {
response = new MockClientHttpResponse(stream, (Integer) this.statusCode);
}
}
else {
if (this.statusCode instanceof HttpStatus) {
response = new MockClientHttpResponse(this.content, (HttpStatus) this.statusCode);
}
else {
response = new MockClientHttpResponse(this.content, (Integer) this.statusCode);
}
}
MockClientHttpResponse response = (this.contentResource != null ?
new MockClientHttpResponse(this.contentResource.getInputStream(), this.statusCode) :
new MockClientHttpResponse(this.content, this.statusCode));
response.getHeaders().putAll(this.headers);
return response;
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -118,10 +118,11 @@ public static DefaultResponseCreator withStatus(HttpStatus status) {
}

/**
* {@code ResponseCreator} with a specific HTTP status.
* Variant of {@link #withStatus(HttpStatus)} for a custom HTTP status code.
* @param status the response status
* @since 5.3.17
*/
public static DefaultResponseCreator withStatus(int status) {
public static DefaultResponseCreator withRawStatus(int status) {
return new DefaultResponseCreator(status);
}

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -130,11 +130,11 @@ void withStatus() throws Exception {

@Test
void withCustomStatus() throws Exception {
DefaultResponseCreator responseCreator = MockRestResponseCreators.withStatus(454);
DefaultResponseCreator responseCreator = MockRestResponseCreators.withRawStatus(454);
MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);

assertThat(response.getRawStatusCode()).isEqualTo(454);
assertThat(response.getStatusText()).isEqualTo("Custom http status");
assertThat(response.getStatusText()).isEmpty();
}

@Test
Expand Down

0 comments on commit ee7f600

Please sign in to comment.