Propagate CoroutineContext in coRouter filters

Closes gh-26977
This commit is contained in:
Sébastien Deleuze
2023-08-29 10:36:03 +02:00
parent bcf11e8919
commit d47c7f9552
2 changed files with 57 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -16,17 +16,20 @@
package org.springframework.web.reactive.function.server
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.withContext
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatExceptionOfType
import org.junit.jupiter.api.Test
import org.springframework.core.io.ClassPathResource
import org.springframework.http.HttpHeaders.*
import org.springframework.http.HttpMethod.*
import org.springframework.http.HttpHeaders.ACCEPT
import org.springframework.http.HttpHeaders.CONTENT_TYPE
import org.springframework.http.HttpMethod.PATCH
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType.*
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest.*
import org.springframework.web.testfixture.server.MockServerWebExchange
import org.springframework.web.reactive.function.server.AttributesTestVisitor
import reactor.test.StepVerifier
/**
@@ -165,6 +168,17 @@ class CoRouterFunctionDslTests {
.verifyComplete()
}
@Test
fun filteringWithContext() {
val mockRequest = get("https://example.com/").build()
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
StepVerifier.create(filterRouterWithContext.route(request).flatMap { it.handle(request) })
.expectNextMatches { response ->
response.headers().getFirst("context")!!.contains("Filter context")
}
.verifyComplete()
}
@Test
fun attributes() {
val visitor = AttributesTestVisitor()
@@ -226,6 +240,17 @@ class CoRouterFunctionDslTests {
}
}
private val filterRouterWithContext = coRouter {
filter { request, next ->
withContext(CoroutineName("Filter context")) {
next(request)
}
}
GET("/") {
ok().header("context", currentCoroutineContext().toString()).buildAndAwait()
}
}
private val otherRouter = router {
"/other" {
ok().build()