File: test_cookie.py

package info (click to toggle)
webtest 1.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 592 kB
  • sloc: python: 3,562; makefile: 42
file content (63 lines) | stat: -rw-r--r-- 1,912 bytes parent folder | download
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
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)]

class TestCookies(unittest.TestCase):

    def test_cookies(self):
        app = webtest.TestApp(cookie_app)
        self.assertTrue(not app.cookies, 'App should initially contain no cookies')
        res = 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)
        go_page = 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')

        res = app.get('/')
        self.assert_(app.cookies, 'Response should have set cookies')
        self.assertIn(app.cookies['spam'], 'eggs')
        self.assertIn(app.cookies['foo'], 'bar;baz')