From 2561a60968f3c488f959dcce516365dbd99d4fe5 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Fri, 10 Jan 2025 14:39:11 +0000 Subject: [PATCH] Update MockRestServiceServer bufferContent Connect the existing bufferContent option to the new RestClient and RestTemplate equivalent feature. See gh-33785 --- .../test/web/client/MockRestServiceServer.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/web/client/MockRestServiceServer.java b/spring-test/src/main/java/org/springframework/test/web/client/MockRestServiceServer.java index eb685296191..03fba3533a2 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/MockRestServiceServer.java +++ b/spring-test/src/main/java/org/springframework/test/web/client/MockRestServiceServer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2025 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. @@ -244,6 +244,10 @@ public final class MockRestServiceServer { return this; } + protected boolean shouldBufferContent() { + return this.bufferContent; + } + @Override public MockRestServiceServer build() { if (this.ignoreExpectOrder) { @@ -258,9 +262,6 @@ public final class MockRestServiceServer { public MockRestServiceServer build(RequestExpectationManager manager) { MockRestServiceServer server = new MockRestServiceServer(manager); ClientHttpRequestFactory factory = server.new MockClientHttpRequestFactory(); - if (this.bufferContent) { - factory = new BufferingClientHttpRequestFactory(factory); - } injectRequestFactory(factory); return server; } @@ -281,6 +282,9 @@ public final class MockRestServiceServer { @Override protected void injectRequestFactory(ClientHttpRequestFactory requestFactory) { + if (shouldBufferContent()) { + this.restClientBuilder.bufferContent((uri, httpMethod) -> true); + } this.restClientBuilder.requestFactory(requestFactory); } } @@ -297,6 +301,9 @@ public final class MockRestServiceServer { @Override protected void injectRequestFactory(ClientHttpRequestFactory requestFactory) { + if (shouldBufferContent()) { + this.restTemplate.setBufferingPredicate((uri, httpMethod) -> true); + } this.restTemplate.setRequestFactory(requestFactory); } }