Skip to content

Commit ac9a11a

Browse files
committed
Merge branch '5.1.x'
2 parents 27aaad5 + 29ef985 commit ac9a11a

File tree

8 files changed

+72
-21
lines changed

8 files changed

+72
-21
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public HttpHeaders getHeaders() {
8888

8989
@Override
9090
public HttpStatus getStatusCode() {
91-
return HttpStatus.valueOf(getRawStatusCode());
91+
return HttpStatus.resolve(getRawStatusCode());
9292
}
9393

9494
@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: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.http.client.reactive.ClientHttpResponse;
3737
import org.springframework.http.codec.HttpMessageReader;
3838
import org.springframework.http.codec.HttpMessageWriter;
39+
import org.springframework.lang.Nullable;
3940
import org.springframework.util.MultiValueMap;
4041
import org.springframework.web.reactive.function.BodyExtractor;
4142

@@ -63,8 +64,9 @@ public interface ClientResponse {
6364
* Return the status code of this response.
6465
* @return the status as an HttpStatus enum value
6566
* @throws IllegalArgumentException in case of an unknown HTTP status code
66-
* @see HttpStatus#valueOf(int)
67+
* @see HttpStatus#resolve(int)
6768
*/
69+
@Nullable
6870
HttpStatus statusCode();
6971

7072
/**
@@ -203,6 +205,17 @@ static Builder create(HttpStatus statusCode, ExchangeStrategies strategies) {
203205
return new DefaultClientResponseBuilder(strategies).statusCode(statusCode);
204206
}
205207

208+
/**
209+
* Create a response builder with the given raw status code and strategies for reading the body.
210+
* @param statusCode the status code
211+
* @param strategies the strategies
212+
* @return the created builder
213+
* @since 5.1.9
214+
*/
215+
static Builder create(int statusCode, ExchangeStrategies strategies) {
216+
return new DefaultClientResponseBuilder(strategies).rawStatusCode(statusCode);
217+
}
218+
206219
/**
207220
* Create a response builder with the given status code and message body readers.
208221
* @param statusCode the status code
@@ -268,6 +281,14 @@ interface Builder {
268281
*/
269282
Builder statusCode(HttpStatus statusCode);
270283

284+
/**
285+
* Set the raw status code of the response.
286+
* @param statusCode the new status code.
287+
* @return this builder
288+
* @since 5.1.9
289+
*/
290+
Builder rawStatusCode(int statusCode);
291+
271292
/**
272293
* Add the given header value(s) under the given name.
273294
* @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
@@ -67,7 +67,7 @@ public HttpHeaders getHeaders() {
6767

6868
private ExchangeStrategies strategies;
6969

70-
private HttpStatus statusCode = HttpStatus.OK;
70+
private int statusCode = 200;
7171

7272
private final HttpHeaders headers = new HttpHeaders();
7373

@@ -87,7 +87,7 @@ public DefaultClientResponseBuilder(ExchangeStrategies strategies) {
8787
public DefaultClientResponseBuilder(ClientResponse other) {
8888
Assert.notNull(other, "ClientResponse must not be null");
8989
this.strategies = other.strategies();
90-
statusCode(other.statusCode());
90+
this.statusCode = other.rawStatusCode();
9191
headers(headers -> headers.addAll(other.headers().asHttpHeaders()));
9292
cookies(cookies -> cookies.addAll(other.cookies()));
9393
if (other instanceof DefaultClientResponse) {
@@ -101,7 +101,12 @@ public DefaultClientResponseBuilder(ClientResponse other) {
101101

102102
@Override
103103
public DefaultClientResponseBuilder statusCode(HttpStatus statusCode) {
104-
Assert.notNull(statusCode, "HttpStatus must not be null");
104+
return rawStatusCode(statusCode.value());
105+
}
106+
107+
@Override
108+
public DefaultClientResponseBuilder rawStatusCode(int statusCode) {
109+
Assert.isTrue(statusCode >= 100 && statusCode < 600, "StatusCode must be between 1xx and 5xx");
105110
this.statusCode = statusCode;
106111
return this;
107112
}
@@ -179,15 +184,15 @@ public ClientResponse build() {
179184

180185
private static class BuiltClientHttpResponse implements ClientHttpResponse {
181186

182-
private final HttpStatus statusCode;
187+
private final int statusCode;
183188

184189
private final HttpHeaders headers;
185190

186191
private final MultiValueMap<String, ResponseCookie> cookies;
187192

188193
private final Flux<DataBuffer> body;
189194

190-
public BuiltClientHttpResponse(HttpStatus statusCode, HttpHeaders headers,
195+
public BuiltClientHttpResponse(int statusCode, HttpHeaders headers,
191196
MultiValueMap<String, ResponseCookie> cookies, Flux<DataBuffer> body) {
192197

193198
this.statusCode = statusCode;
@@ -198,12 +203,12 @@ public BuiltClientHttpResponse(HttpStatus statusCode, HttpHeaders headers,
198203

199204
@Override
200205
public HttpStatus getStatusCode() {
201-
return this.statusCode;
206+
return HttpStatus.resolve(this.statusCode);
202207
}
203208

204209
@Override
205210
public int getRawStatusCode() {
206-
return this.statusCode.value();
211+
return this.statusCode;
207212
}
208213

209214
@Override

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,18 @@ public void from() {
100100
.verifyComplete();
101101
}
102102

103+
@Test
104+
public void fromCustomStatus() {
105+
106+
ClientResponse other = ClientResponse.create(499, ExchangeStrategies.withDefaults())
107+
.build();
108+
109+
ClientResponse result = ClientResponse.from(other)
110+
.build();
111+
112+
assertThat(result.rawStatusCode()).isEqualTo(499);
113+
assertThat(result.statusCode()).isNull();
114+
}
115+
103116

104117
}

0 commit comments

Comments
 (0)