2021-06-03 11:43:42 +02:00
|
|
|
#!/usr/bin/env python3
|
2022-06-24 13:06:16 +02:00
|
|
|
|
|
|
|
# Allow direct execution
|
2018-12-09 16:56:33 +01:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import unittest
|
2022-04-12 00:32:57 +02:00
|
|
|
|
2018-12-09 16:56:33 +01:00
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
|
2022-06-24 13:06:16 +02:00
|
|
|
|
|
|
|
import re
|
|
|
|
import tempfile
|
|
|
|
|
2023-05-27 09:08:19 +02:00
|
|
|
from yt_dlp.cookies import YoutubeDLCookieJar
|
2018-12-09 16:56:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestYoutubeDLCookieJar(unittest.TestCase):
|
|
|
|
def test_keep_session_cookies(self):
|
|
|
|
cookiejar = YoutubeDLCookieJar('./test/testdata/cookies/session_cookies.txt')
|
|
|
|
cookiejar.load(ignore_discard=True, ignore_expires=True)
|
|
|
|
tf = tempfile.NamedTemporaryFile(delete=False)
|
|
|
|
try:
|
|
|
|
cookiejar.save(filename=tf.name, ignore_discard=True, ignore_expires=True)
|
2022-05-09 13:54:28 +02:00
|
|
|
temp = tf.read().decode()
|
2018-12-09 16:56:33 +01:00
|
|
|
self.assertTrue(re.search(
|
|
|
|
r'www\.foobar\.foobar\s+FALSE\s+/\s+TRUE\s+0\s+YoutubeDLExpiresEmpty\s+YoutubeDLExpiresEmptyValue', temp))
|
|
|
|
self.assertTrue(re.search(
|
|
|
|
r'www\.foobar\.foobar\s+FALSE\s+/\s+TRUE\s+0\s+YoutubeDLExpires0\s+YoutubeDLExpires0Value', temp))
|
|
|
|
finally:
|
|
|
|
tf.close()
|
|
|
|
os.remove(tf.name)
|
|
|
|
|
2019-03-03 13:23:59 +01:00
|
|
|
def test_strip_httponly_prefix(self):
|
|
|
|
cookiejar = YoutubeDLCookieJar('./test/testdata/cookies/httponly_cookies.txt')
|
|
|
|
cookiejar.load(ignore_discard=True, ignore_expires=True)
|
|
|
|
|
2020-03-09 22:51:20 +01:00
|
|
|
def assert_cookie_has_value(key):
|
|
|
|
self.assertEqual(cookiejar._cookies['www.foobar.foobar']['/'][key].value, key + '_VALUE')
|
|
|
|
|
|
|
|
assert_cookie_has_value('HTTPONLY_COOKIE')
|
|
|
|
assert_cookie_has_value('JS_ACCESSIBLE_COOKIE')
|
2019-03-03 13:23:59 +01:00
|
|
|
|
2020-05-04 23:19:33 +02:00
|
|
|
def test_malformed_cookies(self):
|
|
|
|
cookiejar = YoutubeDLCookieJar('./test/testdata/cookies/malformed_cookies.txt')
|
|
|
|
cookiejar.load(ignore_discard=True, ignore_expires=True)
|
|
|
|
# Cookies should be empty since all malformed cookie file entries
|
|
|
|
# will be ignored
|
|
|
|
self.assertFalse(cookiejar._cookies)
|
|
|
|
|
2023-05-27 09:08:19 +02:00
|
|
|
def test_get_cookie_header(self):
|
|
|
|
cookiejar = YoutubeDLCookieJar('./test/testdata/cookies/httponly_cookies.txt')
|
|
|
|
cookiejar.load(ignore_discard=True, ignore_expires=True)
|
|
|
|
header = cookiejar.get_cookie_header('https://www.foobar.foobar')
|
|
|
|
self.assertIn('HTTPONLY_COOKIE', header)
|
|
|
|
|
2023-07-15 22:22:10 +02:00
|
|
|
def test_get_cookies_for_url(self):
|
|
|
|
cookiejar = YoutubeDLCookieJar('./test/testdata/cookies/session_cookies.txt')
|
|
|
|
cookiejar.load(ignore_discard=True, ignore_expires=True)
|
|
|
|
cookies = cookiejar.get_cookies_for_url('https://www.foobar.foobar/')
|
|
|
|
self.assertEqual(len(cookies), 2)
|
|
|
|
cookies = cookiejar.get_cookies_for_url('https://foobar.foobar/')
|
|
|
|
self.assertFalse(cookies)
|
|
|
|
|
2018-12-09 16:56:33 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|