cubicweb/wsgi/test/unittest_wsgi.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 23 Jan 2017 11:02:21 +0100
changeset 11913 4516c3956d46
parent 11057 0b59724cb3f2
permissions -rw-r--r--
Drop support for https-url in all-in-one.conf This feature allowing an instance to have both anonymous and authenticated (https) url is not used anymore, let's drop it.
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
10748
a704babdc840 [wsgi/test] use bytes instead of str for POST body
Julien Cristau <julien.cristau@logilab.fr>
parents: 10670
diff changeset
     4
from io import BytesIO
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
9995
c9f1111e0ee8 [wsgi] If multipart cannot parse the POST content, let it pass.
Christophe de Vienne <christophe@unlish.com>
parents: 9988
diff changeset
     9
from cubicweb.multipart import MultipartError
9939
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
46a8ed48636f [wsgi] Honor the 'CONTENT_TYPE' wsgi variable
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    12
class WSGIAppTC(CubicWebTestTC):
46a8ed48636f [wsgi] Honor the 'CONTENT_TYPE' wsgi variable
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    13
    def test_content_type(self):
46a8ed48636f [wsgi] Honor the 'CONTENT_TYPE' wsgi variable
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    14
        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
    15
46a8ed48636f [wsgi] Honor the 'CONTENT_TYPE' wsgi variable
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    16
        req = CubicWebWsgiRequest(r.environ, self.vreg)
46a8ed48636f [wsgi] Honor the 'CONTENT_TYPE' wsgi variable
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    17
46a8ed48636f [wsgi] Honor the 'CONTENT_TYPE' wsgi variable
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    18
        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
    19
292f786009ba [wsgi] Re-set the request content after calling the inherited constructor.
Christophe de Vienne <christophe@unlish.com>
parents: 9939
diff changeset
    20
    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
    21
        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
    22
            '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
    23
            'CONTENT_TYPE': 'text/plain',
10748
a704babdc840 [wsgi/test] use bytes instead of str for POST body
Julien Cristau <julien.cristau@logilab.fr>
parents: 10670
diff changeset
    24
            'wsgi.input': BytesIO(b'some content')})
9940
292f786009ba [wsgi] Re-set the request content after calling the inherited constructor.
Christophe de Vienne <christophe@unlish.com>
parents: 9939
diff changeset
    25
292f786009ba [wsgi] Re-set the request content after calling the inherited constructor.
Christophe de Vienne <christophe@unlish.com>
parents: 9939
diff changeset
    26
        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
    27
10748
a704babdc840 [wsgi/test] use bytes instead of str for POST body
Julien Cristau <julien.cristau@logilab.fr>
parents: 10670
diff changeset
    28
        self.assertEqual(b'some content', req.content.read())
9941
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    29
9942
4b99196102f0 [wsgi] Add missing import
Christophe de Vienne <christophe@unlish.com>
parents: 9941
diff changeset
    30
    def test_big_content(self):
10748
a704babdc840 [wsgi/test] use bytes instead of str for POST body
Julien Cristau <julien.cristau@logilab.fr>
parents: 10670
diff changeset
    31
        content = b'x'*100001
9942
4b99196102f0 [wsgi] Add missing import
Christophe de Vienne <christophe@unlish.com>
parents: 9941
diff changeset
    32
        r = webtest.app.TestRequest.blank('/', {
4b99196102f0 [wsgi] Add missing import
Christophe de Vienne <christophe@unlish.com>
parents: 9941
diff changeset
    33
            'CONTENT_LENGTH': len(content),
4b99196102f0 [wsgi] Add missing import
Christophe de Vienne <christophe@unlish.com>
parents: 9941
diff changeset
    34
            'CONTENT_TYPE': 'text/plain',
10748
a704babdc840 [wsgi/test] use bytes instead of str for POST body
Julien Cristau <julien.cristau@logilab.fr>
parents: 10670
diff changeset
    35
            'wsgi.input': BytesIO(content)})
9942
4b99196102f0 [wsgi] Add missing import
Christophe de Vienne <christophe@unlish.com>
parents: 9941
diff changeset
    36
4b99196102f0 [wsgi] Add missing import
Christophe de Vienne <christophe@unlish.com>
parents: 9941
diff changeset
    37
        req = CubicWebWsgiRequest(r.environ, self.vreg)
4b99196102f0 [wsgi] Add missing import
Christophe de Vienne <christophe@unlish.com>
parents: 9941
diff changeset
    38
4b99196102f0 [wsgi] Add missing import
Christophe de Vienne <christophe@unlish.com>
parents: 9941
diff changeset
    39
        self.assertEqual(content, req.content.read())
4b99196102f0 [wsgi] Add missing import
Christophe de Vienne <christophe@unlish.com>
parents: 9941
diff changeset
    40
