]>
Commit | Line | Data |
---|---|---|
cc52de43 | 1 | #!/usr/bin/env python3 |
54007a45 | 2 | |
3 | # Allow direct execution | |
9e02c2c7 | 4 | import os |
9e02c2c7 | 5 | import sys |
9e02c2c7 | 6 | import unittest |
f8271158 | 7 | |
9e02c2c7 S |
8 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
9 | ||
54007a45 | 10 | |
11 | import re | |
12 | import tempfile | |
13 | ||
7a5c1cfe | 14 | from yt_dlp.utils import YoutubeDLCookieJar |
9e02c2c7 S |
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) | |
0f06bcd7 | 24 | temp = tf.read().decode() |
9e02c2c7 S |
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 | ||
e7e62441 | 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 | ||
042b6649 S |
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') | |
e7e62441 | 42 | |
c380cc28 S |
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 | ||
9e02c2c7 S |
50 | |
51 | if __name__ == '__main__': | |
52 | unittest.main() |