xref: /unit/test/test_settings.py (revision 1017)
1730Szelenkov@nginx.comimport time
2730Szelenkov@nginx.comimport socket
3730Szelenkov@nginx.comimport unittest
4730Szelenkov@nginx.comimport unit
5730Szelenkov@nginx.com
6*1017Szelenkov@nginx.com
7730Szelenkov@nginx.comclass TestUnitSettings(unit.TestUnitApplicationPython):
8730Szelenkov@nginx.com    def setUpClass():
9730Szelenkov@nginx.com        unit.TestUnit().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
14*1017Szelenkov@nginx.com        self.conf({'http': {'header_read_timeout': 2}}, 'settings')
15730Szelenkov@nginx.com
16*1017Szelenkov@nginx.com        (resp, sock) = self.http(
17*1017Szelenkov@nginx.com            b"""GET / HTTP/1.1
18*1017Szelenkov@nginx.com""",
19*1017Szelenkov@nginx.com            start=True,
20*1017Szelenkov@nginx.com            read_timeout=1,
21*1017Szelenkov@nginx.com            raw=True,
22*1017Szelenkov@nginx.com        )
23730Szelenkov@nginx.com
24730Szelenkov@nginx.com        time.sleep(3)
25730Szelenkov@nginx.com
26*1017Szelenkov@nginx.com        resp = self.http(
27*1017Szelenkov@nginx.com            b"""Host: localhost
28730Szelenkov@nginx.comConnection: close
29730Szelenkov@nginx.com
30*1017Szelenkov@nginx.com""",
31*1017Szelenkov@nginx.com            sock=sock,
32*1017Szelenkov@nginx.com            raw=True,
33*1017Szelenkov@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
40*1017Szelenkov@nginx.com        self.conf({'http': {'header_read_timeout': 4}}, 'settings')
41756Szelenkov@nginx.com
42*1017Szelenkov@nginx.com        (resp, sock) = self.http(
43*1017Szelenkov@nginx.com            b"""GET / HTTP/1.1
44*1017Szelenkov@nginx.com""",
45*1017Szelenkov@nginx.com            start=True,
46*1017Szelenkov@nginx.com            read_timeout=1,
47*1017Szelenkov@nginx.com            raw=True,
48*1017Szelenkov@nginx.com            no_recv=True,
49*1017Szelenkov@nginx.com        )
50756Szelenkov@nginx.com
51756Szelenkov@nginx.com        time.sleep(2)
52756Szelenkov@nginx.com
53*1017Szelenkov@nginx.com        (resp, sock) = self.http(
54*1017Szelenkov@nginx.com            b"""Host: localhost
55*1017Szelenkov@nginx.com""",
56*1017Szelenkov@nginx.com            start=True,
57*1017Szelenkov@nginx.com            sock=sock,
58*1017Szelenkov@nginx.com            read_timeout=1,
59*1017Szelenkov@nginx.com            raw=True,
60*1017Szelenkov@nginx.com            no_recv=True,
61*1017Szelenkov@nginx.com        )
62759Szelenkov@nginx.com
63759Szelenkov@nginx.com        time.sleep(2)
64759Szelenkov@nginx.com
65*1017Szelenkov@nginx.com        (resp, sock) = self.http(
66*1017Szelenkov@nginx.com            b"""X-Blah: blah
67*1017Szelenkov@nginx.com""",
68*1017Szelenkov@nginx.com            start=True,
69*1017Szelenkov@nginx.com            sock=sock,
70*1017Szelenkov@nginx.com            read_timeout=1,
71*1017Szelenkov@nginx.com            raw=True,
72*1017Szelenkov@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
80*1017Szelenkov@nginx.com            resp = self.http(
81*1017Szelenkov@nginx.com                b"""Connection: close
82756Szelenkov@nginx.com
83*1017Szelenkov@nginx.com""",
84*1017Szelenkov@nginx.com                sock=sock,
85*1017Szelenkov@nginx.com                raw=True,
86*1017Szelenkov@nginx.com            )
87756Szelenkov@nginx.com
88*1017Szelenkov@nginx.com        self.assertEqual(
89*1017Szelenkov@nginx.com            resp['status'], 408, 'status header read timeout update'
90*1017Szelenkov@nginx.com        )
91756Szelenkov@nginx.com
92730Szelenkov@nginx.com    def test_settings_body_read_timeout(self):
93730Szelenkov@nginx.com        self.load('empty')
94730Szelenkov@nginx.com
95*1017Szelenkov@nginx.com        self.conf({'http': {'body_read_timeout': 2}}, 'settings')
96730Szelenkov@nginx.com
97*1017Szelenkov@nginx.com        (resp, sock) = self.http(
98*1017Szelenkov@nginx.com            b"""POST / HTTP/1.1
99730Szelenkov@nginx.comHost: localhost
100730Szelenkov@nginx.comContent-Length: 10
101730Szelenkov@nginx.comConnection: close
102730Szelenkov@nginx.com
103*1017Szelenkov@nginx.com""",
104*1017Szelenkov@nginx.com            start=True,
105*1017Szelenkov@nginx.com            raw_resp=True,
106*1017Szelenkov@nginx.com            read_timeout=1,
107*1017Szelenkov@nginx.com            raw=True,
108*1017Szelenkov@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
119*1017Szelenkov@nginx.com        self.conf({'http': {'body_read_timeout': 4}}, 'settings')
120756Szelenkov@nginx.com
121*1017Szelenkov@nginx.com        (resp, sock) = self.http(
122*1017Szelenkov@nginx.com            b"""POST / HTTP/1.1
123756Szelenkov@nginx.comHost: localhost
124756Szelenkov@nginx.comContent-Length: 10
125756Szelenkov@nginx.comConnection: close
126756Szelenkov@nginx.com
127*1017Szelenkov@nginx.com""",
128*1017Szelenkov@nginx.com            start=True,
129*1017Szelenkov@nginx.com            read_timeout=1,
130*1017Szelenkov@nginx.com            raw=True,
131*1017Szelenkov@nginx.com        )
132756Szelenkov@nginx.com
133756Szelenkov@nginx.com        time.sleep(2)
134756Szelenkov@nginx.com
135*1017Szelenkov@nginx.com        (resp, sock) = self.http(
136*1017Szelenkov@nginx.com            b"""012""", start=True, sock=sock, read_timeout=1, raw=True
137*1017Szelenkov@nginx.com        )
138756Szelenkov@nginx.com
139756Szelenkov@nginx.com        time.sleep(2)
140756Szelenkov@nginx.com
141*1017Szelenkov@nginx.com        (resp, sock) = self.http(
142*1017Szelenkov@nginx.com            b"""345""", start=True, sock=sock, read_timeout=1, raw=True
143*1017Szelenkov@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
149*1017Szelenkov@nginx.com        self.assertEqual(
150*1017Szelenkov@nginx.com            resp['status'], 200, 'status body read timeout update'
151*1017Szelenkov@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
158*1017Szelenkov@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
173*1017Szelenkov@nginx.com""" % data_len + (
174*1017Szelenkov@nginx.com            'X' * data_len
175*1017Szelenkov@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
193*1017Szelenkov@nginx.com        self.conf({'http': {'idle_timeout': 2}}, 'settings')
194730Szelenkov@nginx.com
195*1017Szelenkov@nginx.com        (resp, sock) = self.get(
196*1017Szelenkov@nginx.com            headers={'Host': 'localhost', 'Connection': 'keep-alive'},
197*1017Szelenkov@nginx.com            start=True,
198*1017Szelenkov@nginx.com            read_timeout=1,
199*1017Szelenkov@nginx.com        )
200730Szelenkov@nginx.com
201730Szelenkov@nginx.com        time.sleep(3)
202730Szelenkov@nginx.com
203*1017Szelenkov@nginx.com        resp = self.get(
204*1017Szelenkov@nginx.com            headers={'Host': 'localhost', 'Connection': 'close'}, sock=sock
205*1017Szelenkov@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
212*1017Szelenkov@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')
215*1017Szelenkov@nginx.com        self.assertEqual(
216*1017Szelenkov@nginx.com            self.post(body='012345')['status'], 413, 'status size max'
217*1017Szelenkov@nginx.com        )
218730Szelenkov@nginx.com
219730Szelenkov@nginx.com    @unittest.expectedFailure
220730Szelenkov@nginx.com    def test_settings_negative_value(self):
221*1017Szelenkov@nginx.com        self.assertIn(
222*1017Szelenkov@nginx.com            'error',
223*1017Szelenkov@nginx.com            self.conf({'http': {'max_body_size': -1}}, 'settings'),
224*1017Szelenkov@nginx.com            'settings negative value',
225*1017Szelenkov@nginx.com        )
226*1017Szelenkov@nginx.com
227730Szelenkov@nginx.com
228730Szelenkov@nginx.comif __name__ == '__main__':
229853Szelenkov@nginx.com    TestUnitSettings.main()
230