CookieBoxTest.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "CookieBox.h"
  2. #include <string>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. using namespace std;
  5. class CookieBoxTest:public CppUnit::TestFixture {
  6. CPPUNIT_TEST_SUITE(CookieBoxTest);
  7. CPPUNIT_TEST(testParse);
  8. CPPUNIT_TEST(testCriteriaFind);
  9. CPPUNIT_TEST_SUITE_END();
  10. private:
  11. public:
  12. void setUp() {
  13. }
  14. void testParse();
  15. void testCriteriaFind();
  16. };
  17. CPPUNIT_TEST_SUITE_REGISTRATION( CookieBoxTest );
  18. void CookieBoxTest::testParse() {
  19. CookieBox box;
  20. string str = "JSESSIONID=123456789; expires=Sat, 16-02-2006 19:38:00 JST; path=/; domain=localhost; secure";
  21. Cookie c;
  22. box.parse(c, str);
  23. CPPUNIT_ASSERT_EQUAL(string("JSESSIONID"), c.name);
  24. CPPUNIT_ASSERT_EQUAL(string("123456789"), c.value);
  25. CPPUNIT_ASSERT_EQUAL(string("Sat, 16-02-2006 19:38:00 JST"), c.expires);
  26. CPPUNIT_ASSERT_EQUAL(string("/"), c.path);
  27. CPPUNIT_ASSERT_EQUAL(string("localhost"), c.domain);
  28. CPPUNIT_ASSERT_EQUAL(true, c.secure);
  29. string str2 = "JSESSIONID=123456789";
  30. box.parse(c, str2);
  31. CPPUNIT_ASSERT_EQUAL(string("JSESSIONID"), c.name);
  32. CPPUNIT_ASSERT_EQUAL(string("123456789"), c.value);
  33. CPPUNIT_ASSERT_EQUAL(string(""), c.expires);
  34. CPPUNIT_ASSERT_EQUAL(string(""), c.path);
  35. CPPUNIT_ASSERT_EQUAL(string(""), c.domain);
  36. CPPUNIT_ASSERT_EQUAL(false, c.secure);
  37. }
  38. void CookieBoxTest::testCriteriaFind() {
  39. Cookie c1("SESSIONID1", "1", "", "/downloads", "rednoah.com", false);
  40. Cookie c2("SESSIONID2", "2", "", "/downloads", "rednoah.com", false);
  41. Cookie c3("USER", "user", "", "/home", "aria.rednoah.com", false);
  42. Cookie c4("PASS", "pass", "", "/downloads", "rednoah.com", true);
  43. CookieBox box;
  44. box.add(c1);
  45. box.add(c2);
  46. box.add(c3);
  47. box.add(c4);
  48. Cookies result1 = box.criteriaFind("rednoah.com", "/downloads", false);
  49. CPPUNIT_ASSERT_EQUAL(2, (int)result1.size());
  50. Cookies::iterator itr = result1.begin();
  51. CPPUNIT_ASSERT_EQUAL(string("SESSIONID1=1"), (*itr).toString());
  52. itr++;
  53. CPPUNIT_ASSERT_EQUAL(string("SESSIONID2=2"), (*itr).toString());
  54. result1 = box.criteriaFind("rednoah.com", "/downloads", true);
  55. CPPUNIT_ASSERT_EQUAL(3, (int)result1.size());
  56. itr = result1.begin();
  57. CPPUNIT_ASSERT_EQUAL(string("SESSIONID1=1"), (*itr).toString());
  58. itr++;
  59. CPPUNIT_ASSERT_EQUAL(string("SESSIONID2=2"), (*itr).toString());
  60. itr++;
  61. CPPUNIT_ASSERT_EQUAL(string("PASS=pass"), (*itr).toString());
  62. result1 = box.criteriaFind("aria.rednoah.com", "/", false);
  63. CPPUNIT_ASSERT_EQUAL(0, (int)result1.size());
  64. result1 = box.criteriaFind("aria.rednoah.com", "/home", false);
  65. CPPUNIT_ASSERT_EQUAL(1, (int)result1.size());
  66. itr = result1.begin();
  67. CPPUNIT_ASSERT_EQUAL(string("USER=user"), (*itr).toString());
  68. }