9943
a4aeee690bff [wsgi] Set self.vreg
Christophe de Vienne <christophe@unlish.com>
parents: 9942
diff changeset
    41
    def test_post(self):
a4aeee690bff [wsgi] Set self.vreg
Christophe de Vienne <christophe@unlish.com>
parents: 9942
diff changeset
    42
        self.webapp.post(
a4aeee690bff [wsgi] Set self.vreg
Christophe de Vienne <christophe@unlish.com>
parents: 9942
diff changeset
    43
            '/',
a4aeee690bff [wsgi] Set self.vreg
Christophe de Vienne <christophe@unlish.com>
parents: 9942
diff changeset
    44
            params={'__login': self.admlogin, '__password': self.admpassword})
a4aeee690bff [wsgi] Set self.vreg
Christophe de Vienne <christophe@unlish.com>
parents: 9942
diff changeset
    45
9995
c9f1111e0ee8 [wsgi] If multipart cannot parse the POST content, let it pass.
Christophe de Vienne <christophe@unlish.com>
parents: 9988
diff changeset
    46
    def test_post_bad_form(self):
c9f1111e0ee8 [wsgi] If multipart cannot parse the POST content, let it pass.
Christophe de Vienne <christophe@unlish.com>
parents: 9988
diff changeset
    47
        with self.assertRaises(MultipartError):
c9f1111e0ee8 [wsgi] If multipart cannot parse the POST content, let it pass.
Christophe de Vienne <christophe@unlish.com>
parents: 9988
diff changeset
    48
            self.webapp.post(
c9f1111e0ee8 [wsgi] If multipart cannot parse the POST content, let it pass.
Christophe de Vienne <christophe@unlish.com>
parents: 9988
diff changeset
    49
                '/',
c9f1111e0ee8 [wsgi] If multipart cannot parse the POST content, let it pass.
Christophe de Vienne <christophe@unlish.com>
parents: 9988
diff changeset
    50
                params='badcontent',
c9f1111e0ee8 [wsgi] If multipart cannot parse the POST content, let it pass.
Christophe de Vienne <christophe@unlish.com>
parents: 9988
diff changeset
    51
                headers={'Content-Type': 'multipart/form-data'})
c9f1111e0ee8 [wsgi] If multipart cannot parse the POST content, let it pass.
Christophe de Vienne <christophe@unlish.com>
parents: 9988
diff changeset
    52
c9f1111e0ee8 [wsgi] If multipart cannot parse the POST content, let it pass.
Christophe de Vienne <christophe@unlish.com>
parents: 9988
diff changeset
    53
    def test_post_non_form(self):
c9f1111e0ee8 [wsgi] If multipart cannot parse the POST content, let it pass.
Christophe de Vienne <christophe@unlish.com>
parents: 9988
diff changeset
    54
        self.webapp.post(
c9f1111e0ee8 [wsgi] If multipart cannot parse the POST content, let it pass.
Christophe de Vienne <christophe@unlish.com>
parents: 9988
diff changeset
    55
            '/',
c9f1111e0ee8 [wsgi] If multipart cannot parse the POST content, let it pass.
Christophe de Vienne <christophe@unlish.com>
parents: 9988
diff changeset
    56
            params='{}',
c9f1111e0ee8 [wsgi] If multipart cannot parse the POST content, let it pass.
Christophe de Vienne <christophe@unlish.com>
parents: 9988
diff changeset
    57
            headers={'Content-Type': 'application/json'})
c9f1111e0ee8 [wsgi] If multipart cannot parse the POST content, let it pass.
Christophe de Vienne <christophe@unlish.com>
parents: 9988
diff changeset
    58
9944
9b3b21b7ff3e [wsgi] Fix multiple variables reading in params
Christophe de Vienne <christophe@unlish.com>
parents: 9943
diff changeset
    59
    def test_get_multiple_variables(self):
9b3b21b7ff3e [wsgi] Fix multiple variables reading in params
Christophe de Vienne <christophe@unlish.com>
parents: 9943
diff changeset
    60
        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
    61
        req = CubicWebWsgiRequest(r.environ, self.vreg)
9b3b21b7ff3e [wsgi] Fix multiple variables reading in params
Christophe de Vienne <christophe@unlish.com>
parents: 9943
diff changeset
    62
9b3b21b7ff3e [wsgi] Fix multiple variables reading in params
Christophe de Vienne <christophe@unlish.com>
parents: 9943
diff changeset
    63
        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
    64
9b3b21b7ff3e [wsgi] Fix multiple variables reading in params
Christophe de Vienne <christophe@unlish.com>
parents: 9943
diff changeset
    65
    def test_post_multiple_variables(self):
