List of usage examples for org.apache.commons.fileupload MockHttpServletRequest MockHttpServletRequest
public MockHttpServletRequest(final byte[] requestData, final String strContentType)
From source file:com.github.davidcarboni.encryptedfileupload.SizesTest.java
/** Checks, whether limiting the file size works. *//*from ww w.ja v a2 s .c om*/ @Test public void testFileSizeLimit() throws IOException, FileUploadException { final String request = "-----1234\r\n" + "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n" + "Content-Type: text/whatever\r\n" + "\r\n" + "This is the content of the file\n" + "\r\n" + "-----1234--\r\n"; ServletFileUpload upload = new ServletFileUpload(new EncryptedFileItemFactory()); upload.setFileSizeMax(-1); HttpServletRequest req = new MockHttpServletRequest(request.getBytes("US-ASCII"), CONTENT_TYPE); List<FileItem> fileItems = upload.parseRequest(req); assertEquals(1, fileItems.size()); FileItem item = fileItems.get(0); assertEquals("This is the content of the file\n", new String(item.get())); upload = new ServletFileUpload(new EncryptedFileItemFactory()); upload.setFileSizeMax(40); req = new MockHttpServletRequest(request.getBytes("US-ASCII"), CONTENT_TYPE); fileItems = upload.parseRequest(req); assertEquals(1, fileItems.size()); item = fileItems.get(0); assertEquals("This is the content of the file\n", new String(item.get())); upload = new ServletFileUpload(new EncryptedFileItemFactory()); upload.setFileSizeMax(30); req = new MockHttpServletRequest(request.getBytes("US-ASCII"), CONTENT_TYPE); try { upload.parseRequest(req); fail("Expected exception."); } catch (FileUploadBase.FileSizeLimitExceededException e) { assertEquals(30, e.getPermittedSize()); } }
From source file:com.github.davidcarboni.encryptedfileupload.SizesTest.java
/** Checks, whether a faked Content-Length header is detected. *//*from ww w .j a v a 2 s . c o m*/ @Test public void testFileSizeLimitWithFakedContentLength() throws IOException, FileUploadException { final String request = "-----1234\r\n" + "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n" + "Content-Type: text/whatever\r\n" + "Content-Length: 10\r\n" + "\r\n" + "This is the content of the file\n" + "\r\n" + "-----1234--\r\n"; ServletFileUpload upload = new ServletFileUpload(new EncryptedFileItemFactory()); upload.setFileSizeMax(-1); HttpServletRequest req = new MockHttpServletRequest(request.getBytes("US-ASCII"), CONTENT_TYPE); List<FileItem> fileItems = upload.parseRequest(req); assertEquals(1, fileItems.size()); FileItem item = fileItems.get(0); assertEquals("This is the content of the file\n", new String(item.get())); upload = new ServletFileUpload(new EncryptedFileItemFactory()); upload.setFileSizeMax(40); req = new MockHttpServletRequest(request.getBytes("US-ASCII"), CONTENT_TYPE); fileItems = upload.parseRequest(req); assertEquals(1, fileItems.size()); item = fileItems.get(0); assertEquals("This is the content of the file\n", new String(item.get())); // provided Content-Length is larger than the FileSizeMax -> handled by ctor upload = new ServletFileUpload(new EncryptedFileItemFactory()); upload.setFileSizeMax(5); req = new MockHttpServletRequest(request.getBytes("US-ASCII"), CONTENT_TYPE); try { upload.parseRequest(req); fail("Expected exception."); } catch (FileUploadBase.FileSizeLimitExceededException e) { assertEquals(5, e.getPermittedSize()); } // provided Content-Length is wrong, actual content is larger -> handled by LimitedInputStream upload = new ServletFileUpload(new EncryptedFileItemFactory()); upload.setFileSizeMax(15); req = new MockHttpServletRequest(request.getBytes("US-ASCII"), CONTENT_TYPE); try { upload.parseRequest(req); fail("Expected exception."); } catch (FileUploadBase.FileSizeLimitExceededException e) { assertEquals(15, e.getPermittedSize()); } }
From source file:com.github.davidcarboni.encryptedfileupload.SizesTest.java
/** Checks, whether the maxSize works. *///www .ja va 2 s. c om @Test public void testMaxSizeLimit() throws IOException, FileUploadException { final String request = "-----1234\r\n" + "Content-Disposition: form-data; name=\"file1\"; filename=\"foo1.tab\"\r\n" + "Content-Type: text/whatever\r\n" + "Content-Length: 10\r\n" + "\r\n" + "This is the content of the file\n" + "\r\n" + "-----1234\r\n" + "Content-Disposition: form-data; name=\"file2\"; filename=\"foo2.tab\"\r\n" + "Content-Type: text/whatever\r\n" + "\r\n" + "This is the content of the file\n" + "\r\n" + "-----1234--\r\n"; ServletFileUpload upload = new ServletFileUpload(new EncryptedFileItemFactory()); upload.setFileSizeMax(-1); upload.setSizeMax(200); MockHttpServletRequest req = new MockHttpServletRequest(request.getBytes("US-ASCII"), CONTENT_TYPE); try { upload.parseRequest(req); fail("Expected exception."); } catch (FileUploadBase.SizeLimitExceededException e) { assertEquals(200, e.getPermittedSize()); } }
From source file:com.github.davidcarboni.encryptedfileupload.SizesTest.java
@Test public void testMaxSizeLimitUnknownContentLength() throws IOException, FileUploadException { final String request = "-----1234\r\n" + "Content-Disposition: form-data; name=\"file1\"; filename=\"foo1.tab\"\r\n" + "Content-Type: text/whatever\r\n" + "Content-Length: 10\r\n" + "\r\n" + "This is the content of the file\n" + "\r\n" + "-----1234\r\n" + "Content-Disposition: form-data; name=\"file2\"; filename=\"foo2.tab\"\r\n" + "Content-Type: text/whatever\r\n" + "\r\n" + "This is the content of the file\n" + "\r\n" + "-----1234--\r\n"; ServletFileUpload upload = new ServletFileUpload(new EncryptedFileItemFactory()); upload.setFileSizeMax(-1);//from w ww . ja va2 s. co m upload.setSizeMax(300); // the first item should be within the max size limit // set the read limit to 10 to simulate a "real" stream // otherwise the buffer would be immediately filled MockHttpServletRequest req = new MockHttpServletRequest(request.getBytes("US-ASCII"), CONTENT_TYPE); req.setContentLength(-1); req.setReadLimit(10); FileItemIterator it = upload.getItemIterator(req); assertTrue(it.hasNext()); FileItemStream item = it.next(); assertFalse(item.isFormField()); assertEquals("file1", item.getFieldName()); assertEquals("foo1.tab", item.getName()); { @SuppressWarnings("resource") // Streams.copy closes the input file InputStream stream = item.openStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); Streams.copy(stream, baos, true); } // the second item is over the size max, thus we expect an error try { // the header is still within size max -> this shall still succeed assertTrue(it.hasNext()); } catch (Exception e) { // FileUploadBase.SizeException has protected access: if (e.getClass().getSimpleName().equals("SizeException")) { fail(); } else { throw e; } } item = it.next(); try { @SuppressWarnings("resource") // Streams.copy closes the input file InputStream stream = item.openStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); Streams.copy(stream, baos, true); fail(); } catch (FileUploadIOException e) { // expected } }
From source file:org.opencastproject.ingest.endpoint.IngestRestServiceTest.java
private HttpServletRequest newMockRequest() throws Exception { MediaPackage mp = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder().createNew(); StringBuilder requestBody = new StringBuilder(); requestBody.append("-----1234\r\n"); requestBody.append("Content-Disposition: form-data; name=\"flavor\"\r\n"); requestBody.append("\r\ntest/flavor\r\n"); requestBody.append("-----1234\r\n"); requestBody.append("Content-Disposition: form-data; name=\"mediaPackage\"\r\n"); requestBody.append("\r\n"); requestBody.append(MediaPackageParser.getAsXml(mp)); requestBody.append("\r\n"); requestBody.append("-----1234\r\n"); requestBody.append("Content-Disposition: form-data; name=\"file\"; filename=\"catalog.txt\"\r\n"); requestBody.append("Content-Type: text/whatever\r\n"); requestBody.append("\r\n"); requestBody.append("This is the content of the file\n"); requestBody.append("\r\n"); requestBody.append("-----1234"); return new MockHttpServletRequest(requestBody.toString().getBytes("UTF-8"), "multipart/form-data; boundary=---1234"); }
From source file:org.opencastproject.staticfiles.endpoint.StaticFileRestServiceTest.java
private MockHttpServletRequest newMockRequest() throws Exception { StringBuilder requestBody = new StringBuilder(); requestBody.append("-----1234\r\n"); requestBody.append("Content-Disposition: form-data; name=\"file\"; filename=\"other.mov\"\r\n"); requestBody.append("Content-Type: text/whatever\r\n"); requestBody.append("\r\n"); requestBody.append(MOCK_FILE_CONTENT); requestBody.append("\r\n"); requestBody.append("-----1234"); return new MockHttpServletRequest(requestBody.toString().getBytes("UTF-8"), "multipart/form-data; boundary=---1234"); }