Skip to content

Commit

Permalink
Add support for non-standard status codes
Browse files Browse the repository at this point in the history
Added support for status codes that do not occur in HttpStatus in
DefaultClientResponseBuilder and made ClientResponse::statusCode
ClientHttpResponse::getStatusCode @nullable.

Closed gh-23366
  • Loading branch information
poutsma committed Jul 30, 2019
1 parent b2c5659 commit 29ef985
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -47,7 +47,7 @@
*/
public class MockClientHttpResponse implements ClientHttpResponse {

private final HttpStatus status;
private final int status;

private final HttpHeaders headers = new HttpHeaders();

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -19,6 +19,7 @@
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;

/**
Expand All @@ -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();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public JettyClientHttpResponse(ReactiveResponse reactiveResponse, Publisher<Data

@Override
public HttpStatus getStatusCode() {
return HttpStatus.valueOf(getRawStatusCode());
return HttpStatus.resolve(getRawStatusCode());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -87,7 +87,7 @@ public HttpHeaders getHeaders() {

@Override
public HttpStatus getStatusCode() {
return HttpStatus.valueOf(getRawStatusCode());
return HttpStatus.resolve(getRawStatusCode());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -47,7 +47,7 @@
*/
public class MockClientHttpResponse implements ClientHttpResponse {

private final HttpStatus status;
private final int status;

private final HttpHeaders headers = new HttpHeaders();

Expand All @@ -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
Expand Down Expand Up @@ -120,7 +125,7 @@ public Flux<DataBuffer> getBody() {
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -35,6 +35,7 @@
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;

Expand Down Expand Up @@ -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();

/**
Expand Down Expand Up @@ -194,6 +196,17 @@ static Builder create(HttpStatus statusCode, ExchangeStrategies strategies) {
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
Expand Down Expand Up @@ -259,6 +272,14 @@ interface Builder {
*/
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -61,15 +61,20 @@ public DefaultClientResponseBuilder(ExchangeStrategies strategies) {
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()));
}


@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;
}
Expand Down Expand Up @@ -140,15 +145,15 @@ public ClientResponse build() {

private static class BuiltClientHttpResponse implements ClientHttpResponse {

private final HttpStatus statusCode;
private final int statusCode;

private final HttpHeaders headers;

private final MultiValueMap<String, ResponseCookie> cookies;

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;
Expand All @@ -159,12 +164,12 @@ public BuiltClientHttpResponse(HttpStatus statusCode, HttpHeaders headers,

@Override
public HttpStatus getStatusCode() {
return this.statusCode;
return HttpStatus.resolve(this.statusCode);
}

@Override
public int getRawStatusCode() {
return this.statusCode.value();
return this.statusCode;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -30,7 +30,9 @@
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
Expand Down Expand Up @@ -100,5 +102,19 @@ public void from() {
.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());

}


}

0 comments on commit 29ef985

Please sign in to comment.