Skip to content

Commit 29ef985

Browse files
committed
Add support for non-standard status codes
Added support for status codes that do not occur in HttpStatus in DefaultClientResponseBuilder and made ClientResponse::statusCode ClientHttpResponse::getStatusCode @nullable. Closed gh-23366
1 parent b2c5659 commit 29ef985

File tree

8 files changed

+79
-25
lines changed

8 files changed

+79
-25
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 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.
@@ -47,7 +47,7 @@
4747
*/
4848
public class MockClientHttpResponse implements ClientHttpResponse {
4949

50-
private final HttpStatus status;
50+
private final int status;
5151

5252
private final HttpHeaders headers = new HttpHeaders();
5353

@@ -60,18 +60,23 @@ public class MockClientHttpResponse implements ClientHttpResponse {
6060

6161
public MockClientHttpResponse(HttpStatus status) {
6262
Assert.notNull(status, "HttpStatus is required");
63+
this.status = status.value();
64+
}
65+
66+
public MockClientHttpResponse(int status) {
67+
Assert.isTrue(status >= 100 && status < 600, "Status must be between 1xx and 5xx");
6368
this.status = status;
6469
}
6570

6671

6772
@Override
6873
public HttpStatus getStatusCode() {
69-
return this.status;
74+
return HttpStatus.resolve(this.status);
7075
}
7176

7277
@Override
7378
public int getRawStatusCode() {
74-
return this.status.value();
79+
return this.status;
7580
}
7681

7782
@Override

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 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.
@@ -19,6 +19,7 @@
1919
import org.springframework.http.HttpStatus;
2020
import org.springframework.http.ReactiveHttpInputMessage;
2121
import org.springframework.http.ResponseCookie;
22+
import org.springframework.lang.Nullable;
2223
import org.springframework.util.MultiValueMap;
2324

2425
/**
@@ -34,8 +35,9 @@ public interface ClientHttpResponse extends ReactiveHttpInputMessage {
3435
* Return the HTTP status code of the response.
3536
* @return the HTTP status as an HttpStatus enum value
3637
* @throws IllegalArgumentException in case of an unknown HTTP status code
37-
* @see HttpStatus#valueOf(int)
38+
* @see HttpStatus#resolve(int)
3839
*/
40+
@Nullable
3941
HttpStatus getStatusCode();
4042

4143
/**

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.valueOf(getRawStatusCode());
56+
return HttpStatus.resolve(getRawStatusCode());
5757
}
5858

5959
@Override

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 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.
@@ -87,7 +87,7 @@ public HttpHeaders getHeaders() {
8787

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

9393
@Override

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 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.
@@ -47,7 +47,7 @@
4747
*/
4848
public class MockClientHttpResponse implements ClientHttpResponse {
4949

50-
private final HttpStatus status;
50+
private final int status;
5151

5252
private final HttpHeaders headers = new HttpHeaders();
5353

@@ -60,18 +60,23 @@ public class MockClientHttpResponse implements ClientHttpResponse {
6060

6161
public MockClientHttpResponse(HttpStatus status) {
6262
Assert.notNull(status, "HttpStatus is required");
63+
this.status = status.value();
64+
}
65+
66+
public MockClientHttpResponse(int status) {
67+
Assert.isTrue(status >= 100 && status < 600, "Status must be between 1xx and 5xx");
6368
this.status = status;
6469
}
6570

6671

6772
@Override
6873
public HttpStatus getStatusCode() {
69-
return this.status;
74+
return HttpStatus.resolve(this.status);
7075
}
7176

7277
@Override
7378
public int getRawStatusCode() {
74-
return this.status.value();
79+
return this.status;
7580
}
7681

7782
@Override
@@ -120,7 +125,7 @@ public Flux<DataBuffer> getBody() {
120125
public Mono<String> getBodyAsString() {
121126
Charset charset = getCharset();
122127
return Flux.from(getBody())
123-
.reduce(bufferFactory.allocateBuffer(), (previous, current) -> {
128+
.reduce(this.bufferFactory.allocateBuffer(), (previous, current) -> {
124129
previous.write(current);
125130
DataBufferUtils.release(current);
126131
return previous;

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 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,6 +35,7 @@
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;
3839
import org.springframework.util.MultiValueMap;
3940
import org.springframework.web.reactive.function.BodyExtractor;
4041

@@ -62,8 +63,9 @@ public interface ClientResponse {
6263
* Return the status code of this response.
6364
* @return the status as an HttpStatus enum value
6465
* @throws IllegalArgumentException in case of an unknown HTTP status code
65-
* @see HttpStatus#valueOf(int)
66+
* @see HttpStatus#resolve(int)
6667
*/
68+
@Nullable
6769
HttpStatus statusCode();
6870

6971
/**
@@ -194,6 +196,17 @@ static Builder create(HttpStatus statusCode, ExchangeStrategies strategies) {
194196
return new DefaultClientResponseBuilder(strategies).statusCode(statusCode);
195197
}
196198

199+
/**
200+
* Create a response builder with the given raw status code and strategies for reading the body.
201+
* @param statusCode the status code
202+
* @param strategies the strategies
203+
* @return the created builder
204+
* @since 5.1.9
205+
*/
206+
static Builder create(int statusCode, ExchangeStrategies strategies) {
207+
return new DefaultClientResponseBuilder(strategies).rawStatusCode(statusCode);
208+
}
209+
197210
/**
198211
* Create a response builder with the given status code and message body readers.
199212
* @param statusCode the status code
@@ -259,6 +272,14 @@ interface Builder {
259272
*/
260273
Builder statusCode(HttpStatus statusCode);
261274

275+
/**
276+
* Set the raw status code of the response.
277+
* @param statusCode the new status code.
278+
* @return this builder
279+
* @since 5.1.9
280+
*/
281+
Builder rawStatusCode(int statusCode);
282+
262283
/**
263284
* Add the given header value(s) under the given name.
264285
* @param headerName the header name

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder {
4444

4545
private ExchangeStrategies strategies;
4646

47-
private HttpStatus statusCode = HttpStatus.OK;
47+
private int statusCode = 200;
4848

4949
private final HttpHeaders headers = new HttpHeaders();
5050

@@ -61,15 +61,20 @@ public DefaultClientResponseBuilder(ExchangeStrategies strategies) {
6161
public DefaultClientResponseBuilder(ClientResponse other) {
6262
Assert.notNull(other, "ClientResponse must not be null");
6363
this.strategies = other.strategies();
64-
statusCode(other.statusCode());
64+
this.statusCode = other.rawStatusCode();
6565
headers(headers -> headers.addAll(other.headers().asHttpHeaders()));
6666
cookies(cookies -> cookies.addAll(other.cookies()));
6767
}
6868

6969

7070
@Override
7171
public DefaultClientResponseBuilder statusCode(HttpStatus statusCode) {
72-
Assert.notNull(statusCode, "HttpStatus must not be null");
72+
return rawStatusCode(statusCode.value());
73+
}
74+
75+
@Override
76+
public DefaultClientResponseBuilder rawStatusCode(int statusCode) {
77+
Assert.isTrue(statusCode >= 100 && statusCode < 600, "StatusCode must be between 1xx and 5xx");
7378
this.statusCode = statusCode;
7479
return this;
7580
}
@@ -140,15 +145,15 @@ public ClientResponse build() {
140145

141146
private static class BuiltClientHttpResponse implements ClientHttpResponse {
142147

143-
private final HttpStatus statusCode;
148+
private final int statusCode;
144149

145150
private final HttpHeaders headers;
146151

147152
private final MultiValueMap<String, ResponseCookie> cookies;
148153

149154
private final Flux<DataBuffer> body;
150155

151-
public BuiltClientHttpResponse(HttpStatus statusCode, HttpHeaders headers,
156+
public BuiltClientHttpResponse(int statusCode, HttpHeaders headers,
152157
MultiValueMap<String, ResponseCookie> cookies, Flux<DataBuffer> body) {
153158

154159
this.statusCode = statusCode;
@@ -159,12 +164,12 @@ public BuiltClientHttpResponse(HttpStatus statusCode, HttpHeaders headers,
159164

160165
@Override
161166
public HttpStatus getStatusCode() {
162-
return this.statusCode;
167+
return HttpStatus.resolve(this.statusCode);
163168
}
164169

165170
@Override
166171
public int getRawStatusCode() {
167-
return this.statusCode.value();
172+
return this.statusCode;
168173
}
169174

170175
@Override

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 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.
@@ -30,7 +30,9 @@
3030
import org.springframework.http.HttpStatus;
3131
import org.springframework.http.ResponseCookie;
3232

33-
import static org.junit.Assert.*;
33+
import static org.junit.Assert.assertEquals;
34+
import static org.junit.Assert.assertNotNull;
35+
import static org.junit.Assert.assertNull;
3436

3537
/**
3638
* @author Arjen Poutsma
@@ -100,5 +102,19 @@ public void from() {
100102
.verifyComplete();
101103
}
102104

105+
@Test
106+
public void fromCustomStatus() {
107+
108+
ClientResponse other = ClientResponse.create(499, ExchangeStrategies.withDefaults())
109+
.build();
110+
111+
ClientResponse result = ClientResponse.from(other)
112+
.build();
113+
114+
assertEquals(499, result.rawStatusCode());
115+
assertNull(result.statusCode());
116+
117+
}
118+
103119

104120
}

0 commit comments

Comments
 (0)