xref: /unit/test/test_settings.py (revision 1019)
1730Szelenkov@nginx.comimport time
2730Szelenkov@nginx.comimport socket
3730Szelenkov@nginx.comimport unittest
4*1019Szelenkov@nginx.comfrom unit.applications.lang.python import TestApplicationPython
5730Szelenkov@nginx.com
61017Szelenkov@nginx.com
7*1019Szelenkov@nginx.comclass TestSettings(TestApplicationPython):
8730Szelenkov@nginx.com    def setUpClass():
9*1019Szelenkov@nginx.com        TestApplicationPython().check_modules('python')
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            read_timeout=1,
471017Szelenkov@nginx.com            raw=True,
481017Szelenkov@nginx.com            no_recv=True,
491017Szelenkov@nginx.com        )
50756Szelenkov@nginx.com
51756Szelenkov@nginx.com        time.sleep(2)
52756Szelenkov@nginx.com
531017Szelenkov@nginx.com        (resp, sock) = self.http(
541017Szelenkov@nginx.com            b"""Host: localhost
551017Szelenkov@nginx.com""",
561017Szelenkov@nginx.com            start=True,
571017Szelenkov@nginx.com            sock=sock,
581017Szelenkov@nginx.com            read_timeout=1,
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
881017Szelenkov@nginx.com        self.assertEqual(
891017Szelenkov@nginx.com            resp['status'], 408, 'status header read timeout update'
901017Szelenkov@nginx.com        )
91756Szelenkov@nginx.com
92730Szelenkov@nginx.com    def test_settings_body_read_timeout(self):
93730Szelenkov@nginx.com        self.load('empty')
94730Szelenkov@nginx.com
951017Szelenkov@nginx.com        self.conf({'http': {'body_read_timeout': 2}}, 'settings')
96730Szelenkov@nginx.com
971017Szelenkov@nginx.com        (resp, sock) = self.http(
981017Szelenkov@nginx.com            b"""POST / HTTP/1.1
99730Szelenkov@nginx.comHost: localhost
100730Szelenkov@nginx.comContent-Length: 10
101730Szelenkov@nginx.comConnection: close
102730Szelenkov@nginx.com
1031017Szelenkov@nginx.com""",
1041017Szelenkov@nginx.com            start=True,
1051017Szelenkov@nginx.com            raw_resp=True,
1061017Szelenkov@nginx.com            read_timeout=1,
1071017Szelenkov@nginx.com            raw=True,
1081017Szelenkov@nginx.com        )
109730Szelenkov@nginx.com
110730Szelenkov@nginx.com        time.sleep(3)
111730Szelenkov@nginx.com
112730Szelenkov@nginx.com        resp = self.http(b"""0123456789""", sock=sock, raw=True)
113730Szelenkov@nginx.com
114730Szelenkov@nginx.com        self.assertEqual(resp['status'], 408, 'status body read timeout')
115730Szelenkov@nginx.com
116756Szelenkov@nginx.com    def test_settings_body_read_timeout_update(self):
117756Szelenkov@nginx.com        self.load('empty')
118756Szelenkov@nginx.com
1191017Szelenkov@nginx.com        self.conf({'http': {'body_read_timeout': 4}}, 'settings')
120756Szelenkov@nginx.com
1211017Szelenkov@nginx.com        (resp, sock) = self.http(
1221017Szelenkov@nginx.com            b"""POST / HTTP/1.1
123756Szelenkov@nginx.comHost: localhost
124756Szelenkov@nginx.comContent-Length: 10
125756Szelenkov@nginx.comConnection: close
126756Szelenkov@nginx.com
1271017Szelenkov@nginx.com""",
1281017Szelenkov@nginx.com            start=True,
1291017Szelenkov@nginx.com            read_timeout=1,
1301017Szelenkov@nginx.com            raw=True,
1311017Szelenkov@nginx.com        )
132756Szelenkov@nginx.com
133756Szelenkov@nginx.com        time.sleep(2)
134756Szelenkov@nginx.com
1351017Szelenkov@nginx.com        (resp, sock) = self.http(
1361017Szelenkov@nginx.com            b"""012""", start=True, sock=sock, read_timeout=1, raw=True
1371017Szelenkov@nginx.com        )
138756Szelenkov@nginx.com
139756Szelenkov@nginx.com        time.sleep(2)
140756Szelenkov@nginx.com
1411017Szelenkov@nginx.com        (resp, sock) = self.http(
1421017Szelenkov@nginx.com            b"""345""", start=True, sock=sock, read_timeout=1, raw=True
1431017Szelenkov@nginx.com        )
144756Szelenkov@nginx.com
145756Szelenkov@nginx.com        time.sleep(2)
146756Szelenkov@nginx.com
147756Szelenkov@nginx.com        resp = self.http(b"""6789""", sock=sock, raw=True)
148756Szelenkov@nginx.com
1491017Szelenkov@nginx.com        self.assertEqual(
1501017Szelenkov@nginx.com            resp['status'], 200, 'status body read timeout update'
1511017Szelenkov@nginx.com        )
152756Szelenkov@nginx.com
153730Szelenkov@nginx.com    def test_settings_send_timeout(self):
154730Szelenkov@nginx.com        self.load('mirror')
155730Szelenkov@nginx.com
156730Szelenkov@nginx.com        data_len = 1048576
157730Szelenkov@nginx.com
1581017Szelenkov@nginx.com        self.conf({'http': {'send_timeout': 1}}, 'settings')
159730Szelenkov@nginx.com
160730Szelenkov@nginx.com        addr = self.testdir + '/sock'
161730Szelenkov@nginx.com
162760Szelenkov@nginx.com        self.conf({"unix:" + addr: {'application': 'mirror'}}, 'listeners')
163730Szelenkov@nginx.com
164730Szelenkov@nginx.com        sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
165730Szelenkov@nginx.com        sock.connect(addr)
166730Szelenkov@nginx.com
167730Szelenkov@nginx.com        req = """POST / HTTP/1.1
168730Szelenkov@nginx.comHost: localhost
169730Szelenkov@nginx.comContent-Type: text/html
170730Szelenkov@nginx.comContent-Length: %d
171970Szelenkov@nginx.comConnection: close
172730Szelenkov@nginx.com
1731017Szelenkov@nginx.com""" % data_len + (
1741017Szelenkov@nginx.com            'X' * data_len
1751017Szelenkov@nginx.com        )
176730Szelenkov@nginx.com
177730Szelenkov@nginx.com        sock.sendall(req.encode())
178730Szelenkov@nginx.com
179730Szelenkov@nginx.com        data = sock.recv(16).decode()
180730Szelenkov@nginx.com
181730Szelenkov@nginx.com        time.sleep(3)
182730Szelenkov@nginx.com
183738Szelenkov@nginx.com        data += self.recvall(sock).decode()
184730Szelenkov@nginx.com
185730Szelenkov@nginx.com        sock.close()
186730Szelenkov@nginx.com
187730Szelenkov@nginx.com        self.assertRegex(data, r'200 OK', 'status send timeout')
188730Szelenkov@nginx.com        self.assertLess(len(data), data_len, 'data send timeout')
189730Szelenkov@nginx.com
190730Szelenkov@nginx.com    def test_settings_idle_timeout(self):
191730Szelenkov@nginx.com        self.load('empty')
192730Szelenkov@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
219730Szelenkov@nginx.com    @unittest.expectedFailure
220730Szelenkov@nginx.com    def test_settings_negative_value(self):
2211017Szelenkov@nginx.com        self.assertIn(
2221017Szelenkov@nginx.com            'error',
2231017Szelenkov@nginx.com            self.conf({'http': {'max_body_size': -1}}, 'settings'),
2241017Szelenkov@nginx.com            'settings negative value',
2251017Szelenkov@nginx.com        )
2261017Szelenkov@nginx.com
227730Szelenkov@nginx.com
228730Szelenkov@nginx.comif __name__ == '__main__':
229*1019Szelenkov@nginx.com    TestSettings.main()
230