Fallback on underlying server default when determining outcome tag

This commit also changed the default outcome to SUCCESS

Fixes gh-19367
pull/19690/head
Madhura Bhave 5 years ago
parent a017b8905a
commit 7f0573df49

@ -136,14 +136,17 @@ public final class WebFluxTags {
*/ */
public static Tag outcome(ServerWebExchange exchange) { public static Tag outcome(ServerWebExchange exchange) {
Integer statusCode = extractStatusCode(exchange); Integer statusCode = extractStatusCode(exchange);
Outcome outcome = (statusCode != null) ? Outcome.forStatus(statusCode) : Outcome.UNKNOWN; Outcome outcome = (statusCode != null) ? Outcome.forStatus(statusCode) : Outcome.SUCCESS;
return outcome.asTag(); return outcome.asTag();
} }
private static Integer extractStatusCode(ServerWebExchange exchange) { private static Integer extractStatusCode(ServerWebExchange exchange) {
ServerHttpResponse response = exchange.getResponse(); ServerHttpResponse response = exchange.getResponse();
if (response instanceof AbstractServerHttpResponse) { if (response instanceof AbstractServerHttpResponse) {
return ((AbstractServerHttpResponse) response).getStatusCodeValue(); Integer statusCode = ((AbstractServerHttpResponse) response).getStatusCodeValue();
if (statusCode != null) {
return statusCode;
}
} }
HttpStatus status = response.getStatusCode(); HttpStatus status = response.getStatusCode();
return (status != null) ? status.value() : null; return (status != null) ? status.value() : null;

@ -21,6 +21,7 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.AbstractServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest; import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.mock.web.server.MockServerWebExchange; import org.springframework.mock.web.server.MockServerWebExchange;
@ -37,6 +38,7 @@ import static org.mockito.Mockito.mock;
* *
* @author Brian Clozel * @author Brian Clozel
* @author Michael McFadyen * @author Michael McFadyen
* @author Madhura Bhave
*/ */
class WebFluxTagsTests { class WebFluxTagsTests {
@ -114,7 +116,20 @@ class WebFluxTagsTests {
void outcomeTagIsUnknownWhenResponseStatusIsNull() { void outcomeTagIsUnknownWhenResponseStatusIsNull() {
this.exchange.getResponse().setStatusCode(null); this.exchange.getResponse().setStatusCode(null);
Tag tag = WebFluxTags.outcome(this.exchange); Tag tag = WebFluxTags.outcome(this.exchange);
assertThat(tag.getValue()).isEqualTo("UNKNOWN"); assertThat(tag.getValue()).isEqualTo("SUCCESS");
}
@Test
void outcomeTagIsSuccessWhenResponseStatusIsAvailableFromUnderlyingServer() {
ServerWebExchange exchange = mock(ServerWebExchange.class);
ServerHttpRequest request = mock(ServerHttpRequest.class);
AbstractServerHttpResponse response = mock(AbstractServerHttpResponse.class);
given(response.getStatusCode()).willReturn(HttpStatus.OK);
given(response.getStatusCodeValue()).willReturn(null);
given(exchange.getRequest()).willReturn(request);
given(exchange.getResponse()).willReturn(response);
Tag tag = WebFluxTags.outcome(exchange);
assertThat(tag.getValue()).isEqualTo("SUCCESS");
} }
@Test @Test

Loading…
Cancel
Save