3 # Allow direct execution
8 sys
.path
.insert(0, os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
))))
14 from yt_dlp
.cookies
import YoutubeDLCookieJar
17 class TestYoutubeDLCookieJar(unittest
.TestCase
):
18 def test_keep_session_cookies(self
):
19 cookiejar
= YoutubeDLCookieJar('./test/testdata/cookies/session_cookies.txt')
21 tf
= tempfile
.NamedTemporaryFile(delete
=False)
23 cookiejar
.save(filename
=tf
.name
)
24 temp
= tf
.read().decode()
25 self
.assertTrue(re
.search(
26 r
'www\.foobar\.foobar\s+FALSE\s+/\s+TRUE\s+0\s+YoutubeDLExpiresEmpty\s+YoutubeDLExpiresEmptyValue', temp
))
27 self
.assertTrue(re
.search(
28 r
'www\.foobar\.foobar\s+FALSE\s+/\s+TRUE\s+0\s+YoutubeDLExpires0\s+YoutubeDLExpires0Value', temp
))
33 def test_strip_httponly_prefix(self
):
34 cookiejar
= YoutubeDLCookieJar('./test/testdata/cookies/httponly_cookies.txt')
37 def assert_cookie_has_value(key
):
38 self
.assertEqual(cookiejar
._cookies
['www.foobar.foobar']['/'][key
].value
, key
+ '_VALUE')
40 assert_cookie_has_value('HTTPONLY_COOKIE')
41 assert_cookie_has_value('JS_ACCESSIBLE_COOKIE')
43 def test_malformed_cookies(self
):
44 cookiejar
= YoutubeDLCookieJar('./test/testdata/cookies/malformed_cookies.txt')
46 # Cookies should be empty since all malformed cookie file entries
48 self
.assertFalse(cookiejar
._cookies
)
50 def test_get_cookie_header(self
):
51 cookiejar
= YoutubeDLCookieJar('./test/testdata/cookies/httponly_cookies.txt')
53 header
= cookiejar
.get_cookie_header('https://www.foobar.foobar')
54 self
.assertIn('HTTPONLY_COOKIE', header
)
56 def test_get_cookies_for_url(self
):
57 cookiejar
= YoutubeDLCookieJar('./test/testdata/cookies/session_cookies.txt')
59 cookies
= cookiejar
.get_cookies_for_url('https://www.foobar.foobar/')
60 self
.assertEqual(len(cookies
), 2)
61 cookies
= cookiejar
.get_cookies_for_url('https://foobar.foobar/')
62 self
.assertFalse(cookies
)
65 if __name__
== '__main__':