Skip to content

Commit

Permalink
Introduce WebClient.ResponseSpec.awaitBodilessEntity()
Browse files Browse the repository at this point in the history
This commit also allows awaitBody<Unit>() to be used.

Closes gh-26504
  • Loading branch information
gaerfield authored and sdeleuze committed Feb 15, 2021
1 parent 3718c8d commit b00c707
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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>>()
Expand Down

0 comments on commit b00c707

Please sign in to comment.