wsgi/test/unittest_wsgi.py
author Christophe de Vienne <christophe@unlish.com>
Tue, 09 Sep 2014 22:14:20 +0200
changeset 9988 623707a0c404
parent 9946 ec88c1a1904a
child 9995 c9f1111e0ee8
permissions -rw-r--r--
[wsgi] Fix posted files filename reading The filenames are parsed by multipart.parse_form_data, which does the unicode decoding. Trying to re-decode the filename was leading to an error.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9946
ec88c1a1904a [wsgi] Fix unicode decoding in POST
Christophe de Vienne <christophe@unlish.com>
parents: 9944
diff changeset
     1
# encoding=utf-8
ec88c1a1904a [wsgi] Fix unicode decoding in POST
Christophe de Vienne <christophe@unlish.com>
parents: 9944
diff changeset
     2
9939
46a8ed48636f [wsgi] Honor the 'CONTENT_TYPE' wsgi variable
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     3
import webtest.app
9940
292f786009ba [wsgi] Re-set the request content after calling the inherited constructor.
Christophe de Vienne <christophe@unlish.com>
parents: 9939
diff changeset
     4
from StringIO import StringIO
9939
46a8ed48636f [wsgi] Honor the 'CONTENT_TYPE' wsgi variable
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     5
46a8ed48636f [wsgi] Honor the 'CONTENT_TYPE' wsgi variable
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     6
from cubicweb.devtools.webtest import CubicWebTestTC
46a8ed48636f [wsgi] Honor the 'CONTENT_TYPE' wsgi variable
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     7
46a8ed48636f [wsgi] Honor the 'CONTENT_TYPE' wsgi variable
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     8
from cubicweb.wsgi.request import CubicWebWsgiRequest
46a8ed48636f [wsgi] Honor the 'CONTENT_TYPE' wsgi variable
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     9
46a8ed48636f [wsgi] Honor the 'CONTENT_TYPE' wsgi variable
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    10
46a8ed48636f [wsgi] Honor the 'CONTENT_TYPE' wsgi variable
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    11
class WSGIAppTC(CubicWebTestTC):
46a8ed48636f [wsgi] Honor the 'CONTENT_TYPE' wsgi variable
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    12
    def test_content_type(self):
46a8ed48636f [wsgi] Honor the 'CONTENT_TYPE' wsgi variable
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    13
        r = webtest.app.TestRequest.blank('/', {'CONTENT_TYPE': 'text/plain'})
46a8ed48636f [wsgi] Honor the 'CONTENT_TYPE' wsgi variable
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    14
46a8ed48636f [wsgi] Honor the 'CONTENT_TYPE' wsgi variable
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    15
        req = CubicWebWsgiRequest(r.environ, self.vreg)
46a8ed48636f [wsgi] Honor the 'CONTENT_TYPE' wsgi variable
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    16
46a8ed48636f [wsgi] Honor the 'CONTENT_TYPE' wsgi variable
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    17
        self.assertEqual('text/plain', req.get_header('Content-Type'))
9940
292f786009ba [wsgi] Re-set the request content after calling the inherited constructor.
Christophe de Vienne <christophe@unlish.com>
parents: 9939
diff changeset
    18
292f786009ba [wsgi] Re-set the request content after calling the inherited constructor.
Christophe de Vienne <christophe@unlish.com>
parents: 9939
diff changeset
    19
    def test_content_body(self):
292f786009ba [wsgi] Re-set the request content after calling the inherited constructor.
Christophe de Vienne <christophe@unlish.com>
parents: 9939
diff changeset
    20
        r = webtest.app.TestRequest.blank('/', {
292f786009ba [wsgi] Re-set the request content after calling the inherited constructor.
Christophe de Vienne <christophe@unlish.com>
parents: 9939
diff changeset
    21
            'CONTENT_LENGTH': 12,
292f786009ba [wsgi] Re-set the request content after calling the inherited constructor.
Christophe de Vienne <christophe@unlish.com>
parents: 9939
diff changeset
    22
            'CONTENT_TYPE': 'text/plain',
292f786009ba [wsgi] Re-set the request content after calling the inherited constructor.
Christophe de Vienne <christophe@unlish.com>
parents: 9939
diff changeset
    23
            'wsgi.input': StringIO('some content')})
292f786009ba [wsgi] Re-set the request content after calling the inherited constructor.
Christophe de Vienne <christophe@unlish.com>
parents: 9939
diff changeset
    24
292f786009ba [wsgi] Re-set the request content after calling the inherited constructor.
Christophe de Vienne <christophe@unlish.com>
parents: 9939
diff changeset
    25
        req = CubicWebWsgiRequest(r.environ, self.vreg)
292f786009ba [wsgi] Re-set the request content after calling the inherited constructor.
Christophe de Vienne <christophe@unlish.com>
parents: 9939
diff changeset
    26
292f786009ba [wsgi] Re-set the request content after calling the inherited constructor.
Christophe de Vienne <christophe@unlish.com>
parents: 9939
diff changeset
    27
        self.assertEqual('some content', req.content.read())
