Add coroutine context tests for WebClientExtensions

Closes gh-33548
This commit is contained in:
Sébastien Deleuze 2024-09-25 11:47:17 +02:00
parent 478aa250a0
commit 2ab0101b8a
1 changed files with 33 additions and 1 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2021 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,7 +18,9 @@ package org.springframework.web.reactive.function.client
import io.mockk.every import io.mockk.every
import io.mockk.mockk import io.mockk.mockk
import io.mockk.slot
import io.mockk.verify import io.mockk.verify
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.toList import kotlinx.coroutines.flow.toList
@ -32,6 +34,8 @@ import reactor.core.publisher.Flux
import reactor.core.publisher.Mono import reactor.core.publisher.Mono
import java.util.concurrent.CompletableFuture import java.util.concurrent.CompletableFuture
import java.util.function.Function import java.util.function.Function
import kotlin.coroutines.AbstractCoroutineContextElement
import kotlin.coroutines.CoroutineContext
/** /**
* Mock object based tests for [WebClient] Kotlin extensions * Mock object based tests for [WebClient] Kotlin extensions
@ -103,6 +107,18 @@ class WebClientExtensionsTests {
} }
} }
@Test
fun `awaitExchange with coroutines context`() {
val foo = mockk<Foo>()
val slot = slot<Function<ClientResponse, Mono<Foo>>>()
every { requestBodySpec.exchangeToMono(capture(slot)) } answers {
slot.captured.apply(mockk<ClientResponse>())
}
runBlocking(FooContextElement(foo)) {
assertThat(requestBodySpec.awaitExchange { currentCoroutineContext()[FooContextElement]!!.foo }).isEqualTo(foo)
}
}
@Test @Test
fun `awaitExchangeOrNull returning null`() { fun `awaitExchangeOrNull returning null`() {
val foo = mockk<Foo>() val foo = mockk<Foo>()
@ -121,6 +137,18 @@ class WebClientExtensionsTests {
} }
} }
@Test
fun `awaitExchangeOrNull with coroutines context`() {
val foo = mockk<Foo>()
val slot = slot<Function<ClientResponse, Mono<Foo>>>()
every { requestBodySpec.exchangeToMono(capture(slot)) } answers {
slot.captured.apply(mockk<ClientResponse>())
}
runBlocking(FooContextElement(foo)) {
assertThat(requestBodySpec.awaitExchangeOrNull { currentCoroutineContext()[FooContextElement]!!.foo }).isEqualTo(foo)
}
}
@Test @Test
fun exchangeToFlow() { fun exchangeToFlow() {
val foo = mockk<Foo>() val foo = mockk<Foo>()
@ -202,4 +230,8 @@ class WebClientExtensionsTests {
} }
class Foo class Foo
private data class FooContextElement(val foo: Foo) : AbstractCoroutineContextElement(FooContextElement) {
companion object Key : CoroutineContext.Key<FooContextElement>
}
} }