Browse Source

Introduce WebClient.ResponseSpec.awaitBodilessEntity()

This commit also allows awaitBody<Unit>() to be used.

Closes gh-26504
pull/26556/head
gaerfield 5 years ago committed by Sébastien Deleuze
parent
commit
b00c7075a5
  1. 11
      spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/client/WebClientExtensions.kt
  2. 23
      spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/client/WebClientExtensionsTests.kt

11
spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/client/WebClientExtensions.kt

@ -138,7 +138,16 @@ inline fun <reified T : Any> WebClient.ResponseSpec.bodyToFlow(): Flow<T> = @@ -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

23
spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/client/WebClientExtensionsTests.kt

@ -1,5 +1,5 @@ @@ -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 @@ -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 { @@ -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…
Cancel
Save