test_YoutubeDLCookieJar.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. if __name__ == '__main__':
  34. unittest.main()