Esempio n. 1
0
  // ----以下测试普通的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();
  }
Esempio n. 2
0
 /*
  * 测试getFileItemsFromRequest
  */
 @Test
 public void testGetFileItemFromRequest_正常情况() throws Exception {
   byte[] bytes = content.getBytes("US-ASCII");
   HttpServletRequest request = new MockHttpServletRequest(bytes, CONTENT_TYPE);
   List<FileItem> fileItem = LazyParser.getFileItemsFromRequest(request);
   assertEquals(1, fileItem.size());
   FileItem f = fileItem.get(0);
   assertEquals("This is the content of the file\n", new String(f.getBout().toByteArray()));
   assertEquals("text/whatever", f.getContentType());
   assertEquals("foo.tab", f.getFileName());
   LazyParser.release();
 }
Esempio n. 3
0
 @Test
 public void test上传的文件多余1个() throws Exception {
   String temp =
       "-----1234\r\n"
           + "Content-Disposition: form-data; name=\"file\"; filename=\"foo.jpg\"\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=\"file2\"; filename=\"foo2.jpg\"\r\n"
           + "Content-Type: text/whatever\r\n"
           + "\r\n"
           + "This is the file2 content\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 {
     LazyParser.setMaxBufferSize(1000);
     o = LazyParser.getFileItemsFromRequest(request);
   } catch (Exception e) {
     assertTrue(e instanceof FileNumInvalidException);
   }
   assertNull(o);
   LazyParser.release();
 }