1# (c) 2005 Clark C. Evans
2# This module is part of the Python Paste Project and is released under
3# the MIT License: http://www.opensource.org/licenses/mit-license.php
4
5from six.moves import xrange
6import six
7
8from paste.auth import cookie
9from paste.wsgilib import raw_interactive, dump_environ
10from paste.response import header_value
11from paste.httpexceptions import *
12
13def build(application,setenv, *args, **kwargs):
14    def setter(environ, start_response):
15        save = environ['paste.auth.cookie'].append
16        for (k,v) in setenv.items():
17            save(k)
18            environ[k] = v
19        return application(environ, start_response)
20    return cookie.middleware(setter,*args,**kwargs)
21
22def test_noop():
23    app = build(dump_environ,{})
24    (status,headers,content,errors) = \
25        raw_interactive(app)
26    assert not header_value(headers,'Set-Cookie')
27
28def test_basic(key='key', val='bingles'):
29    app = build(dump_environ,{key:val})
30    (status,headers,content,errors) = \
31        raw_interactive(app)
32    value = header_value(headers,'Set-Cookie')
33    assert "Path=/;" in value
34    assert "expires=" not in value
35    cookie = value.split(";")[0]
36    (status,headers,content,errors) = \
37            raw_interactive(app,{'HTTP_COOKIE': cookie})
38    expected = ("%s: %s" % (key,val.replace("\n","\n    ")))
39    if six.PY3:
40        expected = expected.encode('utf8')
41    assert expected in content
42
43def test_roundtrip():
44    roundtrip = str('').join(map(chr, xrange(256)))
45    test_basic(roundtrip,roundtrip)
46
47