Search in sources :

Example 6 with FileUpload

use of jodd.upload.FileUpload in project jodd by oblac.

the class HttpRequestTest method testFileUpload.

@Test
public void testFileUpload() throws IOException {
    HttpRequest request = HttpRequest.get("http://jodd.org/?id=173");
    request.header("User-Agent", "Scaly").form("one", "funny");
    File tempFile = FileUtil.createTempFile();
    tempFile.deleteOnExit();
    FileUtil.writeString(tempFile, "qwerty");
    request.form("two", tempFile);
    byte[] bytes = request.toByteArray();
    // read
    HttpRequest request2 = HttpRequest.readFrom(new ByteArrayInputStream(bytes));
    HttpMultiMap<?> httpParams2 = request2.form();
    assertEquals(request.method(), request2.method());
    assertEquals(request.path(), request2.path());
    assertEquals(request.queryString(), request2.queryString());
    assertEquals(request.header("User-Agent"), request2.header("User-Agent"));
    assertEquals(request.header("Content-Type"), request2.header("content-type"));
    assertEquals(request.header("Content-Length"), request2.header("content-length"));
    HttpMultiMap<?> params1 = request.form();
    HttpMultiMap<?> params2 = request2.form();
    assertEquals(params1.size(), params2.size());
    assertEquals(params2.get("one"), params2.get("one"));
    FileUpload fu = (FileUpload) httpParams2.get("two");
    assertEquals(6, fu.getSize());
    String str = new String(fu.getFileContent());
    assertEquals("qwerty", str);
    tempFile.delete();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) File(java.io.File) FileUpload(jodd.upload.FileUpload) Test(org.junit.Test)

Aggregations

FileUpload (jodd.upload.FileUpload)6 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 IOException (java.io.IOException)2 Enumeration (java.util.Enumeration)2 MultipartRequestWrapper (jodd.servlet.upload.MultipartRequestWrapper)2 InputStream (java.io.InputStream)1 FastCharArrayWriter (jodd.io.FastCharArrayWriter)1 ScopeData (jodd.madvoc.ScopeData)1 Action (jodd.madvoc.meta.Action)1 MadvocAction (jodd.madvoc.meta.MadvocAction)1 MultipartStreamParser (jodd.upload.MultipartStreamParser)1 RandomString (jodd.util.RandomString)1 Test (org.junit.Test)1