You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.9 KiB

  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. from __future__ import unicode_literals
  4. import os
  5. import re
  6. import sys
  7. import tempfile
  8. import unittest
  9. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  10. from youtube_dl.utils import YoutubeDLCookieJar
  11. class TestYoutubeDLCookieJar(unittest.TestCase):
  12. def test_keep_session_cookies(self):
  13. cookiejar = YoutubeDLCookieJar('./test/testdata/cookies/session_cookies.txt')
  14. cookiejar.load(ignore_discard=True, ignore_expires=True)
  15. tf = tempfile.NamedTemporaryFile(delete=False)
  16. try:
  17. cookiejar.save(filename=tf.name, ignore_discard=True, ignore_expires=True)
  18. temp = tf.read().decode('utf-8')
  19. self.assertTrue(re.search(
  20. r'www\.foobar\.foobar\s+FALSE\s+/\s+TRUE\s+0\s+YoutubeDLExpiresEmpty\s+YoutubeDLExpiresEmptyValue', temp))
  21. self.assertTrue(re.search(
  22. r'www\.foobar\.foobar\s+FALSE\s+/\s+TRUE\s+0\s+YoutubeDLExpires0\s+YoutubeDLExpires0Value', temp))
  23. finally:
  24. tf.close()
  25. os.remove(tf.name)
  26. def test_strip_httponly_prefix(self):
  27. cookiejar = YoutubeDLCookieJar('./test/testdata/cookies/httponly_cookies.txt')
  28. cookiejar.load(ignore_discard=True, ignore_expires=True)
  29. def assert_cookie_has_value(key):
  30. self.assertEqual(cookiejar._cookies['www.foobar.foobar']['/'][key].value, key + '_VALUE')
  31. assert_cookie_has_value('HTTPONLY_COOKIE')
  32. assert_cookie_has_value('JS_ACCESSIBLE_COOKIE')
  33. def test_malformed_cookies(self):
  34. cookiejar = YoutubeDLCookieJar('./test/testdata/cookies/malformed_cookies.txt')
  35. cookiejar.load(ignore_discard=True, ignore_expires=True)
  36. # Cookies should be empty since all malformed cookie file entries
  37. # will be ignored
  38. self.assertFalse(cookiejar._cookies)
  39. if __name__ == '__main__':
  40. unittest.main()