MockMultipartHttpServletRequest pre-defines method "POST" and content type "multipart/form-data"

This commit is contained in:
Juergen Hoeller 2010-08-15 22:46:15 +00:00
parent 1841b14b21
commit 5b0448c609
3 changed files with 32 additions and 13 deletions

View File

@ -47,6 +47,12 @@ public class MockMultipartHttpServletRequest extends MockHttpServletRequest impl
new LinkedMultiValueMap<String, MultipartFile>(); new LinkedMultiValueMap<String, MultipartFile>();
public MockMultipartHttpServletRequest() {
setMethod("POST");
setContentType("multipart/form-data");
}
/** /**
* Add a file to this request. The parameter name from the multipart * Add a file to this request. The parameter name from the multipart
* form is taken from the {@link MultipartFile#getName()}. * form is taken from the {@link MultipartFile#getName()}.

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,13 +18,12 @@ package org.springframework.mock.web;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.List; import java.util.List;
import java.util.Map;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.MultiValueMap;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.MultipartHttpServletRequest;
@ -44,7 +43,14 @@ import org.springframework.web.multipart.MultipartHttpServletRequest;
*/ */
public class MockMultipartHttpServletRequest extends MockHttpServletRequest implements MultipartHttpServletRequest { public class MockMultipartHttpServletRequest extends MockHttpServletRequest implements MultipartHttpServletRequest {
private final MultiValueMap<String, MultipartFile> multipartFiles = new LinkedMultiValueMap<String, MultipartFile>(); private final MultiValueMap<String, MultipartFile> multipartFiles =
new LinkedMultiValueMap<String, MultipartFile>();
public MockMultipartHttpServletRequest() {
setMethod("POST");
setContentType("multipart/form-data");
}
/** /**
@ -58,7 +64,7 @@ public class MockMultipartHttpServletRequest extends MockHttpServletRequest impl
} }
public Iterator<String> getFileNames() { public Iterator<String> getFileNames() {
return getFileMap().keySet().iterator(); return this.multipartFiles.keySet().iterator();
} }
public MultipartFile getFile(String name) { public MultipartFile getFile(String name) {
@ -76,10 +82,11 @@ public class MockMultipartHttpServletRequest extends MockHttpServletRequest impl
} }
public Map<String, MultipartFile> getFileMap() { public Map<String, MultipartFile> getFileMap() {
return Collections.unmodifiableMap(this.multipartFiles.toSingleValueMap()); return this.multipartFiles.toSingleValueMap();
} }
public MultiValueMap<String, MultipartFile> getMultiFileMap() { public MultiValueMap<String, MultipartFile> getMultiFileMap() {
return new LinkedMultiValueMap<String, MultipartFile>(Collections.unmodifiableMap(this.multipartFiles)); return new LinkedMultiValueMap<String, MultipartFile>(this.multipartFiles);
} }
} }

View File

@ -32,14 +32,14 @@ import org.springframework.web.multipart.MultipartHttpServletRequest;
* {@link org.springframework.web.multipart.MultipartHttpServletRequest} interface. * {@link org.springframework.web.multipart.MultipartHttpServletRequest} interface.
* *
* <p>Useful for testing application controllers that access multipart uploads. * <p>Useful for testing application controllers that access multipart uploads.
* The {@link org.springframework.mock.web.MockMultipartFile} can be used to populate these mock requests * The {@link MockMultipartFile} can be used to populate these mock requests
* with files. * with files.
* *
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Eric Crampton * @author Eric Crampton
* @author Arjen Poutsma * @author Arjen Poutsma
* @since 2.0 * @since 2.0
* @see org.springframework.mock.web.MockMultipartFile * @see MockMultipartFile
*/ */
public class MockMultipartHttpServletRequest extends MockHttpServletRequest implements MultipartHttpServletRequest { public class MockMultipartHttpServletRequest extends MockHttpServletRequest implements MultipartHttpServletRequest {
@ -47,9 +47,15 @@ public class MockMultipartHttpServletRequest extends MockHttpServletRequest impl
new LinkedMultiValueMap<String, MultipartFile>(); new LinkedMultiValueMap<String, MultipartFile>();
public MockMultipartHttpServletRequest() {
setMethod("POST");
setContentType("multipart/form-data");
}
/** /**
* Add a file to this request. The parameter name from the multipart * Add a file to this request. The parameter name from the multipart
* form is taken from the {@link org.springframework.web.multipart.MultipartFile#getName()}. * form is taken from the {@link MultipartFile#getName()}.
* @param file multipart file to be added * @param file multipart file to be added
*/ */
public void addFile(MultipartFile file) { public void addFile(MultipartFile file) {