@@ -25,7 +25,11 @@ import org.springframework.http.HttpHeaders.CONTENT_TYPE
25
25
import org.springframework.http.HttpMethod.PATCH
26
26
import org.springframework.http.HttpStatus
27
27
import org.springframework.http.MediaType.*
28
+ import org.springframework.web.server.CoWebFilter
29
+ import org.springframework.web.server.CoWebFilterChain
30
+ import org.springframework.web.server.ServerWebExchange
28
31
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest.*
32
+ import org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse
29
33
import org.springframework.web.testfixture.server.MockServerWebExchange
30
34
import reactor.test.StepVerifier
31
35
@@ -204,6 +208,16 @@ class CoRouterFunctionDslTests {
204
208
.verifyComplete()
205
209
}
206
210
211
+ @Test
212
+ fun webFilterAndContext () {
213
+ val strategies = HandlerStrategies .builder().webFilter(MyCoWebFilterWithContext ()).build()
214
+ val httpHandler = RouterFunctions .toHttpHandler(routerWithoutContext, strategies)
215
+ val mockRequest = get(" https://example.com/" ).build()
216
+ val mockResponse = MockServerHttpResponse ()
217
+ StepVerifier .create(httpHandler.handle(mockRequest, mockResponse)).verifyComplete()
218
+ assertThat(mockResponse.headers.getFirst(" context" )).contains(" Filter context" )
219
+ }
220
+
207
221
@Test
208
222
fun multipleContextProviders () {
209
223
assertThatIllegalStateException().isThrownBy {
@@ -309,6 +323,12 @@ class CoRouterFunctionDslTests {
309
323
}
310
324
}
311
325
326
+ private val routerWithoutContext = coRouter {
327
+ GET (" /" ) {
328
+ ok().header(" context" , currentCoroutineContext().toString()).buildAndAwait()
329
+ }
330
+ }
331
+
312
332
private val otherRouter = router {
313
333
" /other" {
314
334
ok().build()
@@ -369,3 +389,12 @@ class CoRouterFunctionDslTests {
369
389
370
390
@Suppress(" UNUSED_PARAMETER" )
371
391
private suspend fun handle (req : ServerRequest ) = ServerResponse .ok().buildAndAwait()
392
+
393
+
394
+ private class MyCoWebFilterWithContext : CoWebFilter () {
395
+ override suspend fun filter (exchange : ServerWebExchange , chain : CoWebFilterChain ) {
396
+ withContext(CoroutineName (" Filter context" )) {
397
+ chain.filter(exchange)
398
+ }
399
+ }
400
+ }
0 commit comments