Skip to content

Commit 960079e

Browse files
committed
Retain non-null HttpStatus return value in Client(Http)Response
See gh-23366
1 parent 29ef985 commit 960079e

File tree

10 files changed

+36
-44
lines changed

10 files changed

+36
-44
lines changed

spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpResponse.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public MockClientHttpResponse(int status) {
7171

7272
@Override
7373
public HttpStatus getStatusCode() {
74-
return HttpStatus.resolve(this.status);
74+
return HttpStatus.valueOf(this.status);
7575
}
7676

7777
@Override
@@ -81,10 +81,9 @@ public int getRawStatusCode() {
8181

8282
@Override
8383
public HttpHeaders getHeaders() {
84-
String headerName = HttpHeaders.SET_COOKIE;
85-
if (!getCookies().isEmpty() && this.headers.get(headerName) == null) {
84+
if (!getCookies().isEmpty() && this.headers.get(HttpHeaders.SET_COOKIE) == null) {
8685
getCookies().values().stream().flatMap(Collection::stream)
87-
.forEach(cookie -> getHeaders().add(headerName, cookie.toString()));
86+
.forEach(cookie -> getHeaders().add(HttpHeaders.SET_COOKIE, cookie.toString()));
8887
}
8988
return this.headers;
9089
}

spring-web/src/main/java/org/springframework/http/client/ClientHttpResponse.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,18 +35,19 @@
3535
public interface ClientHttpResponse extends HttpInputMessage, Closeable {
3636

3737
/**
38-
* Return the HTTP status code of the response.
39-
* @return the HTTP status as an HttpStatus enum value
38+
* Return the HTTP status code as an {@link HttpStatus} enum value.
39+
* @return the HTTP status as an HttpStatus enum value (never {@code null})
4040
* @throws IOException in case of I/O errors
4141
* @throws IllegalArgumentException in case of an unknown HTTP status code
42+
* @since #getRawStatusCode()
4243
* @see HttpStatus#valueOf(int)
4344
*/
4445
HttpStatus getStatusCode() throws IOException;
4546

4647
/**
4748
* Return the HTTP status code (potentially non-standard and not
4849
* resolvable through the {@link HttpStatus} enum) as an integer.
49-
* @return the HTTP status as an integer
50+
* @return the HTTP status as an integer value
5051
* @throws IOException in case of I/O errors
5152
* @since 3.1.1
5253
* @see #getStatusCode()

spring-web/src/main/java/org/springframework/http/client/reactive/ClientHttpResponse.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.springframework.http.HttpStatus;
2020
import org.springframework.http.ReactiveHttpInputMessage;
2121
import org.springframework.http.ResponseCookie;
22-
import org.springframework.lang.Nullable;
2322
import org.springframework.util.MultiValueMap;
2423

2524
/**
@@ -32,18 +31,18 @@
3231
public interface ClientHttpResponse extends ReactiveHttpInputMessage {
3332

3433
/**
35-
* Return the HTTP status code of the response.
36-
* @return the HTTP status as an HttpStatus enum value
34+
* Return the HTTP status code as an {@link HttpStatus} enum value.
35+
* @return the HTTP status as an HttpStatus enum value (never {@code null})
3736
* @throws IllegalArgumentException in case of an unknown HTTP status code
38-
* @see HttpStatus#resolve(int)
37+
* @since #getRawStatusCode()
38+
* @see HttpStatus#valueOf(int)
3939
*/
40-
@Nullable
4140
HttpStatus getStatusCode();
4241

4342
/**
4443
* Return the HTTP status code (potentially non-standard and not
4544
* resolvable through the {@link HttpStatus} enum) as an integer.
46-
* @return the HTTP status as an integer
45+
* @return the HTTP status as an integer value
4746
* @since 5.0.6
4847
* @see #getStatusCode()
4948
* @see HttpStatus#resolve(int)

spring-web/src/main/java/org/springframework/http/client/reactive/ClientHttpResponseDecorator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public ClientHttpResponse getDelegate() {
4848
}
4949

5050

51-
// ServerHttpResponse delegation methods...
51+
// ClientHttpResponse delegation methods...
5252

5353
@Override
5454
public HttpStatus getStatusCode() {

spring-web/src/main/java/org/springframework/http/client/reactive/JettyClientHttpResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public JettyClientHttpResponse(ReactiveResponse reactiveResponse, Publisher<Data
5353

5454
@Override
5555
public HttpStatus getStatusCode() {
56-
return HttpStatus.resolve(getRawStatusCode());
56+
return HttpStatus.valueOf(getRawStatusCode());
5757
}
5858

5959
@Override

spring-web/src/main/java/org/springframework/http/client/reactive/ReactorClientHttpResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public HttpHeaders getHeaders() {
8787

8888
@Override
8989
public HttpStatus getStatusCode() {
90-
return HttpStatus.resolve(getRawStatusCode());
90+
return HttpStatus.valueOf(getRawStatusCode());
9191
}
9292

9393
@Override

spring-web/src/test/java/org/springframework/mock/http/client/reactive/test/MockClientHttpResponse.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public MockClientHttpResponse(int status) {
7171

7272
@Override
7373
public HttpStatus getStatusCode() {
74-
return HttpStatus.resolve(this.status);
74+
return HttpStatus.valueOf(this.status);
7575
}
7676

7777
@Override
@@ -81,10 +81,9 @@ public int getRawStatusCode() {
8181

8282
@Override
8383
public HttpHeaders getHeaders() {
84-
String headerName = HttpHeaders.SET_COOKIE;
85-
if (!getCookies().isEmpty() && this.headers.get(headerName) == null) {
84+
if (!getCookies().isEmpty() && this.headers.get(HttpHeaders.SET_COOKIE) == null) {
8685
getCookies().values().stream().flatMap(Collection::stream)
87-
.forEach(cookie -> getHeaders().add(headerName, cookie.toString()));
86+
.forEach(cookie -> getHeaders().add(HttpHeaders.SET_COOKIE, cookie.toString()));
8887
}
8988
return this.headers;
9089
}

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientResponse.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.springframework.http.client.reactive.ClientHttpResponse;
3636
import org.springframework.http.codec.HttpMessageReader;
3737
import org.springframework.http.codec.HttpMessageWriter;
38-
import org.springframework.lang.Nullable;
3938
import org.springframework.util.MultiValueMap;
4039
import org.springframework.web.reactive.function.BodyExtractor;
4140

@@ -60,17 +59,17 @@
6059
public interface ClientResponse {
6160

6261
/**
63-
* Return the status code of this response.
64-
* @return the status as an HttpStatus enum value
62+
* Return the HTTP status code as an {@link HttpStatus} enum value.
63+
* @return the HTTP status as an HttpStatus enum value (never {@code null})
6564
* @throws IllegalArgumentException in case of an unknown HTTP status code
66-
* @see HttpStatus#resolve(int)
65+
* @since #getRawStatusCode()
66+
* @see HttpStatus#valueOf(int)
6767
*/
68-
@Nullable
6968
HttpStatus statusCode();
7069

7170
/**
7271
* Return the (potentially non-standard) status code of this response.
73-
* @return the status as an integer
72+
* @return the HTTP status as an integer value
7473
* @since 5.1
7574
* @see #statusCode()
7675
* @see HttpStatus#resolve(int)

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public BuiltClientHttpResponse(int statusCode, HttpHeaders headers,
164164

165165
@Override
166166
public HttpStatus getStatusCode() {
167-
return HttpStatus.resolve(this.statusCode);
167+
return HttpStatus.valueOf(this.statusCode);
168168
}
169169

170170
@Override

spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilderTests.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.nio.charset.StandardCharsets;
2020

21-
import org.junit.Before;
2221
import org.junit.Test;
2322
import reactor.core.publisher.Flux;
2423
import reactor.test.StepVerifier;
@@ -32,19 +31,15 @@
3231

3332
import static org.junit.Assert.assertEquals;
3433
import static org.junit.Assert.assertNotNull;
35-
import static org.junit.Assert.assertNull;
34+
import static org.junit.Assert.fail;
3635

3736
/**
3837
* @author Arjen Poutsma
3938
*/
4039
public class DefaultClientResponseBuilderTests {
4140

42-
private DataBufferFactory dataBufferFactory;
41+
private final DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
4342

44-
@Before
45-
public void createBufferFactory() {
46-
this.dataBufferFactory = new DefaultDataBufferFactory();
47-
}
4843

4944
@Test
5045
public void normal() {
@@ -104,17 +99,17 @@ public void from() {
10499

105100
@Test
106101
public void fromCustomStatus() {
107-
108-
ClientResponse other = ClientResponse.create(499, ExchangeStrategies.withDefaults())
109-
.build();
110-
111-
ClientResponse result = ClientResponse.from(other)
112-
.build();
113-
102+
ClientResponse other = ClientResponse.create(499, ExchangeStrategies.withDefaults()).build();
103+
ClientResponse result = ClientResponse.from(other).build();
114104
assertEquals(499, result.rawStatusCode());
115-
assertNull(result.statusCode());
116105

106+
try {
107+
result.statusCode();
108+
fail("Expected IllegalArgumentException");
109+
}
110+
catch (IllegalArgumentException ex) {
111+
// expected
112+
}
117113
}
118114

119-
120115
}

0 commit comments

Comments
 (0)