@@ -18,7 +18,9 @@ package org.springframework.web.reactive.function.client
18
18
19
19
import io.mockk.every
20
20
import io.mockk.mockk
21
+ import io.mockk.slot
21
22
import io.mockk.verify
23
+ import kotlinx.coroutines.currentCoroutineContext
22
24
import kotlinx.coroutines.flow.Flow
23
25
import kotlinx.coroutines.flow.flow
24
26
import kotlinx.coroutines.flow.toList
@@ -32,6 +34,8 @@ import reactor.core.publisher.Flux
32
34
import reactor.core.publisher.Mono
33
35
import java.util.concurrent.CompletableFuture
34
36
import java.util.function.Function
37
+ import kotlin.coroutines.AbstractCoroutineContextElement
38
+ import kotlin.coroutines.CoroutineContext
35
39
36
40
/* *
37
41
* Mock object based tests for [WebClient] Kotlin extensions
@@ -110,6 +114,18 @@ class WebClientExtensionsTests {
110
114
}
111
115
}
112
116
117
+ @Test
118
+ fun `awaitExchange with coroutines context` () {
119
+ val foo = mockk<Foo >()
120
+ val slot = slot<Function <ClientResponse , Mono <Foo >>>()
121
+ every { requestBodySpec.exchangeToMono(capture(slot)) } answers {
122
+ slot.captured.apply (mockk<ClientResponse >())
123
+ }
124
+ runBlocking(FooContextElement (foo)) {
125
+ assertThat(requestBodySpec.awaitExchange { currentCoroutineContext()[FooContextElement ]!! .foo }).isEqualTo(foo)
126
+ }
127
+ }
128
+
113
129
@Test
114
130
fun `awaitExchangeOrNull returning null` () {
115
131
val foo = mockk<Foo >()
@@ -128,6 +144,18 @@ class WebClientExtensionsTests {
128
144
}
129
145
}
130
146
147
+ @Test
148
+ fun `awaitExchangeOrNull with coroutines context` () {
149
+ val foo = mockk<Foo >()
150
+ val slot = slot<Function <ClientResponse , Mono <Foo >>>()
151
+ every { requestBodySpec.exchangeToMono(capture(slot)) } answers {
152
+ slot.captured.apply (mockk<ClientResponse >())
153
+ }
154
+ runBlocking(FooContextElement (foo)) {
155
+ assertThat(requestBodySpec.awaitExchangeOrNull { currentCoroutineContext()[FooContextElement ]!! .foo }).isEqualTo(foo)
156
+ }
157
+ }
158
+
131
159
@Test
132
160
fun exchangeToFlow () {
133
161
val foo = mockk<Foo >()
@@ -209,4 +237,8 @@ class WebClientExtensionsTests {
209
237
}
210
238
211
239
class Foo
240
+
241
+ private data class FooContextElement (val foo : Foo ) : AbstractCoroutineContextElement(FooContextElement ) {
242
+ companion object Key : CoroutineContext.Key<FooContextElement>
243
+ }
212
244
}
0 commit comments