View Javadoc
1   /*
2    * Copyright 2002-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.mock.web.test;
18  
19  import java.util.Collections;
20  import java.util.Enumeration;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.Map;
24  import javax.servlet.ServletContext;
25  
26  import org.springframework.http.HttpHeaders;
27  import org.springframework.http.HttpMethod;
28  import org.springframework.util.Assert;
29  import org.springframework.util.LinkedMultiValueMap;
30  import org.springframework.util.MultiValueMap;
31  import org.springframework.web.multipart.MultipartFile;
32  import org.springframework.web.multipart.MultipartHttpServletRequest;
33  
34  /**
35   * Mock implementation of the
36   * {@link org.springframework.web.multipart.MultipartHttpServletRequest} interface.
37   *
38   * <p>As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline.
39   *
40   * <p>Useful for testing application controllers that access multipart uploads.
41   * The {@link MockMultipartFile} can be used to populate these mock requests
42   * with files.
43   *
44   * @author Juergen Hoeller
45   * @author Eric Crampton
46   * @author Arjen Poutsma
47   * @since 2.0
48   * @see MockMultipartFile
49   */
50  public class MockMultipartHttpServletRequest extends MockHttpServletRequest implements MultipartHttpServletRequest {
51  
52  	private final MultiValueMap<String, MultipartFile> multipartFiles =
53  			new LinkedMultiValueMap<String, MultipartFile>();
54  
55  
56  	/**
57  	 * Create a new {@code MockMultipartHttpServletRequest} with a default
58  	 * {@link MockServletContext}.
59  	 * @see #MockMultipartHttpServletRequest(ServletContext)
60  	 */
61  	public MockMultipartHttpServletRequest() {
62  		this(null);
63  	}
64  
65  	/**
66  	 * Create a new {@code MockMultipartHttpServletRequest} with the supplied {@link ServletContext}.
67  	 * @param servletContext the ServletContext that the request runs in
68  	 * (may be {@code null} to use a default {@link MockServletContext})
69  	 */
70  	public MockMultipartHttpServletRequest(ServletContext servletContext) {
71  		super(servletContext);
72  		setMethod("POST");
73  		setContentType("multipart/form-data");
74  	}
75  
76  
77  	/**
78  	 * Add a file to this request. The parameter name from the multipart
79  	 * form is taken from the {@link MultipartFile#getName()}.
80  	 * @param file multipart file to be added
81  	 */
82  	public void addFile(MultipartFile file) {
83  		Assert.notNull(file, "MultipartFile must not be null");
84  		this.multipartFiles.add(file.getName(), file);
85  	}
86  
87  	@Override
88  	public Iterator<String> getFileNames() {
89  		return this.multipartFiles.keySet().iterator();
90  	}
91  
92  	@Override
93  	public MultipartFile getFile(String name) {
94  		return this.multipartFiles.getFirst(name);
95  	}
96  
97  	@Override
98  	public List<MultipartFile> getFiles(String name) {
99  		List<MultipartFile> multipartFiles = this.multipartFiles.get(name);
100 		if (multipartFiles != null) {
101 			return multipartFiles;
102 		}
103 		else {
104 			return Collections.emptyList();
105 		}
106 	}
107 
108 	@Override
109 	public Map<String, MultipartFile> getFileMap() {
110 		return this.multipartFiles.toSingleValueMap();
111 	}
112 
113 	@Override
114 	public MultiValueMap<String, MultipartFile> getMultiFileMap() {
115 		return new LinkedMultiValueMap<String, MultipartFile>(this.multipartFiles);
116 	}
117 
118 	@Override
119 	public String getMultipartContentType(String paramOrFileName) {
120 		MultipartFile file = getFile(paramOrFileName);
121 		if (file != null) {
122 			return file.getContentType();
123 		}
124 		else {
125 			return null;
126 		}
127 	}
128 
129 	@Override
130 	public HttpMethod getRequestMethod() {
131 		return HttpMethod.valueOf(getMethod());
132 	}
133 
134 	@Override
135 	public HttpHeaders getRequestHeaders() {
136 		HttpHeaders headers = new HttpHeaders();
137 		Enumeration<String> headerNames = getHeaderNames();
138 		while (headerNames.hasMoreElements()) {
139 			String headerName = headerNames.nextElement();
140 			headers.put(headerName, Collections.list(getHeaders(headerName)));
141 		}
142 		return headers;
143 	}
144 
145 	@Override
146 	public HttpHeaders getMultipartHeaders(String paramOrFileName) {
147 		String contentType = getMultipartContentType(paramOrFileName);
148 		if (contentType != null) {
149 			HttpHeaders headers = new HttpHeaders();
150 			headers.add("Content-Type", contentType);
151 			return headers;
152 		}
153 		else {
154 			return null;
155 		}
156 	}
157 
158 }