CookieParserTest.cc 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "CookieParser.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. namespace aria2 {
  4. class CookieParserTest:public CppUnit::TestFixture {
  5. CPPUNIT_TEST_SUITE(CookieParserTest);
  6. CPPUNIT_TEST(testParse);
  7. CPPUNIT_TEST_SUITE_END();
  8. public:
  9. void testParse();
  10. };
  11. CPPUNIT_TEST_SUITE_REGISTRATION( CookieParserTest );
  12. void CookieParserTest::testParse()
  13. {
  14. std::string str = "JSESSIONID=123456789; expires=Sun, 10-Jun-2007 11:00:00 GMT; path=/; domain=localhost; secure";
  15. Cookie c = CookieParser().parse(str, "", "");
  16. CPPUNIT_ASSERT(c.good());
  17. CPPUNIT_ASSERT_EQUAL(std::string("JSESSIONID"), c.getName());
  18. CPPUNIT_ASSERT_EQUAL(std::string("123456789"), c.getValue());
  19. CPPUNIT_ASSERT_EQUAL((time_t)1181473200, c.getExpiry());
  20. CPPUNIT_ASSERT_EQUAL(std::string("/"), c.getPath());
  21. CPPUNIT_ASSERT_EQUAL(std::string(".localhost.local"), c.getDomain());
  22. CPPUNIT_ASSERT_EQUAL(true, c.isSecureCookie());
  23. CPPUNIT_ASSERT_EQUAL(false, c.isSessionCookie());
  24. std::string str2 = "JSESSIONID=123456789";
  25. c = CookieParser().parse(str2, "default.domain", "/default/path");
  26. CPPUNIT_ASSERT(c.good());
  27. CPPUNIT_ASSERT_EQUAL(std::string("JSESSIONID"), c.getName());
  28. CPPUNIT_ASSERT_EQUAL(std::string("123456789"), c.getValue());
  29. CPPUNIT_ASSERT_EQUAL((time_t)0, c.getExpiry());
  30. CPPUNIT_ASSERT_EQUAL(std::string("default.domain"), c.getDomain());
  31. CPPUNIT_ASSERT_EQUAL(std::string("/default/path"), c.getPath());
  32. CPPUNIT_ASSERT_EQUAL(false, c.isSecureCookie());
  33. CPPUNIT_ASSERT_EQUAL(true, c.isSessionCookie());
  34. std::string str3 = "";
  35. c = CookieParser().parse(str3, "", "");
  36. CPPUNIT_ASSERT(!c.good());
  37. #ifndef __MINGW32__
  38. std::string str4 = "UID=300; expires=Wed, 01-Jan-1960 00:00:00 GMT";
  39. time_t expire_time = (time_t) -315619200;
  40. #else
  41. std::string str4 = "UID=300; expires=Wed, 01-Jan-1970 00:00:01 GMT";
  42. time_t expire_time = (time_t) 1;
  43. #endif
  44. c = CookieParser().parse(str4, "localhost", "/");
  45. CPPUNIT_ASSERT(c.good());
  46. CPPUNIT_ASSERT(!c.isSessionCookie());
  47. CPPUNIT_ASSERT_EQUAL(expire_time, c.getExpiry());
  48. std::string str5 = "k=v; expires=Sun, 10-Jun-07 11:00:00 GMT";
  49. c = CookieParser().parse(str5, "", "");
  50. CPPUNIT_ASSERT(c.good());
  51. CPPUNIT_ASSERT_EQUAL(std::string("k"), c.getName());
  52. CPPUNIT_ASSERT_EQUAL(std::string("v"), c.getValue());
  53. CPPUNIT_ASSERT_EQUAL((time_t)1181473200, c.getExpiry());
  54. CPPUNIT_ASSERT_EQUAL(std::string(""), c.getDomain());
  55. CPPUNIT_ASSERT_EQUAL(std::string(""), c.getPath());
  56. CPPUNIT_ASSERT(!c.isSecureCookie());
  57. CPPUNIT_ASSERT(!c.isSessionCookie());
  58. }
  59. } // namespace aria2