Skip to content

Commit

Permalink
Add awaitExchangeOrNull extension function to reactive webclient
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel authored and sdeleuze committed Jun 8, 2021
1 parent aff0d8e commit e24b2e6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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].
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>()
Expand Down

0 comments on commit e24b2e6

Please sign in to comment.