Skip to content

Commit

Permalink
Add coroutine context tests for WebClientExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
sdeleuze committed Sep 25, 2024
1 parent 478aa25 commit 2ab0101
Showing 1 changed file with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2024 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 All @@ -18,7 +18,9 @@ package org.springframework.web.reactive.function.client

import io.mockk.every
import io.mockk.mockk
import io.mockk.slot
import io.mockk.verify
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.toList
Expand All @@ -32,6 +34,8 @@ import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import java.util.concurrent.CompletableFuture
import java.util.function.Function
import kotlin.coroutines.AbstractCoroutineContextElement
import kotlin.coroutines.CoroutineContext

/**
* Mock object based tests for [WebClient] Kotlin extensions
Expand Down Expand Up @@ -103,6 +107,18 @@ class WebClientExtensionsTests {
}
}

@Test
fun `awaitExchange with coroutines context`() {
val foo = mockk<Foo>()
val slot = slot<Function<ClientResponse, Mono<Foo>>>()
every { requestBodySpec.exchangeToMono(capture(slot)) } answers {
slot.captured.apply(mockk<ClientResponse>())
}
runBlocking(FooContextElement(foo)) {
assertThat(requestBodySpec.awaitExchange { currentCoroutineContext()[FooContextElement]!!.foo }).isEqualTo(foo)
}
}

@Test
fun `awaitExchangeOrNull returning null`() {
val foo = mockk<Foo>()
Expand All @@ -121,6 +137,18 @@ class WebClientExtensionsTests {
}
}

@Test
fun `awaitExchangeOrNull with coroutines context`() {
val foo = mockk<Foo>()
val slot = slot<Function<ClientResponse, Mono<Foo>>>()
every { requestBodySpec.exchangeToMono(capture(slot)) } answers {
slot.captured.apply(mockk<ClientResponse>())
}
runBlocking(FooContextElement(foo)) {
assertThat(requestBodySpec.awaitExchangeOrNull { currentCoroutineContext()[FooContextElement]!!.foo }).isEqualTo(foo)
}
}

@Test
fun exchangeToFlow() {
val foo = mockk<Foo>()
Expand Down Expand Up @@ -202,4 +230,8 @@ class WebClientExtensionsTests {
}

class Foo

private data class FooContextElement(val foo: Foo) : AbstractCoroutineContextElement(FooContextElement) {
companion object Key : CoroutineContext.Key<FooContextElement>
}
}

0 comments on commit 2ab0101

Please sign in to comment.