xref: /unit/test/test_settings.py (revision 1635)
1*1635Szelenkov@nginx.comimport re
21477Szelenkov@nginx.comimport socket
3730Szelenkov@nginx.comimport time
41477Szelenkov@nginx.com
5*1635Szelenkov@nginx.comimport pytest
6*1635Szelenkov@nginx.com
71019Szelenkov@nginx.comfrom unit.applications.lang.python import TestApplicationPython
8730Szelenkov@nginx.com
91017Szelenkov@nginx.com
101019Szelenkov@nginx.comclass TestSettings(TestApplicationPython):
111467Szelenkov@nginx.com    prerequisites = {'modules': {'python': 'any'}}
12730Szelenkov@nginx.com
13730Szelenkov@nginx.com    def test_settings_header_read_timeout(self):
14730Szelenkov@nginx.com        self.load('empty')
15730Szelenkov@nginx.com
161017Szelenkov@nginx.com        self.conf({'http': {'header_read_timeout': 2}}, 'settings')
17730Szelenkov@nginx.com
181017Szelenkov@nginx.com        (resp, sock) = self.http(
191017Szelenkov@nginx.com            b"""GET / HTTP/1.1
201017Szelenkov@nginx.com""",
211017Szelenkov@nginx.com            start=True,
221017Szelenkov@nginx.com            read_timeout=1,
231017Szelenkov@nginx.com            raw=True,
241017Szelenkov@nginx.com        )
25730Szelenkov@nginx.com
26730Szelenkov@nginx.com        time.sleep(3)
27730Szelenkov@nginx.com
281017Szelenkov@nginx.com        resp = self.http(
291017Szelenkov@nginx.com            b"""Host: localhost
30730Szelenkov@nginx.comConnection: close
31730Szelenkov@nginx.com
321017Szelenkov@nginx.com""",
331017Szelenkov@nginx.com            sock=sock,
341017Szelenkov@nginx.com            raw=True,
351017Szelenkov@nginx.com        )
36730Szelenkov@nginx.com
371596Szelenkov@nginx.com        assert resp['status'] == 408, 'status header read timeout'
38730Szelenkov@nginx.com
39756Szelenkov@nginx.com    def test_settings_header_read_timeout_update(self):
40756Szelenkov@nginx.com        self.load('empty')
41756Szelenkov@nginx.com
421017Szelenkov@nginx.com        self.conf({'http': {'header_read_timeout': 4}}, 'settings')
43756Szelenkov@nginx.com
441017Szelenkov@nginx.com        (resp, sock) = self.http(
451017Szelenkov@nginx.com            b"""GET / HTTP/1.1
461017Szelenkov@nginx.com""",
471017Szelenkov@nginx.com            start=True,
481017Szelenkov@nginx.com            raw=True,
491017Szelenkov@nginx.com            no_recv=True,
501017Szelenkov@nginx.com        )
51756Szelenkov@nginx.com
52756Szelenkov@nginx.com        time.sleep(2)
53756Szelenkov@nginx.com
541017Szelenkov@nginx.com        (resp, sock) = self.http(
551017Szelenkov@nginx.com            b"""Host: localhost
561017Szelenkov@nginx.com""",
571017Szelenkov@nginx.com            start=True,
581017Szelenkov@nginx.com            sock=sock,
591017Szelenkov@nginx.com            raw=True,
601017Szelenkov@nginx.com            no_recv=True,
611017Szelenkov@nginx.com        )
62759Szelenkov@nginx.com
63759Szelenkov@nginx.com        time.sleep(2)
64759Szelenkov@nginx.com
651017Szelenkov@nginx.com        (resp, sock) = self.http(
661017Szelenkov@nginx.com            b"""X-Blah: blah
671017Szelenkov@nginx.com""",
681017Szelenkov@nginx.com            start=True,
691017Szelenkov@nginx.com            sock=sock,
701017Szelenkov@nginx.com            read_timeout=1,
711017Szelenkov@nginx.com            raw=True,
721017Szelenkov@nginx.com        )
73767Szelenkov@nginx.com
74767Szelenkov@nginx.com        if len(resp) != 0:
75767Szelenkov@nginx.com            sock.close()
76756Szelenkov@nginx.com
77767Szelenkov@nginx.com        else:
78767Szelenkov@nginx.com            time.sleep(2)
79756Szelenkov@nginx.com
801017Szelenkov@nginx.com            resp = self.http(
811017Szelenkov@nginx.com                b"""Connection: close
82756Szelenkov@nginx.com
831017Szelenkov@nginx.com""",
841017Szelenkov@nginx.com                sock=sock,
851017Szelenkov@nginx.com                raw=True,
861017Szelenkov@nginx.com            )
87756Szelenkov@nginx.com
881596Szelenkov@nginx.com        assert resp['status'] == 408, 'status header read timeout update'
89756Szelenkov@nginx.com
90730Szelenkov@nginx.com    def test_settings_body_read_timeout(self):
91730Szelenkov@nginx.com        self.load('empty')
92730Szelenkov@nginx.com
931017Szelenkov@nginx.com        self.conf({'http': {'body_read_timeout': 2}}, 'settings')
94730Szelenkov@nginx.com
951017Szelenkov@nginx.com        (resp, sock) = self.http(
961017Szelenkov@nginx.com            b"""POST / HTTP/1.1
97730Szelenkov@nginx.comHost: localhost
98730Szelenkov@nginx.comContent-Length: 10
99730Szelenkov@nginx.comConnection: close
100730Szelenkov@nginx.com
1011017Szelenkov@nginx.com""",
1021017Szelenkov@nginx.com            start=True,
1031017Szelenkov@nginx.com            raw_resp=True,
1041017Szelenkov@nginx.com            read_timeout=1,
1051017Szelenkov@nginx.com            raw=True,
1061017Szelenkov@nginx.com        )
107730Szelenkov@nginx.com
108730Szelenkov@nginx.com        time.sleep(3)
109730Szelenkov@nginx.com
110730Szelenkov@nginx.com        resp = self.http(b"""0123456789""", sock=sock, raw=True)
111730Szelenkov@nginx.com
1121596Szelenkov@nginx.com        assert resp['status'] == 408, 'status body read timeout'
113730Szelenkov@nginx.com
114756Szelenkov@nginx.com    def test_settings_body_read_timeout_update(self):
115756Szelenkov@nginx.com        self.load('empty')
116756Szelenkov@nginx.com
1171017Szelenkov@nginx.com        self.conf({'http': {'body_read_timeout': 4}}, 'settings')
118756Szelenkov@nginx.com
1191017Szelenkov@nginx.com        (resp, sock) = self.http(
1201017Szelenkov@nginx.com            b"""POST / HTTP/1.1
121756Szelenkov@nginx.comHost: localhost
122756Szelenkov@nginx.comContent-Length: 10
123756Szelenkov@nginx.comConnection: close
124756Szelenkov@nginx.com
1251017Szelenkov@nginx.com""",
1261017Szelenkov@nginx.com            start=True,
1271017Szelenkov@nginx.com            read_timeout=1,
1281017Szelenkov@nginx.com            raw=True,
1291017Szelenkov@nginx.com        )
130756Szelenkov@nginx.com
131756Szelenkov@nginx.com        time.sleep(2)
132756Szelenkov@nginx.com
1331017Szelenkov@nginx.com        (resp, sock) = self.http(
1341017Szelenkov@nginx.com            b"""012""", start=True, sock=sock, read_timeout=1, raw=True
1351017Szelenkov@nginx.com        )
136756Szelenkov@nginx.com
137756Szelenkov@nginx.com        time.sleep(2)
138756Szelenkov@nginx.com
1391017Szelenkov@nginx.com        (resp, sock) = self.http(
1401017Szelenkov@nginx.com            b"""345""", start=True, sock=sock, read_timeout=1, raw=True
1411017Szelenkov@nginx.com        )
142756Szelenkov@nginx.com
143756Szelenkov@nginx.com        time.sleep(2)
144756Szelenkov@nginx.com
145756Szelenkov@nginx.com        resp = self.http(b"""6789""", sock=sock, raw=True)
146756Szelenkov@nginx.com
1471596Szelenkov@nginx.com        assert resp['status'] == 200, 'status body read timeout update'
148756Szelenkov@nginx.com
149730Szelenkov@nginx.com    def test_settings_send_timeout(self):
150730Szelenkov@nginx.com        self.load('mirror')
151730Szelenkov@nginx.com
152730Szelenkov@nginx.com        data_len = 1048576
153730Szelenkov@nginx.com
1541017Szelenkov@nginx.com        self.conf({'http': {'send_timeout': 1}}, 'settings')
155730Szelenkov@nginx.com
1561596Szelenkov@nginx.com        addr = self.temp_dir + '/sock'
157730Szelenkov@nginx.com
158760Szelenkov@nginx.com        self.conf({"unix:" + addr: {'application': 'mirror'}}, 'listeners')
159730Szelenkov@nginx.com
160730Szelenkov@nginx.com        sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
161730Szelenkov@nginx.com        sock.connect(addr)
162730Szelenkov@nginx.com
163730Szelenkov@nginx.com        req = """POST / HTTP/1.1
164730Szelenkov@nginx.comHost: localhost
165730Szelenkov@nginx.comContent-Type: text/html
166730Szelenkov@nginx.comContent-Length: %d
167970Szelenkov@nginx.comConnection: close
168730Szelenkov@nginx.com
1691017Szelenkov@nginx.com""" % data_len + (
1701017Szelenkov@nginx.com            'X' * data_len
1711017Szelenkov@nginx.com        )
172730Szelenkov@nginx.com
173730Szelenkov@nginx.com        sock.sendall(req.encode())
174730Szelenkov@nginx.com
175730Szelenkov@nginx.com        data = sock.recv(16).decode()
176730Szelenkov@nginx.com
177730Szelenkov@nginx.com        time.sleep(3)
178730Szelenkov@nginx.com
179738Szelenkov@nginx.com        data += self.recvall(sock).decode()
180730Szelenkov@nginx.com
181730Szelenkov@nginx.com        sock.close()
182730Szelenkov@nginx.com
1831596Szelenkov@nginx.com        assert re.search(r'200 OK', data), 'status send timeout'
1841596Szelenkov@nginx.com        assert len(data) < data_len, 'data send timeout'
185730Szelenkov@nginx.com
186730Szelenkov@nginx.com    def test_settings_idle_timeout(self):
187730Szelenkov@nginx.com        self.load('empty')
188730Szelenkov@nginx.com
1891596Szelenkov@nginx.com        assert self.get()['status'] == 200, 'init'
1901029Szelenkov@nginx.com
1911017Szelenkov@nginx.com        self.conf({'http': {'idle_timeout': 2}}, 'settings')
192730Szelenkov@nginx.com
1931017Szelenkov@nginx.com        (resp, sock) = self.get(
1941017Szelenkov@nginx.com            headers={'Host': 'localhost', 'Connection': 'keep-alive'},
1951017Szelenkov@nginx.com            start=True,
1961017Szelenkov@nginx.com            read_timeout=1,
1971017Szelenkov@nginx.com        )
198730Szelenkov@nginx.com
199730Szelenkov@nginx.com        time.sleep(3)
200730Szelenkov@nginx.com
2011017Szelenkov@nginx.com        resp = self.get(
2021017Szelenkov@nginx.com            headers={'Host': 'localhost', 'Connection': 'close'}, sock=sock
2031017Szelenkov@nginx.com        )
204730Szelenkov@nginx.com
2051596Szelenkov@nginx.com        assert resp['status'] == 408, 'status idle timeout'
206730Szelenkov@nginx.com
2071606Szelenkov@nginx.com    def test_settings_idle_timeout_2(self):
2081606Szelenkov@nginx.com        self.load('empty')
2091606Szelenkov@nginx.com
2101606Szelenkov@nginx.com        assert self.get()['status'] == 200, 'init'
2111606Szelenkov@nginx.com
2121606Szelenkov@nginx.com        self.conf({'http': {'idle_timeout': 1}}, 'settings')
2131606Szelenkov@nginx.com
2141606Szelenkov@nginx.com        _, sock = self.http(b'', start=True, raw=True, no_recv=True)
2151606Szelenkov@nginx.com
2161613Szelenkov@nginx.com        time.sleep(3)
2171606Szelenkov@nginx.com
2181606Szelenkov@nginx.com        assert (
2191606Szelenkov@nginx.com            self.get(
2201606Szelenkov@nginx.com                headers={'Host': 'localhost', 'Connection': 'close'}, sock=sock
2211606Szelenkov@nginx.com            )['status']
2221606Szelenkov@nginx.com            == 408
2231606Szelenkov@nginx.com        ), 'status idle timeout'
2241606Szelenkov@nginx.com
225730Szelenkov@nginx.com    def test_settings_max_body_size(self):
226730Szelenkov@nginx.com        self.load('empty')
227730Szelenkov@nginx.com
2281017Szelenkov@nginx.com        self.conf({'http': {'max_body_size': 5}}, 'settings')
229730Szelenkov@nginx.com
2301596Szelenkov@nginx.com        assert self.post(body='01234')['status'] == 200, 'status size'
2311596Szelenkov@nginx.com        assert self.post(body='012345')['status'] == 413, 'status size max'
232730Szelenkov@nginx.com
2331365Szelenkov@nginx.com    def test_settings_max_body_size_large(self):
2341365Szelenkov@nginx.com        self.load('mirror')
2351365Szelenkov@nginx.com
2361365Szelenkov@nginx.com        self.conf({'http': {'max_body_size': 32 * 1024 * 1024}}, 'settings')
2371365Szelenkov@nginx.com
2381365Szelenkov@nginx.com        body = '0123456789abcdef' * 4 * 64 * 1024
2391365Szelenkov@nginx.com        resp = self.post(body=body, read_buffer_size=1024 * 1024)
2401596Szelenkov@nginx.com        assert resp['status'] == 200, 'status size 4'
2411596Szelenkov@nginx.com        assert resp['body'] == body, 'status body 4'
2421365Szelenkov@nginx.com
2431365Szelenkov@nginx.com        body = '0123456789abcdef' * 8 * 64 * 1024
2441365Szelenkov@nginx.com        resp = self.post(body=body, read_buffer_size=1024 * 1024)
2451596Szelenkov@nginx.com        assert resp['status'] == 200, 'status size 8'
2461596Szelenkov@nginx.com        assert resp['body'] == body, 'status body 8'
2471365Szelenkov@nginx.com
2481365Szelenkov@nginx.com        body = '0123456789abcdef' * 16 * 64 * 1024
2491365Szelenkov@nginx.com        resp = self.post(body=body, read_buffer_size=1024 * 1024)
2501596Szelenkov@nginx.com        assert resp['status'] == 200, 'status size 16'
2511596Szelenkov@nginx.com        assert resp['body'] == body, 'status body 16'
2521365Szelenkov@nginx.com
2531365Szelenkov@nginx.com        body = '0123456789abcdef' * 32 * 64 * 1024
2541365Szelenkov@nginx.com        resp = self.post(body=body, read_buffer_size=1024 * 1024)
2551596Szelenkov@nginx.com        assert resp['status'] == 200, 'status size 32'
2561596Szelenkov@nginx.com        assert resp['body'] == body, 'status body 32'
2571365Szelenkov@nginx.com
2581596Szelenkov@nginx.com    @pytest.mark.skip('not yet')
259730Szelenkov@nginx.com    def test_settings_negative_value(self):
2601596Szelenkov@nginx.com        assert 'error' in self.conf(
2611596Szelenkov@nginx.com            {'http': {'max_body_size': -1}}, 'settings'
2621596Szelenkov@nginx.com        ), 'settings negative value'
263