Skip to content

Commit 797e588

Browse files
treclouxUpStreamThomas
authored andcommitted
fix: RestTestClient.mutate() should not have side effects
Before this change, DefaultRestTestClient.mutate() was reusing the underlying builder all calls. Building a new builder for each call clones the RestTestClientBuilder, protecting from side effects. Signed-off-by: Thomas Recloux <[email protected]>
1 parent 9c93ece commit 797e588

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/client/DefaultRestTestClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ private RequestBodyUriSpec methodInternal(HttpMethod httpMethod) {
132132
@SuppressWarnings("unchecked")
133133
@Override
134134
public <B extends Builder<B>> Builder<B> mutate() {
135-
return (Builder<B>) this.restTestClientBuilder;
135+
return new DefaultRestTestClientBuilder<B>((DefaultRestTestClientBuilder<B>) this.restTestClientBuilder);
136136
}
137137

138138

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2002-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.web.servlet.client;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
23+
class DefaultRestTestClientBuilderTests {
24+
25+
@Test
26+
void testMutateHasNoSideEffects() {
27+
RestTestClient baseTestClient = new DefaultRestTestClientBuilder().baseUrl("http://localhost").build();
28+
baseTestClient.mutate().defaultHeader("foo", "bar").build();
29+
baseTestClient.mutate().defaultHeaders(headers -> assertThat(headers.containsHeader("foo")).isFalse());
30+
}
31+
32+
}

0 commit comments

Comments
 (0)