diff --git a/spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpResponse.java b/spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpResponse.java index f69bcf012f..eea0ebb07c 100644 --- a/spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpResponse.java +++ b/spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpResponse.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -47,7 +47,7 @@ import org.springframework.util.MultiValueMap; */ public class MockClientHttpResponse implements ClientHttpResponse { - private final HttpStatus status; + private final int status; private final HttpHeaders headers = new HttpHeaders(); @@ -60,18 +60,23 @@ public class MockClientHttpResponse implements ClientHttpResponse { public MockClientHttpResponse(HttpStatus status) { Assert.notNull(status, "HttpStatus is required"); + this.status = status.value(); + } + + public MockClientHttpResponse(int status) { + Assert.isTrue(status >= 100 && status < 600, "Status must be between 1xx and 5xx"); this.status = status; } @Override public HttpStatus getStatusCode() { - return this.status; + return HttpStatus.resolve(this.status); } @Override public int getRawStatusCode() { - return this.status.value(); + return this.status; } @Override diff --git a/spring-web/src/main/java/org/springframework/http/client/reactive/ClientHttpResponse.java b/spring-web/src/main/java/org/springframework/http/client/reactive/ClientHttpResponse.java index 9b712cf5f1..559ff2ac12 100644 --- a/spring-web/src/main/java/org/springframework/http/client/reactive/ClientHttpResponse.java +++ b/spring-web/src/main/java/org/springframework/http/client/reactive/ClientHttpResponse.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -19,6 +19,7 @@ package org.springframework.http.client.reactive; import org.springframework.http.HttpStatus; import org.springframework.http.ReactiveHttpInputMessage; import org.springframework.http.ResponseCookie; +import org.springframework.lang.Nullable; import org.springframework.util.MultiValueMap; /** @@ -34,8 +35,9 @@ public interface ClientHttpResponse extends ReactiveHttpInputMessage { * Return the HTTP status code of the response. * @return the HTTP status as an HttpStatus enum value * @throws IllegalArgumentException in case of an unknown HTTP status code - * @see HttpStatus#valueOf(int) + * @see HttpStatus#resolve(int) */ + @Nullable HttpStatus getStatusCode(); /** diff --git a/spring-web/src/main/java/org/springframework/http/client/reactive/JettyClientHttpResponse.java b/spring-web/src/main/java/org/springframework/http/client/reactive/JettyClientHttpResponse.java index 60d94517da..51b6c0ce42 100644 --- a/spring-web/src/main/java/org/springframework/http/client/reactive/JettyClientHttpResponse.java +++ b/spring-web/src/main/java/org/springframework/http/client/reactive/JettyClientHttpResponse.java @@ -53,7 +53,7 @@ class JettyClientHttpResponse implements ClientHttpResponse { @Override public HttpStatus getStatusCode() { - return HttpStatus.valueOf(getRawStatusCode()); + return HttpStatus.resolve(getRawStatusCode()); } @Override diff --git a/spring-web/src/main/java/org/springframework/http/client/reactive/ReactorClientHttpResponse.java b/spring-web/src/main/java/org/springframework/http/client/reactive/ReactorClientHttpResponse.java index 3a447bd89b..c1e29443aa 100644 --- a/spring-web/src/main/java/org/springframework/http/client/reactive/ReactorClientHttpResponse.java +++ b/spring-web/src/main/java/org/springframework/http/client/reactive/ReactorClientHttpResponse.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -87,7 +87,7 @@ class ReactorClientHttpResponse implements ClientHttpResponse { @Override public HttpStatus getStatusCode() { - return HttpStatus.valueOf(getRawStatusCode()); + return HttpStatus.resolve(getRawStatusCode()); } @Override diff --git a/spring-web/src/test/java/org/springframework/mock/http/client/reactive/test/MockClientHttpResponse.java b/spring-web/src/test/java/org/springframework/mock/http/client/reactive/test/MockClientHttpResponse.java index 09eee693e1..4ca93984de 100644 --- a/spring-web/src/test/java/org/springframework/mock/http/client/reactive/test/MockClientHttpResponse.java +++ b/spring-web/src/test/java/org/springframework/mock/http/client/reactive/test/MockClientHttpResponse.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -47,7 +47,7 @@ import org.springframework.util.MultiValueMap; */ public class MockClientHttpResponse implements ClientHttpResponse { - private final HttpStatus status; + private final int status; private final HttpHeaders headers = new HttpHeaders(); @@ -60,18 +60,23 @@ public class MockClientHttpResponse implements ClientHttpResponse { public MockClientHttpResponse(HttpStatus status) { Assert.notNull(status, "HttpStatus is required"); + this.status = status.value(); + } + + public MockClientHttpResponse(int status) { + Assert.isTrue(status >= 100 && status < 600, "Status must be between 1xx and 5xx"); this.status = status; } @Override public HttpStatus getStatusCode() { - return this.status; + return HttpStatus.resolve(this.status); } @Override public int getRawStatusCode() { - return this.status.value(); + return this.status; } @Override @@ -120,7 +125,7 @@ public class MockClientHttpResponse implements ClientHttpResponse { public Mono getBodyAsString() { Charset charset = getCharset(); return Flux.from(getBody()) - .reduce(bufferFactory.allocateBuffer(), (previous, current) -> { + .reduce(this.bufferFactory.allocateBuffer(), (previous, current) -> { previous.write(current); DataBufferUtils.release(current); return previous; diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientResponse.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientResponse.java index 9e43af5abc..5190debac9 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientResponse.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientResponse.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -35,6 +35,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.http.client.reactive.ClientHttpResponse; import org.springframework.http.codec.HttpMessageReader; import org.springframework.http.codec.HttpMessageWriter; +import org.springframework.lang.Nullable; import org.springframework.util.MultiValueMap; import org.springframework.web.reactive.function.BodyExtractor; @@ -62,8 +63,9 @@ public interface ClientResponse { * Return the status code of this response. * @return the status as an HttpStatus enum value * @throws IllegalArgumentException in case of an unknown HTTP status code - * @see HttpStatus#valueOf(int) + * @see HttpStatus#resolve(int) */ + @Nullable HttpStatus statusCode(); /** @@ -194,6 +196,17 @@ public interface ClientResponse { return new DefaultClientResponseBuilder(strategies).statusCode(statusCode); } + /** + * Create a response builder with the given raw status code and strategies for reading the body. + * @param statusCode the status code + * @param strategies the strategies + * @return the created builder + * @since 5.1.9 + */ + static Builder create(int statusCode, ExchangeStrategies strategies) { + return new DefaultClientResponseBuilder(strategies).rawStatusCode(statusCode); + } + /** * Create a response builder with the given status code and message body readers. * @param statusCode the status code @@ -259,6 +272,14 @@ public interface ClientResponse { */ Builder statusCode(HttpStatus statusCode); + /** + * Set the raw status code of the response. + * @param statusCode the new status code. + * @return this builder + * @since 5.1.9 + */ + Builder rawStatusCode(int statusCode); + /** * Add the given header value(s) under the given name. * @param headerName the header name diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilder.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilder.java index a955762803..5fd19969eb 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilder.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilder.java @@ -44,7 +44,7 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder { private ExchangeStrategies strategies; - private HttpStatus statusCode = HttpStatus.OK; + private int statusCode = 200; private final HttpHeaders headers = new HttpHeaders(); @@ -61,7 +61,7 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder { public DefaultClientResponseBuilder(ClientResponse other) { Assert.notNull(other, "ClientResponse must not be null"); this.strategies = other.strategies(); - statusCode(other.statusCode()); + this.statusCode = other.rawStatusCode(); headers(headers -> headers.addAll(other.headers().asHttpHeaders())); cookies(cookies -> cookies.addAll(other.cookies())); } @@ -69,7 +69,12 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder { @Override public DefaultClientResponseBuilder statusCode(HttpStatus statusCode) { - Assert.notNull(statusCode, "HttpStatus must not be null"); + return rawStatusCode(statusCode.value()); + } + + @Override + public DefaultClientResponseBuilder rawStatusCode(int statusCode) { + Assert.isTrue(statusCode >= 100 && statusCode < 600, "StatusCode must be between 1xx and 5xx"); this.statusCode = statusCode; return this; } @@ -140,7 +145,7 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder { private static class BuiltClientHttpResponse implements ClientHttpResponse { - private final HttpStatus statusCode; + private final int statusCode; private final HttpHeaders headers; @@ -148,7 +153,7 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder { private final Flux body; - public BuiltClientHttpResponse(HttpStatus statusCode, HttpHeaders headers, + public BuiltClientHttpResponse(int statusCode, HttpHeaders headers, MultiValueMap cookies, Flux body) { this.statusCode = statusCode; @@ -159,12 +164,12 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder { @Override public HttpStatus getStatusCode() { - return this.statusCode; + return HttpStatus.resolve(this.statusCode); } @Override public int getRawStatusCode() { - return this.statusCode.value(); + return this.statusCode; } @Override diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilderTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilderTests.java index 68b2c6c4be..0a1edc1ee6 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilderTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -30,7 +30,9 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseCookie; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; /** * @author Arjen Poutsma @@ -100,5 +102,19 @@ public class DefaultClientResponseBuilderTests { .verifyComplete(); } + @Test + public void fromCustomStatus() { + + ClientResponse other = ClientResponse.create(499, ExchangeStrategies.withDefaults()) + .build(); + + ClientResponse result = ClientResponse.from(other) + .build(); + + assertEquals(499, result.rawStatusCode()); + assertNull(result.statusCode()); + + } + }