xref: /unit/test/test_settings.py (revision 1477)
1*1477Szelenkov@nginx.comimport socket
2730Szelenkov@nginx.comimport time
3730Szelenkov@nginx.comimport unittest
4*1477Szelenkov@nginx.com
51019Szelenkov@nginx.comfrom unit.applications.lang.python import TestApplicationPython
6730Szelenkov@nginx.com
71017Szelenkov@nginx.com
81019Szelenkov@nginx.comclass TestSettings(TestApplicationPython):
91467Szelenkov@nginx.com    prerequisites = {'modules': {'python': 'any'}}
10730Szelenkov@nginx.com
11730Szelenkov@nginx.com    def test_settings_header_read_timeout(self):
12730Szelenkov@nginx.com        self.load('empty')
13730Szelenkov@nginx.com
141017Szelenkov@nginx.com        self.conf({'http': {'header_read_timeout': 2}}, 'settings')
15730Szelenkov@nginx.com
161017Szelenkov@nginx.com        (resp, sock) = self.http(
171017Szelenkov@nginx.com            b"""GET / HTTP/1.1
181017Szelenkov@nginx.com""",
191017Szelenkov@nginx.com            start=True,
201017Szelenkov@nginx.com            read_timeout=1,
211017Szelenkov@nginx.com            raw=True,
221017Szelenkov@nginx.com        )
23730Szelenkov@nginx.com
24730Szelenkov@nginx.com        time.sleep(3)
25730Szelenkov@nginx.com
261017Szelenkov@nginx.com        resp = self.http(
271017Szelenkov@nginx.com            b"""Host: localhost
28730Szelenkov@nginx.comConnection: close
29730Szelenkov@nginx.com
301017Szelenkov@nginx.com""",
311017Szelenkov@nginx.com            sock=sock,
321017Szelenkov@nginx.com            raw=True,
331017Szelenkov@nginx.com        )
34730Szelenkov@nginx.com
35730Szelenkov@nginx.com        self.assertEqual(resp['status'], 408, 'status header read timeout')
36730Szelenkov@nginx.com
37756Szelenkov@nginx.com    def test_settings_header_read_timeout_update(self):
38756Szelenkov@nginx.com        self.load('empty')
39756Szelenkov@nginx.com
401017Szelenkov@nginx.com        self.conf({'http': {'header_read_timeout': 4}}, 'settings')
41756Szelenkov@nginx.com
421017Szelenkov@nginx.com        (resp, sock) = self.http(
431017Szelenkov@nginx.com            b"""GET / HTTP/1.1
441017Szelenkov@nginx.com""",
451017Szelenkov@nginx.com            start=True,
461017Szelenkov@nginx.com            raw=True,
471017Szelenkov@nginx.com            no_recv=True,
481017Szelenkov@nginx.com        )
49756Szelenkov@nginx.com
50756Szelenkov@nginx.com        time.sleep(2)
51756Szelenkov@nginx.com
521017Szelenkov@nginx.com        (resp, sock) = self.http(
531017Szelenkov@nginx.com            b"""Host: localhost
541017Szelenkov@nginx.com""",
551017Szelenkov@nginx.com            start=True,
561017Szelenkov@nginx.com            sock=sock,
571017Szelenkov@nginx.com            raw=True,
581017Szelenkov@nginx.com            no_recv=True,
591017Szelenkov@nginx.com        )
60759Szelenkov@nginx.com
61759Szelenkov@nginx.com        time.sleep(2)
62759Szelenkov@nginx.com
631017Szelenkov@nginx.com        (resp, sock) = self.http(
641017Szelenkov@nginx.com            b"""X-Blah: blah
651017Szelenkov@nginx.com""",
661017Szelenkov@nginx.com            start=True,
671017Szelenkov@nginx.com            sock=sock,
681017Szelenkov@nginx.com            read_timeout=1,
691017Szelenkov@nginx.com            raw=True,
701017Szelenkov@nginx.com        )
71767Szelenkov@nginx.com
72767Szelenkov@nginx.com        if len(resp) != 0:
73767Szelenkov@nginx.com            sock.close()
74756Szelenkov@nginx.com
75767Szelenkov@nginx.com        else:
76767Szelenkov@nginx.com            time.sleep(2)
77756Szelenkov@nginx.com
781017Szelenkov@nginx.com            resp = self.http(
791017Szelenkov@nginx.com                b"""Connection: close
80756Szelenkov@nginx.com
811017Szelenkov@nginx.com""",
821017Szelenkov@nginx.com                sock=sock,
831017Szelenkov@nginx.com                raw=True,
841017Szelenkov@nginx.com            )
85756Szelenkov@nginx.com
861017Szelenkov@nginx.com        self.assertEqual(
871017Szelenkov@nginx.com            resp['status'], 408, 'status header read timeout update'
881017Szelenkov@nginx.com        )
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
112730Szelenkov@nginx.com        self.assertEqual(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
1471017Szelenkov@nginx.com        self.assertEqual(
1481017Szelenkov@nginx.com            resp['status'], 200, 'status body read timeout update'
1491017Szelenkov@nginx.com        )
150756Szelenkov@nginx.com
151730Szelenkov@nginx.com    def test_settings_send_timeout(self):
152730Szelenkov@nginx.com        self.load('mirror')
153730Szelenkov@nginx.com
154730Szelenkov@nginx.com        data_len = 1048576
155730Szelenkov@nginx.com
1561017Szelenkov@nginx.com        self.conf({'http': {'send_timeout': 1}}, 'settings')
157730Szelenkov@nginx.com
158730Szelenkov@nginx.com        addr = self.testdir + '/sock'
159730Szelenkov@nginx.com
160760Szelenkov@nginx.com        self.conf({"unix:" + addr: {'application': 'mirror'}}, 'listeners')
161730Szelenkov@nginx.com
162730Szelenkov@nginx.com        sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
163730Szelenkov@nginx.com        sock.connect(addr)
164730Szelenkov@nginx.com
165730Szelenkov@nginx.com        req = """POST / HTTP/1.1
166730Szelenkov@nginx.comHost: localhost
167730Szelenkov@nginx.comContent-Type: text/html
168730Szelenkov@nginx.comContent-Length: %d
169970Szelenkov@nginx.comConnection: close
170730Szelenkov@nginx.com
1711017Szelenkov@nginx.com""" % data_len + (
1721017Szelenkov@nginx.com            'X' * data_len
1731017Szelenkov@nginx.com        )
174730Szelenkov@nginx.com
175730Szelenkov@nginx.com        sock.sendall(req.encode())
176730Szelenkov@nginx.com
177730Szelenkov@nginx.com        data = sock.recv(16).decode()
178730Szelenkov@nginx.com
179730Szelenkov@nginx.com        time.sleep(3)
180730Szelenkov@nginx.com
181738Szelenkov@nginx.com        data += self.recvall(sock).decode()
182730Szelenkov@nginx.com
183730Szelenkov@nginx.com        sock.close()
184730Szelenkov@nginx.com
185730Szelenkov@nginx.com        self.assertRegex(data, r'200 OK', 'status send timeout')
186730Szelenkov@nginx.com        self.assertLess(len(data), data_len, 'data send timeout')
187730Szelenkov@nginx.com
188730Szelenkov@nginx.com    def test_settings_idle_timeout(self):
189730Szelenkov@nginx.com        self.load('empty')
190730Szelenkov@nginx.com
1911029Szelenkov@nginx.com        self.assertEqual(self.get()['status'], 200, 'init')
1921029Szelenkov@nginx.com
1931017Szelenkov@nginx.com        self.conf({'http': {'idle_timeout': 2}}, 'settings')
194730Szelenkov@nginx.com
1951017Szelenkov@nginx.com        (resp, sock) = self.get(
1961017Szelenkov@nginx.com            headers={'Host': 'localhost', 'Connection': 'keep-alive'},
1971017Szelenkov@nginx.com            start=True,
1981017Szelenkov@nginx.com            read_timeout=1,
1991017Szelenkov@nginx.com        )
200730Szelenkov@nginx.com
201730Szelenkov@nginx.com        time.sleep(3)
202730Szelenkov@nginx.com
2031017Szelenkov@nginx.com        resp = self.get(
2041017Szelenkov@nginx.com            headers={'Host': 'localhost', 'Connection': 'close'}, sock=sock
2051017Szelenkov@nginx.com        )
206730Szelenkov@nginx.com
207755Szelenkov@nginx.com        self.assertEqual(resp['status'], 408, 'status idle timeout')
208730Szelenkov@nginx.com
209730Szelenkov@nginx.com    def test_settings_max_body_size(self):
210730Szelenkov@nginx.com        self.load('empty')
211730Szelenkov@nginx.com
2121017Szelenkov@nginx.com        self.conf({'http': {'max_body_size': 5}}, 'settings')
213730Szelenkov@nginx.com
214730Szelenkov@nginx.com        self.assertEqual(self.post(body='01234')['status'], 200, 'status size')
2151017Szelenkov@nginx.com        self.assertEqual(
2161017Szelenkov@nginx.com            self.post(body='012345')['status'], 413, 'status size max'
2171017Szelenkov@nginx.com        )
218730Szelenkov@nginx.com
2191365Szelenkov@nginx.com    def test_settings_max_body_size_large(self):
2201365Szelenkov@nginx.com        self.load('mirror')
2211365Szelenkov@nginx.com
2221365Szelenkov@nginx.com        self.conf({'http': {'max_body_size': 32 * 1024 * 1024}}, 'settings')
2231365Szelenkov@nginx.com
2241365Szelenkov@nginx.com        body = '0123456789abcdef' * 4 * 64 * 1024
2251365Szelenkov@nginx.com        resp = self.post(body=body, read_buffer_size=1024 * 1024)
2261365Szelenkov@nginx.com        self.assertEqual(resp['status'], 200, 'status size 4')
2271365Szelenkov@nginx.com        self.assertEqual(resp['body'], body, 'status body 4')
2281365Szelenkov@nginx.com
2291365Szelenkov@nginx.com        body = '0123456789abcdef' * 8 * 64 * 1024
2301365Szelenkov@nginx.com        resp = self.post(body=body, read_buffer_size=1024 * 1024)
2311365Szelenkov@nginx.com        self.assertEqual(resp['status'], 200, 'status size 8')
2321365Szelenkov@nginx.com        self.assertEqual(resp['body'], body, 'status body 8')
2331365Szelenkov@nginx.com
2341365Szelenkov@nginx.com        body = '0123456789abcdef' * 16 * 64 * 1024
2351365Szelenkov@nginx.com        resp = self.post(body=body, read_buffer_size=1024 * 1024)
2361365Szelenkov@nginx.com        self.assertEqual(resp['status'], 200, 'status size 16')
2371365Szelenkov@nginx.com        self.assertEqual(resp['body'], body, 'status body 16')
2381365Szelenkov@nginx.com
2391365Szelenkov@nginx.com        body = '0123456789abcdef' * 32 * 64 * 1024
2401365Szelenkov@nginx.com        resp = self.post(body=body, read_buffer_size=1024 * 1024)
2411365Szelenkov@nginx.com        self.assertEqual(resp['status'], 200, 'status size 32')
2421365Szelenkov@nginx.com        self.assertEqual(resp['body'], body, 'status body 32')
2431365Szelenkov@nginx.com
2441064Szelenkov@nginx.com    @unittest.skip('not yet')
245730Szelenkov@nginx.com    def test_settings_negative_value(self):
2461017Szelenkov@nginx.com        self.assertIn(
2471017Szelenkov@nginx.com            'error',
2481017Szelenkov@nginx.com            self.conf({'http': {'max_body_size': -1}}, 'settings'),
2491017Szelenkov@nginx.com            'settings negative value',
2501017Szelenkov@nginx.com        )
2511017Szelenkov@nginx.com
252730Szelenkov@nginx.com
253730Szelenkov@nginx.comif __name__ == '__main__':
2541019Szelenkov@nginx.com    TestSettings.main()
255