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