diff --git a/spring-test/src/main/java/org/springframework/mock/http/client/MockClientHttpResponse.java b/spring-test/src/main/java/org/springframework/mock/http/client/MockClientHttpResponse.java index 16ea8b4c59f9..c8450ca0beb6 100644 --- a/spring-test/src/main/java/org/springframework/mock/http/client/MockClientHttpResponse.java +++ b/spring-test/src/main/java/org/springframework/mock/http/client/MockClientHttpResponse.java @@ -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. @@ -32,7 +32,7 @@ */ public class MockClientHttpResponse extends MockHttpInputMessage implements ClientHttpResponse { - private final Object status; + private final int statusCode; /** @@ -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; } /** @@ -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 diff --git a/spring-test/src/main/java/org/springframework/test/web/client/response/DefaultResponseCreator.java b/spring-test/src/main/java/org/springframework/test/web/client/response/DefaultResponseCreator.java index c902ea9fe095..001b33e053f2 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/response/DefaultResponseCreator.java +++ b/spring-test/src/main/java/org/springframework/test/web/client/response/DefaultResponseCreator.java @@ -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. @@ -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; @@ -40,7 +39,7 @@ */ public class DefaultResponseCreator implements ResponseCreator { - private final Object statusCode; + private final int statusCode; private byte[] content = new byte[0]; @@ -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; @@ -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; } diff --git a/spring-test/src/main/java/org/springframework/test/web/client/response/MockRestResponseCreators.java b/spring-test/src/main/java/org/springframework/test/web/client/response/MockRestResponseCreators.java index 92b4eabf1a09..73712ea5d895 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/response/MockRestResponseCreators.java +++ b/spring-test/src/main/java/org/springframework/test/web/client/response/MockRestResponseCreators.java @@ -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. @@ -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); } diff --git a/spring-test/src/test/java/org/springframework/test/web/client/response/ResponseCreatorsTests.java b/spring-test/src/test/java/org/springframework/test/web/client/response/ResponseCreatorsTests.java index f42ca19964bd..a6d5f4e904b8 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/response/ResponseCreatorsTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/response/ResponseCreatorsTests.java @@ -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. @@ -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