Skip to content

Commit 1582f5f

Browse files
committed
Handle URI as candidate for a merge from a parent builder
Closes gh-33057
1 parent b879007 commit 1582f5f

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/request/AbstractMockHttpServletRequestBuilder.java

+3
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,9 @@ public Object merge(@Nullable Object parent) {
592592
if (!(parent instanceof AbstractMockHttpServletRequestBuilder<?> parentBuilder)) {
593593
throw new IllegalArgumentException("Cannot merge with [" + parent.getClass().getName() + "]");
594594
}
595+
if (this.uri == null) {
596+
this.uri = parentBuilder.uri;
597+
}
595598
if (!StringUtils.hasText(this.contextPath)) {
596599
this.contextPath = parentBuilder.contextPath;
597600
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2002-2024 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.request;
18+
19+
import jakarta.servlet.ServletContext;
20+
import org.junit.jupiter.api.Test;
21+
22+
import org.springframework.http.HttpMethod;
23+
import org.springframework.mock.web.MockHttpServletRequest;
24+
import org.springframework.mock.web.MockServletContext;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Tests for {@link AbstractMockHttpServletRequestBuilder}
30+
*
31+
* @author Stephane Nicoll
32+
*/
33+
class AbstractMockHttpServletRequestBuilderTests {
34+
35+
private final ServletContext servletContext = new MockServletContext();
36+
37+
38+
@Test
39+
void mergeUriWhenUriIsNotSet() {
40+
TestRequestBuilder parentBuilder = new TestRequestBuilder(HttpMethod.GET).uri("/test");
41+
TestRequestBuilder builder = new TestRequestBuilder(HttpMethod.POST);
42+
builder.merge(parentBuilder);
43+
44+
MockHttpServletRequest request = buildRequest(builder);
45+
assertThat(request.getRequestURI()).isEqualTo("/test");
46+
assertThat(request.getMethod()).isEqualTo(HttpMethod.POST.name());
47+
}
48+
49+
@Test
50+
void mergeUriWhenUriIsSetDoesNotOverride() {
51+
TestRequestBuilder parentBuilder = new TestRequestBuilder(HttpMethod.GET).uri("/test");
52+
TestRequestBuilder builder = new TestRequestBuilder(HttpMethod.POST).uri("/another");
53+
builder.merge(parentBuilder);
54+
55+
MockHttpServletRequest request = buildRequest(builder);
56+
assertThat(request.getRequestURI()).isEqualTo("/another");
57+
assertThat(request.getMethod()).isEqualTo(HttpMethod.POST.name());
58+
}
59+
60+
61+
private MockHttpServletRequest buildRequest(AbstractMockHttpServletRequestBuilder<?> builder) {
62+
return builder.buildRequest(this.servletContext);
63+
}
64+
65+
66+
private static class TestRequestBuilder extends AbstractMockHttpServletRequestBuilder<TestRequestBuilder> {
67+
68+
TestRequestBuilder(HttpMethod httpMethod) {
69+
super(httpMethod);
70+
}
71+
}
72+
73+
}

0 commit comments

Comments
 (0)