Add awaitExchangeOrNull extension function to reactive webclient
Closes gh-26778
This commit is contained in:
parent
aff0d8efe7
commit
e24b2e6b5d
|
|
@ -90,6 +90,15 @@ suspend fun RequestHeadersSpec<out RequestHeadersSpec<*>>.awaitExchange(): Clien
|
|||
suspend fun <T: Any> RequestHeadersSpec<out RequestHeadersSpec<*>>.awaitExchange(responseHandler: suspend (ClientResponse) -> T): T =
|
||||
exchangeToMono { mono(Dispatchers.Unconfined) { responseHandler.invoke(it) } }.awaitSingle()
|
||||
|
||||
/**
|
||||
* Variant of [WebClient.RequestHeadersSpec.awaitExchange] that allows a nullable return
|
||||
*
|
||||
* @since 5.3.8
|
||||
*/
|
||||
@Suppress("DEPRECATION")
|
||||
suspend fun <T: Any> RequestHeadersSpec<out RequestHeadersSpec<*>>.awaitExchangeOrNull(responseHandler: suspend (ClientResponse) -> T?): T? =
|
||||
exchangeToMono { mono(Dispatchers.Unconfined) { responseHandler.invoke(it) } }.awaitSingleOrNull()
|
||||
|
||||
/**
|
||||
* Coroutines variant of [WebClient.RequestHeadersSpec.exchangeToFlux].
|
||||
*
|
||||
|
|
|
|||
|
|
@ -103,6 +103,24 @@ class WebClientExtensionsTests {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `awaitExchangeOrNull returning null`() {
|
||||
val foo = mockk<Foo>()
|
||||
every { requestBodySpec.exchangeToMono(any<Function<ClientResponse, Mono<Foo?>>>()) } returns Mono.empty()
|
||||
runBlocking {
|
||||
assertThat(requestBodySpec.awaitExchangeOrNull { foo }).isEqualTo(null)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `awaitExchangeOrNull returning object`() {
|
||||
val foo = mockk<Foo>()
|
||||
every { requestBodySpec.exchangeToMono(any<Function<ClientResponse, Mono<Foo>>>()) } returns Mono.just(foo)
|
||||
runBlocking {
|
||||
assertThat(requestBodySpec.awaitExchangeOrNull { foo }).isEqualTo(foo)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun exchangeToFlow() {
|
||||
val foo = mockk<Foo>()
|
||||
|
|
|
|||
Loading…
Reference in New Issue