thirdparty/google_appengine/lib/webob/tests/test_request.py
changeset 109 620f9b141567
equal deleted inserted replaced
108:261778de26ff 109:620f9b141567
       
     1 from paste.fixture import *
       
     2 from webob import Request
       
     3 from py.test import raises
       
     4 
       
     5 def simpleapp(environ, start_response):
       
     6     status = '200 OK'
       
     7     response_headers = [('Content-type','text/plain')]
       
     8     start_response(status, response_headers)
       
     9     request = Request(environ)
       
    10     request.remote_user = 'bob'
       
    11     return [
       
    12         'Hello world!\n',
       
    13         'The get is %r' % request.GET,
       
    14         ' and Val is %s\n' % request.GET.get('name'),
       
    15         'The languages are: %s\n' % request.accept_language.best_matches('en-US'),
       
    16         'The accepttypes is: %s\n' % request.accept.best_match(['text/html', 'application/xml']),
       
    17         'post is %r\n' % request.POST,
       
    18         'params is %r\n' % request.params,
       
    19         'cookies is %r\n' % request.cookies,
       
    20         'body: %r\n' % request.body,
       
    21         'method: %s\n' % request.method,
       
    22         'remote_user: %r\n' % request.environ['REMOTE_USER'],
       
    23         'host_url: %r; application_url: %r; path_url: %r; url: %r\n' % (request.host_url, request.application_url, request.path_url, request.url),
       
    24         'urlvars: %r\n' % request.urlvars,
       
    25         'urlargs: %r\n' % (request.urlargs, ),
       
    26         'is_xhr: %r\n' % request.is_xhr,
       
    27         'if_modified_since: %r\n' % request.if_modified_since,
       
    28         'user_agent: %r\n' % request.user_agent,
       
    29         'if_none_match: %r\n' % request.if_none_match,
       
    30         ]
       
    31 
       
    32 def test_gets():
       
    33     app = TestApp(simpleapp)
       
    34     res = app.get('/')
       
    35     print res
       
    36     assert 'Hello' in res
       
    37     assert "get is MultiDict([])" in res
       
    38     assert "post is <NoVars: Not a POST request>" in res
       
    39     
       
    40     res = app.get('/?name=george')
       
    41     res.mustcontain("get is MultiDict([('name', 'george')])")
       
    42     res.mustcontain("Val is george")
       
    43 
       
    44 def test_language_parsing():
       
    45     app = TestApp(simpleapp)
       
    46     res = app.get('/')
       
    47     assert "The languages are: ['en-US']" in res
       
    48     
       
    49     res = app.get('/', headers={'Accept-Language':'da, en-gb;q=0.8, en;q=0.7'})
       
    50     assert "languages are: ['da', 'en-gb', 'en', 'en-US']" in res
       
    51 
       
    52     res = app.get('/', headers={'Accept-Language':'en-gb;q=0.8, da, en;q=0.7'})
       
    53     assert "languages are: ['da', 'en-gb', 'en', 'en-US']" in res
       
    54 
       
    55 def test_mime_parsing():
       
    56     app = TestApp(simpleapp)
       
    57     res = app.get('/', headers={'Accept':'text/html'})
       
    58     assert "accepttypes is: text/html" in res
       
    59     
       
    60     res = app.get('/', headers={'Accept':'application/xml'})
       
    61     assert "accepttypes is: application/xml" in res
       
    62     
       
    63     res = app.get('/', headers={'Accept':'application/xml,*/*'})
       
    64     assert "accepttypes is: application/xml" in res
       
    65 
       
    66 def test_headers():
       
    67     app = TestApp(simpleapp)
       
    68     headers = {
       
    69         'If-Modified-Since': 'Sat, 29 Oct 1994 19:43:31 GMT',
       
    70         'Cookie': 'var1=value1',
       
    71         'User-Agent': 'Mozilla 4.0 (compatible; MSIE)',
       
    72         'If-None-Match': '"etag001", "etag002"',
       
    73         'X-Requested-With': 'XMLHttpRequest',
       
    74         }
       
    75     res = app.get('/?foo=bar&baz', headers=headers)
       
    76     res.mustcontain(
       
    77         'if_modified_since: datetime.datetime(1994, 10, 29, 19, 43, 31, tzinfo=UTC)',
       
    78         "user_agent: 'Mozilla",
       
    79         'is_xhr: True',
       
    80         "cookies is {'var1': 'value1'}",
       
    81         "params is NestedMultiDict([('foo', 'bar'), ('baz', '')])",
       
    82         "if_none_match: <ETag etag001 or etag002>",
       
    83         )
       
    84     
       
    85 def test_bad_cookie():
       
    86     req = Request.blank('/')
       
    87     req.headers['Cookie'] = '070-it-:><?0'
       
    88     assert req.cookies == {}
       
    89     req.headers['Cookie'] = 'foo=bar'
       
    90     assert req.cookies == {'foo': 'bar'}
       
    91     req.headers['Cookie'] = '...'
       
    92     assert req.cookies == {}
       
    93     req.headers['Cookie'] = '=foo'
       
    94     assert req.cookies == {}