|
18 | 18 |
|
19 | 19 | import io.vertx.core.Handler; |
20 | 20 | import io.vertx.core.Vertx; |
| 21 | +import io.vertx.core.http.HttpClientResponse; |
21 | 22 | import io.vertx.core.http.HttpMethod; |
22 | 23 | import io.vertx.ext.web.Router; |
23 | 24 | import io.vertx.ext.web.RoutingContext; |
24 | 25 | import org.junit.Test; |
25 | 26 |
|
| 27 | +import java.io.IOException; |
26 | 28 | import java.util.function.Consumer; |
27 | 29 |
|
28 | 30 | /** |
@@ -743,4 +745,46 @@ public void testHierarchicalWithParamsSimpleWithDummy() throws Exception { |
743 | 745 |
|
744 | 746 | testRequest(HttpMethod.GET, "/rest/product/123/bar", 200, "OK"); |
745 | 747 | } |
| 748 | + |
| 749 | + @Test |
| 750 | + public void testSetExceptionHandler() throws Exception { |
| 751 | + Router restRouter = Router.router(vertx); |
| 752 | + Router productRouter = Router.router(vertx); |
| 753 | + Router instanceRouter = Router.router(vertx); |
| 754 | + |
| 755 | + router.route("/rest*").subRouter(restRouter); |
| 756 | + restRouter.route("/product*").subRouter(productRouter); |
| 757 | + productRouter.route("/:id*").subRouter(instanceRouter); |
| 758 | + instanceRouter.get("/:foo").handler(ctx -> { |
| 759 | + if ("ex".equals(ctx.pathParam("foo"))) { |
| 760 | + throw new RuntimeException("ouch!"); |
| 761 | + } |
| 762 | + ctx.response().end(); |
| 763 | + }); |
| 764 | + |
| 765 | + HttpClientResponse response = requestGet("/rest/product/123/ex").await(); |
| 766 | + assertEquals(500, response.statusCode()); |
| 767 | + assertEquals("Internal Server Error", response.body().await().toString()); |
| 768 | + |
| 769 | + assertRouterErrorHandlers("root", router, 500, "/rest/product/123/ex"); |
| 770 | + assertRouterErrorHandlers("root", router, 404, "/rest/product/123/foo/404"); |
| 771 | + |
| 772 | + assertRouterErrorHandlers("rest", restRouter, 500, "/rest/product/123/ex"); |
| 773 | + assertRouterErrorHandlers("rest", restRouter, 404, "/rest/product/123/foo/404"); |
| 774 | + |
| 775 | + assertRouterErrorHandlers("product", productRouter, 500, "/rest/product/123/ex"); |
| 776 | + assertRouterErrorHandlers("product", productRouter, 404, "/rest/product/123/foo/404"); |
| 777 | + |
| 778 | + assertRouterErrorHandlers("instance", instanceRouter, 500, "/rest/product/123/ex"); |
| 779 | + assertRouterErrorHandlers("instance", instanceRouter, 404, "/rest/product/123/foo/404"); |
| 780 | + } |
| 781 | + |
| 782 | + private void assertRouterErrorHandlers(String name, Router router, int statusCode, String path) { |
| 783 | + String handlerKey = name + "." + statusCode + ".errorHandler"; |
| 784 | + router.errorHandler(statusCode, ctx -> ctx.response().setStatusCode(statusCode).end(handlerKey)); |
| 785 | + |
| 786 | + HttpClientResponse response = requestGet(path).await(); |
| 787 | + assertEquals(statusCode, response.statusCode()); |
| 788 | + assertEquals(handlerKey, response.body().await().toString()); |
| 789 | + } |
746 | 790 | } |
0 commit comments