|
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.web.util.DefaultUriTemplateHandler;
|
46 | 47 |
|
47 |
| -import static org.junit.Assert.assertEquals; |
48 |
| -import static org.junit.Assert.assertFalse; |
49 |
| -import static org.junit.Assert.assertNull; |
50 |
| -import static org.junit.Assert.assertSame; |
51 |
| -import static org.junit.Assert.fail; |
52 |
| -import static org.mockito.BDDMockito.any; |
53 |
| -import static org.mockito.BDDMockito.eq; |
54 |
| -import static org.mockito.BDDMockito.given; |
55 |
| -import static org.mockito.BDDMockito.mock; |
56 |
| -import static org.mockito.BDDMockito.verify; |
57 |
| -import static org.mockito.BDDMockito.willThrow; |
58 |
| -import static org.springframework.http.MediaType.parseMediaType; |
| 48 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 49 | +import static org.hamcrest.collection.IsIterableContainingInOrder.contains; |
| 50 | +import static org.junit.Assert.*; |
| 51 | +import static org.mockito.BDDMockito.*; |
| 52 | +import static org.springframework.http.HttpMethod.POST; |
| 53 | +import static org.springframework.http.MediaType.*; |
59 | 54 |
|
60 | 55 | /**
|
61 | 56 | * @author Arjen Poutsma
|
@@ -840,4 +835,30 @@ public void exchangeParameterizedType() throws Exception {
|
840 | 835 | verify(response).close();
|
841 | 836 | }
|
842 | 837 |
|
| 838 | + @Test // SPR-15066 |
| 839 | + public void requestInterceptorCanAddExistingHeaderValue() throws Exception { |
| 840 | + ClientHttpRequestInterceptor interceptor = (request, body, execution) -> { |
| 841 | + request.getHeaders().add("MyHeader", "MyInterceptorValue"); |
| 842 | + return execution.execute(request, body); |
| 843 | + }; |
| 844 | + template.setInterceptors(Collections.singletonList(interceptor)); |
| 845 | + |
| 846 | + given(requestFactory.createRequest(new URI("http://example.com"), HttpMethod.POST)).willReturn(request); |
| 847 | + HttpHeaders requestHeaders = new HttpHeaders(); |
| 848 | + given(request.getHeaders()).willReturn(requestHeaders); |
| 849 | + given(request.execute()).willReturn(response); |
| 850 | + given(errorHandler.hasError(response)).willReturn(false); |
| 851 | + HttpStatus status = HttpStatus.OK; |
| 852 | + given(response.getStatusCode()).willReturn(status); |
| 853 | + given(response.getStatusText()).willReturn(status.getReasonPhrase()); |
| 854 | + |
| 855 | + HttpHeaders entityHeaders = new HttpHeaders(); |
| 856 | + entityHeaders.add("MyHeader", "MyEntityValue"); |
| 857 | + HttpEntity<Void> entity = new HttpEntity<>(null, entityHeaders); |
| 858 | + template.exchange("http://example.com", HttpMethod.POST, entity, Void.class); |
| 859 | + assertThat(requestHeaders.get("MyHeader"), contains("MyEntityValue", "MyInterceptorValue")); |
| 860 | + |
| 861 | + verify(response).close(); |
| 862 | + } |
| 863 | + |
843 | 864 | }
|
0 commit comments