|
17 | 17 | package org.springframework.web.client; |
18 | 18 |
|
19 | 19 | import java.io.ByteArrayInputStream; |
| 20 | +import java.io.EOFException; |
20 | 21 | import java.io.InputStream; |
| 22 | +import java.util.stream.Stream; |
21 | 23 |
|
22 | 24 | import org.junit.jupiter.api.Test; |
| 25 | +import org.junit.jupiter.params.ParameterizedTest; |
| 26 | +import org.junit.jupiter.params.provider.MethodSource; |
23 | 27 |
|
| 28 | +import org.springframework.http.HttpStatus; |
| 29 | +import org.springframework.http.HttpStatusCode; |
24 | 30 | import org.springframework.http.client.ClientHttpResponse; |
| 31 | +import org.springframework.web.testfixture.http.client.MockClientHttpResponse; |
25 | 32 |
|
26 | 33 | import static org.assertj.core.api.Assertions.assertThat; |
27 | 34 | import static org.mockito.BDDMockito.given; |
|
30 | 37 | /** |
31 | 38 | * Tests for {@link IntrospectingClientHttpResponse}. |
32 | 39 | * |
33 | | - * @since 5.3.10 |
34 | | - * @author Yin-Jui Liao |
| 40 | + * @author Brian Clozel |
35 | 41 | */ |
36 | 42 | class IntrospectingClientHttpResponseTests { |
37 | 43 |
|
38 | | - private final ClientHttpResponse response = mock(); |
39 | 44 |
|
40 | | - private final IntrospectingClientHttpResponse wrappedResponse = new IntrospectingClientHttpResponse(response); |
| 45 | + @ParameterizedTest |
| 46 | + @MethodSource("noBodyHttpStatus") |
| 47 | + void noMessageBodyWhenStatus(HttpStatus status) throws Exception { |
| 48 | + var response = new MockClientHttpResponse(new byte[0], status); |
| 49 | + var wrapped = new IntrospectingClientHttpResponse(response); |
41 | 50 |
|
| 51 | + assertThat(wrapped.hasMessageBody()).isFalse(); |
| 52 | + } |
| 53 | + |
| 54 | + static Stream<HttpStatusCode> noBodyHttpStatus() { |
| 55 | + return Stream.of(HttpStatus.NO_CONTENT, HttpStatus.EARLY_HINTS, HttpStatus.NOT_MODIFIED); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void noMessageBodyWhenContentLength0() throws Exception { |
| 60 | + var response = new MockClientHttpResponse(new byte[0], HttpStatus.OK); |
| 61 | + response.getHeaders().setContentLength(0); |
| 62 | + var wrapped = new IntrospectingClientHttpResponse(response); |
| 63 | + |
| 64 | + assertThat(wrapped.hasMessageBody()).isFalse(); |
| 65 | + } |
42 | 66 |
|
43 | 67 | @Test |
44 | | - void messageBodyDoesNotExist() throws Exception { |
45 | | - given(response.getBody()).willReturn(null); |
46 | | - assertThat(wrappedResponse.hasEmptyMessageBody()).isTrue(); |
| 68 | + void emptyMessageWhenNullInputStream() throws Exception { |
| 69 | + ClientHttpResponse mockResponse = mock(); |
| 70 | + given(mockResponse.getBody()).willReturn(null); |
| 71 | + var wrappedMock = new IntrospectingClientHttpResponse(mockResponse); |
| 72 | + assertThat(wrappedMock.hasEmptyMessageBody()).isTrue(); |
47 | 73 | } |
48 | 74 |
|
49 | 75 | @Test |
50 | 76 | void messageBodyExists() throws Exception { |
51 | | - InputStream stream = new ByteArrayInputStream("content".getBytes()); |
52 | | - given(response.getBody()).willReturn(stream); |
53 | | - assertThat(wrappedResponse.hasEmptyMessageBody()).isFalse(); |
| 77 | + var stream = new ByteArrayInputStream("content".getBytes()); |
| 78 | + var response = new MockClientHttpResponse(stream, HttpStatus.OK); |
| 79 | + var wrapped = new IntrospectingClientHttpResponse(response); |
| 80 | + assertThat(wrapped.hasEmptyMessageBody()).isFalse(); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void emptyMessageWhenEOFException() throws Exception { |
| 85 | + ClientHttpResponse mockResponse = mock(); |
| 86 | + InputStream stream = mock(); |
| 87 | + given(mockResponse.getBody()).willReturn(stream); |
| 88 | + given(stream.read()).willThrow(new EOFException()); |
| 89 | + var wrappedMock = new IntrospectingClientHttpResponse(mockResponse); |
| 90 | + assertThat(wrappedMock.hasEmptyMessageBody()).isTrue(); |
54 | 91 | } |
55 | 92 |
|
56 | 93 | } |
0 commit comments