Search in sources :

Example 21 with MockHttpServletRequest

use of org.nutz.mock.servlet.MockHttpServletRequest 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 22 with MockHttpServletRequest

use of org.nutz.mock.servlet.MockHttpServletRequest 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 23 with MockHttpServletRequest

use of org.nutz.mock.servlet.MockHttpServletRequest 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)

Example 24 with MockHttpServletRequest

use of org.nutz.mock.servlet.MockHttpServletRequest 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 25 with MockHttpServletRequest

use of org.nutz.mock.servlet.MockHttpServletRequest 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)

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