def test_cookie_one_value_with_trailing_semi(): c = cookies.Cookie("dismiss-top=6;") vals = list(c.values()) assert len(vals) == 1 assert vals[0].name == b"dismiss-top" assert vals[0].value == b"6" c = cookies.Cookie("dismiss-top=6;")
def test_cookie_one_value_with_trailing_semi(): c = cookies.Cookie('dismiss-top=6;') vals = list(c.values()) eq_(len(vals), 1) eq_(vals[0].name, b'dismiss-top') eq_(vals[0].value, b'6') c = cookies.Cookie('dismiss-top=6;')
def test_cookie_one_value_with_trailing_semi(): c = cookies.Cookie('dismiss-top=6;') vals = list(c.values()) assert len(vals) == 1 assert vals[0].name == b'dismiss-top' assert vals[0].value == b'6' c = cookies.Cookie('dismiss-top=6;')
def test_cookie_complex_serialize(): c = cookies.Cookie('dismiss-top=6; CP=null*, '\ 'PHPSESSID=0a539d42abc001cdc762809248d4beed, a="42,"') eq_( c.serialize(), 'CP=null*; PHPSESSID=0a539d42abc001cdc762809248d4beed; a="42\\054"; ' 'dismiss-top=6')
def test_cookie_samesite_future__default(): # ensure default behavior when unsupported values are provided c = cookies.Cookie() with pytest.raises(ValueError) as excinfo: c[b"foo"] = b"bar" c[b"foo"].samesite = b"Future" c.serialize() assert excinfo.value.args[0] == "SameSite must be 'strict', 'lax', or 'none'"
def test_cookie_complex_serialize(): c = cookies.Cookie( "dismiss-top=6; CP=null*, " 'PHPSESSID=0a539d42abc001cdc762809248d4beed, a="42"' ) assert ( c.serialize() == "CP=null*; PHPSESSID=0a539d42abc001cdc762809248d4beed;" " a=42; dismiss-top=6" )
def test_cookie_complex(): c = cookies.Cookie('dismiss-top=6; CP=null*, '\ 'PHPSESSID=0a539d42abc001cdc762809248d4beed, a="42,"') d = lambda v: v.decode('ascii') c_dict = dict((d(k),d(v.value)) for k,v in c.items()) eq_(c_dict, {'a': '42,', 'CP': 'null*', 'PHPSESSID': '0a539d42abc001cdc762809248d4beed', 'dismiss-top': '6' })
def test_cookie_complex(): c = cookies.Cookie("dismiss-top=6; CP=null*, " 'PHPSESSID=0a539d42abc001cdc762809248d4beed, a="42,"') def d(v): return v.decode("ascii") c_dict = dict((d(k), d(v.value)) for k, v in c.items()) assert c_dict == { "a": "42,", "CP": "null*", "PHPSESSID": "0a539d42abc001cdc762809248d4beed", "dismiss-top": "6", }
def test_cookie_complex(): c = cookies.Cookie('dismiss-top=6; CP=null*, ' 'PHPSESSID=0a539d42abc001cdc762809248d4beed, a="42,"') def d(v): return v.decode('ascii') c_dict = dict((d(k), d(v.value)) for k, v in c.items()) assert c_dict == { 'a': '42,', 'CP': 'null*', 'PHPSESSID': '0a539d42abc001cdc762809248d4beed', 'dismiss-top': '6' }
def test_cookie_samesite_future__monkeypatched(monkeypatch): # disable validation so future args pass monkeypatch.setattr(cookies, "SAMESITE_VALIDATION", False) c = cookies.Cookie() c[b"foo"] = b"bar" c[b"foo"].samesite = b"Future" assert c.serialize() == "foo=bar; SameSite=Future" # ensure we can toggle it to True and re-achieve default behavior... monkeypatch.setattr(cookies, "SAMESITE_VALIDATION", True) with pytest.raises(ValueError) as excinfo: c[b"foo"] = b"bar" c[b"foo"].samesite = b"Future" c.serialize() assert excinfo.value.args[0] == "SameSite must be 'strict', 'lax', or 'none'"
def test_cookie_one_value(): c = cookies.Cookie('dismiss-top=6') vals = list(c.values()) eq_(len(vals), 1) eq_(vals[0].name, b'dismiss-top') eq_(vals[0].value, b'6')
def test_parse_qmark_in_val(): v = r'x="\"\073\054\""; expires=Sun, 12-Jun-2011 23:16:01 GMT' c = cookies.Cookie(v) eq_(c[b'x'].value, b'";,"') eq_(c[b'x'].expires, b'Sun, 12-Jun-2011 23:16:01 GMT')
def test_cookie_empty(): c = cookies.Cookie() # empty cookie eq_(repr(c), '<Cookie: []>')
def test_cookie_invalid_name(): c = cookies.Cookie() c['La Pe\xc3\xb1a'] = '1' eq_(len(c), 0)
def test_cookie_load_multiple(): c = cookies.Cookie("a=1; Secure=true") vals = list(c.values()) assert len(vals) == 1 assert c[b"a"][b"secure"] == b"true"
def test_escape_comma(): c = cookies.Cookie() c['x'] = b'";,"' eq_(c.serialize(True), r'x="\"\073\054\""')
def test_cookie_empty(): c = cookies.Cookie() # empty cookie assert repr(c) == '<Cookie: []>'
def test_cookie_samesite_lax(): c = cookies.Cookie() c[b"foo"] = b"bar" c[b"foo"].samesite = b"Lax" assert c.serialize() == "foo=bar; SameSite=Lax"
def test_cookie_secure(): c = cookies.Cookie() c[text_("foo")] = b"bar" c[b"foo"].secure = True assert c.serialize() == "foo=bar; secure"
def test_cookie_httponly(): c = cookies.Cookie() c['foo'] = b'bar' c[b'foo'].httponly = True eq_(c.serialize(), 'foo=bar; HttpOnly')
def test_cookie_load_multiple(): c = cookies.Cookie('a=1; Secure=true') vals = list(c.values()) eq_(len(vals), 1) eq_(c[b'a'][b'secure'], b'true')
def test_cookie_httponly(): c = cookies.Cookie() c["foo"] = b"bar" c[b"foo"].httponly = True assert c.serialize() == "foo=bar; HttpOnly"
def test_cookie_one_value(): c = cookies.Cookie('dismiss-top=6') vals = list(c.values()) assert len(vals) == 1 assert vals[0].name == b'dismiss-top' assert vals[0].value == b'6'
def test_escape_comma_semi_dquote(): c = cookies.Cookie() c['x'] = b'";,"' eq_(c.serialize(True), r'x="\042\073\054\042"')
def test_cookie_complex_serialize(): c = cookies.Cookie('dismiss-top=6; CP=null*, ' 'PHPSESSID=0a539d42abc001cdc762809248d4beed, a="42"') assert c.serialize() == 'CP=null*; PHPSESSID=0a539d42abc001cdc762809248d4beed;'\ ' a=42; dismiss-top=6'
def test_cookie_invalid_name(): c = cookies.Cookie() c['La Pe\xc3\xb1a'] = '1' assert len(c) == 0
def test_cookie_secure(): c = cookies.Cookie() c[text_('foo')] = b'bar' c[b'foo'].secure = True eq_(c.serialize(), 'foo=bar; secure')
def test_cookie_load_multiple(): c = cookies.Cookie('a=1; Secure=true') vals = list(c.values()) assert len(vals) == 1 assert c[b'a'][b'secure'] == b'true'
def test_cookie_reserved_keys(): c = cookies.Cookie('dismiss-top=6; CP=null*; $version=42; a=42') assert '$version' not in c c = cookies.Cookie('$reserved=42; a=$42') eq_(list(c.keys()), [b'a'])
def test_cookie_samesite_strict(): c = cookies.Cookie() c[b"foo"] = b"bar" c[b"foo"].samesite = b"Strict" assert c.serialize() == "foo=bar; SameSite=Strict"