xref: /unit/test/test_settings.py (revision 1029)
1730Szelenkov@nginx.comimport time
2730Szelenkov@nginx.comimport socket
3730Szelenkov@nginx.comimport unittest
41019Szelenkov@nginx.comfrom unit.applications.lang.python import TestApplicationPython
5730Szelenkov@nginx.com
61017Szelenkov@nginx.com
71019Szelenkov@nginx.comclass TestSettings(TestApplicationPython):
81025Szelenkov@nginx.com    prerequisites = ['python']
9730Szelenkov@nginx.com
10730Szelenkov@nginx.com    def test_settings_header_read_timeout(self):
11730Szelenkov@nginx.com        self.load('empty')
12730Szelenkov@nginx.com
131017Szelenkov@nginx.com        self.conf({'http': {'header_read_timeout': 2}}, 'settings')
14730Szelenkov@nginx.com
151017Szelenkov@nginx.com        (resp, sock) = self.http(
161017Szelenkov@nginx.com            b"""GET / HTTP/1.1
171017Szelenkov@nginx.com""",
181017Szelenkov@nginx.com            start=True,
191017Szelenkov@nginx.com            read_timeout=1,
201017Szelenkov@nginx.com            raw=True,
211017Szelenkov@nginx.com        )
22730Szelenkov@nginx.com
23730Szelenkov@nginx.com        time.sleep(3)
24730Szelenkov@nginx.com
251017Szelenkov@nginx.com        resp = self.http(
261017Szelenkov@nginx.com            b"""Host: localhost
27730Szelenkov@nginx.comConnection: close
28730Szelenkov@nginx.com
291017Szelenkov@nginx.com""",
301017Szelenkov@nginx.com            sock=sock,
311017Szelenkov@nginx.com            raw=True,
321017Szelenkov@nginx.com        )
33730Szelenkov@nginx.com
34730Szelenkov@nginx.com        self.assertEqual(resp['status'], 408, 'status header read timeout')
35730Szelenkov@nginx.com
36756Szelenkov@nginx.com    def test_settings_header_read_timeout_update(self):
37756Szelenkov@nginx.com        self.load('empty')
38756Szelenkov@nginx.com
391017Szelenkov@nginx.com        self.conf({'http': {'header_read_timeout': 4}}, 'settings')
40756Szelenkov@nginx.com
411017Szelenkov@nginx.com        (resp, sock) = self.http(
421017Szelenkov@nginx.com            b"""GET / HTTP/1.1
431017Szelenkov@nginx.com""",
441017Szelenkov@nginx.com            start=True,
451017Szelenkov@nginx.com            raw=True,
461017Szelenkov@nginx.com            no_recv=True,
471017Szelenkov@nginx.com        )
48756Szelenkov@nginx.com
49756Szelenkov@nginx.com        time.sleep(2)
50756Szelenkov@nginx.com
511017Szelenkov@nginx.com        (resp, sock) = self.http(
521017Szelenkov@nginx.com            b"""Host: localhost
531017Szelenkov@nginx.com""",
541017Szelenkov@nginx.com            start=True,
551017Szelenkov@nginx.com            sock=sock,
561017Szelenkov@nginx.com            raw=True,
571017Szelenkov@nginx.com            no_recv=True,
581017Szelenkov@nginx.com        )
59759Szelenkov@nginx.com
60759Szelenkov@nginx.com        time.sleep(2)
61759Szelenkov@nginx.com
621017Szelenkov@nginx.com        (resp, sock) = self.http(
631017Szelenkov@nginx.com            b"""X-Blah: blah
641017Szelenkov@nginx.com""",
651017Szelenkov@nginx.com            start=True,
661017Szelenkov@nginx.com            sock=sock,
671017Szelenkov@nginx.com            read_timeout=1,
681017Szelenkov@nginx.com            raw=True,
691017Szelenkov@nginx.com        )
70767Szelenkov@nginx.com
71767Szelenkov@nginx.com        if len(resp) != 0:
72767Szelenkov@nginx.com            sock.close()
73756Szelenkov@nginx.com
74767Szelenkov@nginx.com        else:
75767Szelenkov@nginx.com            time.sleep(2)
76756Szelenkov@nginx.com
771017Szelenkov@nginx.com            resp = self.http(
781017Szelenkov@nginx.com                b"""Connection: close
79756Szelenkov@nginx.com
801017Szelenkov@nginx.com""",
811017Szelenkov@nginx.com                sock=sock,
821017Szelenkov@nginx.com                raw=True,
831017Szelenkov@nginx.com            )
84756Szelenkov@nginx.com
851017Szelenkov@nginx.com        self.assertEqual(
861017Szelenkov@nginx.com            resp['status'], 408, 'status header read timeout update'
871017Szelenkov@nginx.com        )
88756Szelenkov@nginx.com
89730Szelenkov@nginx.com    def test_settings_body_read_timeout(self):
90730Szelenkov@nginx.com        self.load('empty')
91730Szelenkov@nginx.com
921017Szelenkov@nginx.com        self.conf({'http': {'body_read_timeout': 2}}, 'settings')
93730Szelenkov@nginx.com
941017Szelenkov@nginx.com        (resp, sock) = self.http(
951017Szelenkov@nginx.com            b"""POST / HTTP/1.1
96730Szelenkov@nginx.comHost: localhost
97730Szelenkov@nginx.comContent-Length: 10
98730Szelenkov@nginx.comConnection: close
99730Szelenkov@nginx.com
1001017Szelenkov@nginx.com""",
1011017Szelenkov@nginx.com            start=True,
1021017Szelenkov@nginx.com            raw_resp=True,
1031017Szelenkov@nginx.com            read_timeout=1,
1041017Szelenkov@nginx.com            raw=True,
1051017Szelenkov@nginx.com        )
106730Szelenkov@nginx.com
107730Szelenkov@nginx.com        time.sleep(3)
108730Szelenkov@nginx.com
109730Szelenkov@nginx.com        resp = self.http(b"""0123456789""", sock=sock, raw=True)
110730Szelenkov@nginx.com
111730Szelenkov@nginx.com        self.assertEqual(resp['status'], 408, 'status body read timeout')
112730Szelenkov@nginx.com
113756Szelenkov@nginx.com    def test_settings_body_read_timeout_update(self):
114756Szelenkov@nginx.com        self.load('empty')
115756Szelenkov@nginx.com
1161017Szelenkov@nginx.com        self.conf({'http': {'body_read_timeout': 4}}, 'settings')
117756Szelenkov@nginx.com
1181017Szelenkov@nginx.com        (resp, sock) = self.http(
1191017Szelenkov@nginx.com            b"""POST / HTTP/1.1
120756Szelenkov@nginx.comHost: localhost
121756Szelenkov@nginx.comContent-Length: 10
122756Szelenkov@nginx.comConnection: close
123756Szelenkov@nginx.com
1241017Szelenkov@nginx.com""",
1251017Szelenkov@nginx.com            start=True,
1261017Szelenkov@nginx.com            read_timeout=1,
1271017Szelenkov@nginx.com            raw=True,
1281017Szelenkov@nginx.com        )
129756Szelenkov@nginx.com
130756Szelenkov@nginx.com        time.sleep(2)
131756Szelenkov@nginx.com
1321017Szelenkov@nginx.com        (resp, sock) = self.http(
1331017Szelenkov@nginx.com            b"""012""", start=True, sock=sock, read_timeout=1, raw=True
1341017Szelenkov@nginx.com        )
135756Szelenkov@nginx.com
136756Szelenkov@nginx.com        time.sleep(2)
137756Szelenkov@nginx.com
1381017Szelenkov@nginx.com        (resp, sock) = self.http(
1391017Szelenkov@nginx.com            b"""345""", start=True, sock=sock, read_timeout=1, raw=True
1401017Szelenkov@nginx.com        )
141756Szelenkov@nginx.com
142756Szelenkov@nginx.com        time.sleep(2)
143756Szelenkov@nginx.com
144756Szelenkov@nginx.com        resp = self.http(b"""6789""", sock=sock, raw=True)
145756Szelenkov@nginx.com
1461017Szelenkov@nginx.com        self.assertEqual(
1471017Szelenkov@nginx.com            resp['status'], 200, 'status body read timeout update'
1481017Szelenkov@nginx.com        )
149756Szelenkov@nginx.com
150730Szelenkov@nginx.com    def test_settings_send_timeout(self):
151730Szelenkov@nginx.com        self.load('mirror')
152730Szelenkov@nginx.com
153730Szelenkov@nginx.com        data_len = 1048576
154730Szelenkov@nginx.com
1551017Szelenkov@nginx.com        self.conf({'http': {'send_timeout': 1}}, 'settings')
156730Szelenkov@nginx.com
157730Szelenkov@nginx.com        addr = self.testdir + '/sock'
158730Szelenkov@nginx.com
159760Szelenkov@nginx.com        self.conf({"unix:" + addr: {'application': 'mirror'}}, 'listeners')
160730Szelenkov@nginx.com
161730Szelenkov@nginx.com        sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
162730Szelenkov@nginx.com        sock.connect(addr)
163730Szelenkov@nginx.com
164730Szelenkov@nginx.com        req = """POST / HTTP/1.1
165730Szelenkov@nginx.comHost: localhost
166730Szelenkov@nginx.comContent-Type: text/html
167730Szelenkov@nginx.comContent-Length: %d
168970Szelenkov@nginx.comConnection: close
169730Szelenkov@nginx.com
1701017Szelenkov@nginx.com""" % data_len + (
1711017Szelenkov@nginx.com            'X' * data_len
1721017Szelenkov@nginx.com        )
173730Szelenkov@nginx.com
174730Szelenkov@nginx.com        sock.sendall(req.encode())
175730Szelenkov@nginx.com
176730Szelenkov@nginx.com        data = sock.recv(16).decode()
177730Szelenkov@nginx.com
178730Szelenkov@nginx.com        time.sleep(3)
179730Szelenkov@nginx.com
180738Szelenkov@nginx.com        data += self.recvall(sock).decode()
181730Szelenkov@nginx.com
182730Szelenkov@nginx.com        sock.close()
183730Szelenkov@nginx.com
184730Szelenkov@nginx.com        self.assertRegex(data, r'200 OK', 'status send timeout')
185730Szelenkov@nginx.com        self.assertLess(len(data), data_len, 'data send timeout')
186730Szelenkov@nginx.com
187730Szelenkov@nginx.com    def test_settings_idle_timeout(self):
188730Szelenkov@nginx.com        self.load('empty')
189730Szelenkov@nginx.com
190*1029Szelenkov@nginx.com        self.assertEqual(self.get()['status'], 200, 'init')
191*1029Szelenkov@nginx.com
1921017Szelenkov@nginx.com        self.conf({'http': {'idle_timeout': 2}}, 'settings')
193730Szelenkov@nginx.com
1941017Szelenkov@nginx.com        (resp, sock) = self.get(
1951017Szelenkov@nginx.com            headers={'Host': 'localhost', 'Connection': 'keep-alive'},
1961017Szelenkov@nginx.com            start=True,
1971017Szelenkov@nginx.com            read_timeout=1,
1981017Szelenkov@nginx.com        )
199730Szelenkov@nginx.com
200730Szelenkov@nginx.com        time.sleep(3)
201730Szelenkov@nginx.com
2021017Szelenkov@nginx.com        resp = self.get(
2031017Szelenkov@nginx.com            headers={'Host': 'localhost', 'Connection': 'close'}, sock=sock
2041017Szelenkov@nginx.com        )
205730Szelenkov@nginx.com
206755Szelenkov@nginx.com        self.assertEqual(resp['status'], 408, 'status idle timeout')
207730Szelenkov@nginx.com
208730Szelenkov@nginx.com    def test_settings_max_body_size(self):
209730Szelenkov@nginx.com        self.load('empty')
210730Szelenkov@nginx.com
2111017Szelenkov@nginx.com        self.conf({'http': {'max_body_size': 5}}, 'settings')
212730Szelenkov@nginx.com
213730Szelenkov@nginx.com        self.assertEqual(self.post(body='01234')['status'], 200, 'status size')
2141017Szelenkov@nginx.com        self.assertEqual(
2151017Szelenkov@nginx.com            self.post(body='012345')['status'], 413, 'status size max'
2161017Szelenkov@nginx.com        )
217730Szelenkov@nginx.com
218730Szelenkov@nginx.com    @unittest.expectedFailure
219730Szelenkov@nginx.com    def test_settings_negative_value(self):
2201017Szelenkov@nginx.com        self.assertIn(
2211017Szelenkov@nginx.com            'error',
2221017Szelenkov@nginx.com            self.conf({'http': {'max_body_size': -1}}, 'settings'),
2231017Szelenkov@nginx.com            'settings negative value',
2241017Szelenkov@nginx.com        )
2251017Szelenkov@nginx.com
226730Szelenkov@nginx.com
227730Szelenkov@nginx.comif __name__ == '__main__':
2281019Szelenkov@nginx.com    TestSettings.main()
229