|
| 1 | +/* |
| 2 | + * Copyright 2002-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.web.client; |
| 18 | + |
| 19 | +import java.io.ByteArrayInputStream; |
| 20 | +import java.io.IOException; |
| 21 | +import java.net.URI; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import org.springframework.core.ParameterizedTypeReference; |
| 27 | +import org.springframework.http.HttpHeaders; |
| 28 | +import org.springframework.http.HttpMethod; |
| 29 | +import org.springframework.http.HttpStatus; |
| 30 | +import org.springframework.http.MediaType; |
| 31 | +import org.springframework.http.client.ClientHttpRequest; |
| 32 | +import org.springframework.http.client.ClientHttpRequestFactory; |
| 33 | +import org.springframework.http.client.ClientHttpResponse; |
| 34 | + |
| 35 | +import static org.assertj.core.api.Assertions.assertThat; |
| 36 | +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; |
| 37 | +import static org.mockito.BDDMockito.given; |
| 38 | +import static org.mockito.Mockito.mock; |
| 39 | + |
| 40 | +/** |
| 41 | + * Tests for {@link DefaultRestClient}. |
| 42 | + * |
| 43 | + * @author Sebastien Deleuze |
| 44 | + */ |
| 45 | +class DefaultRestClientTests { |
| 46 | + |
| 47 | + private final ClientHttpRequestFactory requestFactory = mock(); |
| 48 | + |
| 49 | + private final ClientHttpRequest request = mock(); |
| 50 | + |
| 51 | + private final ClientHttpResponse response = mock(); |
| 52 | + |
| 53 | + private RestClient client; |
| 54 | + |
| 55 | + |
| 56 | + @BeforeEach |
| 57 | + void setup() { |
| 58 | + this.client = RestClient.builder() |
| 59 | + .requestFactory(this.requestFactory) |
| 60 | + .build(); |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + @Test |
| 65 | + void requiredBodyWithClass() throws IOException { |
| 66 | + mockSentRequest(HttpMethod.GET, "https://example.org"); |
| 67 | + mockResponseStatus(HttpStatus.OK); |
| 68 | + mockResponseBody("Hello World", MediaType.TEXT_PLAIN); |
| 69 | + |
| 70 | + String result = this.client.get() |
| 71 | + .uri("https://example.org") |
| 72 | + .retrieve() |
| 73 | + .requiredBody(String.class); |
| 74 | + |
| 75 | + assertThat(result).isEqualTo("Hello World"); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void requiredBodyWithClassAndNullBody() throws IOException { |
| 80 | + mockSentRequest(HttpMethod.GET, "https://example.org"); |
| 81 | + mockResponseStatus(HttpStatus.OK); |
| 82 | + mockEmptyResponseBody(); |
| 83 | + |
| 84 | + assertThatIllegalStateException().isThrownBy(() -> |
| 85 | + this.client.get() |
| 86 | + .uri("https://example.org") |
| 87 | + .retrieve() |
| 88 | + .requiredBody(String.class) |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void requiredBodyWithParameterizedTypeReference() throws IOException { |
| 94 | + mockSentRequest(HttpMethod.GET, "https://example.org"); |
| 95 | + mockResponseStatus(HttpStatus.OK); |
| 96 | + mockResponseBody("Hello World", MediaType.TEXT_PLAIN); |
| 97 | + |
| 98 | + String result = this.client.get() |
| 99 | + .uri("https://example.org") |
| 100 | + .retrieve() |
| 101 | + .requiredBody(new ParameterizedTypeReference<>() {}); |
| 102 | + |
| 103 | + assertThat(result).isEqualTo("Hello World"); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void requiredBodyWithParameterizedTypeReferenceAndNullBody() throws IOException { |
| 108 | + mockSentRequest(HttpMethod.GET, "https://example.org"); |
| 109 | + mockResponseStatus(HttpStatus.OK); |
| 110 | + mockEmptyResponseBody(); |
| 111 | + |
| 112 | + assertThatIllegalStateException().isThrownBy(() -> |
| 113 | + this.client.get() |
| 114 | + .uri("https://example.org") |
| 115 | + .retrieve() |
| 116 | + .requiredBody(new ParameterizedTypeReference<String>() {}) |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + private void mockSentRequest(HttpMethod method, String uri) throws IOException { |
| 122 | + given(this.requestFactory.createRequest(URI.create(uri), method)).willReturn(this.request); |
| 123 | + given(this.request.getHeaders()).willReturn(new HttpHeaders()); |
| 124 | + given(this.request.getMethod()).willReturn(method); |
| 125 | + given(this.request.getURI()).willReturn(URI.create(uri)); |
| 126 | + } |
| 127 | + |
| 128 | + private void mockResponseStatus(HttpStatus responseStatus) throws IOException { |
| 129 | + given(this.request.execute()).willReturn(this.response); |
| 130 | + given(this.response.getStatusCode()).willReturn(responseStatus); |
| 131 | + given(this.response.getStatusText()).willReturn(responseStatus.getReasonPhrase()); |
| 132 | + } |
| 133 | + |
| 134 | + private void mockResponseBody(String expectedBody, MediaType mediaType) throws IOException { |
| 135 | + HttpHeaders responseHeaders = new HttpHeaders(); |
| 136 | + responseHeaders.setContentType(mediaType); |
| 137 | + responseHeaders.setContentLength(expectedBody.length()); |
| 138 | + given(this.response.getHeaders()).willReturn(responseHeaders); |
| 139 | + given(this.response.getBody()).willReturn(new ByteArrayInputStream(expectedBody.getBytes())); |
| 140 | + } |
| 141 | + |
| 142 | + private void mockEmptyResponseBody() throws IOException { |
| 143 | + HttpHeaders responseHeaders = new HttpHeaders(); |
| 144 | + responseHeaders.setContentLength(0); |
| 145 | + given(this.response.getHeaders()).willReturn(responseHeaders); |
| 146 | + given(this.response.getBody()).willReturn(new ByteArrayInputStream(new byte[0])); |
| 147 | + } |
| 148 | + |
| 149 | +} |
0 commit comments