Improve WebFlux support for response status exceptions

Support @ResponseStatus annotated exceptions.
Supported root cause exceptions with response status information.

Issue: SPR-16567
This commit is contained in:
Rossen Stoyanchev
2018-03-14 20:07:00 -04:00
parent cb8c6e3251
commit b8d94f8a20
2 changed files with 66 additions and 18 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -25,6 +25,7 @@ import reactor.test.StepVerifier;
import org.springframework.http.HttpStatus;
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
import org.springframework.mock.web.test.server.MockServerWebExchange;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.server.ResponseStatusException;
import static org.junit.Assert.assertEquals;
@@ -42,21 +43,42 @@ public class ResponseStatusExceptionHandlerTests {
@Test
public void handleException() throws Exception {
public void handleResponseStatusException() {
Throwable ex = new ResponseStatusException(HttpStatus.BAD_REQUEST, "");
this.handler.handle(this.exchange, ex).block(Duration.ofSeconds(5));
assertEquals(HttpStatus.BAD_REQUEST, this.exchange.getResponse().getStatusCode());
}
@Test
public void unresolvedException() throws Exception {
public void handleAnnotatedException() {
Throwable ex = new CustomException();
this.handler.handle(this.exchange, ex).block(Duration.ofSeconds(5));
assertEquals(HttpStatus.I_AM_A_TEAPOT, this.exchange.getResponse().getStatusCode());
}
@Test
public void handleNestedResponseStatusException() {
Throwable ex = new Exception(new ResponseStatusException(HttpStatus.BAD_REQUEST, ""));
this.handler.handle(this.exchange, ex).block(Duration.ofSeconds(5));
assertEquals(HttpStatus.BAD_REQUEST, this.exchange.getResponse().getStatusCode());
}
@Test
public void handleNestedAnnotatedException() {
Throwable ex = new Exception(new CustomException());
this.handler.handle(this.exchange, ex).block(Duration.ofSeconds(5));
assertEquals(HttpStatus.I_AM_A_TEAPOT, this.exchange.getResponse().getStatusCode());
}
@Test
public void unresolvedException() {
Throwable expected = new IllegalStateException();
Mono<Void> mono = this.handler.handle(this.exchange, expected);
StepVerifier.create(mono).consumeErrorWith(actual -> assertSame(expected, actual)).verify();
}
@Test // SPR-16231
public void responseCommitted() throws Exception {
public void responseCommitted() {
Throwable ex = new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Oops");
this.exchange.getResponse().setStatusCode(HttpStatus.CREATED);
Mono<Void> mono = this.exchange.getResponse().setComplete()
@@ -64,4 +86,9 @@ public class ResponseStatusExceptionHandlerTests {
StepVerifier.create(mono).consumeErrorWith(actual -> assertSame(ex, actual)).verify();
}
@ResponseStatus(HttpStatus.I_AM_A_TEAPOT)
private static class CustomException extends Exception {
}
}