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