Skip to content

Commit 3df1c62

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 <trecloux@purse.eu>
1 parent 6ac13d1 commit 3df1c62

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-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
@@ -129,7 +129,7 @@ private RequestBodyUriSpec methodInternal(HttpMethod httpMethod) {
129129
@SuppressWarnings("unchecked")
130130
@Override
131131
public <B extends Builder<B>> Builder<B> mutate() {
132-
return (Builder<B>) this.restTestClientBuilder;
132+
return new DefaultRestTestClientBuilder<B>((DefaultRestTestClientBuilder<B>) this.restTestClientBuilder);
133133
}
134134

135135

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.springframework.test.web.servlet.client;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class DefaultRestTestClientBuilderTest {
9+
10+
@Test
11+
void testMutateHasNoSideEffects() {
12+
RestTestClient baseTestClient = new DefaultRestTestClientBuilder().baseUrl("http://localhost").build();
13+
baseTestClient.mutate().defaultHeader("foo", "bar").build();
14+
baseTestClient.mutate().defaultHeaders(headers -> assertThat(headers.containsHeader("foo")).isFalse());
15+
}
16+
17+
}

0 commit comments

Comments
 (0)