Allow merge of MockMultipart- w/ Mock- request builder

Issue: SPR-10280
This commit is contained in:
Rossen Stoyanchev 2013-02-15 09:41:29 -05:00
parent b47d97c23a
commit eda53ec1d8
2 changed files with 59 additions and 7 deletions

View File

@ -84,15 +84,16 @@ public class MockMultipartHttpServletRequestBuilder extends MockHttpServletReque
if (parent == null) {
return this;
}
if (!(parent instanceof MockMultipartHttpServletRequestBuilder)) {
throw new IllegalArgumentException("Cannot merge with [" + parent.getClass().getName() + "]");
}
if (parent instanceof MockHttpServletRequestBuilder) {
super.merge(parent);
if (parent instanceof MockMultipartHttpServletRequestBuilder) {
MockMultipartHttpServletRequestBuilder parentBuilder = (MockMultipartHttpServletRequestBuilder) parent;
this.files.addAll(parentBuilder.files);
}
}
else {
throw new IllegalArgumentException("Cannot merge with [" + parent.getClass().getName() + "]");
}
return this;
}

View File

@ -0,0 +1,51 @@
/*
* Copyright 2004-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.web.servlet.request;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.springframework.http.HttpMethod;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockServletContext;
/**
* Test fixture for {@link MockMultipartHttpServletRequestBuilder}.
*
* @author Rossen Stoyanchev
*/
public class MockMultipartHttpServletRequestBuilderTests {
@Test
public void test() {
MockHttpServletRequestBuilder parent = new MockHttpServletRequestBuilder(HttpMethod.GET, "/");
parent.characterEncoding("UTF-8");
Object result = new MockMultipartHttpServletRequestBuilder("/fileUpload").merge(parent);
assertNotNull(result);
assertEquals(MockMultipartHttpServletRequestBuilder.class, result.getClass());
MockMultipartHttpServletRequestBuilder builder = (MockMultipartHttpServletRequestBuilder) result;
MockHttpServletRequest request = builder.buildRequest(new MockServletContext());
assertEquals("UTF-8", request.getCharacterEncoding());
}
}