From b065e04be74e045a37f415d56a246df3742b8e7c Mon Sep 17 00:00:00 2001 From: Chinmoy Chakraborty Date: Sat, 1 Apr 2023 22:29:57 +0530 Subject: [PATCH 1/2] Add content buffering support to MockServerRestTemplateCustomizer See gh-34833 --- .../MockServerRestTemplateCustomizer.java | 19 ++++++++++++++++++- ...MockServerRestTemplateCustomizerTests.java | 19 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/MockServerRestTemplateCustomizer.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/MockServerRestTemplateCustomizer.java index 45f95ee538..a7bb97a44c 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/MockServerRestTemplateCustomizer.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/MockServerRestTemplateCustomizer.java @@ -24,7 +24,9 @@ import java.util.function.Supplier; import org.springframework.beans.BeanUtils; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.boot.web.client.RestTemplateCustomizer; +import org.springframework.http.client.BufferingClientHttpRequestFactory; import org.springframework.test.web.client.MockRestServiceServer; +import org.springframework.test.web.client.MockRestServiceServer.MockRestServiceServerBuilder; import org.springframework.test.web.client.RequestExpectationManager; import org.springframework.test.web.client.SimpleRequestExpectationManager; import org.springframework.util.Assert; @@ -49,6 +51,7 @@ import org.springframework.web.client.RestTemplate; * * @author Phillip Webb * @author Moritz Halbritter + * @author Chinmoy Chakraborty * @since 1.4.0 * @see #getServer() * @see #getServer(RestTemplate) @@ -63,6 +66,8 @@ public class MockServerRestTemplateCustomizer implements RestTemplateCustomizer private boolean detectRootUri = true; + private boolean bufferContent = false; + public MockServerRestTemplateCustomizer() { this(SimpleRequestExpectationManager::new); } @@ -96,13 +101,25 @@ public class MockServerRestTemplateCustomizer implements RestTemplateCustomizer this.detectRootUri = detectRootUri; } + /** + * Use the {@link BufferingClientHttpRequestFactory} wrapper to buffer the input and + * output streams, and for example, allow multiple reads of the response body. + */ + public void bufferContent(boolean bufferContent) { + this.bufferContent = bufferContent; + } + @Override public void customize(RestTemplate restTemplate) { RequestExpectationManager expectationManager = createExpectationManager(); if (this.detectRootUri) { expectationManager = RootUriRequestExpectationManager.forRestTemplate(restTemplate, expectationManager); } - MockRestServiceServer server = MockRestServiceServer.bindTo(restTemplate).build(expectationManager); + MockRestServiceServerBuilder serverBuilder = MockRestServiceServer.bindTo(restTemplate); + if (this.bufferContent) { + serverBuilder.bufferContent(); + } + MockRestServiceServer server = serverBuilder.build(expectationManager); this.expectationManagers.put(restTemplate, expectationManager); this.servers.put(restTemplate, server); } diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/MockServerRestTemplateCustomizerTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/MockServerRestTemplateCustomizerTests.java index b92a359f60..bd1e7de4bc 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/MockServerRestTemplateCustomizerTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/MockServerRestTemplateCustomizerTests.java @@ -22,6 +22,8 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.http.client.BufferingClientHttpRequestFactory; +import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.test.web.client.RequestExpectationManager; import org.springframework.test.web.client.SimpleRequestExpectationManager; import org.springframework.test.web.client.UnorderedRequestExpectationManager; @@ -178,4 +180,21 @@ class MockServerRestTemplateCustomizerTests { assertThat(this.customizer.getServer(template2)).extracting("expectationManager").isEqualTo(manager2); } + @Test + void bufferContentShouldBeFalseByDefault() { + MockServerRestTemplateCustomizer customizer = new MockServerRestTemplateCustomizer(); + RestTemplate restTemplate = new RestTemplate(); + customizer.customize(restTemplate); + assertThat(restTemplate.getRequestFactory()).isInstanceOf(ClientHttpRequestFactory.class); + } + + @Test + void enableBufferContent() { + MockServerRestTemplateCustomizer customizer = new MockServerRestTemplateCustomizer(); + RestTemplate restTemplate = new RestTemplate(); + customizer.bufferContent(true); + customizer.customize(restTemplate); + assertThat(restTemplate.getRequestFactory()).isInstanceOf(BufferingClientHttpRequestFactory.class); + } + } From 3f93a4ece608b3f9d902d7a209e18c175378945d Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 17 Apr 2023 18:03:44 +0100 Subject: [PATCH 2/2] Polish "Add content buffering support to MockServerRestTemplateCustomizer" See gh-34833 --- .../MockServerRestTemplateCustomizer.java | 11 ++++--- ...MockServerRestTemplateCustomizerTests.java | 33 +++++++++---------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/MockServerRestTemplateCustomizer.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/MockServerRestTemplateCustomizer.java index a7bb97a44c..aabcd34437 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/MockServerRestTemplateCustomizer.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/MockServerRestTemplateCustomizer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -102,10 +102,13 @@ public class MockServerRestTemplateCustomizer implements RestTemplateCustomizer } /** - * Use the {@link BufferingClientHttpRequestFactory} wrapper to buffer the input and - * output streams, and for example, allow multiple reads of the response body. + * Set if the {@link BufferingClientHttpRequestFactory} wrapper should be used to + * buffer the input and output streams, and for example, allow multiple reads of the + * response body. + * @param bufferContent if request and response content should be buffered + * @since 3.1.0 */ - public void bufferContent(boolean bufferContent) { + public void setBufferContent(boolean bufferContent) { this.bufferContent = bufferContent; } diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/MockServerRestTemplateCustomizerTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/MockServerRestTemplateCustomizerTests.java index bd1e7de4bc..8f08c63abc 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/MockServerRestTemplateCustomizerTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/MockServerRestTemplateCustomizerTests.java @@ -106,7 +106,23 @@ class MockServerRestTemplateCustomizerTests { this.customizer.customize(new RestTemplateBuilder().rootUri("https://example.com").build()); assertThat(this.customizer.getServer()).extracting("expectationManager") .isInstanceOf(SimpleRequestExpectationManager.class); + } + + @Test + void bufferContentShouldDefaultToFalse() { + MockServerRestTemplateCustomizer customizer = new MockServerRestTemplateCustomizer(); + RestTemplate restTemplate = new RestTemplate(); + customizer.customize(restTemplate); + assertThat(restTemplate.getRequestFactory()).isInstanceOf(ClientHttpRequestFactory.class); + } + @Test + void setBufferContentShouldEnableContentBuffering() { + MockServerRestTemplateCustomizer customizer = new MockServerRestTemplateCustomizer(); + RestTemplate restTemplate = new RestTemplate(); + customizer.setBufferContent(true); + customizer.customize(restTemplate); + assertThat(restTemplate.getRequestFactory()).isInstanceOf(BufferingClientHttpRequestFactory.class); } @Test @@ -180,21 +196,4 @@ class MockServerRestTemplateCustomizerTests { assertThat(this.customizer.getServer(template2)).extracting("expectationManager").isEqualTo(manager2); } - @Test - void bufferContentShouldBeFalseByDefault() { - MockServerRestTemplateCustomizer customizer = new MockServerRestTemplateCustomizer(); - RestTemplate restTemplate = new RestTemplate(); - customizer.customize(restTemplate); - assertThat(restTemplate.getRequestFactory()).isInstanceOf(ClientHttpRequestFactory.class); - } - - @Test - void enableBufferContent() { - MockServerRestTemplateCustomizer customizer = new MockServerRestTemplateCustomizer(); - RestTemplate restTemplate = new RestTemplate(); - customizer.bufferContent(true); - customizer.customize(restTemplate); - assertThat(restTemplate.getRequestFactory()).isInstanceOf(BufferingClientHttpRequestFactory.class); - } - }