Search in sources :

Example 11 with MockHttpServletRequest

use of org.nutz.mock.servlet.MockHttpServletRequest in project nutz by nutzam.

the class UploadingUnitTest method test_upload_empty_just_r_n.

/**
     * for issue #617
     */
@Test
public void test_upload_empty_just_r_n() throws Exception {
    MockHttpServletRequest req = Mock.servlet.request();
    req.setPathInfo("/nutz/junit/uploading");
    MultipartInputStream ins = Mock.servlet.insmulti(charset);
    File f = Files.findFile("org/nutz/mvc/upload/files/_r_n.txt");
    ins.append("theF", f);
    req.setInputStream(ins);
    req.init();
    /*
         * 默认不忽略空文件
         */
    Uploading up = UploadUnit.TYPE.born();
    Map<String, Object> map = up.parse(req, UploadingContext.create(tmps));
    assertEquals(1, map.size());
    TempFile tf = (TempFile) map.get("theF");
    assertEquals("_r_n.txt", tf.getSubmittedFileName());
    assertTrue(Streams.equals(Streams.fileIn(f), tf.getInputStream()));
}
Also used : TempFile(org.nutz.mvc.upload.TempFile) 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 12 with MockHttpServletRequest

use of org.nutz.mock.servlet.MockHttpServletRequest in project nutz by nutzam.

the class UploadingUnitTest method test_upload_ignore_null.

/**
     * 测试忽略空文件
     */
@Test
public void test_upload_ignore_null() throws Exception {
    MockHttpServletRequest req = Mock.servlet.request();
    req.setPathInfo("/nutz/junit/uploading");
    File txt = Files.findFile("org/nutz/mvc/upload/files/quick/abc.zdoc");
    File empty = Files.findFile("org/nutz/mvc/upload/files/empty.txt");
    MultipartInputStream ins = Mock.servlet.insmulti(charset);
    ins.append("abc", txt);
    ins.append("empty", empty);
    req.setInputStream(ins);
    req.init();
    /*
         * 默认不忽略空文件
         */
    Uploading up = UploadUnit.TYPE.born();
    Map<String, Object> map = up.parse(req, UploadingContext.create(tmps));
    assertEquals(2, map.size());
    TempFile txt2 = (TempFile) map.get("abc");
    TempFile empty2 = (TempFile) map.get("empty");
    assertEquals("abc.zdoc", txt2.getSubmittedFileName());
    assertTrue(Streams.equals(Streams.fileIn(txt), txt2.getInputStream()));
    assertEquals("empty.txt", empty2.getSubmittedFileName());
    assertTrue(Streams.equals(Streams.fileIn(empty), empty2.getInputStream()));
    /*
         * 设置忽略空文件
         */
    ins = Mock.servlet.insmulti(charset);
    ins.append("abc", txt);
    ins.append("empty", empty);
    req.setInputStream(ins);
    req.init();
    up = UploadUnit.TYPE.born();
    map = up.parse(req, UploadingContext.create(tmps).setIgnoreNull(true));
    assertEquals(1, map.size());
    txt2 = (TempFile) map.get("abc");
    empty2 = (TempFile) map.get("empty");
    assertEquals("abc.zdoc", txt2.getSubmittedFileName());
    assertTrue(Streams.equals(Streams.fileIn(txt), txt2.getInputStream()));
    assertNull(empty2);
}
Also used : TempFile(org.nutz.mvc.upload.TempFile) 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 13 with MockHttpServletRequest

use of org.nutz.mock.servlet.MockHttpServletRequest in project nutz by nutzam.

the class UploadingUnitTest method test_upload_1txt_3img.

