[ie/tiktok] Extract all web formats (#9960)
[yt-dlp3.git] / test / test_YoutubeDLCookieJar.py
blobfdb9baee59cb733241c750fb838a404112e0d43a
1 #!/usr/bin/env python3
3 # Allow direct execution
4 import os
5 import sys
6 import unittest
8 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
11 import re
12 import tempfile
14 from yt_dlp.cookies import YoutubeDLCookieJar
17 class TestYoutubeDLCookieJar(unittest.TestCase):
18 def test_keep_session_cookies(self):
19 cookiejar = YoutubeDLCookieJar('./test/testdata/cookies/session_cookies.txt')
20 cookiejar.load()
21 tf = tempfile.NamedTemporaryFile(delete=False)
22 try:
23 cookiejar.save(filename=tf.name)
24 temp = tf.read().decode()
25 self.assertTrue(re.search(
26 r'www\.foobar\.foobar\s+FALSE\s+/\s+TRUE\s+0\s+YoutubeDLExpiresEmpty\s+YoutubeDLExpiresEmptyValue', temp))
27 self.assertTrue(re.search(
28 r'www\.foobar\.foobar\s+FALSE\s+/\s+TRUE\s+0\s+YoutubeDLExpires0\s+YoutubeDLExpires0Value', temp))
29 finally:
30 tf.close()
31 os.remove(tf.name)
33 def test_strip_httponly_prefix(self):
34 cookiejar = YoutubeDLCookieJar('./test/testdata/cookies/httponly_cookies.txt')
35 cookiejar.load()
37 def assert_cookie_has_value(key):
38 self.assertEqual(cookiejar._cookies['www.foobar.foobar']['/'][key].value, key + '_VALUE')
40 assert_cookie_has_value('HTTPONLY_COOKIE')
41 assert_cookie_has_value('JS_ACCESSIBLE_COOKIE')
43 def test_malformed_cookies(self):
44 cookiejar = YoutubeDLCookieJar('./test/testdata/cookies/malformed_cookies.txt')
45 cookiejar.load()
46 # Cookies should be empty since all malformed cookie file entries
47 # will be ignored
48 self.assertFalse(cookiejar._cookies)
50 def test_get_cookie_header(self):
51 cookiejar = YoutubeDLCookieJar('./test/testdata/cookies/httponly_cookies.txt')
52 cookiejar.load()
53 header = cookiejar.get_cookie_header('https://www.foobar.foobar')
54 self.assertIn('HTTPONLY_COOKIE', header)
56 def test_get_cookies_for_url(self):
57 cookiejar = YoutubeDLCookieJar('./test/testdata/cookies/session_cookies.txt')
58 cookiejar.load()
59 cookies = cookiejar.get_cookies_for_url('https://www.foobar.foobar/')
60 self.assertEqual(len(cookies), 2)
61 cookies = cookiejar.get_cookies_for_url('https://foobar.foobar/')
62 self.assertFalse(cookies)
65 if __name__ == '__main__':
66 unittest.main()