@@ -7,6 +7,7 @@ import io.javalin.event.JavalinLifecycleEvent.SERVER_STARTED
77import io.javalin.http.Context
88import io.javalin.http.HandlerType
99import io.javalin.http.HttpStatus
10+ import io.javalin.router.Endpoint
1011import io.javalin.testtools.HttpClient
1112import io.javalin.testtools.JavalinTest
1213import kong.unirest.Unirest
@@ -36,11 +37,25 @@ class AnnotatedRoutingTest {
3637 object {
3738 // formatter:off
3839 @Before fun beforeEach (ctx : Context ) { ctx.header(" before" , " true" ) }
39- @Before(" /specific" ) fun beforeSpecific (ctx : Context ) { ctx.header(" before" , " specific" ) }
40- @BeforeMatched fun beforeEachMatched (ctx : Context ) { ctx.header(" before-matched" , " true" ) }
41- @AfterMatched fun afterEachMatched (ctx : Context ) { ctx.header(" after-matched" , " true" ) }
42- @After(" /specific" ) fun afterSpecific (ctx : Context ) { ctx.header(" after" , " specific" ) }
4340 @After fun afterEach (ctx : Context ) { ctx.header(" after" , " true" ) }
41+
42+ @Before(" /specific" ) fun beforeSpecific (ctx : Context , endpoint : Endpoint ? ) {
43+ ctx.header(" before" , " specific" )
44+ ctx.header(" before-endpoint" , endpoint?.path ? : " not-matched" )
45+ }
46+ @BeforeMatched fun beforeEachMatched (ctx : Context , endpoint : Endpoint ) {
47+ ctx.header(" before-matched" , " true" )
48+ ctx.header(" before-matched-endpoint" , " ${endpoint.method.name} ${endpoint.path} " )
49+ }
50+ @AfterMatched fun afterEachMatched (ctx : Context , endpoint : Endpoint ) {
51+ ctx.header(" after-matched" , " true" )
52+ ctx.header(" after-matched-endpoint" , " ${endpoint.method.name} ${endpoint.path} " )
53+ }
54+ @After(" /specific" ) fun afterSpecific (ctx : Context , endpoint : Endpoint ? ) {
55+ ctx.header(" after" , " specific" )
56+ ctx.header(" after-endpoint" , endpoint?.path ? : " not-matched" )
57+ }
58+
4459 @Get(" /get" ) fun testGet (ctx : Context ) { ctx.header(" get" , " true" ) }
4560 @Post(" /post" ) fun testPost (ctx : Context ) { ctx.header(" post" , " true" ) }
4661 @Put(" /put" ) fun testPut (ctx : Context ) { ctx.header(" put" , " true" ) }
@@ -64,10 +79,16 @@ class AnnotatedRoutingTest {
6479 .forEach {
6580 val response = request(it.name, " ${client.origin} /test/${it.name.lowercase()} " ).asEmpty()
6681 assertThat(response.headers.getFirst(it.name.lowercase())).isEqualTo(" true" )
82+
6783 assertThat(response.headers.getFirst(" before" )).isEqualTo(" true" )
84+ assertThat(response.headers.getFirst(" before-endpoint" )).isEmpty()
6885 assertThat(response.headers.getFirst(" before-matched" )).isEqualTo(" true" )
86+ assertThat(response.headers.getFirst(" before-matched-endpoint" )).isEqualTo(" ${it.name} /test/${it.name.lowercase()} " )
87+
6988 assertThat(response.headers.getFirst(" after" )).isEqualTo(" true" )
89+ assertThat(response.headers.getFirst(" after-endpoint" )).isEmpty()
7090 assertThat(response.headers.getFirst(" after-matched" )).isEqualTo(" true" )
91+ assertThat(response.headers.getFirst(" after-matched-endpoint" )).isEqualTo(" ${it.name} /test/${it.name.lowercase()} " )
7192 }
7293 }
7394 }
@@ -91,9 +112,11 @@ class AnnotatedRoutingTest {
91112 withinSharedScenario { client ->
92113 val beforeSpecific = request(" GET" , " ${client.origin} /test/specific" ).asEmpty()
93114 assertThat(beforeSpecific.headers.getFirst(" before" )).isEqualTo(" specific" )
115+ assertThat(beforeSpecific.headers.getFirst(" before-endpoint" )).isEqualTo(" not-matched" )
94116
95117 val beforeTooSpecific = request(" GET" , " ${client.origin} /test/specific/too-specific" ).asEmpty()
96118 assertThat(beforeTooSpecific.headers.getFirst(" before" )).isNotEqualTo(" specific" )
119+ assertThat(beforeTooSpecific.headers.getFirst(" before-endpoint" )).isEmpty()
97120 }
98121 }
99122
@@ -102,9 +125,11 @@ class AnnotatedRoutingTest {
102125 withinSharedScenario { client ->
103126 val afterSpecific = request(" GET" , " ${client.origin} /test/specific" ).asEmpty()
104127 assertThat(afterSpecific.headers.getFirst(" after" )).isEqualTo(" specific" )
128+ assertThat(afterSpecific.headers.getFirst(" after-endpoint" )).isEqualTo(" not-matched" )
105129
106130 val afterTooSpecific = request(" GET" , " ${client.origin} /test/specific/too-specific" ).asEmpty()
107131 assertThat(afterTooSpecific.headers.getFirst(" after" )).isNotEqualTo(" specific" )
132+ assertThat(afterTooSpecific.headers.getFirst(" after-endpoint" )).isEmpty()
108133 }
109134 }
110135
0 commit comments