|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2024 the original author or authors. |
| 2 | + * Copyright 2002-2025 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.
|
|
52 | 52 | import org.springframework.http.converter.GenericHttpMessageConverter;
|
53 | 53 | import org.springframework.http.converter.HttpMessageConverter;
|
54 | 54 | import org.springframework.http.converter.SmartHttpMessageConverter;
|
| 55 | +import org.springframework.http.converter.StringHttpMessageConverter; |
55 | 56 | import org.springframework.http.converter.json.KotlinSerializationJsonHttpMessageConverter;
|
56 | 57 | import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
| 58 | +import org.springframework.util.FileCopyUtils; |
57 | 59 | import org.springframework.web.util.DefaultUriBuilderFactory;
|
58 | 60 |
|
| 61 | +import static java.nio.charset.StandardCharsets.UTF_8; |
59 | 62 | import static org.assertj.core.api.Assertions.assertThat;
|
60 | 63 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
61 | 64 | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
@@ -765,6 +768,46 @@ void requestInterceptorCanAddExistingHeaderValueWithBody() throws Exception {
|
765 | 768 | verify(response).close();
|
766 | 769 | }
|
767 | 770 |
|
| 771 | + @Test |
| 772 | + void requestInterceptorWithBuffering() throws Exception { |
| 773 | + try (MockWebServer server = new MockWebServer()) { |
| 774 | + server.enqueue(new MockResponse().setResponseCode(200).setBody("Hello Spring!")); |
| 775 | + server.start(); |
| 776 | + template.setRequestFactory(new SimpleClientHttpRequestFactory()); |
| 777 | + template.setInterceptors(List.of((request, body, execution) -> { |
| 778 | + ClientHttpResponse response = execution.execute(request, body); |
| 779 | + byte[] result = FileCopyUtils.copyToByteArray(response.getBody()); |
| 780 | + assertThat(result).isEqualTo("Hello Spring!".getBytes(UTF_8)); |
| 781 | + return response; |
| 782 | + })); |
| 783 | + template.setBufferingPredicate((uri, httpMethod) -> true); |
| 784 | + template.setMessageConverters(List.of(new StringHttpMessageConverter())); |
| 785 | + String result = template.getForObject(server.url("/").uri(), String.class); |
| 786 | + assertThat(server.getRequestCount()).isEqualTo(1); |
| 787 | + assertThat(result).isEqualTo("Hello Spring!"); |
| 788 | + } |
| 789 | + } |
| 790 | + |
| 791 | + @Test |
| 792 | + void buffering() throws Exception { |
| 793 | + try (MockWebServer server = new MockWebServer()) { |
| 794 | + server.enqueue(new MockResponse().setResponseCode(200).setBody("Hello Spring!")); |
| 795 | + server.start(); |
| 796 | + template.setRequestFactory(new SimpleClientHttpRequestFactory()); |
| 797 | + template.setBufferingPredicate((uri, httpMethod) -> true); |
| 798 | + template.setMessageConverters(List.of(new StringHttpMessageConverter())); |
| 799 | + String result = template.execute(server.url("/").uri(), HttpMethod.GET, req -> {}, response -> { |
| 800 | + byte[] bytes = FileCopyUtils.copyToByteArray(response.getBody()); |
| 801 | + assertThat(bytes).isEqualTo("Hello Spring!".getBytes(UTF_8)); |
| 802 | + bytes = FileCopyUtils.copyToByteArray(response.getBody()); |
| 803 | + assertThat(bytes).isEqualTo("Hello Spring!".getBytes(UTF_8)); |
| 804 | + return new String(bytes, UTF_8); |
| 805 | + }); |
| 806 | + assertThat(server.getRequestCount()).isEqualTo(1); |
| 807 | + assertThat(result).isEqualTo("Hello Spring!"); |
| 808 | + } |
| 809 | + } |
| 810 | + |
768 | 811 | @Test
|
769 | 812 | void clientHttpRequestInitializerAndRequestInterceptorAreBothApplied() throws Exception {
|
770 | 813 | ClientHttpRequestInitializer initializer = request ->
|
|
0 commit comments