-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathtest_cookie_domain_only.py
65 lines (52 loc) · 2.29 KB
/
test_cookie_domain_only.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import pytest
from beaker.middleware import SessionMiddleware
from beaker import crypto
webtest = pytest.importorskip("webtest")
pytest.mark.skipif(not crypto.get_crypto_module('default').has_aes,
reason="No AES library is installed, can't test " +
"cookie-only Sessions")
def simple_app(environ, start_response):
session = environ['beaker.session']
if 'value' not in session:
session['value'] = 0
session['value'] += 1
domain = environ.get('domain')
if domain:
session.domain = domain
if not environ['PATH_INFO'].startswith('/nosave'):
session.save()
start_response('200 OK', [('Content-type', 'text/plain')])
msg = 'The current value is: %d and cookie is %s' % (session['value'], session)
return [msg.encode('utf-8')]
def test_increment():
options = {'session.validate_key':'hoobermas',
'session.type':'cookie'}
app = webtest.TestApp(SessionMiddleware(simple_app, **options))
res = app.get('/')
assert 'current value is: 1' in res
res = app.get('/', extra_environ=dict(domain='.hoop.com',
HTTP_HOST='www.hoop.com'))
assert 'current value is: 1' in res
assert 'Domain=.hoop.com' in res.headers['Set-Cookie']
res = app.get('/', extra_environ=dict(HTTP_HOST='www.hoop.com'))
assert 'Domain=.hoop.com' in res.headers['Set-Cookie']
assert 'current value is: 2' in res
def test_cookie_attributes_are_preserved():
options = {'session.type': 'memory',
'session.httponly': True,
'session.secure': True,
'session.cookie_path': '/app',
'session.cookie_domain': 'localhost'}
app = webtest.TestApp(SessionMiddleware(simple_app, **options))
res = app.get('/app', extra_environ=dict(
HTTP_COOKIE='beaker.session.id=oldsessid', domain='.hoop.com'))
cookie = res.headers['Set-Cookie']
assert 'domain=.hoop.com' in cookie.lower()
assert 'path=/app' in cookie.lower()
assert 'secure' in cookie.lower()
assert 'httponly' in cookie.lower()
assert 'samesite=lax' in cookie.lower()
if __name__ == '__main__':
from paste import httpserver
wsgi_app = SessionMiddleware(simple_app, {})
httpserver.serve(wsgi_app, host='127.0.0.1', port=8080)