@Test
public void test_upload_1txt_3img() throws Exception {
    MockHttpServletRequest req = Mock.servlet.request();
    req.setPathInfo("/nutz/junit/uploading");
    File txt = Files.findFile("org/nutz/mvc/upload/files/quick/abc.zdoc");
    File red = Files.findFile("org/nutz/mvc/upload/files/quick/red.png");
    File blue = Files.findFile("org/nutz/mvc/upload/files/quick/blue.png");
    File green = Files.findFile("org/nutz/mvc/upload/files/quick/green.png");
    MultipartInputStream ins = Mock.servlet.insmulti(charset);
    ins.append("abc", txt);
    ins.append("red", red);
    ins.append("blue", blue);
    ins.append("green", green);
    req.setInputStream(ins);
    req.init();
    Uploading up = UploadUnit.TYPE.born();
    Map<String, Object> map = up.parse(req, UploadingContext.create(tmps));
    assertEquals(4, map.size());
    TempFile txt2 = (TempFile) map.get("abc");
    TempFile red2 = (TempFile) map.get("red");
    TempFile blue2 = (TempFile) map.get("blue");
    TempFile green2 = (TempFile) map.get("green");
    assertEquals("abc.zdoc", txt2.getSubmittedFileName());
    assertTrue(Streams.equals(Streams.fileIn(txt), txt2.getInputStream()));
    assertEquals("red.png", red2.getSubmittedFileName());
    assertTrue(Streams.equals(Streams.fileIn(red), red2.getInputStream()));
    assertEquals("blue.png", blue2.getSubmittedFileName());
    assertTrue(Streams.equals(Streams.fileIn(blue), blue2.getInputStream()));
    assertEquals("green.png", green2.getSubmittedFileName());
    assertTrue(Streams.equals(Streams.fileIn(green), green2.getInputStream()));
}
Also used : TempFile(org.nutz.mvc.upload.TempFile) 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 MockHttpServletRequest

use of org.nutz.mock.servlet.MockHttpServletRequest in project nutz by nutzam.

the class UploadingUnitTest method test_upload_onlyQueryString.

@Test
public void test_upload_onlyQueryString() 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);
    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(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 15 with MockHttpServletRequest

use of org.nutz.mock.servlet.MockHttpServletRequest in project nutz by nutzam.

the class UploadingUnitTest method test_upload_special_char.

/**
     * 检查以下特殊字符作为参数
     */
@Test
public void test_upload_special_char() throws UploadException {
    String val = "sfsafsa<> ' \" & -- / \\ | $@sdsdsdsaaaaaaaaaaaaaa";
    /*
         * 准备请求对象
         */
    MockHttpServletRequest req = Mock.servlet.request();
    MultipartInputStream ins = Mock.servlet.insmulti("GBK");
    ins.append("abc", val);
    req.setInputStream(ins);
    req.init();
    /*
         * 执行上传
         */
    Uploading up = UploadUnit.TYPE.born();
    Map<String, Object> map = up.parse(req, UploadingContext.create(tmps).setCharset("GBK"));
    /*
         * 检查以下是不是 GBK 编码被解析成功
         */
    assertEquals(val, map.get("abc"));
    /*
         * 另一段字符串
         */
    val = "<p>$%&amp;&amp;*(^?jyjmy?876&amp;%4</p>";
    /*
         * 准备请求对象
         */
    ins = Mock.servlet.insmulti("GBK");
    ins.append("abc", val);
    req.setInputStream(ins);
    req.init();
    /*
         * 执行上传
         */
    map = up.parse(req, UploadingContext.create(tmps).setCharset("GBK"));
    /*
         * 检查以下是不是 GBK 编码被解析成功
         */
    assertEquals(val, 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)

Aggregations

MockHttpServletRequest (org.nutz.mock.servlet.MockHttpServletRequest)43 Test (org.junit.Test)42 Uploading (org.nutz.mvc.upload.Uploading)14 MultipartInputStream (org.nutz.mock.servlet.multipart.MultipartInputStream)11 File (java.io.File)9 TempFile (org.nutz.mvc.upload.TempFile)8 ParamInjector (org.nutz.mvc.adaptor.ParamInjector)3 SimpleDateFormat (java.text.SimpleDateFormat)2 List (java.util.List)1 Map (java.util.Map)1 ServletInputStream (javax.servlet.ServletInputStream)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Stopwatch (org.nutz.lang.Stopwatch)1 NutMap (org.nutz.lang.util.NutMap)1 ParamExtractor (org.nutz.mvc.adaptor.ParamExtractor)1 ObjectNaviNode (org.nutz.mvc.adaptor.injector.ObjectNaviNode)1 Atom (org.nutz.trans.Atom)1