summaryrefslogtreecommitdiff
path: root/tests/test_auth/test_auth_cookie.py
blob: 67465f4fca9feb83c822ee7cb0c83c41a34376c3 (plain)
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
# (c) 2005 Clark C. Evans
# This module is part of the Python Paste Project and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php

import os
from six.moves import xrange
try:
    # Python 3
    from http.cookies import SimpleCookie
except ImportError:
    # Python 2
    from Cookie import SimpleCookie
import six

from paste.auth import cookie
from paste.wsgilib import raw_interactive, dump_environ
from paste.response import header_value
from paste.httpexceptions import *

def build(application,setenv, *args, **kwargs):
    def setter(environ, start_response):
        save = environ['paste.auth.cookie'].append
        for (k,v) in setenv.items():
            save(k)
            environ[k] = v
        return application(environ, start_response)
    return cookie.middleware(setter,*args,**kwargs)

def test_noop():
    app = build(dump_environ,{})
    (status,headers,content,errors) = \
        raw_interactive(app)
    assert not header_value(headers,'Set-Cookie')

def test_basic(key='key', val='bingles'):
    app = build(dump_environ,{key:val})
    (status,headers,content,errors) = \
        raw_interactive(app)
    value = header_value(headers,'Set-Cookie')
    assert "Path=/;" in value
    assert "expires=" not in value
    cookie = value.split(";")[0]
    (status,headers,content,errors) = \
            raw_interactive(app,{'HTTP_COOKIE': cookie})
    expected = ("%s: %s" % (key,val.replace("\n","\n    ")))
    if six.PY3:
        expected = expected.encode('utf8')
    assert expected in content

def test_roundtrip():
    roundtrip = str('').join(map(chr, xrange(256)))
    test_basic(roundtrip,roundtrip)