aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_util.py
diff options
context:
space:
mode:
authorLibravatarUnit 193 <unit193@ubuntu.com>2020-03-16 23:20:15 -0400
committerLibravatarUnit 193 <unit193@ubuntu.com>2020-03-16 23:20:15 -0400
commite8cc000750de972384f2f34d02d42222b4018ae9 (patch)
tree26eb0bacedff7480d29bafcf184ca529cf9f1d9f /test/test_util.py
parent4366125d2580982abb57bc65a26fc1fb8ef2a5df (diff)
New upstream version 1.13.2upstream/1.13.2
Diffstat (limited to 'test/test_util.py')
-rw-r--r--test/test_util.py131
1 files changed, 130 insertions, 1 deletions
diff --git a/test/test_util.py b/test/test_util.py
index 5a103cf..ffabd37 100644
--- a/test/test_util.py
+++ b/test/test_util.py
@@ -1,7 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
-# Copyright 2015-2019 Mike Fährmann
+# Copyright 2015-2020 Mike Fährmann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
@@ -9,8 +9,10 @@
import unittest
import sys
+import io
import random
import string
+import http.cookiejar
from gallery_dl import util, text, exception
@@ -158,11 +160,106 @@ class TestISO639_1(unittest.TestCase):
self.assertEqual(func(*args), result)
+class TestCookiesTxt(unittest.TestCase):
+
+ def test_load_cookiestxt(self):
+
+ def _assert(content, expected):
+ cookies = util.load_cookiestxt(io.StringIO(content, None))
+ for c, e in zip(cookies, expected):
+ self.assertEqual(c.__dict__, e.__dict__)
+
+ _assert("", [])
+ _assert("\n\n\n", [])
+ _assert("$ Comment", [])
+ _assert("# Comment", [])
+ _assert(" # Comment \n\n $ Comment ", [])
+ _assert(
+ ".example.org\tTRUE\t/\tTRUE\t0\tname\tvalue",
+ [self._cookie("name", "value", ".example.org")],
+ )
+ _assert(
+ ".example.org\tTRUE\t/\tTRUE\t\tname\t",
+ [self._cookie("name", "", ".example.org")],
+ )
+ _assert(
+ "# Netscape HTTP Cookie File\n"
+ "\n"
+ "# default\n"
+ ".example.org TRUE / FALSE 0 n1 v1\n"
+ ".example.org TRUE / TRUE 2145945600 n2 v2\n"
+ ".example.org TRUE /path FALSE 0 n3\n"
+ "\n"
+ " # # extra # # \n"
+ "www.example.org FALSE / FALSE n4 \n"
+ "www.example.org FALSE /path FALSE 100 n5 v5\n",
+ [
+ self._cookie(
+ "n1", "v1", ".example.org", True, "/", False),
+ self._cookie(
+ "n2", "v2", ".example.org", True, "/", True, 2145945600),
+ self._cookie(
+ "n3", None, ".example.org", True, "/path", False),
+ self._cookie(
+ "n4", "" , "www.example.org", False, "/", False),
+ self._cookie(
+ "n5", "v5", "www.example.org", False, "/path", False, 100),
+ ],
+ )
+
+ with self.assertRaises(ValueError):
+ util.load_cookiestxt("example.org\tTRUE\t/\tTRUE\t0\tname")
+
+ def test_save_cookiestxt(self):
+
+ def _assert(cookies, expected):
+ fp = io.StringIO(newline=None)
+ util.save_cookiestxt(fp, cookies)
+ self.assertMultiLineEqual(fp.getvalue(), expected)
+
+ _assert([], "# Netscape HTTP Cookie File\n\n")
+ _assert(
+ [self._cookie("name", "value", ".example.org")],
+ "# Netscape HTTP Cookie File\n\n"
+ ".example.org\tTRUE\t/\tTRUE\t0\tname\tvalue\n",
+ )
+ _assert(
+ [
+ self._cookie(
+ "n1", "v1", ".example.org", True, "/", False),
+ self._cookie(
+ "n2", "v2", ".example.org", True, "/", True, 2145945600),
+ self._cookie(
+ "n3", None, ".example.org", True, "/path", False),
+ self._cookie(
+ "n4", "" , "www.example.org", False, "/", False),
+ self._cookie(
+ "n5", "v5", "www.example.org", False, "/path", False, 100),
+ ],
+ "# Netscape HTTP Cookie File\n"
+ "\n"
+ ".example.org TRUE / FALSE 0 n1 v1\n"
+ ".example.org TRUE / TRUE 2145945600 n2 v2\n"
+ ".example.org TRUE /path FALSE 0 n3\n"
+ "www.example.org FALSE / FALSE 0 n4 \n"
+ "www.example.org FALSE /path FALSE 100 n5 v5\n",
+ )
+
+ def _cookie(self, name, value, domain, domain_specified=True,
+ path="/", secure=True, expires=None):
+ return http.cookiejar.Cookie(
+ 0, name, value, None, False,
+ domain, domain_specified, domain.startswith("."),
+ path, False, secure, expires, False, None, None, {},
+ )
+
+
class TestFormatter(unittest.TestCase):
kwdict = {
"a": "hElLo wOrLd",
"b": "äöü",
+ "d": {"a": "foo", "b": 0, "c": None},
"l": ["a", "b", "c"],
"n": None,
"u": "%27%3C%20/%20%3E%27",
@@ -227,6 +324,26 @@ class TestFormatter(unittest.TestCase):
self._run_test("{missing[key]}", replacement, default)
self._run_test("{missing:?a//}", "a" + default, default)
+ def test_alternative(self):
+ self._run_test("{a|z}" , "hElLo wOrLd")
+ self._run_test("{z|a}" , "hElLo wOrLd")
+ self._run_test("{z|y|a}" , "hElLo wOrLd")
+ self._run_test("{z|y|x|a}", "hElLo wOrLd")
+ self._run_test("{z|n|a|y}", "hElLo wOrLd")
+
+ self._run_test("{z|a!C}" , "Hello World")
+ self._run_test("{z|a:Rh/C/}" , "CElLo wOrLd")
+ self._run_test("{z|a!C:RH/C/}", "Cello World")
+ self._run_test("{z|y|x:?</>/}", "")
+
+ self._run_test("{d[c]|d[b]|d[a]}", "0")
+ self._run_test("{d[a]|d[b]|d[c]}", "foo")
+ self._run_test("{d[z]|d[y]|d[x]}", "None")
+
+ def test_indexing(self):
+ self._run_test("{l[0]}" , "a")
+ self._run_test("{a[6]}" , "w")
+
def test_slicing(self):
v = self.kwdict["a"]
self._run_test("{a[1:10]}" , v[1:10])
@@ -273,6 +390,18 @@ class TestFormatter(unittest.TestCase):
self._run_test("{a!l:Rl//}" , "heo word")
self._run_test("{name:Rame/othing/}", "Nothing")
+ def test_chain_special(self):
+ # multiple replacements
+ self._run_test("{a:Rh/C/RE/e/RL/l/}", "Cello wOrld")
+ self._run_test("{d[b]!s:R1/Q/R2/A/R0/Y/}", "Y")
+
+ # join-and-replace
+ self._run_test("{l:J-/Rb/E/}", "a-E-c")
+
+ # optional-and-maxlen
+ self._run_test("{d[a]:?</>/L1/too long/}", "<too long>")
+ self._run_test("{d[c]:?</>/L5/too long/}", "")
+
def _run_test(self, format_string, result, default=None):
formatter = util.Formatter(format_string, default)
output = formatter.format_map(self.kwdict)