]>
Commit | Line | Data |
---|---|---|
cc52de43 | 1 | #!/usr/bin/env python3 |
9e02c2c7 S |
2 | # coding: utf-8 |
3 | ||
4 | from __future__ import unicode_literals | |
5 | ||
6 | import os | |
7 | import re | |
8 | import sys | |
9 | import tempfile | |
10 | import unittest | |
11 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
12 | ||
7a5c1cfe | 13 | from yt_dlp.utils import YoutubeDLCookieJar |
9e02c2c7 S |
14 | |
15 | ||
16 | class TestYoutubeDLCookieJar(unittest.TestCase): | |
17 | def test_keep_session_cookies(self): | |
18 | cookiejar = YoutubeDLCookieJar('./test/testdata/cookies/session_cookies.txt') | |
19 | cookiejar.load(ignore_discard=True, ignore_expires=True) | |
20 | tf = tempfile.NamedTemporaryFile(delete=False) | |
21 | try: | |
22 | cookiejar.save(filename=tf.name, ignore_discard=True, ignore_expires=True) | |
23 | temp = tf.read().decode('utf-8') | |
24 | self.assertTrue(re.search( | |
25 | r'www\.foobar\.foobar\s+FALSE\s+/\s+TRUE\s+0\s+YoutubeDLExpiresEmpty\s+YoutubeDLExpiresEmptyValue', temp)) | |
26 | self.assertTrue(re.search( | |
27 | r'www\.foobar\.foobar\s+FALSE\s+/\s+TRUE\s+0\s+YoutubeDLExpires0\s+YoutubeDLExpires0Value', temp)) | |
28 | finally: | |
29 | tf.close() | |
30 | os.remove(tf.name) | |
31 | ||
e7e62441 | 32 | def test_strip_httponly_prefix(self): |
33 | cookiejar = YoutubeDLCookieJar('./test/testdata/cookies/httponly_cookies.txt') | |
34 | cookiejar.load(ignore_discard=True, ignore_expires=True) | |
35 | ||
042b6649 S |
36 | def assert_cookie_has_value(key): |
37 | self.assertEqual(cookiejar._cookies['www.foobar.foobar']['/'][key].value, key + '_VALUE') | |
38 | ||
39 | assert_cookie_has_value('HTTPONLY_COOKIE') | |
40 | assert_cookie_has_value('JS_ACCESSIBLE_COOKIE') | |
e7e62441 | 41 | |
c380cc28 S |
42 | def test_malformed_cookies(self): |
43 | cookiejar = YoutubeDLCookieJar('./test/testdata/cookies/malformed_cookies.txt') | |
44 | cookiejar.load(ignore_discard=True, ignore_expires=True) | |
45 | # Cookies should be empty since all malformed cookie file entries | |
46 | # will be ignored | |
47 | self.assertFalse(cookiejar._cookies) | |
48 | ||
9e02c2c7 S |
49 | |
50 | if __name__ == '__main__': | |
51 | unittest.main() |