Merge branch '5.1.x'

This commit is contained in:
Arjen Poutsma 2019-07-30 18:03:01 +02:00
commit ac9a11a581
8 changed files with 72 additions and 21 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 { public class MockClientHttpResponse implements ClientHttpResponse {
private final HttpStatus status; private final int status;
private final HttpHeaders headers = new HttpHeaders(); private final HttpHeaders headers = new HttpHeaders();
@ -60,18 +60,23 @@ public class MockClientHttpResponse implements ClientHttpResponse {
public MockClientHttpResponse(HttpStatus status) { public MockClientHttpResponse(HttpStatus status) {
Assert.notNull(status, "HttpStatus is required"); 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; this.status = status;
} }
@Override @Override
public HttpStatus getStatusCode() { public HttpStatus getStatusCode() {
return this.status; return HttpStatus.resolve(this.status);
} }
@Override @Override
public int getRawStatusCode() { public int getRawStatusCode() {
return this.status.value(); return this.status;
} }
@Override @Override

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.HttpStatus;
import org.springframework.http.ReactiveHttpInputMessage; import org.springframework.http.ReactiveHttpInputMessage;
import org.springframework.http.ResponseCookie; import org.springframework.http.ResponseCookie;
import org.springframework.lang.Nullable;
import org.springframework.util.MultiValueMap; 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 code of the response.
* @return the HTTP status as an HttpStatus enum value * @return the HTTP status as an HttpStatus enum value
* @throws IllegalArgumentException in case of an unknown HTTP status code * @throws IllegalArgumentException in case of an unknown HTTP status code
* @see HttpStatus#valueOf(int) * @see HttpStatus#resolve(int)
*/ */
@Nullable
HttpStatus getStatusCode(); HttpStatus getStatusCode();
/** /**

View File

@ -53,7 +53,7 @@ class JettyClientHttpResponse implements ClientHttpResponse {
@Override @Override
public HttpStatus getStatusCode() { public HttpStatus getStatusCode() {
return HttpStatus.valueOf(getRawStatusCode()); return HttpStatus.resolve(getRawStatusCode());
} }
@Override @Override

View File

@ -88,7 +88,7 @@ class ReactorClientHttpResponse implements ClientHttpResponse {
@Override @Override
public HttpStatus getStatusCode() { public HttpStatus getStatusCode() {
return HttpStatus.valueOf(getRawStatusCode()); return HttpStatus.resolve(getRawStatusCode());
} }
@Override @Override

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 { public class MockClientHttpResponse implements ClientHttpResponse {
private final HttpStatus status; private final int status;
private final HttpHeaders headers = new HttpHeaders(); private final HttpHeaders headers = new HttpHeaders();
@ -60,18 +60,23 @@ public class MockClientHttpResponse implements ClientHttpResponse {
public MockClientHttpResponse(HttpStatus status) { public MockClientHttpResponse(HttpStatus status) {
Assert.notNull(status, "HttpStatus is required"); 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; this.status = status;
} }
@Override @Override
public HttpStatus getStatusCode() { public HttpStatus getStatusCode() {
return this.status; return HttpStatus.resolve(this.status);
} }
@Override @Override
public int getRawStatusCode() { public int getRawStatusCode() {
return this.status.value(); return this.status;
} }
@Override @Override
@ -120,7 +125,7 @@ public class MockClientHttpResponse implements ClientHttpResponse {
public Mono<String> getBodyAsString() { public Mono<String> getBodyAsString() {
Charset charset = getCharset(); Charset charset = getCharset();
return Flux.from(getBody()) return Flux.from(getBody())
.reduce(bufferFactory.allocateBuffer(), (previous, current) -> { .reduce(this.bufferFactory.allocateBuffer(), (previous, current) -> {
previous.write(current); previous.write(current);
DataBufferUtils.release(current); DataBufferUtils.release(current);
return previous; return previous;

View File

@ -36,6 +36,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.http.client.reactive.ClientHttpResponse; import org.springframework.http.client.reactive.ClientHttpResponse;
import org.springframework.http.codec.HttpMessageReader; import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.codec.HttpMessageWriter; import org.springframework.http.codec.HttpMessageWriter;
import org.springframework.lang.Nullable;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.BodyExtractor; import org.springframework.web.reactive.function.BodyExtractor;
@ -63,8 +64,9 @@ public interface ClientResponse {
* Return the status code of this response. * Return the status code of this response.
* @return the status as an HttpStatus enum value * @return the status as an HttpStatus enum value
* @throws IllegalArgumentException in case of an unknown HTTP status code * @throws IllegalArgumentException in case of an unknown HTTP status code
* @see HttpStatus#valueOf(int) * @see HttpStatus#resolve(int)
*/ */
@Nullable
HttpStatus statusCode(); HttpStatus statusCode();
/** /**
@ -203,6 +205,17 @@ public interface ClientResponse {
return new DefaultClientResponseBuilder(strategies).statusCode(statusCode); 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. * Create a response builder with the given status code and message body readers.
* @param statusCode the status code * @param statusCode the status code
@ -268,6 +281,14 @@ public interface ClientResponse {
*/ */
Builder statusCode(HttpStatus statusCode); 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. * Add the given header value(s) under the given name.
* @param headerName the header name * @param headerName the header name

View File

@ -67,7 +67,7 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder {
private ExchangeStrategies strategies; private ExchangeStrategies strategies;
private HttpStatus statusCode = HttpStatus.OK; private int statusCode = 200;
private final HttpHeaders headers = new HttpHeaders(); private final HttpHeaders headers = new HttpHeaders();
@ -87,7 +87,7 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder {
public DefaultClientResponseBuilder(ClientResponse other) { public DefaultClientResponseBuilder(ClientResponse other) {
Assert.notNull(other, "ClientResponse must not be null"); Assert.notNull(other, "ClientResponse must not be null");
this.strategies = other.strategies(); this.strategies = other.strategies();
statusCode(other.statusCode()); this.statusCode = other.rawStatusCode();
headers(headers -> headers.addAll(other.headers().asHttpHeaders())); headers(headers -> headers.addAll(other.headers().asHttpHeaders()));
cookies(cookies -> cookies.addAll(other.cookies())); cookies(cookies -> cookies.addAll(other.cookies()));
if (other instanceof DefaultClientResponse) { if (other instanceof DefaultClientResponse) {
@ -101,7 +101,12 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder {
@Override @Override
public DefaultClientResponseBuilder statusCode(HttpStatus statusCode) { 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; this.statusCode = statusCode;
return this; return this;
} }
@ -179,7 +184,7 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder {
private static class BuiltClientHttpResponse implements ClientHttpResponse { private static class BuiltClientHttpResponse implements ClientHttpResponse {
private final HttpStatus statusCode; private final int statusCode;
private final HttpHeaders headers; private final HttpHeaders headers;
@ -187,7 +192,7 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder {
private final Flux<DataBuffer> body; private final Flux<DataBuffer> body;
public BuiltClientHttpResponse(HttpStatus statusCode, HttpHeaders headers, public BuiltClientHttpResponse(int statusCode, HttpHeaders headers,
MultiValueMap<String, ResponseCookie> cookies, Flux<DataBuffer> body) { MultiValueMap<String, ResponseCookie> cookies, Flux<DataBuffer> body) {
this.statusCode = statusCode; this.statusCode = statusCode;
@ -198,12 +203,12 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder {
@Override @Override
public HttpStatus getStatusCode() { public HttpStatus getStatusCode() {
return this.statusCode; return HttpStatus.resolve(this.statusCode);
} }
@Override @Override
public int getRawStatusCode() { public int getRawStatusCode() {
return this.statusCode.value(); return this.statusCode;
} }
@Override @Override

View File

@ -100,5 +100,18 @@ public class DefaultClientResponseBuilderTests {
.verifyComplete(); .verifyComplete();
} }
@Test
public void fromCustomStatus() {
ClientResponse other = ClientResponse.create(499, ExchangeStrategies.withDefaults())
.build();
ClientResponse result = ClientResponse.from(other)
.build();
assertThat(result.rawStatusCode()).isEqualTo(499);
assertThat(result.statusCode()).isNull();
}
} }