Introduce WebClient.ResponseSpec.awaitBodilessEntity()
This commit also allows awaitBody<Unit>() to be used. Closes gh-26504
This commit is contained in:
parent
3718c8d0e0
commit
b00c7075a5
|
|
@ -138,7 +138,16 @@ inline fun <reified T : Any> WebClient.ResponseSpec.bodyToFlow(): Flow<T> =
|
|||
* @since 5.2
|
||||
*/
|
||||
suspend inline fun <reified T : Any> WebClient.ResponseSpec.awaitBody() : T =
|
||||
bodyToMono<T>().awaitSingle()
|
||||
when (T::class) {
|
||||
Unit::class -> awaitBodilessEntity().let { Unit as T }
|
||||
else -> bodyToMono<T>().awaitSingle()
|
||||
}
|
||||
|
||||
/**
|
||||
* Coroutines variant of [WebClient.ResponseSpec.toBodilessEntity].
|
||||
*/
|
||||
suspend fun WebClient.ResponseSpec.awaitBodilessEntity() =
|
||||
toBodilessEntity().awaitSingle()
|
||||
|
||||
/**
|
||||
* Extension for [WebClient.ResponseSpec.toEntity] providing a `toEntity<Foo>()` variant
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -27,6 +27,7 @@ import org.assertj.core.api.Assertions.assertThat
|
|||
import org.junit.jupiter.api.Test
|
||||
import org.reactivestreams.Publisher
|
||||
import org.springframework.core.ParameterizedTypeReference
|
||||
import org.springframework.http.ResponseEntity
|
||||
import reactor.core.publisher.Flux
|
||||
import reactor.core.publisher.Mono
|
||||
import java.util.concurrent.CompletableFuture
|
||||
|
|
@ -125,6 +126,26 @@ class WebClientExtensionsTests {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `awaitBody of type Unit`() {
|
||||
val spec = mockk<WebClient.ResponseSpec>()
|
||||
val entity = mockk<ResponseEntity<Void>>()
|
||||
every { spec.toBodilessEntity() } returns Mono.just(entity)
|
||||
runBlocking {
|
||||
assertThat(spec.awaitBody<Unit>()).isEqualTo(Unit)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun awaitBodilessEntity() {
|
||||
val spec = mockk<WebClient.ResponseSpec>()
|
||||
val entity = mockk<ResponseEntity<Void>>()
|
||||
every { spec.toBodilessEntity() } returns Mono.just(entity)
|
||||
runBlocking {
|
||||
assertThat(spec.awaitBodilessEntity()).isEqualTo(entity)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ResponseSpec#toEntity with reified type parameters`() {
|
||||
responseSpec.toEntity<List<Foo>>()
|
||||
|
|
|
|||
Loading…
Reference in New Issue