cubicweb/wsgi/test/unittest_wsgi.py
changeset 11057 0b59724cb3f2
parent 11017 3dfed980071c
child 11913 4516c3956d46
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
       
     1 # encoding=utf-8
       
     2 
       
     3 import webtest.app
       
     4 from io import BytesIO
       
     5 
       
     6 from cubicweb.devtools.webtest import CubicWebTestTC
       
     7 
       
     8 from cubicweb.wsgi.request import CubicWebWsgiRequest
       
     9 from cubicweb.multipart import MultipartError
       
    10 
       
    11 
       
    12 class WSGIAppTC(CubicWebTestTC):
       
    13     def test_content_type(self):
       
    14         r = webtest.app.TestRequest.blank('/', {'CONTENT_TYPE': 'text/plain'})
       
    15 
       
    16         req = CubicWebWsgiRequest(r.environ, self.vreg)
       
    17 
       
    18         self.assertEqual('text/plain', req.get_header('Content-Type'))
       
    19 
       
    20     def test_content_body(self):
       
    21         r = webtest.app.TestRequest.blank('/', {
       
    22             'CONTENT_LENGTH': 12,
       
    23             'CONTENT_TYPE': 'text/plain',
       
    24             'wsgi.input': BytesIO(b'some content')})
       
    25 
       
    26         req = CubicWebWsgiRequest(r.environ, self.vreg)
       
    27 
       
    28         self.assertEqual(b'some content', req.content.read())
       
    29 
       
    30     def test_http_scheme(self):
       
    31         r = webtest.app.TestRequest.blank('/', {
       
    32             'wsgi.url_scheme': 'http'})
       
    33 
       
    34         req = CubicWebWsgiRequest(r.environ, self.vreg)
       
    35 
       
    36         self.assertFalse(req.https)
       
    37 
       
    38     def test_https_scheme(self):
       
    39         r = webtest.app.TestRequest.blank('/', {
       
    40             'wsgi.url_scheme': 'https'})
       
    41 
       
    42         req = CubicWebWsgiRequest(r.environ, self.vreg)
       
    43 
       
    44         self.assertTrue(req.https)
       
    45 
       
    46     def test_https_prefix(self):
       
    47         r = webtest.app.TestRequest.blank('/https/', {
       
    48             'wsgi.url_scheme': 'http'})
       
    49 
       
    50         req = CubicWebWsgiRequest(r.environ, self.vreg)
       
    51 
       
    52         self.assertTrue(req.https)
       
    53 
       
    54     def test_big_content(self):
       
    55         content = b'x'*100001
       
    56         r = webtest.app.TestRequest.blank('/', {
       
    57             'CONTENT_LENGTH': len(content),
       
    58             'CONTENT_TYPE': 'text/plain',
       
    59             'wsgi.input': BytesIO(content)})
       
    60 
       
    61         req = CubicWebWsgiRequest(r.environ, self.vreg)
       
    62 
       
    63         self.assertEqual(content, req.content.read())
       
    64 
       
    65     def test_post(self):
       
    66         self.webapp.post(
       
    67             '/',
       
    68             params={'__login': self.admlogin, '__password': self.admpassword})
       
    69 
       
    70     def test_post_bad_form(self):
       
    71         with self.assertRaises(MultipartError):
       
    72             self.webapp.post(
       
    73                 '/',
       
    74                 params='badcontent',
       
    75                 headers={'Content-Type': 'multipart/form-data'})
       
    76 
       
    77     def test_post_non_form(self):
       
    78         self.webapp.post(
       
    79             '/',
       
    80             params='{}',
       
    81             headers={'Content-Type': 'application/json'})
       
    82 
       
    83     def test_get_multiple_variables(self):
       
    84         r = webtest.app.TestRequest.blank('/?arg=1&arg=2')
       
    85         req = CubicWebWsgiRequest(r.environ, self.vreg)
       
    86 
       
    87         self.assertEqual([u'1', u'2'], req.form['arg'])
       
    88 
       
    89     def test_post_multiple_variables(self):
       
    90         r = webtest.app.TestRequest.blank('/', POST='arg=1&arg=2')
       
    91         req = CubicWebWsgiRequest(r.environ, self.vreg)
       
    92 
       
    93         self.assertEqual([u'1', u'2'], req.form['arg'])
       
    94 
       
    95     def test_post_files(self):
       
    96         content_type, params = self.webapp.encode_multipart(
       
    97             (), (('filefield', 'aname', b'acontent'),))
       
    98         r = webtest.app.TestRequest.blank(
       
    99             '/', POST=params, content_type=content_type)
       
   100         req = CubicWebWsgiRequest(r.environ, self.vreg)
       
   101         self.assertIn('filefield', req.form)
       
   102         fieldvalue = req.form['filefield']
       
   103         self.assertEqual(u'aname', fieldvalue[0])
       
   104         self.assertEqual(b'acontent', fieldvalue[1].read())
       
   105 
       
   106     def test_post_unicode_urlencoded(self):
       
   107         params = 'arg=%C3%A9'
       
   108         r = webtest.app.TestRequest.blank(
       
   109             '/', POST=params, content_type='application/x-www-form-urlencoded')
       
   110         req = CubicWebWsgiRequest(r.environ, self.vreg)
       
   111         self.assertEqual(u"é", req.form['arg'])
       
   112 
       
   113 
       
   114 if __name__ == '__main__':
       
   115     import unittest
       
   116     unittest.main()