CookieParserTest.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "CookieParser.h"
  2. #include <fstream>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. namespace aria2 {
  5. class CookieParserTest:public CppUnit::TestFixture {
  6. CPPUNIT_TEST_SUITE(CookieParserTest);
  7. CPPUNIT_TEST(testParse);
  8. CPPUNIT_TEST(testParse_file);
  9. CPPUNIT_TEST_SUITE_END();
  10. private:
  11. public:
  12. void setUp() {
  13. }
  14. void testParse();
  15. void testParse_file();
  16. };
  17. CPPUNIT_TEST_SUITE_REGISTRATION( CookieParserTest );
  18. void CookieParserTest::testParse()
  19. {
  20. std::string str = "JSESSIONID=123456789; expires=Sun, 10-Jun-2007 11:00:00 GMT; path=/; domain=localhost; secure";
  21. Cookie c = CookieParser().parse(str);
  22. CPPUNIT_ASSERT(c.good());
  23. CPPUNIT_ASSERT_EQUAL(std::string("JSESSIONID"), c.getName());
  24. CPPUNIT_ASSERT_EQUAL(std::string("123456789"), c.getValue());
  25. CPPUNIT_ASSERT_EQUAL((time_t)1181473200, c.getExpiry());
  26. CPPUNIT_ASSERT_EQUAL(std::string("/"), c.getPath());
  27. CPPUNIT_ASSERT_EQUAL(std::string(".localhost.local"), c.getDomain());
  28. CPPUNIT_ASSERT_EQUAL(true, c.isSecureCookie());
  29. CPPUNIT_ASSERT_EQUAL(false, c.isSessionCookie());
  30. std::string str2 = "JSESSIONID=123456789";
  31. c = CookieParser().parse(str2, "default.domain", "/default/path");
  32. CPPUNIT_ASSERT(c.good());
  33. CPPUNIT_ASSERT_EQUAL(std::string("JSESSIONID"), c.getName());
  34. CPPUNIT_ASSERT_EQUAL(std::string("123456789"), c.getValue());
  35. CPPUNIT_ASSERT_EQUAL((time_t)0, c.getExpiry());
  36. CPPUNIT_ASSERT_EQUAL(std::string(".default.domain"), c.getDomain());
  37. CPPUNIT_ASSERT_EQUAL(std::string("/default/path"), c.getPath());
  38. CPPUNIT_ASSERT_EQUAL(false, c.isSecureCookie());
  39. CPPUNIT_ASSERT_EQUAL(true, c.isSessionCookie());
  40. std::string str3 = "";
  41. c = CookieParser().parse(str3);
  42. CPPUNIT_ASSERT(!c.good());
  43. #ifndef __MINGW32__
  44. std::string str4 = "UID=300; expires=Wed, 01-Jan-1960 00:00:00 GMT";
  45. time_t expire_time = (time_t) -315619200;
  46. #else
  47. std::string str4 = "UID=300; expires=Wed, 01-Jan-1970 00:00:01 GMT";
  48. time_t expire_time = (time_t) 1;
  49. #endif
  50. c = CookieParser().parse(str4, "localhost", "/");
  51. CPPUNIT_ASSERT(c.good());
  52. CPPUNIT_ASSERT(!c.isSessionCookie());
  53. CPPUNIT_ASSERT_EQUAL(expire_time, c.getExpiry());
  54. std::string str5 = "k=v; expires=Sun, 10-Jun-07 11:00:00 GMT";
  55. c = CookieParser().parse(str5);
  56. CPPUNIT_ASSERT(c.good());
  57. CPPUNIT_ASSERT_EQUAL(std::string("k"), c.getName());
  58. CPPUNIT_ASSERT_EQUAL(std::string("v"), c.getValue());
  59. CPPUNIT_ASSERT_EQUAL((time_t)1181473200, c.getExpiry());
  60. CPPUNIT_ASSERT_EQUAL(std::string(""), c.getDomain());
  61. CPPUNIT_ASSERT_EQUAL(std::string(""), c.getPath());
  62. CPPUNIT_ASSERT(!c.isSecureCookie());
  63. CPPUNIT_ASSERT(!c.isSessionCookie());
  64. }
  65. void CookieParserTest::testParse_file()
  66. {
  67. std::ifstream f("cookietest.txt");
  68. Cookies cookies = CookieParser().parse(f);
  69. CPPUNIT_ASSERT_EQUAL((int32_t)3, (int32_t)cookies.size());
  70. Cookie c = cookies[0];
  71. CPPUNIT_ASSERT_EQUAL(std::string("JSESSIONID"), c.getName());
  72. CPPUNIT_ASSERT_EQUAL(std::string("123456789"), c.getValue());
  73. c = cookies[1];
  74. CPPUNIT_ASSERT_EQUAL(std::string("user"), c.getName());
  75. CPPUNIT_ASSERT_EQUAL(std::string("me"), c.getValue());
  76. c = cookies[2];
  77. CPPUNIT_ASSERT_EQUAL(std::string("passwd"), c.getName());
  78. CPPUNIT_ASSERT_EQUAL(std::string("secret"), c.getValue());
  79. }
  80. } // namespace aria2