summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAndrey Zelenkov <zelenkov@nginx.com>2019-08-07 14:43:38 +0300
committerAndrey Zelenkov <zelenkov@nginx.com>2019-08-07 14:43:38 +0300
commite8d1c760d6c8134c809d2bb60c212743fdbb9b16 (patch)
tree6f29d32606147b4e1428fe71b4161960a17c7dc8
parentc8c259b9728c57e70042d7630045e5b043f46e5b (diff)
downloadunit-e8d1c760d6c8134c809d2bb60c212743fdbb9b16.tar.gz
unit-e8d1c760d6c8134c809d2bb60c212743fdbb9b16.tar.bz2
Tests: Java multipart test.
-rw-r--r--test/java/multipart/app.java93
-rw-r--r--test/test_java_application.py39
2 files changed, 132 insertions, 0 deletions
diff --git a/test/java/multipart/app.java b/test/java/multipart/app.java
new file mode 100644
index 00000000..c4c89ffb
--- /dev/null
+++ b/test/java/multipart/app.java
@@ -0,0 +1,93 @@
+
+import java.io.IOException;
+import java.io.PrintWriter;
+
+import java.util.Map;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
+
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.annotation.MultipartConfig;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+import javax.servlet.http.Part;
+
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+@WebServlet("/")
+@MultipartConfig(
+ fileSizeThreshold = 1024 * 1024 * 1, // 1 MB
+ maxFileSize = 1024 * 1024 * 10, // 10 MB
+ maxRequestSize = 1024 * 1024 * 15 // 15 MB
+)
+public class app extends HttpServlet
+{
+ @Override
+ public void doPost(HttpServletRequest request, HttpServletResponse response)
+ throws IOException, ServletException
+ {
+ response.setContentType("text/html;charset=UTF-8");
+
+ // Create path components to save the file
+ final String path = request.getParameter("destination");
+ final Part filePart = request.getPart("file");
+ final String fileName = getFileName(filePart);
+
+ OutputStream out = null;
+ InputStream filecontent = null;
+ final PrintWriter writer = response.getWriter();
+
+ try {
+ out = new FileOutputStream(new File(path + File.separator
+ + fileName));
+ filecontent = filePart.getInputStream();
+
+ int read = 0;
+ final byte[] bytes = new byte[1024];
+
+ while ((read = filecontent.read(bytes)) != -1) {
+ out.write(bytes, 0, read);
+ }
+ writer.println(fileName + " created at " + path);
+
+ } catch (FileNotFoundException fne) {
+ writer.println("You either did not specify a file to upload or are "
+ + "trying to upload a file to a protected or nonexistent "
+ + "location.");
+ writer.println("<br/> ERROR: " + fne.getMessage());
+
+ } finally {
+ if (out != null) {
+ out.close();
+ }
+ if (filecontent != null) {
+ filecontent.close();
+ }
+ if (writer != null) {
+ writer.close();
+ }
+ }
+
+ return;
+ }
+
+ private String getFileName(final Part part) {
+ final String partHeader = part.getHeader("content-disposition");
+
+ for (String content : part.getHeader("content-disposition").split(";"))
+ {
+ if (content.trim().startsWith("filename")) {
+ return content.substring(
+ content.indexOf("=") + 1).trim().replace("\"", "");
+ }
+ }
+ return null;
+ }
+}
diff --git a/test/test_java_application.py b/test/test_java_application.py
index f604368c..2e22e545 100644
--- a/test/test_java_application.py
+++ b/test/test_java_application.py
@@ -1,4 +1,5 @@
import time
+import unittest
from unit.applications.lang.java import TestApplicationJava
@@ -1213,6 +1214,44 @@ class TestJavaApplication(TestApplicationJava):
)
self.assertEqual(headers['X-Get-Date'], date, 'get date header')
+ @unittest.skip('not yet')
+ def test_java_application_multipart(self):
+ self.load('multipart')
+
+ body = """Preamble. Should be ignored.\r
+\r
+--12345\r
+Content-Disposition: form-data; name="file"; filename="sample.txt"\r
+Content-Type: text/plain\r
+\r
+Data from sample file\r
+--12345\r
+Content-Disposition: form-data; name="destination"\r
+\r
+%s\r
+--12345\r
+Content-Disposition: form-data; name="upload"\r
+\r
+Upload\r
+--12345--\r
+\r
+Epilogue. Should be ignored.""" % self.testdir
+
+ resp = self.post(
+ headers={
+ 'Content-Type': 'multipart/form-data; boundary=12345',
+ 'Host': 'localhost',
+ 'Connection': 'close',
+ },
+ body=body,
+ )
+
+ self.assertEqual(resp['status'], 200, 'multipart status')
+ self.assertRegex(resp['body'], r'sample\.txt created', 'multipart body')
+ self.assertIsNotNone(
+ self.search_in_log(r'^Data from sample file$', name='sample.txt'),
+ 'file created',
+ )
if __name__ == '__main__':
TestJavaApplication.main()