Skip to content

Commit 2ab0101

Browse files
committed
Add coroutine context tests for WebClientExtensions
Closes gh-33548
1 parent 478aa25 commit 2ab0101

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/client/WebClientExtensionsTests.kt

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,9 @@ package org.springframework.web.reactive.function.client
1818

1919
import io.mockk.every
2020
import io.mockk.mockk
21+
import io.mockk.slot
2122
import io.mockk.verify
23+
import kotlinx.coroutines.currentCoroutineContext
2224
import kotlinx.coroutines.flow.Flow
2325
import kotlinx.coroutines.flow.flow
2426
import kotlinx.coroutines.flow.toList
@@ -32,6 +34,8 @@ import reactor.core.publisher.Flux
3234
import reactor.core.publisher.Mono
3335
import java.util.concurrent.CompletableFuture
3436
import java.util.function.Function
37+
import kotlin.coroutines.AbstractCoroutineContextElement
38+
import kotlin.coroutines.CoroutineContext
3539

3640
/**
3741
* Mock object based tests for [WebClient] Kotlin extensions
@@ -103,6 +107,18 @@ class WebClientExtensionsTests {
103107
}
104108
}
105109

110+
@Test
111+
fun `awaitExchange with coroutines context`() {
112+
val foo = mockk<Foo>()
113+
val slot = slot<Function<ClientResponse, Mono<Foo>>>()
114+
every { requestBodySpec.exchangeToMono(capture(slot)) } answers {
115+
slot.captured.apply(mockk<ClientResponse>())
116+
}
117+
runBlocking(FooContextElement(foo)) {
118+
assertThat(requestBodySpec.awaitExchange { currentCoroutineContext()[FooContextElement]!!.foo }).isEqualTo(foo)
119+
}
120+
}
121+
106122
@Test
107123
fun `awaitExchangeOrNull returning null`() {
108124
val foo = mockk<Foo>()
@@ -121,6 +137,18 @@ class WebClientExtensionsTests {
121137
}
122138
}
123139

140+
@Test
141+
fun `awaitExchangeOrNull with coroutines context`() {
142+
val foo = mockk<Foo>()
143+
val slot = slot<Function<ClientResponse, Mono<Foo>>>()
144+
every { requestBodySpec.exchangeToMono(capture(slot)) } answers {
145+
slot.captured.apply(mockk<ClientResponse>())
146+
}
147+
runBlocking(FooContextElement(foo)) {
148+
assertThat(requestBodySpec.awaitExchangeOrNull { currentCoroutineContext()[FooContextElement]!!.foo }).isEqualTo(foo)
149+
}
150+
}
151+
124152
@Test
125153
fun exchangeToFlow() {
126154
val foo = mockk<Foo>()
@@ -202,4 +230,8 @@ class WebClientExtensionsTests {
202230
}
203231

204232
class Foo
233+
234+
private data class FooContextElement(val foo: Foo) : AbstractCoroutineContextElement(FooContextElement) {
235+
companion object Key : CoroutineContext.Key<FooContextElement>
236+
}
205237
}

0 commit comments

Comments
 (0)