pyramid_cubicweb/tests/test_bw_request.py
author David Douard <david.douard@logilab.fr>
Tue, 22 Mar 2016 14:01:44 +0100
changeset 11611 9d2bb6bdb5c8
parent 11593 73bf8377a3d5
child 11630 1400aee10df4
permissions -rw-r--r--
[tests] add a __main__ handler the relative import in test_rest_api.py needs to be modified to prevent a ValueError: Attempted relative import in non-package
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 -*-
11508
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
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
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     8
from pyramid_cubicweb.core import CubicWebPyramidRequest
11514
82e86cd8e217 Move PyramidCWTest to pyramid_cubicweb.tests
Christophe de Vienne <christophe@unlish.com>
parents: 11508
diff changeset
     9
from pyramid_cubicweb.tests 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',
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    32
                'wsgi.input': StringIO('some content')}))
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    33
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    34
        self.assertEqual('some content', req.content.read())
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/')
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    52
        self.assertIn('https://', r.body)
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):
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    55
        content = 'x'*100001
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',
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    61
                'wsgi.input': StringIO(content)}))
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(
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    84
            (), (('filefield', 'aname', 'acontent'),))
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])
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    90
        self.assertEqual('acontent', fieldvalue[1].read())
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()