summaryrefslogtreecommitdiffhomepage
path: root/test/test_settings.py
blob: d272f7011c6814a18f442c47e1d4fd32ef1e3e21 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import time
import socket
import unittest
import unit

class TestUnitSettings(unit.TestUnitApplicationPython):

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

    def test_settings_header_read_timeout(self):
        self.load('empty')

        self.conf({'http': { 'header_read_timeout': 2 }}, 'settings')

        (resp, sock) = self.http(b"""GET / HTTP/1.1
""", start=True, raw=True)

        time.sleep(3)

        resp = self.http(b"""Host: localhost
Connection: close

""", sock=sock, raw=True)

        self.assertEqual(resp['status'], 408, 'status header read timeout')

    def test_settings_header_read_timeout_update(self):
        self.load('empty')

        self.conf({'http': { 'header_read_timeout': 4 }}, 'settings')

        (resp, sock) = self.http(b"""GET / HTTP/1.1
""", start=True, raw=True, no_recv=True)

        time.sleep(2)

        (resp, sock) = self.http(b"""Host: localhost
""", start=True, sock=sock, raw=True, no_recv=True)

        time.sleep(2)

        (resp, sock) = self.http(b"""X-Blah: blah
""", start=True, sock=sock, raw=True)

        if len(resp) != 0:
            sock.close()

        else:
            time.sleep(2)

            resp = self.http(b"""Connection: close

""", sock=sock, raw=True)

        self.assertEqual(resp['status'], 408,
            'status header read timeout update')

    def test_settings_body_read_timeout(self):
        self.load('empty')

        self.conf({'http': { 'body_read_timeout': 2 }}, 'settings')

        (resp, sock) = self.http(b"""POST / HTTP/1.1
Host: localhost
Content-Length: 10
Connection: close

""", start=True, raw_resp=True, raw=True)

        time.sleep(3)

        resp = self.http(b"""0123456789""", sock=sock, raw=True)

        self.assertEqual(resp['status'], 408, 'status body read timeout')

    def test_settings_body_read_timeout_update(self):
        self.load('empty')

        self.conf({'http': { 'body_read_timeout': 4 }}, 'settings')

        (resp, sock) = self.http(b"""POST / HTTP/1.1
Host: localhost
Content-Length: 10
Connection: close

""", start=True, raw=True)

        time.sleep(2)

        (resp, sock) = self.http(b"""012""", start=True, sock=sock, raw=True)

        time.sleep(2)

        (resp, sock) = self.http(b"""345""", start=True, sock=sock, raw=True)

        time.sleep(2)

        resp = self.http(b"""6789""", sock=sock, raw=True)

        self.assertEqual(resp['status'], 200, 'status body read timeout update')

    def test_settings_send_timeout(self):
        self.load('mirror')

        data_len = 1048576

        self.conf({'http': { 'send_timeout': 1 }}, 'settings')

        addr = self.testdir + '/sock'

        self.conf({"unix:" + addr: {'application': 'mirror'}}, 'listeners')

        sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        sock.connect(addr)

        req = """POST / HTTP/1.1
Host: localhost
Content-Type: text/html
Content-Length: %d

""" % data_len + ('X' * data_len)

        sock.sendall(req.encode())

        data = sock.recv(16).decode()

        time.sleep(3)

        data += self.recvall(sock).decode()

        sock.close()

        self.assertRegex(data, r'200 OK', 'status send timeout')
        self.assertLess(len(data), data_len, 'data send timeout')

    def test_settings_idle_timeout(self):
        self.load('empty')

        self.conf({'http': { 'idle_timeout': 2 }}, 'settings')

        (resp, sock) = self.get(headers={
            'Connection': 'keep-alive',
            'Host': 'localhost'
        }, start=True)

        time.sleep(3)

        resp = self.get(headers={
            'Connection': 'close',
            'Host': 'localhost'
        }, sock=sock)

        self.assertEqual(resp['status'], 408, 'status idle timeout')

    def test_settings_max_body_size(self):
        self.load('empty')

        self.conf({'http': { 'max_body_size': 5 }}, 'settings')

        self.assertEqual(self.post(body='01234')['status'], 200, 'status size')
        self.assertEqual(self.post(body='012345')['status'], 413,
            'status size max')

    @unittest.expectedFailure
    def test_settings_negative_value(self):
        self.assertIn('error', self.conf({'http': { 'max_body_size': -1 }},
            'settings'), 'settings negative value')

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