mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-05-03 04:19:47 +01:00
Merge branch '6.2.x'
This commit is contained in:
@@ -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");
|
* 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.
|
||||||
@@ -18,6 +18,8 @@ package org.springframework.web.client
|
|||||||
|
|
||||||
import org.springframework.core.ParameterizedTypeReference
|
import org.springframework.core.ParameterizedTypeReference
|
||||||
import org.springframework.http.ResponseEntity
|
import org.springframework.http.ResponseEntity
|
||||||
|
import org.springframework.web.client.RestClient.RequestHeadersSpec
|
||||||
|
import org.springframework.web.client.RestClient.RequestHeadersSpec.ExchangeFunction
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extension for [RestClient.RequestBodySpec.body] providing a `bodyWithType<Foo>(...)` variant
|
* Extension for [RestClient.RequestBodySpec.body] providing a `bodyWithType<Foo>(...)` variant
|
||||||
@@ -51,6 +53,15 @@ inline fun <reified T : Any> RestClient.ResponseSpec.body(): T? =
|
|||||||
inline fun <reified T : Any> RestClient.ResponseSpec.requiredBody(): T =
|
inline fun <reified T : Any> RestClient.ResponseSpec.requiredBody(): T =
|
||||||
body(object : ParameterizedTypeReference<T>() {}) ?: throw NoSuchElementException("Response body is required")
|
body(object : ParameterizedTypeReference<T>() {}) ?: throw NoSuchElementException("Response body is required")
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extension for [RestClient.RequestHeadersSpec.exchange] providing a `requiredExchange(...)` variant with a
|
||||||
|
* non-nullable return value.
|
||||||
|
* @throws NoSuchElementException if there is no response value
|
||||||
|
* @since 6.2.6
|
||||||
|
*/
|
||||||
|
fun <T: Any> RequestHeadersSpec<*>.requiredExchange(exchangeFunction: ExchangeFunction<T>, close: Boolean = true): T =
|
||||||
|
exchange(exchangeFunction, close) ?: throw NoSuchElementException("Response value is required")
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extension for [RestClient.ResponseSpec.toEntity] providing a `toEntity<Foo>()` variant
|
* Extension for [RestClient.ResponseSpec.toEntity] providing a `toEntity<Foo>()` variant
|
||||||
* leveraging Kotlin reified type parameters. This extension is not subject to type
|
* leveraging Kotlin reified type parameters. This extension is not subject to type
|
||||||
@@ -61,3 +72,4 @@ inline fun <reified T : Any> RestClient.ResponseSpec.requiredBody(): T =
|
|||||||
*/
|
*/
|
||||||
inline fun <reified T : Any> RestClient.ResponseSpec.toEntity(): ResponseEntity<T> =
|
inline fun <reified T : Any> RestClient.ResponseSpec.toEntity(): ResponseEntity<T> =
|
||||||
toEntity(object : ParameterizedTypeReference<T>() {})
|
toEntity(object : ParameterizedTypeReference<T>() {})
|
||||||
|
|
||||||
|
|||||||
+22
-1
@@ -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");
|
* 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.
|
||||||
@@ -19,9 +19,12 @@ package org.springframework.web.client
|
|||||||
import io.mockk.every
|
import io.mockk.every
|
||||||
import io.mockk.mockk
|
import io.mockk.mockk
|
||||||
import io.mockk.verify
|
import io.mockk.verify
|
||||||
|
import org.assertj.core.api.Assertions.assertThat
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import org.junit.jupiter.api.assertThrows
|
import org.junit.jupiter.api.assertThrows
|
||||||
import org.springframework.core.ParameterizedTypeReference
|
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
|
* Mock object based tests for [RestClient] Kotlin extensions
|
||||||
@@ -59,6 +62,24 @@ class RestClientExtensionsTests {
|
|||||||
assertThrows<NoSuchElementException> { responseSpec.requiredBody<Foo>() }
|
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? =
|
||||||
|
{ request, response -> 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? =
|
||||||
|
{ request, response -> null }
|
||||||
|
assertThrows<NoSuchElementException> { requestBodySpec.requiredExchange(exchangeFunction) }
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `ResponseSpec#toEntity with reified type parameters`() {
|
fun `ResponseSpec#toEntity with reified type parameters`() {
|
||||||
responseSpec.toEntity<List<Foo>>()
|
responseSpec.toEntity<List<Foo>>()
|
||||||
|
|||||||
Reference in New Issue
Block a user