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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
import webtest
from webob import Request
from tests.compat import unittest
from webtest.compat import to_bytes
def cookie_app(environ, start_response):
req = Request(environ)
status = "200 OK"
body = '<html><body><a href="/go/">go</a></body></html>'
headers = [
('Content-Type', 'text/html'),
('Content-Length', str(len(body))),
]
if req.path_info != '/go/':
headers.extend([
('Set-Cookie', 'spam=eggs'),
('Set-Cookie', 'foo="bar;baz"'),
])
start_response(status, headers)
return [to_bytes(body)]
def cookie_app2(environ, start_response):
status = to_bytes("200 OK")
body = ''
headers = [
('Content-Type', 'text/html'),
('Content-Length', str(len(body))),
('Set-Cookie', 'spam=eggs'),
('Set-Cookie', 'foo="bar;baz"'),
]
start_response(status, headers)
return [to_bytes(body)]
def cookie_app3(environ, start_response):
status = to_bytes("200 OK")
body = 'Cookie: %(HTTP_COOKIE)s' % environ
headers = [
('Content-Type', 'text/html'),
('Content-Length', str(len(body))),
]
start_response(status, headers)
return [to_bytes(body)]
class TestCookies(unittest.TestCase):
def test_cookies(self):
app = webtest.TestApp(cookie_app)
self.assertTrue(not app.cookies,
'App should initially contain no cookies')
app.get('/')
cookies = app.cookies
self.assert_(cookies, 'Response should have set cookies')
self.assertEqual(cookies['spam'], 'eggs')
self.assertEqual(cookies['foo'], 'bar;baz')
def test_preserve_cookies(self):
app = webtest.TestApp(cookie_app)
res = app.get('/')
self.assert_(app.cookies)
res.click('go')
self.assert_(app.cookies)
def test_cookies2(self):
app = webtest.TestApp(cookie_app)
self.assertTrue(not app.cookies,
'App should initially contain no cookies')
app.get('/')
self.assert_(app.cookies, 'Response should have set cookies')
self.assertIn(app.cookies['spam'], 'eggs')
self.assertIn(app.cookies['foo'], 'bar;baz')
def test_send_cookies(self):
app = webtest.TestApp(cookie_app3)
self.assertTrue(not app.cookies,
'App should initially contain no cookies')
resp = app.get('/', headers=[('Cookie', 'spam=eggs')])
self.assertFalse(bool(app.cookies),
'Response should not have set cookies')
resp.mustcontain('Cookie: spam=eggs')
|