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");
* 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

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");
* 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();
/**

View File

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

View File

@ -88,7 +88,7 @@ class ReactorClientHttpResponse implements ClientHttpResponse {
@Override
public HttpStatus getStatusCode() {
return HttpStatus.valueOf(getRawStatusCode());
return HttpStatus.resolve(getRawStatusCode());
}
@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");
* 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<String> 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;

View File

@ -36,6 +36,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;
@ -63,8 +64,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();
/**
@ -203,6 +205,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
@ -268,6 +281,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

View File

@ -67,7 +67,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();
@ -87,7 +87,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()));
if (other instanceof DefaultClientResponse) {
@ -101,7 +101,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;
}
@ -179,7 +184,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;
@ -187,7 +192,7 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder {
private final Flux<DataBuffer> body;
public BuiltClientHttpResponse(HttpStatus statusCode, HttpHeaders headers,
public BuiltClientHttpResponse(int statusCode, HttpHeaders headers,
MultiValueMap<String, ResponseCookie> cookies, Flux<DataBuffer> body) {
this.statusCode = statusCode;
@ -198,12 +203,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

View File

@ -100,5 +100,18 @@ public class DefaultClientResponseBuilderTests {
.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();
}
}