2018-12-09 15:56:33 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
import unittest
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
|
2021-02-24 18:45:56 +00:00
|
|
|
from yt_dlp.utils import YoutubeDLCookieJar
|
2018-12-09 15:56:33 +00: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)
|
|
|
|
temp = tf.read().decode('utf-8')
|
|
|
|
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 12:23:59 +00: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 21:51:20 +00: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 12:23:59 +00:00
|
|
|
|
2020-05-04 21:19:33 +00: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)
|
|
|
|
|
2018-12-09 15:56:33 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|