Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine cookies from original request and session file #932

Merged
merged 11 commits into from
Jun 18, 2020
7 changes: 7 additions & 0 deletions httpie/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ def update_headers(self, request_headers: RequestHeadersDict):
if name == 'User-Agent' and value.startswith('HTTPie/'):
continue

if name == 'Cookie':
for cookie in value.split(';'):
name, value = cookie.split("=")
self['cookies'][name.strip()] = {'value': value.strip()}
del request_headers['Cookie']
continue

for prefix in SESSION_IGNORED_HEADER_PREFIXES:
if name.lower().startswith(prefix.lower()):
break
Expand Down
47 changes: 47 additions & 0 deletions tests/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,50 @@ def test_expired_cookies(self, httpbin):
)
def test_get_expired_cookies_manages_multiple_cookie_headers(self, headers, now, expected_expired):
assert get_expired_cookies(headers, now=now) == expected_expired


@pytest.mark.parametrize(
argnames=["new_cookies", "new_cookies_dict", "expected"],
argvalues=[(
"new=bar",
{"new": "bar"},
"existing_cookie=foo; new=bar"
),
(
"new=bar;chocolate=milk",
{"new": "bar", "chocolate": "milk"},
"chocolate=milk; existing_cookie=foo; new=bar"
),
(
"new=bar; chocolate=milk",
{"new": "bar", "chocolate": "milk"},
"chocolate=milk; existing_cookie=foo; new=bar"
)
]
)
def test_existing_and_new_cookies_sent_in_request(new_cookies, new_cookies_dict, expected, httpbin):
config_dir = mk_config_dir()
orig_session = {
'cookies': {
'existing_cookie': {
'value': 'foo',
}
}
}
session_path = config_dir / 'test-session.json'
session_path.write_text(json.dumps(orig_session))

r = http(
'--session', str(session_path),
'--print=H',
httpbin.url,
'Cookie:' + new_cookies,
)
# Note: cookies in response are in alphabetical order
assert 'Cookie: ' + expected in r

updated_session = json.loads(session_path.read_text())
for name, value in new_cookies_dict.items():
assert name, value in updated_session['cookies']
assert "Cookie" not in updated_session['headers']
shutil.rmtree(config_dir)