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