xref: /unit/test/test_proxy.py (revision 1467)
11274Szelenkov@nginx.comimport re
21274Szelenkov@nginx.comimport time
31274Szelenkov@nginx.comimport socket
41274Szelenkov@nginx.comimport unittest
51274Szelenkov@nginx.comfrom unit.applications.lang.python import TestApplicationPython
61274Szelenkov@nginx.com
71274Szelenkov@nginx.com
81274Szelenkov@nginx.comclass TestProxy(TestApplicationPython):
9*1467Szelenkov@nginx.com    prerequisites = {'modules': {'python': 'any'}}
101274Szelenkov@nginx.com
111274Szelenkov@nginx.com    SERVER_PORT = 7999
121274Szelenkov@nginx.com
131353Smax.romanov@nginx.com    @staticmethod
141353Smax.romanov@nginx.com    def run_server(server_port):
151274Szelenkov@nginx.com        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
161274Szelenkov@nginx.com        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
171274Szelenkov@nginx.com
181353Smax.romanov@nginx.com        server_address = ('', server_port)
191274Szelenkov@nginx.com        sock.bind(server_address)
201274Szelenkov@nginx.com        sock.listen(5)
211274Szelenkov@nginx.com
221274Szelenkov@nginx.com        def recvall(sock):
231274Szelenkov@nginx.com            buff_size = 4096
241274Szelenkov@nginx.com            data = b''
251274Szelenkov@nginx.com            while True:
261274Szelenkov@nginx.com                part = sock.recv(buff_size)
271274Szelenkov@nginx.com                data += part
281274Szelenkov@nginx.com                if len(part) < buff_size:
291274Szelenkov@nginx.com                    break
301274Szelenkov@nginx.com            return data
311274Szelenkov@nginx.com
321274Szelenkov@nginx.com        req = b"""HTTP/1.1 200 OK
331274Szelenkov@nginx.comContent-Length: 10
341274Szelenkov@nginx.com
351274Szelenkov@nginx.com"""
361274Szelenkov@nginx.com
371274Szelenkov@nginx.com        while True:
381274Szelenkov@nginx.com            connection, client_address = sock.accept()
391274Szelenkov@nginx.com
401274Szelenkov@nginx.com            data = recvall(connection).decode()
411274Szelenkov@nginx.com
421274Szelenkov@nginx.com            to_send = req
431274Szelenkov@nginx.com
441274Szelenkov@nginx.com            m = re.search('X-Len: (\d+)', data)
451274Szelenkov@nginx.com            if m:
461274Szelenkov@nginx.com                to_send += b'X' * int(m.group(1))
471274Szelenkov@nginx.com
481274Szelenkov@nginx.com            connection.sendall(to_send)
491274Szelenkov@nginx.com
501274Szelenkov@nginx.com            connection.close()
511274Szelenkov@nginx.com
521274Szelenkov@nginx.com    def get_http10(self, *args, **kwargs):
531274Szelenkov@nginx.com        return self.get(*args, http_10=True, **kwargs)
541274Szelenkov@nginx.com
551274Szelenkov@nginx.com    def post_http10(self, *args, **kwargs):
561274Szelenkov@nginx.com        return self.post(*args, http_10=True, **kwargs)
571274Szelenkov@nginx.com
581274Szelenkov@nginx.com    def setUp(self):
591274Szelenkov@nginx.com        super().setUp()
601274Szelenkov@nginx.com
611353Smax.romanov@nginx.com        self.run_process(self.run_server, self.SERVER_PORT)
621274Szelenkov@nginx.com        self.waitforsocket(self.SERVER_PORT)
631274Szelenkov@nginx.com
641274Szelenkov@nginx.com        self.assertIn(
651274Szelenkov@nginx.com            'success',
661274Szelenkov@nginx.com            self.conf(
671274Szelenkov@nginx.com                {
681274Szelenkov@nginx.com                    "listeners": {
691274Szelenkov@nginx.com                        "*:7080": {"pass": "routes"},
701274Szelenkov@nginx.com                        "*:7081": {"pass": "applications/mirror"},
711274Szelenkov@nginx.com                    },
721274Szelenkov@nginx.com                    "routes": [{"action": {"proxy": "http://127.0.0.1:7081"}}],
731274Szelenkov@nginx.com                    "applications": {
741274Szelenkov@nginx.com                        "mirror": {
751274Szelenkov@nginx.com                            "type": "python",
761274Szelenkov@nginx.com                            "processes": {"spare": 0},
771274Szelenkov@nginx.com                            "path": self.current_dir + "/python/mirror",
781274Szelenkov@nginx.com                            "working_directory": self.current_dir
791274Szelenkov@nginx.com                            + "/python/mirror",
801274Szelenkov@nginx.com                            "module": "wsgi",
811274Szelenkov@nginx.com                        },
821274Szelenkov@nginx.com                        "custom_header": {
831274Szelenkov@nginx.com                            "type": "python",
841274Szelenkov@nginx.com                            "processes": {"spare": 0},
851274Szelenkov@nginx.com                            "path": self.current_dir + "/python/custom_header",
861274Szelenkov@nginx.com                            "working_directory": self.current_dir
871274Szelenkov@nginx.com                            + "/python/custom_header",
881274Szelenkov@nginx.com                            "module": "wsgi",
891274Szelenkov@nginx.com                        },
901274Szelenkov@nginx.com                        "delayed": {
911274Szelenkov@nginx.com                            "type": "python",
921274Szelenkov@nginx.com                            "processes": {"spare": 0},
931274Szelenkov@nginx.com                            "path": self.current_dir + "/python/delayed",
941274Szelenkov@nginx.com                            "working_directory": self.current_dir
951274Szelenkov@nginx.com                            + "/python/delayed",
961274Szelenkov@nginx.com                            "module": "wsgi",
971274Szelenkov@nginx.com                        },
981274Szelenkov@nginx.com                    },
991274Szelenkov@nginx.com                }
1001274Szelenkov@nginx.com            ),
1011274Szelenkov@nginx.com            'proxy initial configuration',
1021274Szelenkov@nginx.com        )
1031274Szelenkov@nginx.com
1041274Szelenkov@nginx.com    def test_proxy_http10(self):
1051274Szelenkov@nginx.com        for _ in range(10):
1061274Szelenkov@nginx.com            self.assertEqual(self.get_http10()['status'], 200, 'status')
1071274Szelenkov@nginx.com
1081274Szelenkov@nginx.com    def test_proxy_chain(self):
1091274Szelenkov@nginx.com        self.assertIn(
1101274Szelenkov@nginx.com            'success',
1111274Szelenkov@nginx.com            self.conf(
1121274Szelenkov@nginx.com                {
1131274Szelenkov@nginx.com                    "listeners": {
1141274Szelenkov@nginx.com                        "*:7080": {"pass": "routes/first"},
1151274Szelenkov@nginx.com                        "*:7081": {"pass": "routes/second"},
1161274Szelenkov@nginx.com                        "*:7082": {"pass": "routes/third"},
1171274Szelenkov@nginx.com                        "*:7083": {"pass": "routes/fourth"},
1181274Szelenkov@nginx.com                        "*:7084": {"pass": "routes/fifth"},
1191274Szelenkov@nginx.com                        "*:7085": {"pass": "applications/mirror"},
1201274Szelenkov@nginx.com                    },
1211274Szelenkov@nginx.com                    "routes": {
1221274Szelenkov@nginx.com                        "first": [
1231274Szelenkov@nginx.com                            {"action": {"proxy": "http://127.0.0.1:7081"}}
1241274Szelenkov@nginx.com                        ],
1251274Szelenkov@nginx.com                        "second": [
1261274Szelenkov@nginx.com                            {"action": {"proxy": "http://127.0.0.1:7082"}}
1271274Szelenkov@nginx.com                        ],
1281274Szelenkov@nginx.com                        "third": [
1291274Szelenkov@nginx.com                            {"action": {"proxy": "http://127.0.0.1:7083"}}
1301274Szelenkov@nginx.com                        ],
1311274Szelenkov@nginx.com                        "fourth": [
1321274Szelenkov@nginx.com                            {"action": {"proxy": "http://127.0.0.1:7084"}}
1331274Szelenkov@nginx.com                        ],
1341274Szelenkov@nginx.com                        "fifth": [
1351274Szelenkov@nginx.com                            {"action": {"proxy": "http://127.0.0.1:7085"}}
1361274Szelenkov@nginx.com                        ],
1371274Szelenkov@nginx.com                    },
1381274Szelenkov@nginx.com                    "applications": {
1391274Szelenkov@nginx.com                        "mirror": {
1401274Szelenkov@nginx.com                            "type": "python",
1411274Szelenkov@nginx.com                            "processes": {"spare": 0},
1421274Szelenkov@nginx.com                            "path": self.current_dir + "/python/mirror",
1431274Szelenkov@nginx.com                            "working_directory": self.current_dir
1441274Szelenkov@nginx.com                            + "/python/mirror",
1451274Szelenkov@nginx.com                            "module": "wsgi",
1461274Szelenkov@nginx.com                        }
1471274Szelenkov@nginx.com                    },
1481274Szelenkov@nginx.com                }
1491274Szelenkov@nginx.com            ),
1501274Szelenkov@nginx.com            'proxy chain configuration',
1511274Szelenkov@nginx.com        )
1521274Szelenkov@nginx.com
1531274Szelenkov@nginx.com        self.assertEqual(self.get_http10()['status'], 200, 'status')
1541274Szelenkov@nginx.com
1551274Szelenkov@nginx.com    def test_proxy_body(self):
1561274Szelenkov@nginx.com        payload = '0123456789'
1571274Szelenkov@nginx.com        for _ in range(10):
1581274Szelenkov@nginx.com            resp = self.post_http10(body=payload)
1591274Szelenkov@nginx.com
1601274Szelenkov@nginx.com            self.assertEqual(resp['status'], 200, 'status')
1611274Szelenkov@nginx.com            self.assertEqual(resp['body'], payload, 'body')
1621274Szelenkov@nginx.com
1631274Szelenkov@nginx.com        payload = 'X' * 4096
1641274Szelenkov@nginx.com        for _ in range(10):
1651274Szelenkov@nginx.com            resp = self.post_http10(body=payload)
1661274Szelenkov@nginx.com
1671274Szelenkov@nginx.com            self.assertEqual(resp['status'], 200, 'status')
1681274Szelenkov@nginx.com            self.assertEqual(resp['body'], payload, 'body')
1691274Szelenkov@nginx.com
1701274Szelenkov@nginx.com        payload = 'X' * 4097
1711274Szelenkov@nginx.com        for _ in range(10):
1721274Szelenkov@nginx.com            resp = self.post_http10(body=payload)
1731274Szelenkov@nginx.com
1741274Szelenkov@nginx.com            self.assertEqual(resp['status'], 200, 'status')
1751274Szelenkov@nginx.com            self.assertEqual(resp['body'], payload, 'body')
1761274Szelenkov@nginx.com
1771274Szelenkov@nginx.com        payload = 'X' * 4096 * 256
1781274Szelenkov@nginx.com        for _ in range(10):
1791274Szelenkov@nginx.com            resp = self.post_http10(body=payload, read_buffer_size=4096 * 128)
1801274Szelenkov@nginx.com
1811274Szelenkov@nginx.com            self.assertEqual(resp['status'], 200, 'status')
1821274Szelenkov@nginx.com            self.assertEqual(resp['body'], payload, 'body')
1831274Szelenkov@nginx.com
1841274Szelenkov@nginx.com        payload = 'X' * 4096 * 257
1851274Szelenkov@nginx.com        for _ in range(10):
1861274Szelenkov@nginx.com            resp = self.post_http10(body=payload, read_buffer_size=4096 * 128)
1871274Szelenkov@nginx.com
1881274Szelenkov@nginx.com            self.assertEqual(resp['status'], 200, 'status')
1891274Szelenkov@nginx.com            self.assertEqual(resp['body'], payload, 'body')
1901274Szelenkov@nginx.com
1911366Szelenkov@nginx.com        self.conf({'http': {'max_body_size': 32 * 1024 * 1024}}, 'settings')
1921366Szelenkov@nginx.com
1931366Szelenkov@nginx.com        payload = '0123456789abcdef' * 32 * 64 * 1024
1941366Szelenkov@nginx.com        resp = self.post_http10(body=payload, read_buffer_size=1024 * 1024)
1951366Szelenkov@nginx.com        self.assertEqual(resp['status'], 200, 'status')
1961366Szelenkov@nginx.com        self.assertEqual(resp['body'], payload, 'body')
1971366Szelenkov@nginx.com
1981274Szelenkov@nginx.com    def test_proxy_parallel(self):
1991274Szelenkov@nginx.com        payload = 'X' * 4096 * 257
2001274Szelenkov@nginx.com        buff_size = 4096 * 258
2011274Szelenkov@nginx.com
2021274Szelenkov@nginx.com        socks = []
2031274Szelenkov@nginx.com        for i in range(10):
2041274Szelenkov@nginx.com            _, sock = self.post_http10(
2051274Szelenkov@nginx.com                body=payload + str(i),
2061274Szelenkov@nginx.com                start=True,
2071274Szelenkov@nginx.com                no_recv=True,
2081274Szelenkov@nginx.com                read_buffer_size=buff_size,
2091274Szelenkov@nginx.com            )
2101274Szelenkov@nginx.com            socks.append(sock)
2111274Szelenkov@nginx.com
2121274Szelenkov@nginx.com        for i in range(10):
2131274Szelenkov@nginx.com            resp = self.recvall(socks[i], buff_size=buff_size).decode()
2141274Szelenkov@nginx.com            socks[i].close()
2151274Szelenkov@nginx.com
2161274Szelenkov@nginx.com            resp = self._resp_to_dict(resp)
2171274Szelenkov@nginx.com
2181274Szelenkov@nginx.com            self.assertEqual(resp['status'], 200, 'status')
2191274Szelenkov@nginx.com            self.assertEqual(resp['body'], payload + str(i), 'body')
2201274Szelenkov@nginx.com
2211274Szelenkov@nginx.com    def test_proxy_header(self):
2221274Szelenkov@nginx.com        self.assertIn(
2231274Szelenkov@nginx.com            'success',
2241274Szelenkov@nginx.com            self.conf(
2251274Szelenkov@nginx.com                {"pass": "applications/custom_header"}, 'listeners/*:7081'
2261274Szelenkov@nginx.com            ),
2271274Szelenkov@nginx.com            'custom_header configure',
2281274Szelenkov@nginx.com        )
2291274Szelenkov@nginx.com
2301274Szelenkov@nginx.com        header_value = 'blah'
2311274Szelenkov@nginx.com        self.assertEqual(
2321274Szelenkov@nginx.com            self.get_http10(
2331274Szelenkov@nginx.com                headers={'Host': 'localhost', 'Custom-Header': header_value}
2341274Szelenkov@nginx.com            )['headers']['Custom-Header'],
2351274Szelenkov@nginx.com            header_value,
2361274Szelenkov@nginx.com            'custom header',
2371274Szelenkov@nginx.com        )
2381274Szelenkov@nginx.com
2391274Szelenkov@nginx.com        header_value = '(),/:;<=>?@[\]{}\t !#$%&\'*+-.^_`|~'
2401274Szelenkov@nginx.com        self.assertEqual(
2411274Szelenkov@nginx.com            self.get_http10(
2421274Szelenkov@nginx.com                headers={'Host': 'localhost', 'Custom-Header': header_value}
2431274Szelenkov@nginx.com            )['headers']['Custom-Header'],
2441274Szelenkov@nginx.com            header_value,
2451274Szelenkov@nginx.com            'custom header 2',
2461274Szelenkov@nginx.com        )
2471274Szelenkov@nginx.com
2481274Szelenkov@nginx.com        header_value = 'X' * 4096
2491274Szelenkov@nginx.com        self.assertEqual(
2501274Szelenkov@nginx.com            self.get_http10(
2511274Szelenkov@nginx.com                headers={'Host': 'localhost', 'Custom-Header': header_value}
2521274Szelenkov@nginx.com            )['headers']['Custom-Header'],
2531274Szelenkov@nginx.com            header_value,
2541274Szelenkov@nginx.com            'custom header 3',
2551274Szelenkov@nginx.com        )
2561274Szelenkov@nginx.com
2571274Szelenkov@nginx.com        header_value = 'X' * 8191
2581274Szelenkov@nginx.com        self.assertEqual(
2591274Szelenkov@nginx.com            self.get_http10(
2601274Szelenkov@nginx.com                headers={'Host': 'localhost', 'Custom-Header': header_value}
2611274Szelenkov@nginx.com            )['headers']['Custom-Header'],
2621274Szelenkov@nginx.com            header_value,
2631274Szelenkov@nginx.com            'custom header 4',
2641274Szelenkov@nginx.com        )
2651274Szelenkov@nginx.com
2661274Szelenkov@nginx.com        header_value = 'X' * 8192
2671274Szelenkov@nginx.com        self.assertEqual(
2681274Szelenkov@nginx.com            self.get_http10(
2691274Szelenkov@nginx.com                headers={'Host': 'localhost', 'Custom-Header': header_value}
2701274Szelenkov@nginx.com            )['status'],
2711274Szelenkov@nginx.com            431,
2721274Szelenkov@nginx.com            'custom header 5',
2731274Szelenkov@nginx.com        )
2741274Szelenkov@nginx.com
2751274Szelenkov@nginx.com    def test_proxy_fragmented(self):
2761274Szelenkov@nginx.com        _, sock = self.http(
2771274Szelenkov@nginx.com            b"""GET / HTT""", raw=True, start=True, no_recv=True
2781274Szelenkov@nginx.com        )
2791274Szelenkov@nginx.com
2801274Szelenkov@nginx.com        time.sleep(1)
2811274Szelenkov@nginx.com
2821274Szelenkov@nginx.com        sock.sendall("P/1.0\r\nHost: localhos".encode())
2831274Szelenkov@nginx.com
2841274Szelenkov@nginx.com        time.sleep(1)
2851274Szelenkov@nginx.com
2861274Szelenkov@nginx.com        sock.sendall("t\r\n\r\n".encode())
2871274Szelenkov@nginx.com
2881274Szelenkov@nginx.com        self.assertRegex(
2891274Szelenkov@nginx.com            self.recvall(sock).decode(), '200 OK', 'fragmented send'
2901274Szelenkov@nginx.com        )
2911274Szelenkov@nginx.com        sock.close()
2921274Szelenkov@nginx.com
2931274Szelenkov@nginx.com    def test_proxy_fragmented_close(self):
2941274Szelenkov@nginx.com        _, sock = self.http(
2951274Szelenkov@nginx.com            b"""GET / HTT""", raw=True, start=True, no_recv=True
2961274Szelenkov@nginx.com        )
2971274Szelenkov@nginx.com
2981274Szelenkov@nginx.com        time.sleep(1)
2991274Szelenkov@nginx.com
3001274Szelenkov@nginx.com        sock.sendall("P/1.0\r\nHo".encode())
3011274Szelenkov@nginx.com
3021274Szelenkov@nginx.com        sock.close()
3031274Szelenkov@nginx.com
3041274Szelenkov@nginx.com    def test_proxy_fragmented_body(self):
3051274Szelenkov@nginx.com        _, sock = self.http(
3061274Szelenkov@nginx.com            b"""GET / HTT""", raw=True, start=True, no_recv=True
3071274Szelenkov@nginx.com        )
3081274Szelenkov@nginx.com
3091274Szelenkov@nginx.com        time.sleep(1)
3101274Szelenkov@nginx.com
3111274Szelenkov@nginx.com        sock.sendall("P/1.0\r\nHost: localhost\r\n".encode())
3121274Szelenkov@nginx.com        sock.sendall("Content-Length: 30000\r\n".encode())
3131274Szelenkov@nginx.com
3141274Szelenkov@nginx.com        time.sleep(1)
3151274Szelenkov@nginx.com
3161274Szelenkov@nginx.com        sock.sendall("\r\n".encode())
3171274Szelenkov@nginx.com        sock.sendall(("X" * 10000).encode())
3181274Szelenkov@nginx.com
3191274Szelenkov@nginx.com        time.sleep(1)
3201274Szelenkov@nginx.com
3211274Szelenkov@nginx.com        sock.sendall(("X" * 10000).encode())
3221274Szelenkov@nginx.com
3231274Szelenkov@nginx.com        time.sleep(1)
3241274Szelenkov@nginx.com
3251274Szelenkov@nginx.com        sock.sendall(("X" * 10000).encode())
3261274Szelenkov@nginx.com
3271274Szelenkov@nginx.com        resp = self._resp_to_dict(self.recvall(sock).decode())
3281274Szelenkov@nginx.com        sock.close()
3291274Szelenkov@nginx.com
3301274Szelenkov@nginx.com        self.assertEqual(resp['status'], 200, 'status')
3311274Szelenkov@nginx.com        self.assertEqual(resp['body'], "X" * 30000, 'body')
3321274Szelenkov@nginx.com
3331274Szelenkov@nginx.com    def test_proxy_fragmented_body_close(self):
3341274Szelenkov@nginx.com        _, sock = self.http(
3351274Szelenkov@nginx.com            b"""GET / HTT""", raw=True, start=True, no_recv=True
3361274Szelenkov@nginx.com        )
3371274Szelenkov@nginx.com
3381274Szelenkov@nginx.com        time.sleep(1)
3391274Szelenkov@nginx.com
3401274Szelenkov@nginx.com        sock.sendall("P/1.0\r\nHost: localhost\r\n".encode())
3411274Szelenkov@nginx.com        sock.sendall("Content-Length: 30000\r\n".encode())
3421274Szelenkov@nginx.com
3431274Szelenkov@nginx.com        time.sleep(1)
3441274Szelenkov@nginx.com
3451274Szelenkov@nginx.com        sock.sendall("\r\n".encode())
3461274Szelenkov@nginx.com        sock.sendall(("X" * 10000).encode())
3471274Szelenkov@nginx.com
3481274Szelenkov@nginx.com        sock.close()
3491274Szelenkov@nginx.com
3501274Szelenkov@nginx.com    def test_proxy_nowhere(self):
3511274Szelenkov@nginx.com        self.assertIn(
3521274Szelenkov@nginx.com            'success',
3531274Szelenkov@nginx.com            self.conf(
3541274Szelenkov@nginx.com                [{"action": {"proxy": "http://127.0.0.1:7082"}}], 'routes'
3551274Szelenkov@nginx.com            ),
3561274Szelenkov@nginx.com            'proxy path changed',
3571274Szelenkov@nginx.com        )
3581274Szelenkov@nginx.com
3591274Szelenkov@nginx.com        self.assertEqual(self.get_http10()['status'], 502, 'status')
3601274Szelenkov@nginx.com
3611274Szelenkov@nginx.com    def test_proxy_ipv6(self):
3621274Szelenkov@nginx.com        self.assertIn(
3631274Szelenkov@nginx.com            'success',
3641274Szelenkov@nginx.com            self.conf(
3651274Szelenkov@nginx.com                {
3661274Szelenkov@nginx.com                    "*:7080": {"pass": "routes"},
3671274Szelenkov@nginx.com                    "[::1]:7081": {'application': 'mirror'},
3681274Szelenkov@nginx.com                },
3691274Szelenkov@nginx.com                'listeners',
3701274Szelenkov@nginx.com            ),
3711274Szelenkov@nginx.com            'add ipv6 listener configure',
3721274Szelenkov@nginx.com        )
3731274Szelenkov@nginx.com
3741274Szelenkov@nginx.com        self.assertIn(
3751274Szelenkov@nginx.com            'success',
3761274Szelenkov@nginx.com            self.conf([{"action": {"proxy": "http://[::1]:7081"}}], 'routes'),
3771274Szelenkov@nginx.com            'proxy ipv6 configure',
3781274Szelenkov@nginx.com        )
3791274Szelenkov@nginx.com
3801274Szelenkov@nginx.com        self.assertEqual(self.get_http10()['status'], 200, 'status')
3811274Szelenkov@nginx.com
3821274Szelenkov@nginx.com    def test_proxy_unix(self):
3831274Szelenkov@nginx.com        addr = self.testdir + '/sock'
3841274Szelenkov@nginx.com
3851274Szelenkov@nginx.com        self.assertIn(
3861274Szelenkov@nginx.com            'success',
3871274Szelenkov@nginx.com            self.conf(
3881274Szelenkov@nginx.com                {
3891274Szelenkov@nginx.com                    "*:7080": {"pass": "routes"},
3901274Szelenkov@nginx.com                    "unix:" + addr: {'application': 'mirror'},
3911274Szelenkov@nginx.com                },
3921274Szelenkov@nginx.com                'listeners',
3931274Szelenkov@nginx.com            ),
3941274Szelenkov@nginx.com            'add unix listener configure',
3951274Szelenkov@nginx.com        )
3961274Szelenkov@nginx.com
3971274Szelenkov@nginx.com        self.assertIn(
3981274Szelenkov@nginx.com            'success',
3991274Szelenkov@nginx.com            self.conf(
4001274Szelenkov@nginx.com                [{"action": {"proxy": 'http://unix:' + addr}}], 'routes'
4011274Szelenkov@nginx.com            ),
4021274Szelenkov@nginx.com            'proxy unix configure',
4031274Szelenkov@nginx.com        )
4041274Szelenkov@nginx.com
4051274Szelenkov@nginx.com        self.assertEqual(self.get_http10()['status'], 200, 'status')
4061274Szelenkov@nginx.com
4071274Szelenkov@nginx.com    def test_proxy_delayed(self):
4081274Szelenkov@nginx.com        self.assertIn(
4091274Szelenkov@nginx.com            'success',
4101274Szelenkov@nginx.com            self.conf(
4111274Szelenkov@nginx.com                {"pass": "applications/delayed"}, 'listeners/*:7081'
4121274Szelenkov@nginx.com            ),
4131274Szelenkov@nginx.com            'delayed configure',
4141274Szelenkov@nginx.com        )
4151274Szelenkov@nginx.com
4161274Szelenkov@nginx.com        body = '0123456789' * 1000
4171274Szelenkov@nginx.com        resp = self.post_http10(
4181274Szelenkov@nginx.com            headers={
4191274Szelenkov@nginx.com                'Host': 'localhost',
4201274Szelenkov@nginx.com                'Content-Type': 'text/html',
4211274Szelenkov@nginx.com                'Content-Length': str(len(body)),
4221274Szelenkov@nginx.com                'X-Parts': '2',
4231274Szelenkov@nginx.com                'X-Delay': '1',
4241274Szelenkov@nginx.com            },
4251274Szelenkov@nginx.com            body=body,
4261274Szelenkov@nginx.com        )
4271274Szelenkov@nginx.com
4281274Szelenkov@nginx.com        self.assertEqual(resp['status'], 200, 'status')
4291274Szelenkov@nginx.com        self.assertEqual(resp['body'], body, 'body')
4301274Szelenkov@nginx.com
4311274Szelenkov@nginx.com        resp = self.post_http10(
4321274Szelenkov@nginx.com            headers={
4331274Szelenkov@nginx.com                'Host': 'localhost',
4341274Szelenkov@nginx.com                'Content-Type': 'text/html',
4351274Szelenkov@nginx.com                'Content-Length': str(len(body)),
4361274Szelenkov@nginx.com                'X-Parts': '2',
4371274Szelenkov@nginx.com                'X-Delay': '1',
4381274Szelenkov@nginx.com            },
4391274Szelenkov@nginx.com            body=body,
4401274Szelenkov@nginx.com        )
4411274Szelenkov@nginx.com
4421274Szelenkov@nginx.com        self.assertEqual(resp['status'], 200, 'status')
4431274Szelenkov@nginx.com        self.assertEqual(resp['body'], body, 'body')
4441274Szelenkov@nginx.com
4451274Szelenkov@nginx.com    def test_proxy_delayed_close(self):
4461274Szelenkov@nginx.com        self.assertIn(
4471274Szelenkov@nginx.com            'success',
4481274Szelenkov@nginx.com            self.conf(
4491274Szelenkov@nginx.com                {"pass": "applications/delayed"}, 'listeners/*:7081'
4501274Szelenkov@nginx.com            ),
4511274Szelenkov@nginx.com            'delayed configure',
4521274Szelenkov@nginx.com        )
4531274Szelenkov@nginx.com
4541274Szelenkov@nginx.com        _, sock = self.post_http10(
4551274Szelenkov@nginx.com            headers={
4561274Szelenkov@nginx.com                'Host': 'localhost',
4571274Szelenkov@nginx.com                'Content-Type': 'text/html',
4581274Szelenkov@nginx.com                'Content-Length': '10000',
4591274Szelenkov@nginx.com                'X-Parts': '3',
4601274Szelenkov@nginx.com                'X-Delay': '1',
4611274Szelenkov@nginx.com            },
4621274Szelenkov@nginx.com            body='0123456789' * 1000,
4631274Szelenkov@nginx.com            start=True,
4641274Szelenkov@nginx.com            no_recv=True,
4651274Szelenkov@nginx.com        )
4661274Szelenkov@nginx.com
4671274Szelenkov@nginx.com        self.assertRegex(
4681274Szelenkov@nginx.com            sock.recv(100).decode(), '200 OK', 'first'
4691274Szelenkov@nginx.com        )
4701274Szelenkov@nginx.com        sock.close()
4711274Szelenkov@nginx.com
4721274Szelenkov@nginx.com        _, sock = self.post_http10(
4731274Szelenkov@nginx.com            headers={
4741274Szelenkov@nginx.com                'Host': 'localhost',
4751274Szelenkov@nginx.com                'Content-Type': 'text/html',
4761274Szelenkov@nginx.com                'Content-Length': '10000',
4771274Szelenkov@nginx.com                'X-Parts': '3',
4781274Szelenkov@nginx.com                'X-Delay': '1',
4791274Szelenkov@nginx.com            },
4801274Szelenkov@nginx.com            body='0123456789' * 1000,
4811274Szelenkov@nginx.com            start=True,
4821274Szelenkov@nginx.com            no_recv=True,
4831274Szelenkov@nginx.com        )
4841274Szelenkov@nginx.com
4851274Szelenkov@nginx.com        self.assertRegex(
4861274Szelenkov@nginx.com            sock.recv(100).decode(), '200 OK', 'second'
4871274Szelenkov@nginx.com        )
4881274Szelenkov@nginx.com        sock.close()
4891274Szelenkov@nginx.com
4901274Szelenkov@nginx.com    @unittest.skip('not yet')
4911274Szelenkov@nginx.com    def test_proxy_content_length(self):
4921274Szelenkov@nginx.com        self.assertIn(
4931274Szelenkov@nginx.com            'success',
4941274Szelenkov@nginx.com            self.conf(
4951274Szelenkov@nginx.com                [
4961274Szelenkov@nginx.com                    {
4971274Szelenkov@nginx.com                        "action": {
4981274Szelenkov@nginx.com                            "proxy": "http://127.0.0.1:"
4991274Szelenkov@nginx.com                            + str(self.SERVER_PORT)
5001274Szelenkov@nginx.com                        }
5011274Szelenkov@nginx.com                    }
5021274Szelenkov@nginx.com                ],
5031274Szelenkov@nginx.com                'routes',
5041274Szelenkov@nginx.com            ),
5051274Szelenkov@nginx.com            'proxy backend configure',
5061274Szelenkov@nginx.com        )
5071274Szelenkov@nginx.com
5081274Szelenkov@nginx.com        resp = self.get_http10()
5091274Szelenkov@nginx.com        self.assertEqual(len(resp['body']), 0, 'body lt Content-Length 0')
5101274Szelenkov@nginx.com
5111274Szelenkov@nginx.com        resp = self.get_http10(headers={'Host': 'localhost', 'X-Len': '5'})
5121274Szelenkov@nginx.com        self.assertEqual(len(resp['body']), 5, 'body lt Content-Length 5')
5131274Szelenkov@nginx.com
5141274Szelenkov@nginx.com        resp = self.get_http10(headers={'Host': 'localhost', 'X-Len': '9'})
5151274Szelenkov@nginx.com        self.assertEqual(len(resp['body']), 9, 'body lt Content-Length 9')
5161274Szelenkov@nginx.com
5171274Szelenkov@nginx.com        resp = self.get_http10(headers={'Host': 'localhost', 'X-Len': '11'})
5181274Szelenkov@nginx.com        self.assertEqual(len(resp['body']), 10, 'body gt Content-Length 11')
5191274Szelenkov@nginx.com
5201274Szelenkov@nginx.com        resp = self.get_http10(headers={'Host': 'localhost', 'X-Len': '15'})
5211274Szelenkov@nginx.com        self.assertEqual(len(resp['body']), 10, 'body gt Content-Length 15')
5221274Szelenkov@nginx.com
5231274Szelenkov@nginx.com    def test_proxy_invalid(self):
5241274Szelenkov@nginx.com        self.assertIn(
5251274Szelenkov@nginx.com            'error',
5261274Szelenkov@nginx.com            self.conf([{"action": {"proxy": 'blah'}}], 'routes'),
5271274Szelenkov@nginx.com            'proxy invalid',
5281274Szelenkov@nginx.com        )
5291274Szelenkov@nginx.com        self.assertIn(
5301274Szelenkov@nginx.com            'error',
5311274Szelenkov@nginx.com            self.conf([{"action": {"proxy": '/blah'}}], 'routes'),
5321274Szelenkov@nginx.com            'proxy invalid 2',
5331274Szelenkov@nginx.com        )
5341274Szelenkov@nginx.com        self.assertIn(
5351274Szelenkov@nginx.com            'error',
5361274Szelenkov@nginx.com            self.conf([{"action": {"proxy": 'unix:/blah'}}], 'routes'),
5371274Szelenkov@nginx.com            'proxy unix invalid 2',
5381274Szelenkov@nginx.com        )
5391274Szelenkov@nginx.com        self.assertIn(
5401274Szelenkov@nginx.com            'error',
5411274Szelenkov@nginx.com            self.conf([{"action": {"proxy": 'http://blah'}}], 'routes'),
5421274Szelenkov@nginx.com            'proxy unix invalid 3',
5431274Szelenkov@nginx.com        )
5441274Szelenkov@nginx.com        self.assertIn(
5451274Szelenkov@nginx.com            'error',
5461274Szelenkov@nginx.com            self.conf([{"action": {"proxy": 'http://127.0.0.1'}}], 'routes'),
5471274Szelenkov@nginx.com            'proxy ipv4 invalid',
5481274Szelenkov@nginx.com        )
5491274Szelenkov@nginx.com        self.assertIn(
5501274Szelenkov@nginx.com            'error',
5511274Szelenkov@nginx.com            self.conf([{"action": {"proxy": 'http://127.0.0.1:'}}], 'routes'),
5521274Szelenkov@nginx.com            'proxy ipv4 invalid 2',
5531274Szelenkov@nginx.com        )
5541274Szelenkov@nginx.com        self.assertIn(
5551274Szelenkov@nginx.com            'error',
5561274Szelenkov@nginx.com            self.conf(
5571274Szelenkov@nginx.com                [{"action": {"proxy": 'http://127.0.0.1:blah'}}], 'routes'
5581274Szelenkov@nginx.com            ),
5591274Szelenkov@nginx.com            'proxy ipv4 invalid 3',
5601274Szelenkov@nginx.com        )
5611274Szelenkov@nginx.com        self.assertIn(
5621274Szelenkov@nginx.com            'error',
5631274Szelenkov@nginx.com            self.conf(
5641274Szelenkov@nginx.com                [{"action": {"proxy": 'http://127.0.0.1:-1'}}], 'routes'
5651274Szelenkov@nginx.com            ),
5661274Szelenkov@nginx.com            'proxy ipv4 invalid 4',
5671274Szelenkov@nginx.com        )
5681274Szelenkov@nginx.com        self.assertIn(
5691274Szelenkov@nginx.com            'error',
5701274Szelenkov@nginx.com            self.conf(
5711274Szelenkov@nginx.com                [{"action": {"proxy": 'http://127.0.0.1:7080b'}}], 'routes'
5721274Szelenkov@nginx.com            ),
5731274Szelenkov@nginx.com            'proxy ipv4 invalid 5',
5741274Szelenkov@nginx.com        )
5751274Szelenkov@nginx.com        self.assertIn(
5761274Szelenkov@nginx.com            'error',
5771274Szelenkov@nginx.com            self.conf(
5781274Szelenkov@nginx.com                [{"action": {"proxy": 'http://[]'}}], 'routes'
5791274Szelenkov@nginx.com            ),
5801274Szelenkov@nginx.com            'proxy ipv6 invalid',
5811274Szelenkov@nginx.com        )
5821274Szelenkov@nginx.com        self.assertIn(
5831274Szelenkov@nginx.com            'error',
5841274Szelenkov@nginx.com            self.conf(
5851274Szelenkov@nginx.com                [{"action": {"proxy": 'http://[]:7080'}}], 'routes'
5861274Szelenkov@nginx.com            ),
5871274Szelenkov@nginx.com            'proxy ipv6 invalid 2',
5881274Szelenkov@nginx.com        )
5891274Szelenkov@nginx.com        self.assertIn(
5901274Szelenkov@nginx.com            'error',
5911274Szelenkov@nginx.com            self.conf(
5921274Szelenkov@nginx.com                [{"action": {"proxy": 'http://[:]:7080'}}], 'routes'
5931274Szelenkov@nginx.com            ),
5941274Szelenkov@nginx.com            'proxy ipv6 invalid 3',
5951274Szelenkov@nginx.com        )
5961274Szelenkov@nginx.com        self.assertIn(
5971274Szelenkov@nginx.com            'error',
5981274Szelenkov@nginx.com            self.conf(
5991274Szelenkov@nginx.com                [{"action": {"proxy": 'http://[::7080'}}], 'routes'
6001274Szelenkov@nginx.com            ),
6011274Szelenkov@nginx.com            'proxy ipv6 invalid 4',
6021274Szelenkov@nginx.com        )
6031274Szelenkov@nginx.com
6041274Szelenkov@nginx.com    def test_proxy_loop(self):
6051465Szelenkov@nginx.com        self.skip_alerts.extend(
6061465Szelenkov@nginx.com            [
6071465Szelenkov@nginx.com                r'socket.*failed',
6081465Szelenkov@nginx.com                r'accept.*failed',
6091465Szelenkov@nginx.com                r'new connections are not accepted',
6101465Szelenkov@nginx.com            ]
6111465Szelenkov@nginx.com        )
6121274Szelenkov@nginx.com        self.conf(
6131274Szelenkov@nginx.com            {
6141274Szelenkov@nginx.com                "listeners": {
6151274Szelenkov@nginx.com                    "*:7080": {"pass": "routes"},
6161274Szelenkov@nginx.com                    "*:7081": {"pass": "applications/mirror"},
6171274Szelenkov@nginx.com                    "*:7082": {"pass": "routes"},
6181274Szelenkov@nginx.com                },
6191274Szelenkov@nginx.com                "routes": [{"action": {"proxy": "http://127.0.0.1:7082"}}],
6201274Szelenkov@nginx.com                "applications": {
6211274Szelenkov@nginx.com                    "mirror": {
6221274Szelenkov@nginx.com                        "type": "python",
6231274Szelenkov@nginx.com                        "processes": {"spare": 0},
6241274Szelenkov@nginx.com                        "path": self.current_dir + "/python/mirror",
6251274Szelenkov@nginx.com                        "working_directory": self.current_dir
6261274Szelenkov@nginx.com                        + "/python/mirror",
6271274Szelenkov@nginx.com                        "module": "wsgi",
6281274Szelenkov@nginx.com                    },
6291274Szelenkov@nginx.com                },
6301274Szelenkov@nginx.com            }
6311274Szelenkov@nginx.com        )
6321274Szelenkov@nginx.com
6331274Szelenkov@nginx.com        self.get_http10(no_recv=True)
6341465Szelenkov@nginx.com        self.get_http10(read_timeout=1)
6351274Szelenkov@nginx.com
6361274Szelenkov@nginx.comif __name__ == '__main__':
6371274Szelenkov@nginx.com    TestProxy.main()
638