logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

test_YoutubeDLCookieJar.py (2670B)


  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. def test_get_cookie_header(self):
  40. cookiejar = YoutubeDLCookieJar('./test/testdata/cookies/httponly_cookies.txt')
  41. cookiejar.load(ignore_discard=True, ignore_expires=True)
  42. header = cookiejar.get_cookie_header('https://www.foobar.foobar')
  43. self.assertIn('HTTPONLY_COOKIE', header)
  44. def test_get_cookies_for_url(self):
  45. cookiejar = YoutubeDLCookieJar('./test/testdata/cookies/session_cookies.txt')
  46. cookiejar.load(ignore_discard=True, ignore_expires=True)
  47. cookies = cookiejar.get_cookies_for_url('https://www.foobar.foobar/')
  48. self.assertEqual(len(cookies), 2)
  49. cookies = cookiejar.get_cookies_for_url('https://foobar.foobar/')
  50. self.assertFalse(cookies)
  51. if __name__ == '__main__':
  52. unittest.main()