From 6f55f446ae6ddeb905d45eb557056c031029df8c Mon Sep 17 00:00:00 2001 From: Marius Gedminas Date: Fri, 16 Mar 2018 13:23:26 +0200 Subject: [PATCH] Load cookies from the --cookiefile correctly requests.cookies.merge_cookies() requires a dict or a CookieJar as the second argument. We've been passing lists of Cookie objects instead. Fixes #62, harder this time. --- linkcheck/cookies.py | 8 +++++--- linkcheck/director/aggregator.py | 2 +- tests/cookies.txt | 4 ++++ tests/test_cookies.py | 4 ++++ 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/linkcheck/cookies.py b/linkcheck/cookies.py index ffe8a693..ba31d4db 100644 --- a/linkcheck/cookies.py +++ b/linkcheck/cookies.py @@ -42,12 +42,12 @@ def from_file (filename): line = line.rstrip() if not line: if lines: - entries.append(from_headers("\r\n".join(lines))) + entries.extend(from_headers("\r\n".join(lines))) lines = [] else: lines.append(line) if lines: - entries.append(from_headers("\r\n".join(lines))) + entries.extend(from_headers("\r\n".join(lines))) return entries @@ -63,7 +63,9 @@ def from_headers (strheader): if "Host" not in headers: raise ValueError("Required header 'Host:' missing") host = headers["Host"] - path= headers.get("Path", "/") + # XXX: our --help says we also pay attention to the Scheme: header, + # but we don't?! + path = headers.get("Path", "/") for header in headers.getallmatchingheaders("Set-Cookie"): headervalue = header.split(':', 1)[1] for pairs in split_header_words([headervalue]): diff --git a/linkcheck/director/aggregator.py b/linkcheck/director/aggregator.py index a2f2bcc6..feef67af 100644 --- a/linkcheck/director/aggregator.py +++ b/linkcheck/director/aggregator.py @@ -53,7 +53,7 @@ def new_request_session(config, cookies): }) if config["cookiefile"]: for cookie in from_file(config["cookiefile"]): - session.cookies = requests.cookies.merge_cookies(session.cookies, cookie) + session.cookies.set_cookie(cookie) return session diff --git a/tests/cookies.txt b/tests/cookies.txt index 584386f7..c5d206b5 100644 --- a/tests/cookies.txt +++ b/tests/cookies.txt @@ -1,3 +1,7 @@ Host: example.org Path: /hello Set-cookie: om="nomnom" + +Host: example.com +Set-cookie: multiple=cookies +Set-cookie: are=allowed diff --git a/tests/test_cookies.py b/tests/test_cookies.py index 8ed698b7..c55cb886 100644 --- a/tests/test_cookies.py +++ b/tests/test_cookies.py @@ -72,8 +72,12 @@ class TestCookies (unittest.TestCase): self.assertRaises(ValueError, from_headers, "\r\n".join(lines)) def test_cookie_file (self): + # Regression test for https://github.com/linkcheck/linkchecker/issues/62 config = linkcheck.configuration.Configuration() here = os.path.dirname(__file__) config['cookiefile'] = os.path.join(here, 'cookies.txt') aggregate = linkcheck.director.get_aggregate(config) aggregate.add_request_session() + session = aggregate.get_request_session() + self.assertEqual({c.name for c in session.cookies}, + {'om', 'multiple', 'are'})