Search in sources :

Example 6 with Uploading

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

the class UploadingUnitTest method test_limit_file_name_fail.

/**
     * 测试限制文件名:限制文件名称
     */
@Test(expected = UploadUnsupportedFileNameException.class)
public void test_limit_file_name_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).setNameFilter("^(.+[.])(gif|jpg)$"));
}
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 7 with Uploading

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

the class UploadingUnitTest method test_upload_chinese_filename.

/**
     * @author lAndRaxeE(landraxee@gmail.com)
     */
@Test
public void test_upload_chinese_filename() throws UploadException {
    /*
         * 准备模拟对象
         */
    MockHttpServletRequest req = Mock.servlet.request();
    File txt = Files.findFile("org/nutz/mvc/upload/files/quick/中文.txt");
    /*
         * 如果模拟上传时request使用 GBK 编码,用 GBK 来解码,应该会生成正确的文件名
         */
    req.setInputStream(Mock.servlet.insmulti("GBK", txt)).init();
    Uploading up = UploadUnit.TYPE.born();
    TempFile txt2 = (TempFile) up.parse(req, UploadingContext.create(tmps).setCharset("GBK")).get("F0");
    // 测试本地的默认编码是否是GBK,即模拟中文环境,本人环境为中文Windows XP
    // 在JVM参数中增加-Dfile.encoding=GBK即可设置好
    // assertEquals("GBK", Charset.defaultCharset().name());
    //
    // zzh: JUnit 测试必须在多数常用环境下可以比较方便的测试通过,经过这次修改,相信
    // 即可以达到这个目的,又可以测试出中文文件名的编码问题。如果没有其他的问题,在
    // 1.a.30 发布前,这段注释将被删除
    assertEquals("中文.txt", txt2.getSubmittedFileName());
    /*
         * 为了验证上传是否是真的可以解码,再次准备模拟 GBK 的输入流,但是这次将用 UTF-8 来解码
         */
    req.setInputStream(Mock.servlet.insmulti("GBK", txt)).init();
    up = UploadUnit.TYPE.born();
    txt2 = (TempFile) up.parse(req, UploadingContext.create(tmps)).get("F0");
    assertFalse("中文.txt".equals(txt2.getSubmittedFileName()));
}
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) Test(org.junit.Test)

Example 8 with Uploading

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

the class UploadingUnitTest method test_limit_file_size_ok.

/**
     * 测试限制文件大小:文件不超过尺寸,不会会抛错
     */
@Test
public void test_limit_file_size_ok() 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).setBufferSize(1024).setMaxFileSize(19152));
}
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 9 with Uploading

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

the class UploadingUnitTest method test_upload_text_with_newline_ending.

@Test
public void test_upload_text_with_newline_ending() throws UploadException {
    MockHttpServletRequest req = request().setInputStream(ins(Streams.fileIn("org/nutz/mvc/upload/unit/plaint.s")));
    req.setHeader("content-type", "multipart/form-data; boundary=------NutzMockHTTPBoundary@129021a3e21");
    req.setHeader("content-length", "200");
    req.setSession(session(context()));
    req.init();
    Uploading up = UploadUnit.TYPE.born();
    up.parse(req, UploadingContext.create(tmps));
}
Also used : MockHttpServletRequest(org.nutz.mock.servlet.MockHttpServletRequest) Uploading(org.nutz.mvc.upload.Uploading) Test(org.junit.Test)

Example 10 with Uploading

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

the class UploadingUnitTest method test_limit_file_size_fail.

/**
     * 测试限制文件大小:文件超出尺寸,会抛错
     */
@Test(expected = UploadOutOfSizeException.class)
public void test_limit_file_size_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();
    // 当设置为170,pass
    // 设置为171,fail 原因未明
    // zzh: FastUploading 的限制不是特别精确
    // 因为是按块读取的, 每次循环,要读1-3个块,所以尺寸的限制同 缓冲大小,也会有关系
    // 如果缓冲是 171, 可能正好读完
    up.parse(req, UploadingContext.create(tmps).setBufferSize(171).setMaxFileSize(18620));
}
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)

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