-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix five year old getSize() bug in StreamingMultipartFile found throu…
…gh unit tests
- Loading branch information
1 parent
5a6ff54
commit 738ec05
Showing
2 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
spring/test/org/tangram/spring/test/StreamingMultipartResolverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* | ||
* Copyright 2016 Martin Goellnitz | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
package org.tangram.spring.test; | ||
|
||
import org.springframework.mock.web.MockMultipartHttpServletRequest; | ||
import org.springframework.mock.web.MockServletContext; | ||
import org.springframework.web.multipart.MultipartFile; | ||
import org.springframework.web.multipart.MultipartHttpServletRequest; | ||
import org.tangram.spring.StreamingMultipartResolver; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
|
||
/** | ||
* Test handling of multipart form-data requests. | ||
*/ | ||
public class StreamingMultipartResolverTest { | ||
|
||
@Test | ||
public void testMultipartResolver() throws Exception { | ||
MockServletContext context = new MockServletContext(); | ||
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest(context); | ||
request.setMethod("POST"); | ||
request.setRequestURI("/testapp/id_RootTopic:1"); | ||
request.setContentType("multipart/form-data; boundary=----------tangram"); | ||
String content = "\r\n------------tangram\r\n" | ||
+"Content-Disposition: form-data; name=\"field\"\r\n\r\n" | ||
+"content of the field\r\n" | ||
+"------------tangram\r\n" | ||
+"Content-Disposition: form-data; name=\"multi\"\r\n\r\n" | ||
+"content one\r\n" | ||
+"------------tangram\r\n" | ||
+"Content-Disposition: form-data; name=\"multi\"\r\n\r\n" | ||
+"content two\r\n" | ||
+"------------tangram\r\n" | ||
+"Content-Disposition: form-data; name=\"file\"; filename=\"testfile.txt\"\r\n" | ||
+"Content-Type: text/plain\r\n\r\n" | ||
+"Please test for these contents here.\r\n\r\n" | ||
+"------------tangram--\r\n\r\n"; | ||
byte[] bytes = content.getBytes("ISO-8859-1"); | ||
request.setContent(bytes); | ||
StreamingMultipartResolver resolver = new StreamingMultipartResolver(); | ||
Assert.assertEquals(resolver.getMaxUploadSize(), 50000, "Non default value for upload max size found."); | ||
resolver.setMaxUploadSize(12345); | ||
Assert.assertEquals(resolver.getMaxUploadSize(), 12345, "Cannot customize max upload size."); | ||
Assert.assertTrue(resolver.isMultipart(request), "We have prepared a multipart request which is not recognized."); | ||
MultipartHttpServletRequest resolved = resolver.resolveMultipart(request); | ||
Assert.assertEquals(resolved.getParameterMap().size(), 2, "Unexpected number of parameters."); | ||
Assert.assertEquals(resolved.getFileMap().size(), 1, "Expected one available blob in the request parameters."); | ||
Assert.assertEquals(resolved.getParameter("field"), "content of the field", "Unexpected field value."); | ||
String[] values = resolved.getParameterValues("multi"); | ||
Assert.assertNotNull(values, "Multivalued field should be accessible."); | ||
Assert.assertEquals(values.length, 2, "Multivalued field should have two values."); | ||
Assert.assertEquals(values[0], "content one", "Multivalued has unexpected contents."); | ||
MultipartFile file = resolved.getFile("file"); | ||
Assert.assertEquals(file.getBytes().length, 38, "Unexpected file size."); | ||
Assert.assertEquals(file.getSize(), 38, "Unexpected file size."); | ||
Assert.assertEquals(file.getName(), "testfile.txt", "Unexpected file name."); | ||
Assert.assertEquals(file.getOriginalFilename(), "file", "Unexpected original file name."); | ||
Assert.assertFalse(file.isEmpty(), "Unexpected empty flag result."); | ||
} // testMultipartResolver() | ||
|
||
} // StreamingMultipartResolverTest |