Add a requiredBody() extension to RestClient.ResponseSpec
Closes gh-32703
This commit is contained in:
parent
3002813598
commit
a662f5e719
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2023 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.
|
||||
|
|
@ -42,6 +42,14 @@ inline fun <reified T : Any> RestClient.RequestBodySpec.bodyWithType(body: T): R
|
|||
inline fun <reified T : Any> RestClient.ResponseSpec.body(): T? =
|
||||
body(object : ParameterizedTypeReference<T>() {})
|
||||
|
||||
/**
|
||||
* Extension for [RestClient.ResponseSpec.body] providing a `requiredBody<Foo>()` variant with a non-nullable
|
||||
* return value.
|
||||
* @throws NoSuchElementException if there is no response body
|
||||
* @since 6.2
|
||||
*/
|
||||
inline fun <reified T : Any> RestClient.ResponseSpec.requiredBody(): T =
|
||||
body(object : ParameterizedTypeReference<T>() {}) ?: throw NoSuchElementException("Response body is required")
|
||||
|
||||
/**
|
||||
* Extension for [RestClient.ResponseSpec.toEntity] providing a `toEntity<Foo>()` variant
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2023 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.
|
||||
|
|
@ -16,9 +16,11 @@
|
|||
|
||||
package org.springframework.web.client
|
||||
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.verify
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.assertThrows
|
||||
import org.springframework.core.ParameterizedTypeReference
|
||||
|
||||
/**
|
||||
|
|
@ -45,6 +47,18 @@ class RestClientExtensionsTests {
|
|||
verify { responseSpec.body(object : ParameterizedTypeReference<List<Foo>>() {}) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ResponseSpec#requiredBody with reified type parameters`() {
|
||||
responseSpec.requiredBody<List<Foo>>()
|
||||
verify { responseSpec.body(object : ParameterizedTypeReference<List<Foo>>() {}) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ResponseSpec#requiredBody with null response throws NoSuchElementException`() {
|
||||
every { responseSpec.body(any<ParameterizedTypeReference<Foo>>()) } returns null
|
||||
assertThrows<NoSuchElementException> { responseSpec.requiredBody<Foo>() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ResponseSpec#toEntity with reified type parameters`() {
|
||||
responseSpec.toEntity<List<Foo>>()
|
||||
|
|
|
|||
Loading…
Reference in New Issue