pyramid_cubicweb/tests/test_bw_request.py
changeset 11508 ef8b9021b47b
child 11514 82e86cd8e217
equal deleted inserted replaced
11507:4d7286f079e1 11508:ef8b9021b47b
       
     1 # -*- coding: utf8 -*-
       
     2 from StringIO import StringIO
       
     3 
       
     4 import webtest
       
     5 import pyramid.request
       
     6 
       
     7 from cubicweb.devtools.webtest import CubicWebTestTC
       
     8 
       
     9 from pyramid_cubicweb import make_cubicweb_application
       
    10 from pyramid_cubicweb.core import CubicWebPyramidRequest
       
    11 
       
    12 
       
    13 class PyramidCWTest(CubicWebTestTC):
       
    14     @classmethod
       
    15     def init_config(cls, config):
       
    16         super(PyramidCWTest, cls).init_config(config)
       
    17         config.global_set_option('https-url', 'https://localhost.local/')
       
    18         config['pyramid-auth-secret'] = 'authsecret'
       
    19         config['pyramid-session-secret'] = 'sessionsecret'
       
    20 
       
    21     def setUp(self):
       
    22         # Skip CubicWebTestTC setUp
       
    23         super(CubicWebTestTC, self).setUp()
       
    24         config = make_cubicweb_application(self.config)
       
    25         self.pyr_registry = config.registry
       
    26         self.webapp = webtest.TestApp(config.make_wsgi_app())
       
    27 
       
    28 
       
    29 class WSGIAppTest(PyramidCWTest):
       
    30     def make_request(self, path, environ=None, **kw):
       
    31         r = webtest.app.TestRequest.blank(path, environ, **kw)
       
    32 
       
    33         request = pyramid.request.Request(r.environ)
       
    34         request.registry = self.pyr_registry
       
    35 
       
    36         return request
       
    37 
       
    38     def test_content_type(self):
       
    39         req = CubicWebPyramidRequest(
       
    40             self.make_request('/', {'CONTENT_TYPE': 'text/plain'}))
       
    41 
       
    42         self.assertEqual('text/plain', req.get_header('Content-Type'))
       
    43 
       
    44     def test_content_body(self):
       
    45         req = CubicWebPyramidRequest(
       
    46             self.make_request('/', {
       
    47                 'CONTENT_LENGTH': 12,
       
    48                 'CONTENT_TYPE': 'text/plain',
       
    49                 'wsgi.input': StringIO('some content')}))
       
    50 
       
    51         self.assertEqual('some content', req.content.read())
       
    52 
       
    53     def test_http_scheme(self):
       
    54         req = CubicWebPyramidRequest(
       
    55             self.make_request('/', {
       
    56                 'wsgi.url_scheme': 'http'}))
       
    57 
       
    58         self.assertFalse(req.https)
       
    59 
       
    60     def test_https_scheme(self):
       
    61         req = CubicWebPyramidRequest(
       
    62             self.make_request('/', {
       
    63                 'wsgi.url_scheme': 'https'}))
       
    64 
       
    65         self.assertTrue(req.https)
       
    66 
       
    67     def test_https_prefix(self):
       
    68         r = self.webapp.get('/https/')
       
    69         self.assertIn('https://', r.body)
       
    70 
       
    71     def test_big_content(self):
       
    72         content = 'x'*100001
       
    73 
       
    74         req = CubicWebPyramidRequest(
       
    75             self.make_request('/', {
       
    76                 'CONTENT_LENGTH': len(content),
       
    77                 'CONTENT_TYPE': 'text/plain',
       
    78                 'wsgi.input': StringIO(content)}))
       
    79 
       
    80         self.assertEqual(content, req.content.read())
       
    81 
       
    82     def test_post(self):
       
    83         self.webapp.post(
       
    84             '/',
       
    85             params={'__login': self.admlogin, '__password': self.admpassword})
       
    86 
       
    87     def test_get_multiple_variables(self):
       
    88         req = CubicWebPyramidRequest(
       
    89             self.make_request('/?arg=1&arg=2'))
       
    90 
       
    91         self.assertEqual([u'1', u'2'], req.form['arg'])
       
    92 
       
    93     def test_post_multiple_variables(self):
       
    94         req = CubicWebPyramidRequest(
       
    95             self.make_request('/', POST='arg=1&arg=2'))
       
    96 
       
    97         self.assertEqual([u'1', u'2'], req.form['arg'])
       
    98 
       
    99     def test_post_files(self):
       
   100         content_type, params = self.webapp.encode_multipart(
       
   101             (), (('filefield', 'aname', 'acontent'),))
       
   102         req = CubicWebPyramidRequest(
       
   103             self.make_request('/', POST=params, content_type=content_type))
       
   104         self.assertIn('filefield', req.form)
       
   105         fieldvalue = req.form['filefield']
       
   106         self.assertEqual(u'aname', fieldvalue[0])
       
   107         self.assertEqual('acontent', fieldvalue[1].read())
       
   108 
       
   109     def test_post_unicode_urlencoded(self):
       
   110         params = 'arg=%C3%A9'
       
   111         req = CubicWebPyramidRequest(
       
   112             self.make_request(
       
   113                 '/', POST=params,
       
   114                 content_type='application/x-www-form-urlencoded'))
       
   115         self.assertEqual(u"é", req.form['arg'])
       
   116 
       
   117     @classmethod
       
   118     def init_config(cls, config):
       
   119         super(WSGIAppTest, cls).init_config(config)
       
   120         config.https_uiprops = None
       
   121         config.https_datadir_url = None