You've already forked httpie-cli
mirror of
https://github.com/httpie/cli.git
synced 2025-08-10 22:42:05 +02:00
Remove expired cookies (#929)
* added a test for expiring cookies * updated tests * set up util for extracting expired cookies from response header * Revert "updated tests" This reverts commita4eb5c4498
. * Revert "Revert "updated tests"" This reverts commitd242e21bce
. * added more functionality to get-expired-cookies * add 'clear expired cookies' from session.json files * refactored get_expired_cookies * fixed formatting issues * ensured key exists in cookie_header dict * fixed linting errors * removed unused import * Added tests for get_expired_cookies util * Added additional test for get_expired_cookies * added remove_expired_cookies method directly to sessions class * extracted logic to clear cookies to sessions.py * refactored utils * added tests to check expired cookies being removed from session obj * added type annotations for methods * Refactored test_sessions * Seperated out expiry related tests into own class * Refactored get_expired_cookies in utils * Refactored remove cookie methods * fixed linting errors * fixed indentation and also pluralized test class name * removed inheritance from SessionTestbase class * Moved related test to TestExpiredCookies class Co-authored-by: kbanc <katherine.bancoft@gmail.com>
This commit is contained in:
@@ -1,14 +1,17 @@
|
||||
# coding=utf-8
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
from datetime import datetime
|
||||
from tempfile import gettempdir
|
||||
|
||||
import pytest
|
||||
|
||||
from httpie.plugins.builtin import HTTPBasicAuth
|
||||
from utils import MockEnvironment, mk_config_dir, http, HTTP_OK
|
||||
from fixtures import UNICODE
|
||||
from httpie.plugins.builtin import HTTPBasicAuth
|
||||
from httpie.sessions import Session
|
||||
from httpie.utils import get_expired_cookies
|
||||
from utils import MockEnvironment, mk_config_dir, http, HTTP_OK
|
||||
|
||||
|
||||
class SessionTestBase:
|
||||
@@ -186,3 +189,93 @@ class TestSession(SessionTestBase):
|
||||
httpbin.url + '/get', env=self.env())
|
||||
finally:
|
||||
os.chdir(cwd)
|
||||
|
||||
|
||||
class TestExpiredCookies:
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
argnames=['initial_cookie', 'expired_cookie'],
|
||||
argvalues=[
|
||||
({'id': {'value': 123}}, 'id'),
|
||||
({'id': {'value': 123}}, 'token')
|
||||
]
|
||||
)
|
||||
def test_removes_expired_cookies_from_session_obj(self, initial_cookie, expired_cookie, httpbin):
|
||||
config_dir = mk_config_dir()
|
||||
session = Session(config_dir)
|
||||
session['cookies'] = initial_cookie
|
||||
session.remove_cookies([expired_cookie])
|
||||
assert expired_cookie not in session.cookies
|
||||
|
||||
shutil.rmtree(config_dir)
|
||||
|
||||
def test_expired_cookies(self, httpbin):
|
||||
orig_session = {
|
||||
'cookies': {
|
||||
'to_expire': {
|
||||
'value': 'foo'
|
||||
},
|
||||
'to_stay': {
|
||||
'value': 'foo'
|
||||
},
|
||||
}
|
||||
}
|
||||
config_dir = mk_config_dir()
|
||||
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 + '/cookies/delete?to_expire',
|
||||
)
|
||||
assert 'Cookie: to_expire=foo; to_stay=foo' in r
|
||||
|
||||
updated_session = json.loads(session_path.read_text())
|
||||
assert 'to_stay' in updated_session['cookies']
|
||||
assert 'to_expire' not in updated_session['cookies']
|
||||
|
||||
shutil.rmtree(config_dir)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
argnames=['raw_header', 'timestamp', 'expected'],
|
||||
argvalues=[
|
||||
(
|
||||
[
|
||||
('Set-Cookie', 'hello=world; Path=/; Expires=Thu, 01-Jan-1970 00:00:00 GMT; HttpOnly'),
|
||||
('Connection', 'keep-alive')
|
||||
],
|
||||
None,
|
||||
[
|
||||
{
|
||||
'name': 'hello',
|
||||
'path': '/'
|
||||
}
|
||||
]
|
||||
),
|
||||
(
|
||||
[
|
||||
('Set-Cookie', 'hello=world; Path=/; Expires=Thu, 01-Jan-1970 00:00:00 GMT; HttpOnly'),
|
||||
('Set-Cookie', 'pea=pod; Path=/ab; Expires=Thu, 01-Jan-1970 00:00:00 GMT; HttpOnly'),
|
||||
('Connection', 'keep-alive')
|
||||
],
|
||||
None,
|
||||
[
|
||||
{'name': 'hello', 'path': '/'},
|
||||
{'name': 'pea', 'path': '/ab'}
|
||||
]
|
||||
),
|
||||
(
|
||||
[
|
||||
('Set-Cookie', 'hello=world; Path=/; Expires=Fri, 12 Jun 2020 12:28:55 GMT; HttpOnly'),
|
||||
('Connection', 'keep-alive')
|
||||
],
|
||||
datetime(2020, 6, 11).timestamp(),
|
||||
[
|
||||
|
||||
]
|
||||
)
|
||||
]
|
||||
)
|
||||
def test_get_expired_cookies_manages_multiple_cookie_headers(self, raw_header, timestamp, expected):
|
||||
assert get_expired_cookies(raw_header, curr_timestamp=timestamp) == expected
|
||||
|
Reference in New Issue
Block a user