MockMultipartHttpServletRequest pre-defines method "POST" and content type "multipart/form-data"
This commit is contained in:
parent
1841b14b21
commit
5b0448c609
|
|
@ -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()}.
|
||||||
|
|
|
||||||
|
|
@ -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) {
|
||||||
|
|
@ -73,13 +79,14 @@ public class MockMultipartHttpServletRequest extends MockHttpServletRequest impl
|
||||||
else {
|
else {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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) {
|
||||||
|
|
@ -83,4 +89,4 @@ public class MockMultipartHttpServletRequest extends MockHttpServletRequest impl
|
||||||
return new LinkedMultiValueMap<String, MultipartFile>(this.multipartFiles);
|
return new LinkedMultiValueMap<String, MultipartFile>(this.multipartFiles);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue