|
1 | 1 | package io.vertx.ext.web.tests; |
2 | 2 |
|
| 3 | +import io.vertx.core.Vertx; |
3 | 4 | import io.vertx.ext.auth.authorization.RoleBasedAuthorization; |
4 | 5 | import io.vertx.ext.auth.oauth2.OAuth2Auth; |
5 | 6 | import io.vertx.ext.auth.oauth2.OAuth2Options; |
6 | | -import io.vertx.ext.unit.junit.RunTestOnContext; |
7 | | -import io.vertx.ext.unit.junit.VertxUnitRunner; |
8 | 7 | import io.vertx.ext.web.Router; |
9 | 8 | import io.vertx.ext.web.RoutingContext; |
10 | 9 | import io.vertx.ext.web.handler.*; |
11 | 10 | import io.vertx.ext.web.handler.sockjs.SockJSBridgeOptions; |
12 | 11 | import io.vertx.ext.web.handler.sockjs.SockJSHandler; |
13 | 12 | import io.vertx.ext.web.sstore.SessionStore; |
14 | | -import org.junit.Rule; |
15 | | -import org.junit.Test; |
16 | | -import org.junit.runner.RunWith; |
| 13 | +import io.vertx.junit5.VertxTest; |
| 14 | +import org.junit.jupiter.api.BeforeEach; |
| 15 | +import org.junit.jupiter.api.Test; |
17 | 16 |
|
18 | | -@RunWith(VertxUnitRunner.class) |
| 17 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 18 | + |
| 19 | +@VertxTest |
19 | 20 | public class RouterValidationTest { |
20 | 21 |
|
21 | | - @Rule |
22 | | - public final RunTestOnContext rule = new RunTestOnContext(); |
| 22 | + Vertx vertx; |
| 23 | + |
| 24 | + @BeforeEach |
| 25 | + public void setUp(Vertx vertx) { |
| 26 | + this.vertx = vertx; |
| 27 | + } |
23 | 28 |
|
24 | 29 | @Test |
25 | 30 | public void addBodyHandler() { |
26 | | - Router router = Router.router(rule.vertx()); |
| 31 | + Router router = Router.router(vertx); |
27 | 32 | router.route().handler(BodyHandler.create()); |
28 | 33 | } |
29 | 34 |
|
30 | 35 | @Test |
31 | 36 | public void addBodyHandlerAndUserHandler() { |
32 | | - Router router = Router.router(rule.vertx()); |
| 37 | + Router router = Router.router(vertx); |
33 | 38 | router.route() |
34 | 39 | .handler(BodyHandler.create()) |
35 | 40 | .handler(RoutingContext::end); |
36 | 41 | } |
37 | 42 |
|
38 | | - @Test(expected = IllegalStateException.class) |
| 43 | + @Test |
39 | 44 | public void addBodyHandlerAndUserHandlerBadOrder() { |
40 | | - Router router = Router.router(rule.vertx()); |
41 | | - router.route() |
42 | | - .handler(RoutingContext::end) |
43 | | - .handler(BodyHandler.create()); |
| 45 | + Router router = Router.router(vertx); |
| 46 | + assertThrows(IllegalStateException.class, () -> { |
| 47 | + router.route() |
| 48 | + .handler(RoutingContext::end) |
| 49 | + .handler(BodyHandler.create()); |
| 50 | + }); |
44 | 51 | } |
45 | 52 |
|
46 | 53 | @Test |
47 | 54 | public void addBodyHandlerAndAuthn() { |
48 | | - Router router = Router.router(rule.vertx()); |
| 55 | + Router router = Router.router(vertx); |
49 | 56 | router.route() |
50 | 57 | .handler(BodyHandler.create()) |
51 | | - .handler(OAuth2AuthHandler.create(rule.vertx(), OAuth2Auth.create(rule.vertx(), new OAuth2Options().setClientId("test-id")))); |
| 58 | + .handler(OAuth2AuthHandler.create(vertx, OAuth2Auth.create(vertx, new OAuth2Options().setClientId("test-id")))); |
52 | 59 | } |
53 | 60 |
|
54 | 61 | @Test |
55 | 62 | public void addBodyHandlerAndAuthnAuthz() { |
56 | | - Router router = Router.router(rule.vertx()); |
| 63 | + Router router = Router.router(vertx); |
57 | 64 | router.route() |
58 | | - .handler(SessionHandler.create(SessionStore.create(rule.vertx()))) |
59 | | - .handler(OAuth2AuthHandler.create(rule.vertx(), OAuth2Auth.create(rule.vertx(), new OAuth2Options().setClientId("test-id")))) |
| 65 | + .handler(SessionHandler.create(SessionStore.create(vertx))) |
| 66 | + .handler(OAuth2AuthHandler.create(vertx, OAuth2Auth.create(vertx, new OAuth2Options().setClientId("test-id")))) |
60 | 67 | .handler(AuthorizationHandler.create(RoleBasedAuthorization.create("my-role"))); |
61 | 68 | } |
62 | 69 |
|
63 | | - @Test(expected = IllegalStateException.class) |
| 70 | + @Test |
64 | 71 | public void addBodyHandlerAndAuthnAuthzBadOrder() { |
65 | | - Router router = Router.router(rule.vertx()); |
66 | | - router.route() |
67 | | - .handler(SessionHandler.create(SessionStore.create(rule.vertx()))) |
68 | | - .handler(AuthorizationHandler.create(RoleBasedAuthorization.create("my-role"))) |
69 | | - // will fail as authz depends on authn (so adding authn after authz is clearly a mistake) |
70 | | - .handler(OAuth2AuthHandler.create(rule.vertx(), OAuth2Auth.create(rule.vertx(), new OAuth2Options().setClientId("test-id")))); |
| 72 | + Router router = Router.router(vertx); |
| 73 | + // will fail as authz depends on authn (so adding authn after authz is clearly a mistake) |
| 74 | + assertThrows(IllegalStateException.class, () -> { |
| 75 | + router.route() |
| 76 | + .handler(SessionHandler.create(SessionStore.create(vertx))) |
| 77 | + .handler(AuthorizationHandler.create(RoleBasedAuthorization.create("my-role"))) |
| 78 | + .handler(OAuth2AuthHandler.create(vertx, OAuth2Auth.create(vertx, new OAuth2Options().setClientId("test-id")))); |
| 79 | + }); |
71 | 80 | } |
72 | 81 |
|
73 | | - @Test(expected = IllegalStateException.class) |
| 82 | + @Test |
74 | 83 | public void addBodyHandlerAndAuthnAuthzBadOrder2() { |
75 | | - Router router = Router.router(rule.vertx()); |
76 | | - router.route() |
77 | | - .handler(OAuth2AuthHandler.create(rule.vertx(), OAuth2Auth.create(rule.vertx(), new OAuth2Options().setClientId("test-id")))) |
78 | | - .handler(AuthorizationHandler.create(RoleBasedAuthorization.create("my-role"))) |
79 | | - // will fail as platform handlers should be mounted earlier as they have dependants |
80 | | - .handler(SessionHandler.create(SessionStore.create(rule.vertx()))); |
| 84 | + Router router = Router.router(vertx); |
| 85 | + assertThrows(IllegalStateException.class, () -> { |
| 86 | + router.route() |
| 87 | + .handler(OAuth2AuthHandler.create(vertx, OAuth2Auth.create(vertx, new OAuth2Options().setClientId("test-id")))) |
| 88 | + .handler(AuthorizationHandler.create(RoleBasedAuthorization.create("my-role"))) |
| 89 | + .handler(SessionHandler.create(SessionStore.create(vertx))); |
| 90 | + }); |
81 | 91 | } |
82 | 92 |
|
83 | 93 | @Test |
84 | 94 | public void addSecurityPolicyAndUserHandlers() { |
85 | | - Router router = Router.router(rule.vertx()); |
| 95 | + Router router = Router.router(vertx); |
86 | 96 | router.route() |
87 | 97 | .handler(CorsHandler.create()) |
88 | | - .subRouter(SockJSHandler.create(rule.vertx()).bridge(new SockJSBridgeOptions())); |
| 98 | + .subRouter(SockJSHandler.create(vertx).bridge(new SockJSBridgeOptions())); |
89 | 99 | } |
90 | 100 |
|
91 | | - @Test(expected = IllegalStateException.class) |
| 101 | + @Test |
92 | 102 | public void addSecurityPolicyAndUserHandlersBadOrder() { |
93 | | - Router router = Router.router(rule.vertx()); |
94 | | - router.route() |
95 | | - .subRouter(SockJSHandler.create(rule.vertx()).bridge(new SockJSBridgeOptions())) |
| 103 | + Router router = Router.router(vertx); |
| 104 | + assertThrows(IllegalStateException.class, () -> { |
96 | 105 | // will fail as the route should be exclusive |
97 | | - .handler(CorsHandler.create()); |
| 106 | + router.route() |
| 107 | + .subRouter(SockJSHandler.create(vertx).bridge(new SockJSBridgeOptions())) |
| 108 | + .handler(CorsHandler.create()); |
| 109 | + }); |
98 | 110 | } |
99 | 111 |
|
100 | | - @Test(expected = IllegalStateException.class) |
| 112 | + @Test |
101 | 113 | public void addSecurityPolicyAndUserHandlersBadOrder2() { |
102 | | - Router router = Router.router(rule.vertx()); |
103 | | - router.route() |
104 | | - .handler(RoutingContext::end) |
| 114 | + Router router = Router.router(vertx); |
| 115 | + assertThrows(IllegalStateException.class, () -> { |
105 | 116 | // will fail as the route has a user handler but a policy one is being added after |
106 | | - .handler(CorsHandler.create()); |
| 117 | + router.route() |
| 118 | + .handler(RoutingContext::end) |
| 119 | + .handler(CorsHandler.create()); |
| 120 | + }); |
107 | 121 | } |
108 | 122 | } |
0 commit comments