File: test_wsgi_compliance.py

package info (click to toggle)
python-wsgi-intercept 1.13.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 560 kB
  • sloc: python: 1,390; makefile: 56; sh: 5
file content (123 lines) | stat: -rw-r--r-- 4,772 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import sys
import pytest
from urllib.parse import unquote
from wsgi_intercept import httplib2_intercept
from . import wsgi_app
from .install import installer_class
import httplib2

HOST = 'some_hopefully_nonexistant_domain'

InstalledApp = installer_class(httplib2_intercept)


def test_simple_override():
    with InstalledApp(wsgi_app.simple_app, host=HOST) as app:
        http = httplib2.Http()
        resp, content = http.request(
            'http://some_hopefully_nonexistant_domain:80/', 'GET')
        assert app.success()


def test_simple_override_default_port():
    with InstalledApp(wsgi_app.simple_app, host=HOST) as app:
        http = httplib2.Http()
        resp, content = http.request(
            'http://some_hopefully_nonexistant_domain/', 'GET')
        assert app.success()


def test_https_in_environ():
    with InstalledApp(wsgi_app.simple_app, host=HOST, port=443) as app:
        http = httplib2.Http()
        resp, content = http.request(
            'https://some_hopefully_nonexistant_domain/', 'GET')
        assert app.success()
        internal_env = app.get_internals()
        assert internal_env['wsgi.url_scheme'] == 'https'


def test_more_interesting():
    expected_uri = ('/%E4%B8%96%E4%B8%8A%E5%8E%9F%E4%BE%86%E9%82%84%E6'
                    '%9C%89%E3%80%8C%E7%BE%9A%E7%89%9B%E3%80%8D%E9%80%99'
                    '%E7%A8%AE%E5%8B%95%E7%89%A9%EF%BC%81%2Fbarney'
                    '?bar=baz%20zoom')
    with InstalledApp(wsgi_app.more_interesting_app, host=HOST) as app:
        http = httplib2.Http()
        resp, content = http.request(
            'http://some_hopefully_nonexistant_domain' + expected_uri,
            'GET',
            headers={'Accept': 'application/json',
                     'Cookie': 'foo=bar'})
        internal_env = app.get_internals()

        expected_path_info = unquote(expected_uri.split('?')[0])
        assert internal_env['REQUEST_URI'] == expected_uri
        assert internal_env['RAW_URI'] == expected_uri
        assert internal_env['QUERY_STRING'] == 'bar=baz%20zoom'
        assert internal_env['HTTP_ACCEPT'] == 'application/json'
        assert internal_env['HTTP_COOKIE'] == 'foo=bar'
        # In this test we are ensuring the value, in the environ, of
        # a request header has a value which is a str, as native to
        # that version of Python. That means always a str, despite
        # the fact that a str in Python 2 and 3 are different
        # things! PEP 3333 requires this. isinstance is not used to
        # avoid confusion over class hierarchies.
        assert type(internal_env['HTTP_COOKIE']) == type('') # noqa E721

        # Do the rather painful wsgi encoding dance.
        if sys.version_info[0] > 2:
            assert internal_env['PATH_INFO'].encode('latin-1').decode(
                'UTF-8') == expected_path_info
        else:
            assert internal_env['PATH_INFO'].decode(
                'UTF-8') == expected_path_info.decode('UTF-8')


def test_script_name():
    with InstalledApp(wsgi_app.more_interesting_app, host=HOST,
                      script_name='/funky') as app:
        http = httplib2.Http()
        response, content = http.request(
            'http://some_hopefully_nonexistant_domain/funky/boom/baz')
        internal_env = app.get_internals()

        assert internal_env['SCRIPT_NAME'] == '/funky'
        assert internal_env['PATH_INFO'] == '/boom/baz'


def test_encoding_errors():
    with InstalledApp(wsgi_app.more_interesting_app, host=HOST):
        http = httplib2.Http()
        with pytest.raises(UnicodeEncodeError):
            response, content = http.request(
                'http://some_hopefully_nonexistant_domain/boom/baz',
                headers={'Accept': u'application/\u2603'})


def test_post_status_headers():
    with InstalledApp(wsgi_app.post_status_headers_app, host=HOST) as app:
        http = httplib2.Http()
        resp, content = http.request(
            'http://some_hopefully_nonexistant_domain/', 'GET')
        assert app.success()
        assert resp.get('content-type') == 'text/plain'


def test_empty_iterator():
    with InstalledApp(wsgi_app.empty_string_app, host=HOST) as app:
        http = httplib2.Http()
        resp, content = http.request(
            'http://some_hopefully_nonexistant_domain/', 'GET')
        assert app.success()
        assert content == b'second'


def test_generator():
    with InstalledApp(wsgi_app.generator_app, host=HOST) as app:
        http = httplib2.Http()
        resp, content = http.request(
            'http://some_hopefully_nonexistant_domain/', 'GET')
        assert app.success()
        assert resp.get('content-type') == 'text/plain'
        assert content == b'First generated line\nSecond generated line\n'