Tests: Java multipart test.
This commit is contained in:
93
test/java/multipart/app.java
Normal file
93
test/java/multipart/app.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user