Add awaitBodyOrNull to WebClient.ResponseSpec
Such variant is already provided for ClientResponse but not for WebClient.ResponseSpec. Closes gh-26731
This commit is contained in:
committed by
Sébastien Deleuze
parent
85dbd2a4c3
commit
58e40d1aa2
@@ -21,6 +21,7 @@ import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.reactive.asFlow
|
||||
import kotlinx.coroutines.reactive.awaitSingle
|
||||
import kotlinx.coroutines.reactive.awaitSingle
|
||||
import kotlinx.coroutines.reactive.awaitSingleOrNull
|
||||
import kotlinx.coroutines.reactor.asFlux
|
||||
import kotlinx.coroutines.reactor.mono
|
||||
import org.reactivestreams.Publisher
|
||||
@@ -143,6 +144,17 @@ suspend inline fun <reified T : Any> WebClient.ResponseSpec.awaitBody() : T =
|
||||
else -> bodyToMono<T>().awaitSingle()
|
||||
}
|
||||
|
||||
/**
|
||||
* Coroutines variant of [WebClient.ResponseSpec.bodyToMono].
|
||||
*
|
||||
* @author Valentin Shakhov
|
||||
*/
|
||||
suspend inline fun <reified T : Any> WebClient.ResponseSpec.awaitBodyOrNull() : T? =
|
||||
when (T::class) {
|
||||
Unit::class -> awaitBodilessEntity().let { Unit as T? }
|
||||
else -> bodyToMono<T>().awaitSingleOrNull()
|
||||
}
|
||||
|
||||
/**
|
||||
* Coroutines variant of [WebClient.ResponseSpec.toBodilessEntity].
|
||||
*/
|
||||
|
||||
@@ -136,6 +136,25 @@ class WebClientExtensionsTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun awaitBodyOrNull() {
|
||||
val spec = mockk<WebClient.ResponseSpec>()
|
||||
every { spec.bodyToMono<String>() } returns Mono.just("foo")
|
||||
runBlocking {
|
||||
assertThat(spec.awaitBodyOrNull<String>()).isEqualTo("foo")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `awaitBodyOrNull of type Unit`() {
|
||||
val spec = mockk<WebClient.ResponseSpec>()
|
||||
val entity = mockk<ResponseEntity<Void>>()
|
||||
every { spec.toBodilessEntity() } returns Mono.just(entity)
|
||||
runBlocking {
|
||||
assertThat(spec.awaitBodyOrNull<Unit>()).isEqualTo(Unit)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun awaitBodilessEntity() {
|
||||
val spec = mockk<WebClient.ResponseSpec>()
|
||||
|
||||
Reference in New Issue
Block a user