Search in sources :

Example 11 with Uploading

use of org.nutz.mvc.upload.Uploading in project nutz by nutzam.

the class UploadingUnitTest method test_upload_multi_item_in_GBK.

/**
     * 检查一下普通的非文件数据项是否能被正确解码
     */
@Test
public void test_upload_multi_item_in_GBK() throws UploadException {
    /*
         * 准备请求对象
         */
    MockHttpServletRequest req = Mock.servlet.request();
    MultipartInputStream ins = Mock.servlet.insmulti("GBK");
    ins.append("abc", "程序员s");
    req.setInputStream(ins);
    req.init();
    /*
         * 执行上传
         */
    Uploading up = UploadUnit.TYPE.born();
    Map<String, Object> map = up.parse(req, UploadingContext.create(tmps).setCharset("GBK"));
    /*
         * 检查以下是不是 GBK 编码被解析成功
         */
    assertEquals("程序员s", map.get("abc"));
}
Also used : MockHttpServletRequest(org.nutz.mock.servlet.MockHttpServletRequest) Uploading(org.nutz.mvc.upload.Uploading) MultipartInputStream(org.nutz.mock.servlet.multipart.MultipartInputStream) Test(org.junit.Test)

Example 12 with Uploading

use of org.nutz.mvc.upload.Uploading in project nutz by nutzam.

the class UploadingUnitTest method test_upload_queryString.

@Test
public void test_upload_queryString() throws UploadException {
    MockHttpServletRequest req = Mock.servlet.request();
    req.setParameter("id", "1");
    req.setParameter("name", "nutz");
    Uploading up = UploadUnit.TYPE.born();
    MultipartInputStream ins = Mock.servlet.insmulti(charset);
    ins.append("age", "1");
    req.setInputStream(ins);
    req.init();
    Map<String, Object> map = up.parse(req, UploadingContext.create(tmps));
    assertEquals("1", map.get("id"));
    assertEquals("nutz", map.get("name"));
    assertEquals("1", map.get("age"));
    assertEquals(null, map.get("null"));
}
Also used : MockHttpServletRequest(org.nutz.mock.servlet.MockHttpServletRequest) Uploading(org.nutz.mvc.upload.Uploading) MultipartInputStream(org.nutz.mock.servlet.multipart.MultipartInputStream) Test(org.junit.Test)

Example 13 with Uploading

use of org.nutz.mvc.upload.Uploading in project nutz by nutzam.

the class UploadingUnitTest method test_limit_file_content_type_fail.

/**
     * 测试限制文件类型:限制文件类型
     */
@Test(expected = UploadUnsupportedFileTypeException.class)
public void test_limit_file_content_type_fail() throws UploadException {
    MockHttpServletRequest req = Mock.servlet.request();
    req.setPathInfo("/nutz/junit/uploading");
    File blue = Files.findFile("org/nutz/mvc/upload/files/quick/blue.png");
    MultipartInputStream ins = Mock.servlet.insmulti(charset);
    ins.append("blue", blue);
    req.setInputStream(ins);
    req.init();
    /*
         * 文件超大,会限制
         */
    Uploading up = UploadUnit.TYPE.born();
    up.parse(req, UploadingContext.create(tmps).setContentTypeFilter("^image/gif$"));
}
Also used : MockHttpServletRequest(org.nutz.mock.servlet.MockHttpServletRequest) Uploading(org.nutz.mvc.upload.Uploading) TempFile(org.nutz.mvc.upload.TempFile) File(java.io.File) MultipartInputStream(org.nutz.mock.servlet.multipart.MultipartInputStream) Test(org.junit.Test)

Example 14 with Uploading

use of org.nutz.mvc.upload.Uploading in project nutz by nutzam.

the class UploadingUnitTest method test_cast_dt01.

@Test
public void test_cast_dt01() throws UploadException {
    MockHttpServletRequest req = Mock.servlet.request();
    req.setHeader("content-type", "multipart/form-data; boundary=----ESDT-321271401654cc6d669eef664aac");
    Uploading up = UploadUnit.TYPE.born();
    ServletInputStream ins = Mock.servlet.ins("org/nutz/mvc/upload/files/cast_dt01");
    req.setInputStream(ins);
    req.init();
    Map<String, Object> map = up.parse(req, UploadingContext.create(tmps));
    assertEquals(1, map.size());
    assertEquals("Shapes100.jpg", ((TempFile) map.get("fileData")).getSubmittedFileName());
}
Also used : ServletInputStream(javax.servlet.ServletInputStream) MockHttpServletRequest(org.nutz.mock.servlet.MockHttpServletRequest) Uploading(org.nutz.mvc.upload.Uploading) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)14 MockHttpServletRequest (org.nutz.mock.servlet.MockHttpServletRequest)14 Uploading (org.nutz.mvc.upload.Uploading)14 MultipartInputStream (org.nutz.mock.servlet.multipart.MultipartInputStream)11 File (java.io.File)8 TempFile (org.nutz.mvc.upload.TempFile)8 ServletInputStream (javax.servlet.ServletInputStream)1