]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/env python3 | |
2 | ||
3 | # Allow direct execution | |
4 | import os | |
5 | import sys | |
6 | import unittest | |
7 | ||
8 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
9 | ||
10 | ||
11 | import re | |
12 | import tempfile | |
13 | ||
14 | from yt_dlp.utils import YoutubeDLCookieJar | |
15 | ||
16 | ||
17 | class TestYoutubeDLCookieJar(unittest.TestCase): | |
18 | def test_keep_session_cookies(self): | |
19 | cookiejar = YoutubeDLCookieJar('./test/testdata/cookies/session_cookies.txt') | |
20 | cookiejar.load(ignore_discard=True, ignore_expires=True) | |
21 | tf = tempfile.NamedTemporaryFile(delete=False) | |
22 | try: | |
23 | cookiejar.save(filename=tf.name, ignore_discard=True, ignore_expires=True) | |
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) | |
32 | ||
33 | def test_strip_httponly_prefix(self): | |
34 | cookiejar = YoutubeDLCookieJar('./test/testdata/cookies/httponly_cookies.txt') | |
35 | cookiejar.load(ignore_discard=True, ignore_expires=True) | |
36 | ||
37 | def assert_cookie_has_value(key): | |
38 | self.assertEqual(cookiejar._cookies['www.foobar.foobar']['/'][key].value, key + '_VALUE') | |
39 | ||
40 | assert_cookie_has_value('HTTPONLY_COOKIE') | |
41 | assert_cookie_has_value('JS_ACCESSIBLE_COOKIE') | |
42 | ||
43 | def test_malformed_cookies(self): | |
44 | cookiejar = YoutubeDLCookieJar('./test/testdata/cookies/malformed_cookies.txt') | |
45 | cookiejar.load(ignore_discard=True, ignore_expires=True) | |
46 | # Cookies should be empty since all malformed cookie file entries | |
47 | # will be ignored | |
48 | self.assertFalse(cookiejar._cookies) | |
49 | ||
50 | ||
51 | if __name__ == '__main__': | |
52 | unittest.main() |