9941
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    28
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    29
    def test_http_scheme(self):
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    30
        r = webtest.app.TestRequest.blank('/', {
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    31
            'wsgi.url_scheme': 'http'})
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    32
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    33
        req = CubicWebWsgiRequest(r.environ, self.vreg)
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    34
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    35
        self.assertFalse(req.https)
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    36
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    37
    def test_https_scheme(self):
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    38
        r = webtest.app.TestRequest.blank('/', {
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    39
            'wsgi.url_scheme': 'https'})
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    40
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    41
        req = CubicWebWsgiRequest(r.environ, self.vreg)
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    42
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    43
        self.assertTrue(req.https)
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    44
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    45
    def test_https_prefix(self):
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    46
        r = webtest.app.TestRequest.blank('/https/', {
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    47
            'wsgi.url_scheme': 'http'})
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    48
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    49
        req = CubicWebWsgiRequest(r.environ, self.vreg)
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    50
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    51
        self.assertTrue(req.https)
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    52
9942
4b99196102f0 [wsgi] Add missing import
Christophe de Vienne <christophe@unlish.com>
parents: 9941
diff changeset
    53
    def test_big_content(self):
4b99196102f0 [wsgi] Add missing import
Christophe de Vienne <christophe@unlish.com>
parents: 9941
diff changeset
    54
        content = 'x'*100001
4b99196102f0 [wsgi] Add missing import
Christophe de Vienne <christophe@unlish.com>
parents: 9941
diff changeset
    55
        r = webtest.app.TestRequest.blank('/', {
4b99196102f0 [wsgi] Add missing import
Christophe de Vienne <christophe@unlish.com>
parents: 9941
diff changeset
    56
            'CONTENT_LENGTH': len(content),
4b99196102f0 [wsgi] Add missing import
Christophe de Vienne <christophe@unlish.com>
parents: 9941
diff changeset
    57
            'CONTENT_TYPE': 'text/plain',
4b99196102f0 [wsgi] Add missing import
Christophe de Vienne <christophe@unlish.com>
parents: 9941
diff changeset
    58
            'wsgi.input': StringIO(content)})
4b99196102f0 [wsgi] Add missing import
Christophe de Vienne <christophe@unlish.com>
parents: 9941
diff changeset
    59
4b99196102f0 [wsgi] Add missing import
Christophe de Vienne <christophe@unlish.com>
parents: 9941
diff changeset
    60
        req = CubicWebWsgiRequest(r.environ, self.vreg)
4b99196102f0 [wsgi] Add missing import
Christophe de Vienne <christophe@unlish.com>
parents: 9941
diff changeset
    61
4b99196102f0 [wsgi] Add missing import
Christophe de Vienne <christophe@unlish.com>
parents: 9941
diff changeset
    62
        self.assertEqual(content, req.content.read())
4b99196102f0 [wsgi] Add missing import
Christophe de Vienne <christophe@unlish.com>
parents: 9941
diff changeset
    63
9943
a4aeee690bff [wsgi] Set self.vreg
Christophe de Vienne <christophe@unlish.com>
parents: 9942
diff changeset
    64
    def test_post(self):
a4aeee690bff [wsgi] Set self.vreg
Christophe de Vienne <christophe@unlish.com>
parents: 9942
diff changeset
    65
        self.webapp.post(
a4aeee690bff [wsgi] Set self.vreg
Christophe de Vienne <christophe@unlish.com>
parents: 9942
diff changeset
    66
            '/',
a4aeee690bff [wsgi] Set self.vreg
Christophe de Vienne <christophe@unlish.com>
parents: 9942
diff changeset
    67
            params={'__login': self.admlogin, '__password': self.admpassword})
a4aeee690bff [wsgi] Set self.vreg
Christophe de Vienne <christophe@unlish.com>
parents: 9942
diff changeset
    68
9944
9b3b21b7ff3e [wsgi] Fix multiple variables reading in params
Christophe de Vienne <christophe@unlish.com>
parents: 9943
diff changeset
    69
    def test_get_multiple_variables(self):
9b3b21b7ff3e [wsgi] Fix multiple variables reading in params
Christophe de Vienne <christophe@unlish.com>
parents: 9943
diff changeset
    70
        r = webtest.app.TestRequest.blank('/?arg=1&arg=2')
9b3b21b7ff3e [wsgi] Fix multiple variables reading in params
Christophe de Vienne <christophe@unlish.com>
parents: 9943
diff changeset
    71
        req = CubicWebWsgiRequest(r.environ, self.vreg)
9b3b21b7ff3e [wsgi] Fix multiple variables reading in params
Christophe de Vienne <christophe@unlish.com>
parents: 9943
diff changeset
    72
9b3b21b7ff3e [wsgi] Fix multiple variables reading in params
Christophe de Vienne <christophe@unlish.com>
parents: 9943
diff changeset
    73
        self.assertEqual([u'1', u'2'], req.form['arg'])
9b3b21b7ff3e [wsgi] Fix multiple variables reading in params
Christophe de Vienne <christophe@unlish.com>
parents: 9943
diff changeset
    74
