Package org.nutz.mock.servlet

Examples of org.nutz.mock.servlet.MockHttpServletRequest


    }

    @Test
    public void test_normal_int() {
        // 准备数据
        MockHttpServletRequest req = Mock.servlet.request();
        req.setParameter("abc", "1");

        // 执行 & 检测
        assertEquals(Integer.valueOf(1), inj("abc", int.class, req));
    }
View Full Code Here


    @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,
View Full Code Here

    @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.getMeta().getFileLocalName());

        /*
         * 为了验证上传是否是真的可以解码,再次准备模拟 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.getMeta().getFileLocalName()));
    }
View Full Code Here

        assertFalse("中文.txt".equals(txt2.getMeta().getFileLocalName()));
    }

    @Test
    public void test_upload_1txt_3img() throws UploadException {
        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");
View Full Code Here

    }

    @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"));
View Full Code Here

        assertEquals(null, map.get("null"));
    }

    @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"));
    }
View Full Code Here

        assertEquals(null, map.get("null"));
    }

    @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")).getMeta()
                                                     .getFileLocalPath());
View Full Code Here

                                                     .getFileLocalPath());
    }

    @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));
    }
View Full Code Here

     * 根据 Issue 272,如果为空串,原生类型的外覆类应该返回 null
     */
    @Test
    public void test_balnk_param_to_number() {
        // 准备数据
        MockHttpServletRequest req = Mock.servlet.request();
        req.setParameter("pojo.longValue", "  ");
        req.setParameter("pojo.num", "  ");

        // 执行
        MvcTestPojo pojo = (MvcTestPojo) inj().get(null, req, null, null);

        // 检测
View Full Code Here

     * 这个测试将检验在 HTTP 请求中,如果存在多个参数同名的情况,本注入器能否正确处理
     */
    @Test
    public void test_duplicated_name_params() {
        // 准备数据
        MockHttpServletRequest req = Mock.servlet.request();
        req.setParameter("pojo.num", 23);
        req.setParameterValues("pojo.names", Lang.array("A", "B", "C"));

        // 执行
        MvcTestPojo pojo = (MvcTestPojo) inj().get(null, req, null, null);

        // 检测
View Full Code Here

TOP

Related Classes of org.nutz.mock.servlet.MockHttpServletRequest

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.