Provide WebClient#exchange() alternative for Coroutines

This commit adds awaitExchange { } and
exchangeToFlow { } extensions as Coroutines variants for
exchangeToMono() and exchangeToFlux().

Closes gh-25751
This commit is contained in:
Sébastien Deleuze
2020-10-07 10:58:57 +02:00
parent e899397438
commit 7d7ed88739
3 changed files with 70 additions and 5 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -20,13 +20,17 @@ import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.runBlocking
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.reactivestreams.Publisher
import org.springframework.core.ParameterizedTypeReference
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import java.util.concurrent.CompletableFuture
import java.util.function.Function
/**
* Mock object based tests for [WebClient] Kotlin extensions
@@ -89,6 +93,29 @@ class WebClientExtensionsTests {
}
}
@Test
fun `awaitExchange with function parameter`() {
val foo = mockk<Foo>()
every { requestBodySpec.exchangeToMono(any<Function<ClientResponse, Mono<Foo>>>()) } returns Mono.just(foo)
runBlocking {
assertThat(requestBodySpec.awaitExchange { foo }).isEqualTo(foo)
}
}
@Test
fun exchangeToFlow() {
val foo = mockk<Foo>()
every { requestBodySpec.exchangeToFlux(any<Function<ClientResponse, Flux<Foo>>>()) } returns Flux.just(foo, foo)
runBlocking {
assertThat(requestBodySpec.exchangeToFlow {
flow {
emit(foo)
emit(foo)
}
}.toList()).isEqualTo(listOf(foo, foo))
}
}
@Test
fun awaitBody() {
val spec = mockk<WebClient.ResponseSpec>()