9b3b21b7ff3e [wsgi] Fix multiple variables reading in params
Christophe de Vienne <christophe@unlish.com>
parents: 9943
diff changeset
    75
    def test_post_multiple_variables(self):
9b3b21b7ff3e [wsgi] Fix multiple variables reading in params
Christophe de Vienne <christophe@unlish.com>
parents: 9943
diff changeset
    76
        r = webtest.app.TestRequest.blank('/', POST='arg=1&arg=2')
9b3b21b7ff3e [wsgi] Fix multiple variables reading in params
Christophe de Vienne <christophe@unlish.com>
parents: 9943
diff changeset
    77
        req = CubicWebWsgiRequest(r.environ, self.vreg)
9b3b21b7ff3e [wsgi] Fix multiple variables reading in params
Christophe de Vienne <christophe@unlish.com>
parents: 9943
diff changeset
    78
9b3b21b7ff3e [wsgi] Fix multiple variables reading in params
Christophe de Vienne <christophe@unlish.com>
parents: 9943
diff changeset
    79
        self.assertEqual([u'1', u'2'], req.form['arg'])
9b3b21b7ff3e [wsgi] Fix multiple variables reading in params
Christophe de Vienne <christophe@unlish.com>
parents: 9943
diff changeset
    80
9988
623707a0c404 [wsgi] Fix posted files filename reading
Christophe de Vienne <christophe@unlish.com>
parents: 9946
diff changeset
    81
    def test_post_files(self):
623707a0c404 [wsgi] Fix posted files filename reading
Christophe de Vienne <christophe@unlish.com>
parents: 9946
diff changeset
    82
        content_type, params = self.webapp.encode_multipart(
623707a0c404 [wsgi] Fix posted files filename reading
Christophe de Vienne <christophe@unlish.com>
parents: 9946
diff changeset
    83
            (), (('filefield', 'aname', 'acontent'),))
623707a0c404 [wsgi] Fix posted files filename reading
Christophe de Vienne <christophe@unlish.com>
parents: 9946
diff changeset
    84
        r = webtest.app.TestRequest.blank(
623707a0c404 [wsgi] Fix posted files filename reading
Christophe de Vienne <christophe@unlish.com>
parents: 9946
diff changeset
    85
            '/', POST=params, content_type=content_type)
623707a0c404 [wsgi] Fix posted files filename reading
Christophe de Vienne <christophe@unlish.com>
parents: 9946
diff changeset
    86
        req = CubicWebWsgiRequest(r.environ, self.vreg)
623707a0c404 [wsgi] Fix posted files filename reading
Christophe de Vienne <christophe@unlish.com>
parents: 9946
diff changeset
    87
        self.assertIn('filefield', req.form)
623707a0c404 [wsgi] Fix posted files filename reading
Christophe de Vienne <christophe@unlish.com>
parents: 9946
diff changeset
    88
        fieldvalue = req.form['filefield']
623707a0c404 [wsgi] Fix posted files filename reading
Christophe de Vienne <christophe@unlish.com>
parents: 9946
diff changeset
    89
        self.assertEqual(u'aname', fieldvalue[0])
623707a0c404 [wsgi] Fix posted files filename reading
Christophe de Vienne <christophe@unlish.com>
parents: 9946
diff changeset
    90
        self.assertEqual('acontent', fieldvalue[1].read())
623707a0c404 [wsgi] Fix posted files filename reading
Christophe de Vienne <christophe@unlish.com>
parents: 9946
diff changeset
    91
9946
ec88c1a1904a [wsgi] Fix unicode decoding in POST
Christophe de Vienne <christophe@unlish.com>
parents: 9944
diff changeset
    92
    def test_post_unicode_urlencoded(self):
ec88c1a1904a [wsgi] Fix unicode decoding in POST
Christophe de Vienne <christophe@unlish.com>
parents: 9944
diff changeset
    93
        params = 'arg=%C3%A9'
ec88c1a1904a [wsgi] Fix unicode decoding in POST
Christophe de Vienne <christophe@unlish.com>
parents: 9944
diff changeset
    94
        r = webtest.app.TestRequest.blank(
ec88c1a1904a [wsgi] Fix unicode decoding in POST
Christophe de Vienne <christophe@unlish.com>
parents: 9944
diff changeset
    95
            '/', POST=params, content_type='application/x-www-form-urlencoded')
ec88c1a1904a [wsgi] Fix unicode decoding in POST
Christophe de Vienne <christophe@unlish.com>
parents: 9944
diff changeset
    96
        req = CubicWebWsgiRequest(r.environ, self.vreg)
ec88c1a1904a [wsgi] Fix unicode decoding in POST
Christophe de Vienne <christophe@unlish.com>
parents: 9944
diff changeset
    97
        self.assertEqual(u"é", req.form['arg'])
ec88c1a1904a [wsgi] Fix unicode decoding in POST
Christophe de Vienne <christophe@unlish.com>
parents: 9944
diff changeset
    98
9941
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    99
    @classmethod
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
   100
    def init_config(cls, config):
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
   101
        super(WSGIAppTC, cls).init_config(config)
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
   102
        config.https_uiprops = None
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
   103
        config.https_datadir_url = None