summaryrefslogtreecommitdiffhomepage
path: root/test/test_python_application.py
blob: 80854eb5f9d2083a3254ab784082a49a0e3c0c1b (plain) (blame)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
import time
import unittest
import unit

class TestUnitPythonApplication(unit.TestUnitControl):

    def setUpClass():
        unit.TestUnit().check_modules('python')

    def conf_with_name(self, name):
        self.conf({
            "listeners": {
                "*:7080": {
                    "application": "app"
                }
            },
            "applications": {
                "app": {
                    "type": "python",
                    "processes": { "spare": 0 },
                    "path": self.testdir + '/' + name,
                    "module": "wsgi"
                }
            }
        })

    def test_python_application_simple(self):
        code, name = """

def application(environ, start_response):

    content_length = int(environ.get('CONTENT_LENGTH', 0))
    body = bytes(environ['wsgi.input'].read(content_length))

    start_response('200', [
        ('Content-Type', environ.get('CONTENT_TYPE')),
        ('Content-Length', str(len(body))),
        ('Request-Method', environ.get('REQUEST_METHOD')),
        ('Request-Uri', environ.get('REQUEST_URI')),
        ('Http-Host', environ.get('HTTP_HOST')),
        ('Server-Protocol', environ.get('SERVER_PROTOCOL')),
        ('Custom-Header', environ.get('HTTP_CUSTOM_HEADER'))
    ])
    return [body]

""", 'py_app'

        self.python_application(name, code)
        self.conf_with_name(name)

        body = 'Test body string.'

        resp = self.post(headers={
            'Host': 'localhost',
            'Content-Type': 'text/html',
            'Custom-Header': 'blah'
        }, body=body)

        self.assertEqual(resp['status'], 200, 'status')
        headers = resp['headers']
        self.assertRegex(headers.pop('Server'), r'unit/[\d\.]+',
            'server header')
        self.assertLess(abs(time.mktime(time.gmtime()) -
            time.mktime(time.strptime(headers.pop('Date'),
            '%a, %d %b %Y %H:%M:%S GMT'))), 5, 'date header')
        self.assertDictEqual(headers, {
            'Content-Length': str(len(body)),
            'Content-Type': 'text/html',
            'Request-Method': 'POST',
            'Request-Uri': '/',
            'Http-Host': 'localhost',
            'Server-Protocol': 'HTTP/1.1',
            'Custom-Header': 'blah'
        }, 'headers')
        self.assertEqual(resp['body'], body, 'body')

    def test_python_application_query_string(self):
        code, name = """

def application(environ, start_response):

    start_response('200', [
        ('Content-Length', '0'),
        ('Query-String', environ.get('QUERY_STRING'))
    ])
    return []

""", 'py_app'

        self.python_application(name, code)
        self.conf_with_name(name)

        resp = self.get(url='/?var1=val1&var2=val2')

        self.assertEqual(resp['headers']['Query-String'], 'var1=val1&var2=val2',
            'Query-String header')

    @unittest.expectedFailure
    def test_python_application_server_port(self):
        code, name = """

def application(environ, start_response):

    start_response('200', [
        ('Content-Length', '0'),
        ('Server-Port', environ.get('SERVER_PORT'))
    ])
    return []

""", 'py_app'

        self.python_application(name, code)
        self.conf_with_name(name)

        self.assertEqual(self.get()['headers']['Server-Port'], '7080',
            'Server-Port header')

    @unittest.expectedFailure
    def test_python_application_204_transfer_encoding(self):
        code, name = """

def application(environ, start_response):

    start_response('204 No Content', [])
    return []

""", 'py_app'

        self.python_application(name, code)
        self.conf_with_name(name)

        self.assertNotIn('Transfer-Encoding', self.get()['headers'],
            '204 header transfer encoding')

if __name__ == '__main__':
    unittest.main()