cubicweb/pyramid/test/test_bw_request.py
author Yann Voté <yann.vote@logilab.fr>
Mon, 26 Sep 2016 14:52:12 +0200
changeset 11631 faf279e33298
parent 11630 pyramid_cubicweb/tests/test_bw_request.py@1400aee10df4
child 11811 f09efeead7f9
permissions -rw-r--r--
Merge with pyramid-cubicweb The following tasks have been done: - merge packaging files - merge documentation - move pyramid_cubicweb package at cubicweb/pyramid and update imports accordingly - rename tests directory into test - move pyramid-cubicweb README.rst into README.pyramid.rst until better idea - add a test dependency on unreleased cubicweb-pyramid to have both py27 and py34 tests pass Closes #14023058.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11611
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11593
diff changeset
     1
# -*- coding: utf-8 -*-
11630
1400aee10df4 Port to Python3 (closes #14159555)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11611
diff changeset
     2
from io import BytesIO
11508
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
11514
82e86cd8e217 Move PyramidCWTest to pyramid_cubicweb.tests
Christophe de Vienne <christophe@unlish.com>
parents: 11508
diff changeset
     5
11508
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     6
import pyramid.request
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     7
11631
faf279e33298 Merge with pyramid-cubicweb
Yann Voté <yann.vote@logilab.fr>
parents: 11630
diff changeset
     8
from cubicweb.pyramid.core import CubicWebPyramidRequest
faf279e33298 Merge with pyramid-cubicweb
Yann Voté <yann.vote@logilab.fr>
parents: 11630
diff changeset
     9
from cubicweb.pyramid.test import PyramidCWTest
11508
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    10
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
class WSGIAppTest(PyramidCWTest):
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    13
    def make_request(self, path, environ=None, **kw):
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    14
        r = webtest.app.TestRequest.blank(path, environ, **kw)
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    15
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    16
        request = pyramid.request.Request(r.environ)
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    17
        request.registry = self.pyr_registry
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    18
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    19
        return request
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 test_content_type(self):
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    22
        req = CubicWebPyramidRequest(
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    23
            self.make_request('/', {'CONTENT_TYPE': 'text/plain'}))
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    24
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    25
        self.assertEqual('text/plain', req.get_header('Content-Type'))
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    26
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    27
    def test_content_body(self):
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    28
        req = CubicWebPyramidRequest(
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    29
            self.make_request('/', {
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    30
                'CONTENT_LENGTH': 12,
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    31
                'CONTENT_TYPE': 'text/plain',
11630
1400aee10df4 Port to Python3 (closes #14159555)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11611
diff changeset
    32
                'wsgi.input': BytesIO(b'some content')}))
11508
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    33
11630
1400aee10df4 Port to Python3 (closes #14159555)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11611
diff changeset
    34
        self.assertEqual(b'some content', req.content.read())
11508
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
    def test_http_scheme(self):
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    37
        req = CubicWebPyramidRequest(
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    38
            self.make_request('/', {
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    39
                'wsgi.url_scheme': 'http'}))
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    40
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    41
        self.assertFalse(req.https)
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    42
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    43
    def test_https_scheme(self):
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    44
        req = CubicWebPyramidRequest(
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    45
            self.make_request('/', {
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    46
                'wsgi.url_scheme': 'https'}))
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    47
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    48
        self.assertTrue(req.https)
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    49
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    50
    def test_https_prefix(self):
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    51
        r = self.webapp.get('/https/')
11630
1400aee10df4 Port to Python3 (closes #14159555)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11611
diff changeset
    52
        self.assertIn('https://', r.text)
11508
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    53
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    54
    def test_big_content(self):
11630
1400aee10df4 Port to Python3 (closes #14159555)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11611
diff changeset
    55
        content = b'x'*100001
11508
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    56
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    57
        req = CubicWebPyramidRequest(
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    58
            self.make_request('/', {
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    59
                'CONTENT_LENGTH': len(content),
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    60
                'CONTENT_TYPE': 'text/plain',
11630
1400aee10df4 Port to Python3 (closes #14159555)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11611
diff changeset
    61
                'wsgi.input': BytesIO(content)}))
11508
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    62
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    63
        self.assertEqual(content, req.content.read())
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
    def test_post(self):
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    66
        self.webapp.post(
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    67
            '/',
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    68
            params={'__login': self.admlogin, '__password': self.admpassword})
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    69
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    70
    def test_get_multiple_variables(self):
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    71
        req = CubicWebPyramidRequest(
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    72
            self.make_request('/?arg=1&arg=2'))
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
        self.assertEqual([u'1', u'2'], req.form['arg'])
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    75
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    76
    def test_post_multiple_variables(self):
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    77
        req = CubicWebPyramidRequest(
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    78
            self.make_request('/', POST='arg=1&arg=2'))
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([u'1', u'2'], req.form['arg'])
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_files(self):
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    83
        content_type, params = self.webapp.encode_multipart(
11630
1400aee10df4 Port to Python3 (closes #14159555)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11611
diff changeset
    84
            (), (('filefield', 'aname', b'acontent'),))
11508
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    85
        req = CubicWebPyramidRequest(
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    86
            self.make_request('/', POST=params, content_type=content_type))
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    87
        self.assertIn('filefield', req.form)
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    88
        fieldvalue = req.form['filefield']
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    89
        self.assertEqual(u'aname', fieldvalue[0])
11630
1400aee10df4 Port to Python3 (closes #14159555)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11611
diff changeset
    90
        self.assertEqual(b'acontent', fieldvalue[1].read())
11508
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    91
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    92
    def test_post_unicode_urlencoded(self):
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    93
        params = 'arg=%C3%A9'
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(
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    96
                '/', POST=params,
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    97
                content_type='application/x-www-form-urlencoded'))
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    98
        self.assertEqual(u"é", req.form['arg'])
11611
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11593
diff changeset
    99
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11593
diff changeset
   100
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11593
diff changeset
   101
if __name__ == '__main__':
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11593
diff changeset
   102
    from unittest import main
9d2bb6bdb5c8 [tests] add a __main__ handler
David Douard <david.douard@logilab.fr>
parents: 11593
diff changeset
   103
    main()