Skip to content

Commit

Permalink
Allow merge of MockMultipart- w/ Mock- request builder
Browse files Browse the repository at this point in the history
Issue: SPR-10280
  • Loading branch information
rstoyanchev committed Feb 15, 2013
1 parent b47d97c commit eda53ec
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,16 @@ public Object merge(Object parent) {
if (parent == null) {
return this;
}
if (!(parent instanceof MockMultipartHttpServletRequestBuilder)) {
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() + "]");
}

super.merge(parent);

MockMultipartHttpServletRequestBuilder parentBuilder = (MockMultipartHttpServletRequestBuilder) parent;
this.files.addAll(parentBuilder.files);

return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}

}

0 comments on commit eda53ec

Please sign in to comment.