xref: /unit/test/test_settings.py (revision 755)
1730Szelenkov@nginx.comimport time
2730Szelenkov@nginx.comimport socket
3730Szelenkov@nginx.comimport unittest
4730Szelenkov@nginx.comimport unit
5730Szelenkov@nginx.com
6730Szelenkov@nginx.comclass TestUnitSettings(unit.TestUnitApplicationPython):
7730Szelenkov@nginx.com
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
14730Szelenkov@nginx.com        self.conf({'http': { 'header_read_timeout': 2 }}, '/settings')
15730Szelenkov@nginx.com
16730Szelenkov@nginx.com        (resp, sock) = self.http(b"""GET / HTTP/1.1
17730Szelenkov@nginx.com""", start=True, raw=True)
18730Szelenkov@nginx.com
19730Szelenkov@nginx.com        time.sleep(3)
20730Szelenkov@nginx.com
21730Szelenkov@nginx.com        resp = self.http(b"""Host: localhost
22730Szelenkov@nginx.comConnection: close
23730Szelenkov@nginx.com
24730Szelenkov@nginx.com""", sock=sock, raw=True)
25730Szelenkov@nginx.com
26730Szelenkov@nginx.com        self.assertEqual(resp['status'], 408, 'status header read timeout')
27730Szelenkov@nginx.com
28730Szelenkov@nginx.com    def test_settings_body_read_timeout(self):
29730Szelenkov@nginx.com        self.load('empty')
30730Szelenkov@nginx.com
31730Szelenkov@nginx.com        self.conf({'http': { 'body_read_timeout': 2 }}, '/settings')
32730Szelenkov@nginx.com
33730Szelenkov@nginx.com        (resp, sock) = self.http(b"""POST / HTTP/1.1
34730Szelenkov@nginx.comHost: localhost
35730Szelenkov@nginx.comContent-Length: 10
36730Szelenkov@nginx.comConnection: close
37730Szelenkov@nginx.com
38730Szelenkov@nginx.com""", start=True, raw_resp=True, raw=True)
39730Szelenkov@nginx.com
40730Szelenkov@nginx.com        time.sleep(3)
41730Szelenkov@nginx.com
42730Szelenkov@nginx.com        resp = self.http(b"""0123456789""", sock=sock, raw=True)
43730Szelenkov@nginx.com
44730Szelenkov@nginx.com        self.assertEqual(resp['status'], 408, 'status body read timeout')
45730Szelenkov@nginx.com
46730Szelenkov@nginx.com    def test_settings_send_timeout(self):
47730Szelenkov@nginx.com        self.load('mirror')
48730Szelenkov@nginx.com
49730Szelenkov@nginx.com        data_len = 1048576
50730Szelenkov@nginx.com
51730Szelenkov@nginx.com        self.conf({'http': { 'send_timeout': 1 }}, '/settings')
52730Szelenkov@nginx.com
53730Szelenkov@nginx.com        addr = self.testdir + '/sock'
54730Szelenkov@nginx.com
55730Szelenkov@nginx.com        self.conf({"unix:" + addr: {'application': 'mirror'}}, '/listeners')
56730Szelenkov@nginx.com
57730Szelenkov@nginx.com        sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
58730Szelenkov@nginx.com        sock.connect(addr)
59730Szelenkov@nginx.com
60730Szelenkov@nginx.com        req = """POST / HTTP/1.1
61730Szelenkov@nginx.comHost: localhost
62730Szelenkov@nginx.comContent-Type: text/html
63730Szelenkov@nginx.comContent-Length: %d
64730Szelenkov@nginx.com
65730Szelenkov@nginx.com""" % data_len + ('X' * data_len)
66730Szelenkov@nginx.com
67730Szelenkov@nginx.com        sock.sendall(req.encode())
68730Szelenkov@nginx.com
69730Szelenkov@nginx.com        data = sock.recv(16).decode()
70730Szelenkov@nginx.com
71730Szelenkov@nginx.com        time.sleep(3)
72730Szelenkov@nginx.com
73738Szelenkov@nginx.com        data += self.recvall(sock).decode()
74730Szelenkov@nginx.com
75730Szelenkov@nginx.com        sock.close()
76730Szelenkov@nginx.com
77730Szelenkov@nginx.com        self.assertRegex(data, r'200 OK', 'status send timeout')
78730Szelenkov@nginx.com        self.assertLess(len(data), data_len, 'data send timeout')
79730Szelenkov@nginx.com
80730Szelenkov@nginx.com    def test_settings_idle_timeout(self):
81730Szelenkov@nginx.com        self.load('empty')
82730Szelenkov@nginx.com
83730Szelenkov@nginx.com        self.conf({'http': { 'idle_timeout': 2 }}, '/settings')
84730Szelenkov@nginx.com
85730Szelenkov@nginx.com        (resp, sock) = self.get(headers={
86730Szelenkov@nginx.com            'Connection': 'keep-alive',
87730Szelenkov@nginx.com            'Host': 'localhost'
88730Szelenkov@nginx.com        }, start=True)
89730Szelenkov@nginx.com
90730Szelenkov@nginx.com        time.sleep(3)
91730Szelenkov@nginx.com
92730Szelenkov@nginx.com        resp = self.get(headers={
93730Szelenkov@nginx.com            'Connection': 'close',
94730Szelenkov@nginx.com            'Host': 'localhost'
95730Szelenkov@nginx.com        }, sock=sock)
96730Szelenkov@nginx.com
97*755Szelenkov@nginx.com        self.assertEqual(resp['status'], 408, 'status idle timeout')
98730Szelenkov@nginx.com
99730Szelenkov@nginx.com    def test_settings_max_body_size(self):
100730Szelenkov@nginx.com        self.load('empty')
101730Szelenkov@nginx.com
102730Szelenkov@nginx.com        self.conf({'http': { 'max_body_size': 5 }}, '/settings')
103730Szelenkov@nginx.com
104730Szelenkov@nginx.com        self.assertEqual(self.post(body='01234')['status'], 200, 'status size')
105730Szelenkov@nginx.com        self.assertEqual(self.post(body='012345')['status'], 413,
106730Szelenkov@nginx.com            'status size max')
107730Szelenkov@nginx.com
108730Szelenkov@nginx.com    @unittest.expectedFailure
109730Szelenkov@nginx.com    def test_settings_negative_value(self):
110730Szelenkov@nginx.com        self.assertIn('error', self.conf({'http': { 'max_body_size': -1 }},
111730Szelenkov@nginx.com            '/settings'), 'settings negative value')
112730Szelenkov@nginx.com
113730Szelenkov@nginx.com
114730Szelenkov@nginx.comif __name__ == '__main__':
115730Szelenkov@nginx.com    unittest.main()
116