test_cookiejar.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. """
  2. """
  3. """
  4. websocket - WebSocket client library for Python
  5. Copyright (C) 2010 Hiroki Ohtani(liris)
  6. This library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. """
  18. import unittest
  19. from websocket._cookiejar import SimpleCookieJar
  20. class CookieJarTest(unittest.TestCase):
  21. def testAdd(self):
  22. cookie_jar = SimpleCookieJar()
  23. cookie_jar.add("")
  24. self.assertFalse(cookie_jar.jar, "Cookie with no domain should not be added to the jar")
  25. cookie_jar = SimpleCookieJar()
  26. cookie_jar.add("a=b")
  27. self.assertFalse(cookie_jar.jar, "Cookie with no domain should not be added to the jar")
  28. cookie_jar = SimpleCookieJar()
  29. cookie_jar.add("a=b; domain=.abc")
  30. self.assertTrue(".abc" in cookie_jar.jar)
  31. cookie_jar = SimpleCookieJar()
  32. cookie_jar.add("a=b; domain=abc")
  33. self.assertTrue(".abc" in cookie_jar.jar)
  34. self.assertTrue("abc" not in cookie_jar.jar)
  35. cookie_jar = SimpleCookieJar()
  36. cookie_jar.add("a=b; c=d; domain=abc")
  37. self.assertEqual(cookie_jar.get("abc"), "a=b; c=d")
  38. cookie_jar = SimpleCookieJar()
  39. cookie_jar.add("a=b; c=d; domain=abc")
  40. cookie_jar.add("e=f; domain=abc")
  41. self.assertEqual(cookie_jar.get("abc"), "a=b; c=d; e=f")
  42. cookie_jar = SimpleCookieJar()
  43. cookie_jar.add("a=b; c=d; domain=abc")
  44. cookie_jar.add("e=f; domain=.abc")
  45. self.assertEqual(cookie_jar.get("abc"), "a=b; c=d; e=f")
  46. cookie_jar = SimpleCookieJar()
  47. cookie_jar.add("a=b; c=d; domain=abc")
  48. cookie_jar.add("e=f; domain=xyz")
  49. self.assertEqual(cookie_jar.get("abc"), "a=b; c=d")
  50. self.assertEqual(cookie_jar.get("xyz"), "e=f")
  51. self.assertEqual(cookie_jar.get("something"), "")
  52. def testSet(self):
  53. cookie_jar = SimpleCookieJar()
  54. cookie_jar.set("a=b")
  55. self.assertFalse(cookie_jar.jar, "Cookie with no domain should not be added to the jar")
  56. cookie_jar = SimpleCookieJar()
  57. cookie_jar.set("a=b; domain=.abc")
  58. self.assertTrue(".abc" in cookie_jar.jar)
  59. cookie_jar = SimpleCookieJar()
  60. cookie_jar.set("a=b; domain=abc")
  61. self.assertTrue(".abc" in cookie_jar.jar)
  62. self.assertTrue("abc" not in cookie_jar.jar)
  63. cookie_jar = SimpleCookieJar()
  64. cookie_jar.set("a=b; c=d; domain=abc")
  65. self.assertEqual(cookie_jar.get("abc"), "a=b; c=d")
  66. cookie_jar = SimpleCookieJar()
  67. cookie_jar.set("a=b; c=d; domain=abc")
  68. cookie_jar.set("e=f; domain=abc")
  69. self.assertEqual(cookie_jar.get("abc"), "e=f")
  70. cookie_jar = SimpleCookieJar()
  71. cookie_jar.set("a=b; c=d; domain=abc")
  72. cookie_jar.set("e=f; domain=.abc")
  73. self.assertEqual(cookie_jar.get("abc"), "e=f")
  74. cookie_jar = SimpleCookieJar()
  75. cookie_jar.set("a=b; c=d; domain=abc")
  76. cookie_jar.set("e=f; domain=xyz")
  77. self.assertEqual(cookie_jar.get("abc"), "a=b; c=d")
  78. self.assertEqual(cookie_jar.get("xyz"), "e=f")
  79. self.assertEqual(cookie_jar.get("something"), "")
  80. def testGet(self):
  81. cookie_jar = SimpleCookieJar()
  82. cookie_jar.set("a=b; c=d; domain=abc.com")
  83. self.assertEqual(cookie_jar.get("abc.com"), "a=b; c=d")
  84. self.assertEqual(cookie_jar.get("x.abc.com"), "a=b; c=d")
  85. self.assertEqual(cookie_jar.get("abc.com.es"), "")
  86. self.assertEqual(cookie_jar.get("xabc.com"), "")
  87. cookie_jar.set("a=b; c=d; domain=.abc.com")
  88. self.assertEqual(cookie_jar.get("abc.com"), "a=b; c=d")
  89. self.assertEqual(cookie_jar.get("x.abc.com"), "a=b; c=d")
  90. self.assertEqual(cookie_jar.get("abc.com.es"), "")
  91. self.assertEqual(cookie_jar.get("xabc.com"), "")