|
1 | 1 | /* |
2 | | - * Copyright 2002-2022 the original author or authors. |
| 2 | + * Copyright 2002-2024 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
16 | 16 |
|
17 | 17 | package org.springframework.http.client; |
18 | 18 |
|
| 19 | +import java.io.InputStreamReader; |
19 | 20 | import java.net.URI; |
| 21 | +import java.util.stream.Stream; |
20 | 22 |
|
21 | 23 | import org.apache.hc.client5.http.classic.HttpClient; |
22 | 24 | import org.apache.hc.client5.http.config.Configurable; |
|
26 | 28 | import org.apache.hc.client5.http.protocol.HttpClientContext; |
27 | 29 | import org.apache.hc.core5.util.Timeout; |
28 | 30 | import org.junit.jupiter.api.Test; |
| 31 | +import org.junit.jupiter.params.ParameterizedTest; |
| 32 | +import org.junit.jupiter.params.provider.MethodSource; |
29 | 33 |
|
30 | 34 | import org.springframework.http.HttpMethod; |
| 35 | +import org.springframework.http.HttpStatus; |
| 36 | +import org.springframework.util.FileCopyUtils; |
31 | 37 |
|
32 | 38 | import static java.util.concurrent.TimeUnit.MILLISECONDS; |
33 | 39 | import static org.assertj.core.api.Assertions.assertThat; |
|
37 | 43 |
|
38 | 44 | /** |
39 | 45 | * @author Stephane Nicoll |
| 46 | + * @author Brian Clozel |
40 | 47 | */ |
41 | 48 | class HttpComponentsClientHttpRequestFactoryTests extends AbstractHttpRequestFactoryTests { |
42 | 49 |
|
@@ -145,6 +152,36 @@ public HttpClient getHttpClient() { |
145 | 152 | assertThat(requestConfig2.getConnectionRequestTimeout()).isEqualTo(Timeout.of(7000, MILLISECONDS)); |
146 | 153 | } |
147 | 154 |
|
| 155 | + @ParameterizedTest |
| 156 | + @MethodSource("unsafeHttpMethods") |
| 157 | + void shouldSetContentLengthWhenEmptyBody(HttpMethod method) throws Exception { |
| 158 | + ClientHttpRequest request = factory.createRequest(URI.create(baseUrl + "/header/Content-Length"), method); |
| 159 | + try (ClientHttpResponse response = request.execute()) { |
| 160 | + assertThat(response.getStatusCode()).as("Invalid status code").isEqualTo(HttpStatus.OK); |
| 161 | + String result = FileCopyUtils.copyToString(new InputStreamReader(response.getBody())); |
| 162 | + assertThat(result).as("Invalid body").isEqualTo("Content-Length:0"); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + static Stream<HttpMethod> unsafeHttpMethods() { |
| 167 | + return Stream.of(HttpMethod.POST, HttpMethod.PUT, HttpMethod.DELETE, HttpMethod.PATCH); |
| 168 | + } |
| 169 | + |
| 170 | + @ParameterizedTest |
| 171 | + @MethodSource("safeHttpMethods") |
| 172 | + void shouldNotSetContentLengthWhenEmptyBodyAndSafeMethod(HttpMethod method) throws Exception { |
| 173 | + ClientHttpRequest request = factory.createRequest(URI.create(baseUrl + "/header/Content-Length"), method); |
| 174 | + try (ClientHttpResponse response = request.execute()) { |
| 175 | + assertThat(response.getStatusCode()).as("Invalid status code").isEqualTo(HttpStatus.OK); |
| 176 | + String result = FileCopyUtils.copyToString(new InputStreamReader(response.getBody())); |
| 177 | + assertThat(result).as("Invalid body").isEqualTo("Content-Length:null"); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + static Stream<HttpMethod> safeHttpMethods() { |
| 182 | + return Stream.of(HttpMethod.GET, HttpMethod.OPTIONS, HttpMethod.TRACE); |
| 183 | + } |
| 184 | + |
148 | 185 | private RequestConfig retrieveRequestConfig(HttpComponentsClientHttpRequestFactory factory) throws Exception { |
149 | 186 | URI uri = URI.create(baseUrl + "/status/ok"); |
150 | 187 | HttpComponentsClientHttpRequest request = (HttpComponentsClientHttpRequest) |
|
0 commit comments