// ----以下测试普通的post数据 @Test public void test普通的post数据() throws Exception { String temp = "product=mail163&" + "savelogin=&" + "url=http%3A%2F%2Fmail.163.com%2Ferrorpage%2Ferr_163.htm&" + "username=2005081097&" + "password=013579"; String normal_con_type = "application/x-www-form-urlencoded"; byte[] bytes = temp.getBytes("US-ASCII"); HttpServletRequest request = new MockHttpServletRequest(bytes, normal_con_type); Object o = null; o = LazyParser.getParameter(request, "product"); assertEquals("mail163", (String) o); o = LazyParser.getParameter(request, "savelogin"); assertNull(o); o = LazyParser.getParameter(request, "url"); assertEquals("http://mail.163.com/errorpage/err_163.htm", (String) o); List<FileItem> fileItemList = LazyParser.getFileItemsFromRequest(request); assertTrue(fileItemList.size() == 0); Map map = LazyParser.getParameterMap(request); assertEquals(5, map.size()); assertEquals("mail163", map.get("product")); assertEquals("http://mail.163.com/errorpage/err_163.htm", map.get("url")); Set<String> paramNames = LazyParser.getParameterNames(request); assertEquals(5, paramNames.size()); assertTrue(paramNames.contains("product")); LazyParser.release(); }
/** 测试上传的文件类型不对 */ @Test public void test上传的文件类型不对() throws Exception { LazyParser.setFileExts(new String[] {".jpg", ".tab"}); String temp = "-----1234\r\n" + "Content-Disposition: form-data; name=\"file\"; filename=\"foo.png\"\r\n" + "Content-Type: text/whatever\r\n" + "\r\n" + "This is the content of the file\n" + "\r\n" + "-----1234\r\n" + "Content-Disposition: form-data; name=\"field\"\r\n" + "\r\n" + "fieldValue\r\n" + "-----1234\r\n" + "Content-Disposition: form-data; name=\"multi\"\r\n" + "\r\n" + "value1\r\n" + "-----1234\r\n"; byte[] bytes = temp.getBytes("US-ASCII"); HttpServletRequest request = new MockHttpServletRequest(bytes, CONTENT_TYPE); Object o = null; try { o = LazyParser.getParameter(request, "file"); } catch (Exception e) { assertTrue(e instanceof FileTypeInvalidException); } assertNull(o); LazyParser.release(); }
/** 测试getParameter的正常情况 */ @Test public void testGetParameter_正常情况_上传文件() throws Exception { byte[] bytes = content.getBytes("US-ASCII"); HttpServletRequest request = new MockHttpServletRequest(bytes, CONTENT_TYPE); // 文件域 Object o = LazyParser.getParameter(request, "file"); assertTrue(o instanceof FileItem); FileItem f = (FileItem) o; assertEquals("This is the content of the file\n", new String(f.getBout().toByteArray())); assertEquals("text/whatever", f.getContentType()); assertEquals("foo.tab", f.getFileName()); // 普通域 o = LazyParser.getParameter(request, "field"); assertTrue(o instanceof FileItem); f = (FileItem) o; assertEquals("fieldValue", f.getValue()); LazyParser.release(); }
@Test public void test上传的文件大小超过限制大小() throws Exception { LazyParser.setMaxBufferSize(10); // 允许上传的文件大小为10byte byte[] bytes = content.getBytes("US-ASCII"); HttpServletRequest request = new MockHttpServletRequest(bytes, CONTENT_TYPE); Object o = null; try { o = LazyParser.getParameter(request, "file"); } catch (Exception e) { assertTrue(e instanceof FileSizeInvalidException); } assertNull(o); LazyParser.release(); }