|
39 | 39 | import org.springframework.http.ResponseEntity;
|
40 | 40 | import org.springframework.http.client.ClientHttpRequest;
|
41 | 41 | import org.springframework.http.client.ClientHttpRequestFactory;
|
| 42 | +import org.springframework.http.client.ClientHttpRequestInterceptor; |
42 | 43 | import org.springframework.http.client.ClientHttpResponse;
|
43 | 44 | import org.springframework.http.converter.GenericHttpMessageConverter;
|
44 | 45 | import org.springframework.http.converter.HttpMessageConverter;
|
45 | 46 | import org.springframework.util.StreamUtils;
|
46 | 47 | import org.springframework.web.util.DefaultUriBuilderFactory;
|
47 | 48 |
|
| 49 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 50 | +import static org.hamcrest.collection.IsIterableContainingInOrder.contains; |
48 | 51 | import static org.junit.Assert.*;
|
49 | 52 | import static org.mockito.BDDMockito.*;
|
50 | 53 | import static org.springframework.http.HttpMethod.POST;
|
@@ -822,4 +825,30 @@ public void exchangeParameterizedType() throws Exception {
|
822 | 825 | verify(response).close();
|
823 | 826 | }
|
824 | 827 |
|
| 828 | + @Test // SPR-15066 |
| 829 | + public void requestInterceptorCanAddExistingHeaderValue() throws Exception { |
| 830 | + ClientHttpRequestInterceptor interceptor = (request, body, execution) -> { |
| 831 | + request.getHeaders().add("MyHeader", "MyInterceptorValue"); |
| 832 | + return execution.execute(request, body); |
| 833 | + }; |
| 834 | + template.setInterceptors(Collections.singletonList(interceptor)); |
| 835 | + |
| 836 | + given(requestFactory.createRequest(new URI("http://example.com"), HttpMethod.POST)).willReturn(request); |
| 837 | + HttpHeaders requestHeaders = new HttpHeaders(); |
| 838 | + given(request.getHeaders()).willReturn(requestHeaders); |
| 839 | + given(request.execute()).willReturn(response); |
| 840 | + given(errorHandler.hasError(response)).willReturn(false); |
| 841 | + HttpStatus status = HttpStatus.OK; |
| 842 | + given(response.getStatusCode()).willReturn(status); |
| 843 | + given(response.getStatusText()).willReturn(status.getReasonPhrase()); |
| 844 | + |
| 845 | + HttpHeaders entityHeaders = new HttpHeaders(); |
| 846 | + entityHeaders.add("MyHeader", "MyEntityValue"); |
| 847 | + HttpEntity<Void> entity = new HttpEntity<>(null, entityHeaders); |
| 848 | + template.exchange("http://example.com", HttpMethod.POST, entity, Void.class); |
| 849 | + assertThat(requestHeaders.get("MyHeader"), contains("MyEntityValue", "MyInterceptorValue")); |
| 850 | + |
| 851 | + verify(response).close(); |
| 852 | + } |
| 853 | + |
825 | 854 | }
|
0 commit comments