Refine KotlinWebClientHttpServiceProxyTests

See gh-29527
This commit is contained in:
Sébastien Deleuze 2023-02-07 10:19:10 +01:00
parent 1d4bf58e8d
commit 9f061f1eec
1 changed files with 27 additions and 30 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2022 the original author or authors. * Copyright 2002-2023 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -15,7 +15,6 @@
*/ */
package org.springframework.web.reactive.function.client.support package org.springframework.web.reactive.function.client.support
import kotlinx.coroutines.reactor.mono
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import okhttp3.mockwebserver.MockResponse import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer import okhttp3.mockwebserver.MockWebServer
@ -35,13 +34,17 @@ import java.time.Duration
import java.util.function.Consumer import java.util.function.Consumer
/** /**
* Integration tests for [HTTP Service proxy][HttpServiceProxyFactory] * Kotlin integration tests for [HTTP Service proxy][HttpServiceProxyFactory]
* using [WebClient] and [MockWebServer]. * using [WebClient] and [MockWebServer].
* *
* @author DongHyeon Kim (wplong11) * @author DongHyeon Kim
* @author Sebastien Deleuze
*
*/ */
class WebClientHttpServiceProxyKotlinTests { class KotlinWebClientHttpServiceProxyTests {
private var server: MockWebServer? = null
private lateinit var server: MockWebServer
@BeforeEach @BeforeEach
fun setUp() { fun setUp() {
server = MockWebServer() server = MockWebServer()
@ -49,21 +52,21 @@ class WebClientHttpServiceProxyKotlinTests {
@AfterEach @AfterEach
fun shutdown() { fun shutdown() {
server?.shutdown() server.shutdown()
} }
@Test @Test
fun greeting() { fun greetingSuspending() {
prepareResponse { response: MockResponse -> prepareResponse { response: MockResponse ->
response.setHeader( response.setHeader(
"Content-Type", "Content-Type",
"text/plain" "text/plain"
).setBody("Hello Spring!") ).setBody("Hello Spring!")
} }
StepVerifier.create(mono<String> { initHttpService().getGreeting() }) runBlocking {
.expectNext("Hello Spring!") val greeting = initHttpService().getGreetingSuspending()
.expectComplete() Assertions.assertThat(greeting).isEqualTo("Hello Spring!")
.verify(Duration.ofSeconds(5)) }
} }
@Test @Test
@ -88,17 +91,15 @@ class WebClientHttpServiceProxyKotlinTests {
"text/plain" "text/plain"
).setBody("Hello Spring!") ).setBody("Hello Spring!")
} }
StepVerifier.create(mono<String> { initHttpService().getGreetingBlocking() }) val greeting = initHttpService().getGreetingBlocking()
.expectNext("Hello Spring!") Assertions.assertThat(greeting).isEqualTo("Hello Spring!")
.expectComplete()
.verify(Duration.ofSeconds(5))
} }
@Test @Test
fun greetingWithRequestAttribute() { fun greetingSuspendingWithRequestAttribute() {
val attributes: MutableMap<String, Any> = HashMap() val attributes: MutableMap<String, Any> = HashMap()
val webClient = WebClient.builder() val webClient = WebClient.builder()
.baseUrl(server!!.url("/").toString()) .baseUrl(server.url("/").toString())
.filter { request: ClientRequest, next: ExchangeFunction -> .filter { request: ClientRequest, next: ExchangeFunction ->
attributes.putAll(request.attributes()) attributes.putAll(request.attributes())
next.exchange(request) next.exchange(request)
@ -110,21 +111,17 @@ class WebClientHttpServiceProxyKotlinTests {
"text/plain" "text/plain"
).setBody("Hello Spring!") ).setBody("Hello Spring!")
} }
val service = initHttpService(webClient) val service = initHttpService(webClient)
val value = runBlocking { runBlocking {
service.getGreetingWithAttribute("myAttributeValue") val greeting = service.getGreetingSuspendingWithAttribute("myAttributeValue")
Assertions.assertThat(greeting).isEqualTo("Hello Spring!")
Assertions.assertThat(attributes).containsEntry("myAttribute", "myAttributeValue")
} }
StepVerifier.create(mono<String> { value })
.expectNext("Hello Spring!")
.expectComplete()
.verify(Duration.ofSeconds(5))
Assertions.assertThat(attributes).containsEntry("myAttribute", "myAttributeValue")
} }
private fun initHttpService(): TestHttpService { private fun initHttpService(): TestHttpService {
val webClient = WebClient.builder().baseUrl( val webClient = WebClient.builder().baseUrl(
server!!.url("/").toString() server.url("/").toString()
).build() ).build()
return initHttpService(webClient) return initHttpService(webClient)
} }
@ -139,12 +136,12 @@ class WebClientHttpServiceProxyKotlinTests {
private fun prepareResponse(consumer: Consumer<MockResponse>) { private fun prepareResponse(consumer: Consumer<MockResponse>) {
val response = MockResponse() val response = MockResponse()
consumer.accept(response) consumer.accept(response)
server!!.enqueue(response) server.enqueue(response)
} }
private interface TestHttpService { private interface TestHttpService {
@GetExchange("/greeting") @GetExchange("/greeting")
suspend fun getGreeting(): String suspend fun getGreetingSuspending(): String
@GetExchange("/greeting") @GetExchange("/greeting")
fun getGreetingMono(): Mono<String> fun getGreetingMono(): Mono<String>
@ -153,6 +150,6 @@ class WebClientHttpServiceProxyKotlinTests {
fun getGreetingBlocking(): String fun getGreetingBlocking(): String
@GetExchange("/greeting") @GetExchange("/greeting")
suspend fun getGreetingWithAttribute(@RequestAttribute myAttribute: String): String suspend fun getGreetingSuspendingWithAttribute(@RequestAttribute myAttribute: String): String
} }
} }