Home | History | Annotate | Download | only in tests
      1 # (c) 2005 Ben Bangert
      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 from paste.fixture import *
      5 from paste.request import *
      6 from paste.wsgiwrappers import WSGIRequest
      7 import six
      8 
      9 def simpleapp(environ, start_response):
     10     status = '200 OK'
     11     response_headers = [('Content-type','text/plain')]
     12     start_response(status, response_headers)
     13     request = WSGIRequest(environ)
     14     body = [
     15         'Hello world!\n', 'The get is %s' % str(request.GET),
     16         ' and Val is %s\n' % request.GET.get('name'),
     17         'The languages are: %s\n' % request.languages,
     18         'The accepttypes is: %s\n' % request.match_accept(['text/html', 'application/xml'])]
     19     if six.PY3:
     20         body = [line.encode('utf8')  for line in body]
     21     return body
     22 
     23 def test_gets():
     24     app = TestApp(simpleapp)
     25     res = app.get('/')
     26     assert 'Hello' in res
     27     assert "get is MultiDict([])" in res
     28 
     29     res = app.get('/?name=george')
     30     res.mustcontain("get is MultiDict([('name', 'george')])")
     31     res.mustcontain("Val is george")
     32 
     33 def test_language_parsing():
     34     app = TestApp(simpleapp)
     35     res = app.get('/')
     36     assert "The languages are: ['en-us']" in res
     37 
     38     res = app.get('/', headers={'Accept-Language':'da, en-gb;q=0.8, en;q=0.7'})
     39     assert "languages are: ['da', 'en-gb', 'en', 'en-us']" in res
     40 
     41     res = app.get('/', headers={'Accept-Language':'en-gb;q=0.8, da, en;q=0.7'})
     42     assert "languages are: ['da', 'en-gb', 'en', 'en-us']" in res
     43 
     44 def test_mime_parsing():
     45     app = TestApp(simpleapp)
     46     res = app.get('/', headers={'Accept':'text/html'})
     47     assert "accepttypes is: ['text/html']" in res
     48 
     49     res = app.get('/', headers={'Accept':'application/xml'})
     50     assert "accepttypes is: ['application/xml']" in res
     51 
     52     res = app.get('/', headers={'Accept':'application/xml,*/*'})
     53     assert "accepttypes is: ['text/html', 'application/xml']" in res
     54 
     55 def test_bad_cookie():
     56     env = {}
     57     env['HTTP_COOKIE'] = '070-it-:><?0'
     58     assert get_cookie_dict(env) == {}
     59     env['HTTP_COOKIE'] = 'foo=bar'
     60     assert get_cookie_dict(env) == {'foo': 'bar'}
     61     env['HTTP_COOKIE'] = '...'
     62     assert get_cookie_dict(env) == {}
     63     env['HTTP_COOKIE'] = '=foo'
     64     assert get_cookie_dict(env) == {}
     65     env['HTTP_COOKIE'] = '?='
     66     assert get_cookie_dict(env) == {}
     67