Merge branch '6.2.x'

This commit is contained in:
Sébastien Deleuze
2025-04-02 17:10:56 +02:00
5 changed files with 128 additions and 45 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 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.
@@ -59,6 +59,7 @@ import org.springframework.web.testfixture.xml.Pojo;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
import static org.junit.jupiter.params.provider.Arguments.argumentSet;
@@ -765,6 +766,39 @@ class RestClientIntegrationTests {
expectRequest(request -> assertThat(request.getPath()).isEqualTo("/greeting"));
}
@ParameterizedRestClientTest
void exchangeForRequiredValue(ClientHttpRequestFactory requestFactory) {
startServer(requestFactory);
prepareResponse(response -> response.setBody("Hello Spring!"));
String result = this.restClient.get()
.uri("/greeting")
.header("X-Test-Header", "testvalue")
.exchangeForRequiredValue((request, response) -> new String(RestClientUtils.getBody(response), UTF_8));
assertThat(result).isEqualTo("Hello Spring!");
expectRequestCount(1);
expectRequest(request -> {
assertThat(request.getHeader("X-Test-Header")).isEqualTo("testvalue");
assertThat(request.getPath()).isEqualTo("/greeting");
});
}
@ParameterizedRestClientTest
@SuppressWarnings("DataFlowIssue")
void exchangeForNullRequiredValue(ClientHttpRequestFactory requestFactory) {
startServer(requestFactory);
prepareResponse(response -> response.setBody("Hello Spring!"));
assertThatIllegalStateException().isThrownBy(() -> this.restClient.get()
.uri("/greeting")
.header("X-Test-Header", "testvalue")
.exchangeForRequiredValue((request, response) -> null));
}
@ParameterizedRestClientTest
void requestInitializer(ClientHttpRequestFactory requestFactory) {
startServer(requestFactory);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2025 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.
@@ -19,12 +19,9 @@ package org.springframework.web.client
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.springframework.core.ParameterizedTypeReference
import org.springframework.http.HttpRequest
import org.springframework.web.client.RestClient.RequestHeadersSpec
/**
* Mock object based tests for [RestClient] Kotlin extensions
@@ -62,24 +59,6 @@ class RestClientExtensionsTests {
assertThrows<NoSuchElementException> { responseSpec.requiredBody<Foo>() }
}
@Test
fun `RequestHeadersSpec#requiredExchange`() {
val foo = Foo()
every { requestBodySpec.exchange(any<RequestHeadersSpec.ExchangeFunction<Foo>>(), any()) } returns foo
val exchangeFunction: (HttpRequest, RequestHeadersSpec.ConvertibleClientHttpResponse) -> Foo? =
{ _, _ -> foo }
val value = requestBodySpec.requiredExchange(exchangeFunction)
assertThat(value).isEqualTo(foo)
}
@Test
fun `RequestHeadersSpec#requiredExchange with null response throws NoSuchElementException`() {
every { requestBodySpec.exchange(any<RequestHeadersSpec.ExchangeFunction<Foo>>(), any()) } returns null
val exchangeFunction: (HttpRequest, RequestHeadersSpec.ConvertibleClientHttpResponse) -> Foo? =
{ _, _ -> null }
assertThrows<NoSuchElementException> { requestBodySpec.requiredExchange(exchangeFunction) }
}
@Test
fun `ResponseSpec#toEntity with reified type parameters`() {
responseSpec.toEntity<List<Foo>>()