wsgi/test/unittest_wsgi.py
author Christophe de Vienne <christophe@unlish.com>
Tue, 02 Sep 2014 10:30:28 +0200
changeset 9941 8dc1c96d29f1
parent 9940 292f786009ba
child 9942 4b99196102f0
permissions -rw-r--r--
[wsgi] Fix https detection - 'HTTPS' is not part of wsgi, it is a CGI variable. The right wsgi variable is 'wsgi.url_scheme' - Force https if url starts with '/https/' (and remove the prefix from the path. Closes #4305985
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9939
46a8ed48636f [wsgi] Honor the 'CONTENT_TYPE' wsgi variable
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     1
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
     2
from StringIO import StringIO
9939
46a8ed48636f [wsgi] Honor the 'CONTENT_TYPE' wsgi variable
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     3
46a8ed48636f [wsgi] Honor the 'CONTENT_TYPE' wsgi variable
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     4
from cubicweb.devtools.webtest import CubicWebTestTC
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.wsgi.request import CubicWebWsgiRequest
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
46a8ed48636f [wsgi] Honor the 'CONTENT_TYPE' wsgi variable
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     9
class WSGIAppTC(CubicWebTestTC):
46a8ed48636f [wsgi] Honor the 'CONTENT_TYPE' wsgi variable
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    10
    def test_content_type(self):
46a8ed48636f [wsgi] Honor the 'CONTENT_TYPE' wsgi variable
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    11
        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
    12
46a8ed48636f [wsgi] Honor the 'CONTENT_TYPE' wsgi variable
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    13
        req = CubicWebWsgiRequest(r.environ, self.vreg)
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
        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
    16
292f786009ba [wsgi] Re-set the request content after calling the inherited constructor.
Christophe de Vienne <christophe@unlish.com>
parents: 9939
diff changeset
    17
    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
    18
        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
    19
            '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
    20
            '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
    21
            '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
    22
292f786009ba [wsgi] Re-set the request content after calling the inherited constructor.
Christophe de Vienne <christophe@unlish.com>
parents: 9939
diff changeset
    23
        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
    24
292f786009ba [wsgi] Re-set the request content after calling the inherited constructor.
Christophe de Vienne <christophe@unlish.com>
parents: 9939
diff changeset
    25
        self.assertEqual('some content', req.content.read())
9941
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    26
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    27
    def test_http_scheme(self):
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    28
        r = webtest.app.TestRequest.blank('/', {
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    29
            'wsgi.url_scheme': 'http'})
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    30
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    31
        req = CubicWebWsgiRequest(r.environ, self.vreg)
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
        self.assertFalse(req.https)
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
    def test_https_scheme(self):
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    36
        r = webtest.app.TestRequest.blank('/', {
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    37
            'wsgi.url_scheme': 'https'})
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    38
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    39
        req = CubicWebWsgiRequest(r.environ, self.vreg)
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
        self.assertTrue(req.https)
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
    def test_https_prefix(self):
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    44
        r = webtest.app.TestRequest.blank('/https/', {
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    45
            'wsgi.url_scheme': 'http'})
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    46
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    47
        req = CubicWebWsgiRequest(r.environ, self.vreg)
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
        self.assertTrue(req.https)
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
    @classmethod
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    52
    def init_config(cls, config):
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    53
        super(WSGIAppTC, cls).init_config(config)
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    54
        config.https_uiprops = None
8dc1c96d29f1 [wsgi] Fix https detection
Christophe de Vienne <christophe@unlish.com>
parents: 9940
diff changeset
    55
        config.https_datadir_url = None