9b3b21b7ff3e [wsgi] Fix multiple variables reading in params
Christophe de Vienne <christophe@unlish.com>
parents: 9943
diff changeset
    66
        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
    67
        req = CubicWebWsgiRequest(r.environ, self.vreg)
9b3b21b7ff3e [wsgi] Fix multiple variables reading in params
Christophe de Vienne <christophe@unlish.com>
parents: 9943
diff changeset
    68
9b3b21b7ff3e [wsgi] Fix multiple variables reading in params
Christophe de Vienne <christophe@unlish.com>
parents: 9943
diff changeset
    69
        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
    70
9988
623707a0c404 [wsgi] Fix posted files filename reading
Christophe de Vienne <christophe@unlish.com>
parents: 9946
diff changeset
    71
    def test_post_files(self):
623707a0c404 [wsgi] Fix posted files filename reading
Christophe de Vienne <christophe@unlish.com>
parents: 9946
diff changeset
    72
        content_type, params = self.webapp.encode_multipart(
10748
a704babdc840 [wsgi/test] use bytes instead of str for POST body
Julien Cristau <julien.cristau@logilab.fr>
parents: 10670
diff changeset
    73
            (), (('filefield', 'aname', b'acontent'),))
9988
623707a0c404 [wsgi] Fix posted files filename reading
Christophe de Vienne <christophe@unlish.com>
parents: 9946
diff changeset
    74
        r = webtest.app.TestRequest.blank(
623707a0c404 [wsgi] Fix posted files filename reading
Christophe de Vienne <christophe@unlish.com>
parents: 9946
diff changeset
    75
            '/', POST=params, content_type=content_type)
623707a0c404 [wsgi] Fix posted files filename reading
Christophe de Vienne <christophe@unlish.com>
parents: 9946
diff changeset
    76
        req = CubicWebWsgiRequest(r.environ, self.vreg)
623707a0c404 [wsgi] Fix posted files filename reading
Christophe de Vienne <christophe@unlish.com>
parents: 9946
diff changeset
    77
        self.assertIn('filefield', req.form)
623707a0c404 [wsgi] Fix posted files filename reading
Christophe de Vienne <christophe@unlish.com>
parents: 9946
diff changeset
    78
        fieldvalue = req.form['filefield']
623707a0c404 [wsgi] Fix posted files filename reading
Christophe de Vienne <christophe@unlish.com>
parents: 9946
diff changeset
    79
        self.assertEqual(u'aname', fieldvalue[0])
10748
a704babdc840 [wsgi/test] use bytes instead of str for POST body
Julien Cristau <julien.cristau@logilab.fr>
parents: 10670
diff changeset
    80
        self.assertEqual(b'acontent', fieldvalue[1].read())
9988
623707a0c404 [wsgi] Fix posted files filename reading
Christophe de Vienne <christophe@unlish.com>
parents: 9946
diff changeset
    81
9946
ec88c1a1904a [wsgi] Fix unicode decoding in POST
Christophe de Vienne <christophe@unlish.com>
parents: 9944
diff changeset
    82
    def test_post_unicode_urlencoded(self):
ec88c1a1904a [wsgi] Fix unicode decoding in POST
Christophe de Vienne <christophe@unlish.com>
parents: 9944
diff changeset
    83
        params = 'arg=%C3%A9'
ec88c1a1904a [wsgi] Fix unicode decoding in POST
Christophe de Vienne <christophe@unlish.com>
parents: 9944
diff changeset
    84
        r = webtest.app.TestRequest.blank(
ec88c1a1904a [wsgi] Fix unicode decoding in POST
Christophe de Vienne <christophe@unlish.com>
parents: 9944
diff changeset
    85
            '/', 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
    86
        req = CubicWebWsgiRequest(r.environ, self.vreg)
ec88c1a1904a [wsgi] Fix unicode decoding in POST
Christophe de Vienne <christophe@unlish.com>
parents: 9944
diff changeset
    87
        self.assertEqual(u"é", req.form['arg'])
ec88c1a1904a [wsgi] Fix unicode decoding in POST
Christophe de Vienne <christophe@unlish.com>
parents: 9944
diff changeset
    88
10670
96380c1524b0 [wsgi/test] add __main__ section
Julien Cristau <julien.cristau@logilab.fr>
parents: 9995
diff changeset
    89
96380c1524b0 [wsgi/test] add __main__ section
Julien Cristau <julien.cristau@logilab.fr>
parents: 9995
diff changeset
    90
if __name__ == '__main__':
96380c1524b0 [wsgi/test] add __main__ section
Julien Cristau <julien.cristau@logilab.fr>
parents: 9995
diff changeset
    91
    import unittest
96380c1524b0 [wsgi/test] add __main__ section
Julien Cristau <julien.cristau@logilab.fr>
parents: 9995
diff changeset
    92
    unittest.main()