Search in sources :

Example 16 with MockHttpServletRequest

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

the class ObjectPairInjectorTest method test_date.

@Test
public void test_date() {
    // 准备数据
    MockHttpServletRequest req = Mock.servlet.request();
    req.setParameter("date", "2010-01-01");
    // 执行
    MvcTestPojo pojo = (MvcTestPojo) inj().get(null, req, null, null);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    System.out.println(sdf.format(pojo.date));
    assertEquals("2010-01-01", sdf.format(pojo.date));
    req.setParameter("date", "");
    // 执行
    MvcTestPojo pojoNull = (MvcTestPojo) inj().get(null, req, null, null);
    assertEquals(null, pojoNull.date);
}
Also used : MockHttpServletRequest(org.nutz.mock.servlet.MockHttpServletRequest) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 17 with MockHttpServletRequest

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

the class ObjectPairInjectorTest method test_string_to_string.

@Test
public void test_string_to_string() {
    // 准备数据
    MockHttpServletRequest req = Mock.servlet.request();
    req.setParameterValues("str", Lang.array("A"));
    // 执行
    MvcTestPojo pojo = (MvcTestPojo) inj().get(null, req, null, null);
    // 检测
    assertEquals("A", pojo.str);
}
Also used : MockHttpServletRequest(org.nutz.mock.servlet.MockHttpServletRequest) Test(org.junit.Test)

Example 18 with MockHttpServletRequest

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

the class ObjectPairInjectorTest method test_array_to_string.

@Test
public void test_array_to_string() {
    // 准备数据
    MockHttpServletRequest req = Mock.servlet.request();
    req.setParameterValues("str", Lang.array("A", "B", "C"));
    // 执行
    MvcTestPojo pojo = (MvcTestPojo) inj().get(null, req, null, null);
    // 检测
    assertEquals("A,B,C", pojo.str);
}
Also used : MockHttpServletRequest(org.nutz.mock.servlet.MockHttpServletRequest) Test(org.junit.Test)

Example 19 with MockHttpServletRequest

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

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

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