Inherit parent context in coRouter DSL
This commit also allows context override, as it is useful for the nested router use case. Closes gh-31831
This commit is contained in:
@@ -193,6 +193,45 @@ class CoRouterFunctionDslTests {
|
||||
.verifyComplete()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun nestedContextProvider() {
|
||||
val mockRequest = get("https://example.com/nested/")
|
||||
.header("Custom-Header", "foo")
|
||||
.build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(nestedRouterWithContextProvider.route(request).flatMap { it.handle(request) })
|
||||
.expectNextMatches { response ->
|
||||
response.headers().getFirst("context")!!.contains("foo")
|
||||
}
|
||||
.verifyComplete()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun nestedContextProviderWithOverride() {
|
||||
val mockRequest = get("https://example.com/nested/")
|
||||
.header("Custom-Header", "foo")
|
||||
.build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(nestedRouterWithContextProviderOverride.route(request).flatMap { it.handle(request) })
|
||||
.expectNextMatches { response ->
|
||||
response.headers().getFirst("context")!!.contains("foo")
|
||||
}
|
||||
.verifyComplete()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun doubleNestedContextProvider() {
|
||||
val mockRequest = get("https://example.com/nested/nested/")
|
||||
.header("Custom-Header", "foo")
|
||||
.build()
|
||||
val request = DefaultServerRequest(MockServerWebExchange.from(mockRequest), emptyList())
|
||||
StepVerifier.create(nestedRouterWithContextProvider.route(request).flatMap { it.handle(request) })
|
||||
.expectNextMatches { response ->
|
||||
response.headers().getFirst("context")!!.contains("foo")
|
||||
}
|
||||
.verifyComplete()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun contextProviderAndFilter() {
|
||||
val mockRequest = get("https://example.com/")
|
||||
@@ -323,6 +362,36 @@ class CoRouterFunctionDslTests {
|
||||
}
|
||||
}
|
||||
|
||||
private val nestedRouterWithContextProvider = coRouter {
|
||||
context {
|
||||
CoroutineName(it.headers().firstHeader("Custom-Header")!!)
|
||||
}
|
||||
"/nested".nest {
|
||||
GET("/") {
|
||||
ok().header("context", currentCoroutineContext().toString()).buildAndAwait()
|
||||
}
|
||||
"/nested".nest {
|
||||
GET("/") {
|
||||
ok().header("context", currentCoroutineContext().toString()).buildAndAwait()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val nestedRouterWithContextProviderOverride = coRouter {
|
||||
context {
|
||||
CoroutineName("parent-context")
|
||||
}
|
||||
"/nested".nest {
|
||||
context {
|
||||
CoroutineName(it.headers().firstHeader("Custom-Header")!!)
|
||||
}
|
||||
GET("/") {
|
||||
ok().header("context", currentCoroutineContext().toString()).buildAndAwait()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val routerWithoutContext = coRouter {
|
||||
GET("/") {
|
||||
ok().header("context", currentCoroutineContext().toString()).buildAndAwait()
|
||||
|
||||
Reference in New Issue
Block a user