From 8e56e9fc6449a1cdfd90596e3ccbb1a5b71950d1 Mon Sep 17 00:00:00 2001 From: Jakub Roztocil Date: Fri, 19 May 2023 21:51:52 +0200 Subject: [PATCH] Fix a failing test --- tests/test_sessions.py | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/tests/test_sessions.py b/tests/test_sessions.py index 0083ea52..b0f3c656 100644 --- a/tests/test_sessions.py +++ b/tests/test_sessions.py @@ -446,7 +446,7 @@ class TestExpiredCookies(CookieTestBase): class TestCookieStorage(CookieTestBase): @pytest.mark.parametrize( - 'new_cookies, new_cookies_dict, expected', + ['specified_cookie_header', 'new_cookies_dict', 'expected_effective_cookie_header'], [( 'new=bar', {'new': 'bar'}, @@ -463,9 +463,9 @@ class TestCookieStorage(CookieTestBase): 'chocolate=milk; cookie1=foo; cookie2=foo; new=bar' ), ( - 'new=bar;; chocolate=milk;;;', + 'new=bar; chocolate=milk', {'new': 'bar', 'chocolate': 'milk'}, - 'cookie1=foo; cookie2=foo; new=bar' + 'cookie1=foo; cookie2=foo; new=bar; chocolate=milk' ), ( 'new=bar; chocolate=milk;;;', @@ -474,20 +474,35 @@ class TestCookieStorage(CookieTestBase): ) ] ) - def test_existing_and_new_cookies_sent_in_request(self, new_cookies, new_cookies_dict, expected, httpbin): + def test_existing_and_new_cookies_sent_in_request( + self, + specified_cookie_header, + new_cookies_dict, + expected_effective_cookie_header, + httpbin, + ): r = http( '--session', str(self.session_path), '--print=H', httpbin.url, - 'Cookie:' + new_cookies, + 'Cookie:' + specified_cookie_header, ) - # Note: cookies in response are in alphabetical order - assert f'Cookie: {expected}' in r + parsed_request_headers = { + name: value for name, value in [ + line.split(': ', 1) + for line in r.splitlines() + if line and ':' in line + ] + } + # Note: cookies in the request are in an undefined order. + expected_request_cookie_set = set(expected_effective_cookie_header.split('; ')) + actual_request_cookie_set = set(parsed_request_headers['Cookie'].split('; ')) + assert actual_request_cookie_set == expected_request_cookie_set updated_session = json.loads(self.session_path.read_text(encoding=UTF8)) + assert 'Cookie' not in updated_session['headers'] for name, value in new_cookies_dict.items(): - assert name, value in updated_session['cookies'] - assert 'Cookie' not in updated_session['headers'] + assert updated_session['cookies'][name]['value'] == value @pytest.mark.parametrize( 'cli_cookie, set_cookie, expected',