Browse Source

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
pull/23837/head
Arjen Poutsma 6 years ago
parent
commit
29ef985411
  1. 13
      spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpResponse.java
  2. 6
      spring-web/src/main/java/org/springframework/http/client/reactive/ClientHttpResponse.java
  3. 2
      spring-web/src/main/java/org/springframework/http/client/reactive/JettyClientHttpResponse.java
  4. 4
      spring-web/src/main/java/org/springframework/http/client/reactive/ReactorClientHttpResponse.java
  5. 15
      spring-web/src/test/java/org/springframework/mock/http/client/reactive/test/MockClientHttpResponse.java
  6. 25
      spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientResponse.java
  7. 19
      spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilder.java
  8. 20
      spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilderTests.java

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

@ -1,5 +1,5 @@ @@ -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; @@ -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 { @@ -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

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

@ -1,5 +1,5 @@ @@ -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; @@ -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 { @@ -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();
/**

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

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

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

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

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

@ -1,5 +1,5 @@ @@ -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; @@ -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 { @@ -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 { @@ -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;

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

@ -1,5 +1,5 @@ @@ -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.
@ -35,6 +35,7 @@ import org.springframework.http.ResponseEntity; @@ -35,6 +35,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;
@ -62,8 +63,9 @@ public interface ClientResponse { @@ -62,8 +63,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();
/**
@ -194,6 +196,17 @@ public interface ClientResponse { @@ -194,6 +196,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
@ -259,6 +272,14 @@ public interface ClientResponse { @@ -259,6 +272,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

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

@ -44,7 +44,7 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder { @@ -44,7 +44,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();
@ -61,7 +61,7 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder { @@ -61,7 +61,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()));
}
@ -69,7 +69,12 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder { @@ -69,7 +69,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;
}
@ -140,7 +145,7 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder { @@ -140,7 +145,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;
@ -148,7 +153,7 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder { @@ -148,7 +153,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;
@ -159,12 +164,12 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder { @@ -159,12 +164,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

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

@ -1,5 +1,5 @@ @@ -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.
@ -30,7 +30,9 @@ import org.springframework.http.HttpHeaders; @@ -30,7 +30,9 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseCookie;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
/**
* @author Arjen Poutsma
@ -100,5 +102,19 @@ public class DefaultClientResponseBuilderTests { @@ -100,5 +102,19 @@ public class DefaultClientResponseBuilderTests {
.verifyComplete();
}
@Test
public void fromCustomStatus() {
ClientResponse other = ClientResponse.create(499, ExchangeStrategies.withDefaults())
.build();
ClientResponse result = ClientResponse.from(other)
.build();
assertEquals(499, result.rawStatusCode());
assertNull(result.statusCode());
}
}

Loading…
Cancel
Save