From d7caeaf372b2f90aff38b5196b0b10c23d40b422 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Schoentgen?= Date: Mon, 16 Aug 2021 14:50:46 +0200 Subject: [PATCH] Fix handling of session files with `Cookie:` followed by other headers (#1127) * Fix the handling of cookies from session files * Apply suggestions from code review Co-authored-by: Jakub Roztocil * Fix test docstring formatting Co-authored-by: Jakub Roztocil --- CHANGELOG.rst | 1 + httpie/sessions.py | 2 +- tests/test_sessions.py | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index b92847f8..8a908227 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -13,6 +13,7 @@ This project adheres to `Semantic Versioning `_. an alternative to ``stdin``. (`#534`_) * Fixed ``--continue --download`` with a single byte to be downloaded left. (`#1032`_) * Fixed ``--verbose`` HTTP 307 redirects with streamed request body. (`#1088`_) +* Fixed handling of session files with `Cookie:` followed by other headers. (`#1126`_) * Add internal support for file-like object responses to improve adapter plugin support. (`#1094`_) diff --git a/httpie/sessions.py b/httpie/sessions.py index 0d800c0f..2876935a 100644 --- a/httpie/sessions.py +++ b/httpie/sessions.py @@ -72,7 +72,7 @@ class Session(BaseConfigDict): """ headers = self.headers - for name, value in request_headers.items(): + for name, value in request_headers.copy().items(): if value is None: continue # Ignore explicitly unset headers diff --git a/tests/test_sessions.py b/tests/test_sessions.py index 23e1dd9c..3568382d 100644 --- a/tests/test_sessions.py +++ b/tests/test_sessions.py @@ -178,6 +178,24 @@ class TestSession(SessionTestBase): assert HTTP_OK in r2 assert r2.json['headers']['Foo'] == 'Bar' + def test_session_with_cookie_followed_by_another_header(self, httpbin): + """ + Make sure headers don’t get mutated — + """ + self.start_session(httpbin) + session_data = { + "headers": { + "cookie": "...", + "zzz": "..." + } + } + session_path = self.config_dir / 'session-data.json' + session_path.write_text(json.dumps(session_data)) + r = http('--session', str(session_path), 'GET', httpbin.url + '/get', + env=self.env()) + assert HTTP_OK in r + assert 'Zzz' in r + def test_session_unicode(self, httpbin): self.start_session(httpbin)