Add awaitExchangeOrNull extension function to reactive webclient

Closes gh-26778
This commit is contained in:
Gabriel 2021-04-08 14:39:53 -03:00 committed by Sébastien Deleuze
parent aff0d8efe7
commit e24b2e6b5d
2 changed files with 27 additions and 0 deletions

View File

@ -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].
*

View File

@ -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>()