Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add awaitExchangeOrNull extension function to reactive webclient #26778

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add awaitExchangeOrNull extension function to reactive webclient
  • Loading branch information
Gabriel committed Apr 8, 2021
commit d8916ce41d770c7da1af30e5078cdf1cad45a367
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ 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
*/
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