Skip to content

Commit eda53ec

Browse files
committed
Allow merge of MockMultipart- w/ Mock- request builder
Issue: SPR-10280
1 parent b47d97c commit eda53ec

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,16 @@ public Object merge(Object parent) {
8484
if (parent == null) {
8585
return this;
8686
}
87-
if (!(parent instanceof MockMultipartHttpServletRequestBuilder)) {
87+
if (parent instanceof MockHttpServletRequestBuilder) {
88+
super.merge(parent);
89+
if (parent instanceof MockMultipartHttpServletRequestBuilder) {
90+
MockMultipartHttpServletRequestBuilder parentBuilder = (MockMultipartHttpServletRequestBuilder) parent;
91+
this.files.addAll(parentBuilder.files);
92+
}
93+
}
94+
else {
8895
throw new IllegalArgumentException("Cannot merge with [" + parent.getClass().getName() + "]");
8996
}
90-
91-
super.merge(parent);
92-
93-
MockMultipartHttpServletRequestBuilder parentBuilder = (MockMultipartHttpServletRequestBuilder) parent;
94-
this.files.addAll(parentBuilder.files);
95-
9697
return this;
9798
}
9899

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2004-2013 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+
* http://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 static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertNotNull;
21+
22+
import org.junit.Test;
23+
import org.springframework.http.HttpMethod;
24+
import org.springframework.mock.web.MockHttpServletRequest;
25+
import org.springframework.mock.web.MockServletContext;
26+
27+
28+
/**
29+
* Test fixture for {@link MockMultipartHttpServletRequestBuilder}.
30+
*
31+
* @author Rossen Stoyanchev
32+
*/
33+
public class MockMultipartHttpServletRequestBuilderTests {
34+
35+
36+
@Test
37+
public void test() {
38+
MockHttpServletRequestBuilder parent = new MockHttpServletRequestBuilder(HttpMethod.GET, "/");
39+
parent.characterEncoding("UTF-8");
40+
Object result = new MockMultipartHttpServletRequestBuilder("/fileUpload").merge(parent);
41+
42+
assertNotNull(result);
43+
assertEquals(MockMultipartHttpServletRequestBuilder.class, result.getClass());
44+
45+
MockMultipartHttpServletRequestBuilder builder = (MockMultipartHttpServletRequestBuilder) result;
46+
MockHttpServletRequest request = builder.buildRequest(new MockServletContext());
47+
48+
assertEquals("UTF-8", request.getCharacterEncoding());
49+
}
50+
51+
}

0 commit comments

Comments
 (0)