xref: /unit/test/test_proxy.py (revision 1353)
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):
91274Szelenkov@nginx.com    prerequisites = {'modules': ['python']}
101274Szelenkov@nginx.com
111274Szelenkov@nginx.com    SERVER_PORT = 7999
121274Szelenkov@nginx.com
13*1353Smax.romanov@nginx.com    @staticmethod
14*1353Smax.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
18*1353Smax.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
61*1353Smax.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
1911274Szelenkov@nginx.com    def test_proxy_parallel(self):
1921274Szelenkov@nginx.com        payload = 'X' * 4096 * 257
1931274Szelenkov@nginx.com        buff_size = 4096 * 258
1941274Szelenkov@nginx.com
1951274Szelenkov@nginx.com        socks = []
1961274Szelenkov@nginx.com        for i in range(10):
1971274Szelenkov@nginx.com            _, sock = self.post_http10(
1981274Szelenkov@nginx.com                body=payload + str(i),
1991274Szelenkov@nginx.com                start=True,
2001274Szelenkov@nginx.com                no_recv=True,
2011274Szelenkov@nginx.com                read_buffer_size=buff_size,
2021274Szelenkov@nginx.com            )
2031274Szelenkov@nginx.com            socks.append(sock)
2041274Szelenkov@nginx.com
2051274Szelenkov@nginx.com        for i in range(10):
2061274Szelenkov@nginx.com            resp = self.recvall(socks[i], buff_size=buff_size).decode()
2071274Szelenkov@nginx.com            socks[i].close()
2081274Szelenkov@nginx.com
2091274Szelenkov@nginx.com            resp = self._resp_to_dict(resp)
2101274Szelenkov@nginx.com
2111274Szelenkov@nginx.com            self.assertEqual(resp['status'], 200, 'status')
2121274Szelenkov@nginx.com            self.assertEqual(resp['body'], payload + str(i), 'body')
2131274Szelenkov@nginx.com
2141274Szelenkov@nginx.com    def test_proxy_header(self):
2151274Szelenkov@nginx.com        self.assertIn(
2161274Szelenkov@nginx.com            'success',
2171274Szelenkov@nginx.com            self.conf(
2181274Szelenkov@nginx.com                {"pass": "applications/custom_header"}, 'listeners/*:7081'
2191274Szelenkov@nginx.com            ),
2201274Szelenkov@nginx.com            'custom_header configure',
2211274Szelenkov@nginx.com        )
2221274Szelenkov@nginx.com
2231274Szelenkov@nginx.com        header_value = 'blah'
2241274Szelenkov@nginx.com        self.assertEqual(
2251274Szelenkov@nginx.com            self.get_http10(
2261274Szelenkov@nginx.com                headers={'Host': 'localhost', 'Custom-Header': header_value}
2271274Szelenkov@nginx.com            )['headers']['Custom-Header'],
2281274Szelenkov@nginx.com            header_value,
2291274Szelenkov@nginx.com            'custom header',
2301274Szelenkov@nginx.com        )
2311274Szelenkov@nginx.com
2321274Szelenkov@nginx.com        header_value = '(),/:;<=>?@[\]{}\t !#$%&\'*+-.^_`|~'
2331274Szelenkov@nginx.com        self.assertEqual(
2341274Szelenkov@nginx.com            self.get_http10(
2351274Szelenkov@nginx.com                headers={'Host': 'localhost', 'Custom-Header': header_value}
2361274Szelenkov@nginx.com            )['headers']['Custom-Header'],
2371274Szelenkov@nginx.com            header_value,
2381274Szelenkov@nginx.com            'custom header 2',
2391274Szelenkov@nginx.com        )
2401274Szelenkov@nginx.com
2411274Szelenkov@nginx.com        header_value = 'X' * 4096
2421274Szelenkov@nginx.com        self.assertEqual(
2431274Szelenkov@nginx.com            self.get_http10(
2441274Szelenkov@nginx.com                headers={'Host': 'localhost', 'Custom-Header': header_value}
2451274Szelenkov@nginx.com            )['headers']['Custom-Header'],
2461274Szelenkov@nginx.com            header_value,
2471274Szelenkov@nginx.com            'custom header 3',
2481274Szelenkov@nginx.com        )
2491274Szelenkov@nginx.com
2501274Szelenkov@nginx.com        header_value = 'X' * 8191
2511274Szelenkov@nginx.com        self.assertEqual(
2521274Szelenkov@nginx.com            self.get_http10(
2531274Szelenkov@nginx.com                headers={'Host': 'localhost', 'Custom-Header': header_value}
2541274Szelenkov@nginx.com            )['headers']['Custom-Header'],
2551274Szelenkov@nginx.com            header_value,
2561274Szelenkov@nginx.com            'custom header 4',
2571274Szelenkov@nginx.com        )
2581274Szelenkov@nginx.com
2591274Szelenkov@nginx.com        header_value = 'X' * 8192
2601274Szelenkov@nginx.com        self.assertEqual(
2611274Szelenkov@nginx.com            self.get_http10(
2621274Szelenkov@nginx.com                headers={'Host': 'localhost', 'Custom-Header': header_value}
2631274Szelenkov@nginx.com            )['status'],
2641274Szelenkov@nginx.com            431,
2651274Szelenkov@nginx.com            'custom header 5',
2661274Szelenkov@nginx.com        )
2671274Szelenkov@nginx.com
2681274Szelenkov@nginx.com    def test_proxy_fragmented(self):
2691274Szelenkov@nginx.com        _, sock = self.http(
2701274Szelenkov@nginx.com            b"""GET / HTT""", raw=True, start=True, no_recv=True
2711274Szelenkov@nginx.com        )
2721274Szelenkov@nginx.com
2731274Szelenkov@nginx.com        time.sleep(1)
2741274Szelenkov@nginx.com
2751274Szelenkov@nginx.com        sock.sendall("P/1.0\r\nHost: localhos".encode())
2761274Szelenkov@nginx.com
2771274Szelenkov@nginx.com        time.sleep(1)
2781274Szelenkov@nginx.com
2791274Szelenkov@nginx.com        sock.sendall("t\r\n\r\n".encode())
2801274Szelenkov@nginx.com
2811274Szelenkov@nginx.com        self.assertRegex(
2821274Szelenkov@nginx.com            self.recvall(sock).decode(), '200 OK', 'fragmented send'
2831274Szelenkov@nginx.com        )
2841274Szelenkov@nginx.com        sock.close()
2851274Szelenkov@nginx.com
2861274Szelenkov@nginx.com    def test_proxy_fragmented_close(self):
2871274Szelenkov@nginx.com        _, sock = self.http(
2881274Szelenkov@nginx.com            b"""GET / HTT""", raw=True, start=True, no_recv=True
2891274Szelenkov@nginx.com        )
2901274Szelenkov@nginx.com
2911274Szelenkov@nginx.com        time.sleep(1)
2921274Szelenkov@nginx.com
2931274Szelenkov@nginx.com        sock.sendall("P/1.0\r\nHo".encode())
2941274Szelenkov@nginx.com
2951274Szelenkov@nginx.com        sock.close()
2961274Szelenkov@nginx.com
2971274Szelenkov@nginx.com    def test_proxy_fragmented_body(self):
2981274Szelenkov@nginx.com        _, sock = self.http(
2991274Szelenkov@nginx.com            b"""GET / HTT""", raw=True, start=True, no_recv=True
3001274Szelenkov@nginx.com        )
3011274Szelenkov@nginx.com
3021274Szelenkov@nginx.com        time.sleep(1)
3031274Szelenkov@nginx.com
3041274Szelenkov@nginx.com        sock.sendall("P/1.0\r\nHost: localhost\r\n".encode())
3051274Szelenkov@nginx.com        sock.sendall("Content-Length: 30000\r\n".encode())
3061274Szelenkov@nginx.com
3071274Szelenkov@nginx.com        time.sleep(1)
3081274Szelenkov@nginx.com
3091274Szelenkov@nginx.com        sock.sendall("\r\n".encode())
3101274Szelenkov@nginx.com        sock.sendall(("X" * 10000).encode())
3111274Szelenkov@nginx.com
3121274Szelenkov@nginx.com        time.sleep(1)
3131274Szelenkov@nginx.com
3141274Szelenkov@nginx.com        sock.sendall(("X" * 10000).encode())
3151274Szelenkov@nginx.com
3161274Szelenkov@nginx.com        time.sleep(1)
3171274Szelenkov@nginx.com
3181274Szelenkov@nginx.com        sock.sendall(("X" * 10000).encode())
3191274Szelenkov@nginx.com
3201274Szelenkov@nginx.com        resp = self._resp_to_dict(self.recvall(sock).decode())
3211274Szelenkov@nginx.com        sock.close()
3221274Szelenkov@nginx.com
3231274Szelenkov@nginx.com        self.assertEqual(resp['status'], 200, 'status')
3241274Szelenkov@nginx.com        self.assertEqual(resp['body'], "X" * 30000, 'body')
3251274Szelenkov@nginx.com
3261274Szelenkov@nginx.com    def test_proxy_fragmented_body_close(self):
3271274Szelenkov@nginx.com        _, sock = self.http(
3281274Szelenkov@nginx.com            b"""GET / HTT""", raw=True, start=True, no_recv=True
3291274Szelenkov@nginx.com        )
3301274Szelenkov@nginx.com
3311274Szelenkov@nginx.com        time.sleep(1)
3321274Szelenkov@nginx.com
3331274Szelenkov@nginx.com        sock.sendall("P/1.0\r\nHost: localhost\r\n".encode())
3341274Szelenkov@nginx.com        sock.sendall("Content-Length: 30000\r\n".encode())
3351274Szelenkov@nginx.com
3361274Szelenkov@nginx.com        time.sleep(1)
3371274Szelenkov@nginx.com
3381274Szelenkov@nginx.com        sock.sendall("\r\n".encode())
3391274Szelenkov@nginx.com        sock.sendall(("X" * 10000).encode())
3401274Szelenkov@nginx.com
3411274Szelenkov@nginx.com        sock.close()
3421274Szelenkov@nginx.com
3431274Szelenkov@nginx.com    def test_proxy_nowhere(self):
3441274Szelenkov@nginx.com        self.assertIn(
3451274Szelenkov@nginx.com            'success',
3461274Szelenkov@nginx.com            self.conf(
3471274Szelenkov@nginx.com                [{"action": {"proxy": "http://127.0.0.1:7082"}}], 'routes'
3481274Szelenkov@nginx.com            ),
3491274Szelenkov@nginx.com            'proxy path changed',
3501274Szelenkov@nginx.com        )
3511274Szelenkov@nginx.com
3521274Szelenkov@nginx.com        self.assertEqual(self.get_http10()['status'], 502, 'status')
3531274Szelenkov@nginx.com
3541274Szelenkov@nginx.com    def test_proxy_ipv6(self):
3551274Szelenkov@nginx.com        self.assertIn(
3561274Szelenkov@nginx.com            'success',
3571274Szelenkov@nginx.com            self.conf(
3581274Szelenkov@nginx.com                {
3591274Szelenkov@nginx.com                    "*:7080": {"pass": "routes"},
3601274Szelenkov@nginx.com                    "[::1]:7081": {'application': 'mirror'},
3611274Szelenkov@nginx.com                },
3621274Szelenkov@nginx.com                'listeners',
3631274Szelenkov@nginx.com            ),
3641274Szelenkov@nginx.com            'add ipv6 listener configure',
3651274Szelenkov@nginx.com        )
3661274Szelenkov@nginx.com
3671274Szelenkov@nginx.com        self.assertIn(
3681274Szelenkov@nginx.com            'success',
3691274Szelenkov@nginx.com            self.conf([{"action": {"proxy": "http://[::1]:7081"}}], 'routes'),
3701274Szelenkov@nginx.com            'proxy ipv6 configure',
3711274Szelenkov@nginx.com        )
3721274Szelenkov@nginx.com
3731274Szelenkov@nginx.com        self.assertEqual(self.get_http10()['status'], 200, 'status')
3741274Szelenkov@nginx.com
3751274Szelenkov@nginx.com    def test_proxy_unix(self):
3761274Szelenkov@nginx.com        addr = self.testdir + '/sock'
3771274Szelenkov@nginx.com
3781274Szelenkov@nginx.com        self.assertIn(
3791274Szelenkov@nginx.com            'success',
3801274Szelenkov@nginx.com            self.conf(
3811274Szelenkov@nginx.com                {
3821274Szelenkov@nginx.com                    "*:7080": {"pass": "routes"},
3831274Szelenkov@nginx.com                    "unix:" + addr: {'application': 'mirror'},
3841274Szelenkov@nginx.com                },
3851274Szelenkov@nginx.com                'listeners',
3861274Szelenkov@nginx.com            ),
3871274Szelenkov@nginx.com            'add unix listener configure',
3881274Szelenkov@nginx.com        )
3891274Szelenkov@nginx.com
3901274Szelenkov@nginx.com        self.assertIn(
3911274Szelenkov@nginx.com            'success',
3921274Szelenkov@nginx.com            self.conf(
3931274Szelenkov@nginx.com                [{"action": {"proxy": 'http://unix:' + addr}}], 'routes'
3941274Szelenkov@nginx.com            ),
3951274Szelenkov@nginx.com            'proxy unix configure',
3961274Szelenkov@nginx.com        )
3971274Szelenkov@nginx.com
3981274Szelenkov@nginx.com        self.assertEqual(self.get_http10()['status'], 200, 'status')
3991274Szelenkov@nginx.com
4001274Szelenkov@nginx.com    def test_proxy_delayed(self):
4011274Szelenkov@nginx.com        self.assertIn(
4021274Szelenkov@nginx.com            'success',
4031274Szelenkov@nginx.com            self.conf(
4041274Szelenkov@nginx.com                {"pass": "applications/delayed"}, 'listeners/*:7081'
4051274Szelenkov@nginx.com            ),
4061274Szelenkov@nginx.com            'delayed configure',
4071274Szelenkov@nginx.com        )
4081274Szelenkov@nginx.com
4091274Szelenkov@nginx.com        body = '0123456789' * 1000
4101274Szelenkov@nginx.com        resp = self.post_http10(
4111274Szelenkov@nginx.com            headers={
4121274Szelenkov@nginx.com                'Host': 'localhost',
4131274Szelenkov@nginx.com                'Content-Type': 'text/html',
4141274Szelenkov@nginx.com                'Content-Length': str(len(body)),
4151274Szelenkov@nginx.com                'X-Parts': '2',
4161274Szelenkov@nginx.com                'X-Delay': '1',
4171274Szelenkov@nginx.com            },
4181274Szelenkov@nginx.com            body=body,
4191274Szelenkov@nginx.com        )
4201274Szelenkov@nginx.com
4211274Szelenkov@nginx.com        self.assertEqual(resp['status'], 200, 'status')
4221274Szelenkov@nginx.com        self.assertEqual(resp['body'], body, 'body')
4231274Szelenkov@nginx.com
4241274Szelenkov@nginx.com        resp = self.post_http10(
4251274Szelenkov@nginx.com            headers={
4261274Szelenkov@nginx.com                'Host': 'localhost',
4271274Szelenkov@nginx.com                'Content-Type': 'text/html',
4281274Szelenkov@nginx.com                'Content-Length': str(len(body)),
4291274Szelenkov@nginx.com                'X-Parts': '2',
4301274Szelenkov@nginx.com                'X-Delay': '1',
4311274Szelenkov@nginx.com            },
4321274Szelenkov@nginx.com            body=body,
4331274Szelenkov@nginx.com        )
4341274Szelenkov@nginx.com
4351274Szelenkov@nginx.com        self.assertEqual(resp['status'], 200, 'status')
4361274Szelenkov@nginx.com        self.assertEqual(resp['body'], body, 'body')
4371274Szelenkov@nginx.com
4381274Szelenkov@nginx.com    def test_proxy_delayed_close(self):
4391274Szelenkov@nginx.com        self.assertIn(
4401274Szelenkov@nginx.com            'success',
4411274Szelenkov@nginx.com            self.conf(
4421274Szelenkov@nginx.com                {"pass": "applications/delayed"}, 'listeners/*:7081'
4431274Szelenkov@nginx.com            ),
4441274Szelenkov@nginx.com            'delayed configure',
4451274Szelenkov@nginx.com        )
4461274Szelenkov@nginx.com
4471274Szelenkov@nginx.com        _, sock = self.post_http10(
4481274Szelenkov@nginx.com            headers={
4491274Szelenkov@nginx.com                'Host': 'localhost',
4501274Szelenkov@nginx.com                'Content-Type': 'text/html',
4511274Szelenkov@nginx.com                'Content-Length': '10000',
4521274Szelenkov@nginx.com                'X-Parts': '3',
4531274Szelenkov@nginx.com                'X-Delay': '1',
4541274Szelenkov@nginx.com            },
4551274Szelenkov@nginx.com            body='0123456789' * 1000,
4561274Szelenkov@nginx.com            start=True,
4571274Szelenkov@nginx.com            no_recv=True,
4581274Szelenkov@nginx.com        )
4591274Szelenkov@nginx.com
4601274Szelenkov@nginx.com        self.assertRegex(
4611274Szelenkov@nginx.com            sock.recv(100).decode(), '200 OK', 'first'
4621274Szelenkov@nginx.com        )
4631274Szelenkov@nginx.com        sock.close()
4641274Szelenkov@nginx.com
4651274Szelenkov@nginx.com        _, sock = self.post_http10(
4661274Szelenkov@nginx.com            headers={
4671274Szelenkov@nginx.com                'Host': 'localhost',
4681274Szelenkov@nginx.com                'Content-Type': 'text/html',
4691274Szelenkov@nginx.com                'Content-Length': '10000',
4701274Szelenkov@nginx.com                'X-Parts': '3',
4711274Szelenkov@nginx.com                'X-Delay': '1',
4721274Szelenkov@nginx.com            },
4731274Szelenkov@nginx.com            body='0123456789' * 1000,
4741274Szelenkov@nginx.com            start=True,
4751274Szelenkov@nginx.com            no_recv=True,
4761274Szelenkov@nginx.com        )
4771274Szelenkov@nginx.com
4781274Szelenkov@nginx.com        self.assertRegex(
4791274Szelenkov@nginx.com            sock.recv(100).decode(), '200 OK', 'second'
4801274Szelenkov@nginx.com        )
4811274Szelenkov@nginx.com        sock.close()
4821274Szelenkov@nginx.com
4831274Szelenkov@nginx.com    @unittest.skip('not yet')
4841274Szelenkov@nginx.com    def test_proxy_content_length(self):
4851274Szelenkov@nginx.com        self.assertIn(
4861274Szelenkov@nginx.com            'success',
4871274Szelenkov@nginx.com            self.conf(
4881274Szelenkov@nginx.com                [
4891274Szelenkov@nginx.com                    {
4901274Szelenkov@nginx.com                        "action": {
4911274Szelenkov@nginx.com                            "proxy": "http://127.0.0.1:"
4921274Szelenkov@nginx.com                            + str(self.SERVER_PORT)
4931274Szelenkov@nginx.com                        }
4941274Szelenkov@nginx.com                    }
4951274Szelenkov@nginx.com                ],
4961274Szelenkov@nginx.com                'routes',
4971274Szelenkov@nginx.com            ),
4981274Szelenkov@nginx.com            'proxy backend configure',
4991274Szelenkov@nginx.com        )
5001274Szelenkov@nginx.com
5011274Szelenkov@nginx.com        resp = self.get_http10()
5021274Szelenkov@nginx.com        self.assertEqual(len(resp['body']), 0, 'body lt Content-Length 0')
5031274Szelenkov@nginx.com
5041274Szelenkov@nginx.com        resp = self.get_http10(headers={'Host': 'localhost', 'X-Len': '5'})
5051274Szelenkov@nginx.com        self.assertEqual(len(resp['body']), 5, 'body lt Content-Length 5')
5061274Szelenkov@nginx.com
5071274Szelenkov@nginx.com        resp = self.get_http10(headers={'Host': 'localhost', 'X-Len': '9'})
5081274Szelenkov@nginx.com        self.assertEqual(len(resp['body']), 9, 'body lt Content-Length 9')
5091274Szelenkov@nginx.com
5101274Szelenkov@nginx.com        resp = self.get_http10(headers={'Host': 'localhost', 'X-Len': '11'})
5111274Szelenkov@nginx.com        self.assertEqual(len(resp['body']), 10, 'body gt Content-Length 11')
5121274Szelenkov@nginx.com
5131274Szelenkov@nginx.com        resp = self.get_http10(headers={'Host': 'localhost', 'X-Len': '15'})
5141274Szelenkov@nginx.com        self.assertEqual(len(resp['body']), 10, 'body gt Content-Length 15')
5151274Szelenkov@nginx.com
5161274Szelenkov@nginx.com    def test_proxy_invalid(self):
5171274Szelenkov@nginx.com        self.assertIn(
5181274Szelenkov@nginx.com            'error',
5191274Szelenkov@nginx.com            self.conf([{"action": {"proxy": 'blah'}}], 'routes'),
5201274Szelenkov@nginx.com            'proxy invalid',
5211274Szelenkov@nginx.com        )
5221274Szelenkov@nginx.com        self.assertIn(
5231274Szelenkov@nginx.com            'error',
5241274Szelenkov@nginx.com            self.conf([{"action": {"proxy": '/blah'}}], 'routes'),
5251274Szelenkov@nginx.com            'proxy invalid 2',
5261274Szelenkov@nginx.com        )
5271274Szelenkov@nginx.com        self.assertIn(
5281274Szelenkov@nginx.com            'error',
5291274Szelenkov@nginx.com            self.conf([{"action": {"proxy": 'unix:/blah'}}], 'routes'),
5301274Szelenkov@nginx.com            'proxy unix invalid 2',
5311274Szelenkov@nginx.com        )
5321274Szelenkov@nginx.com        self.assertIn(
5331274Szelenkov@nginx.com            'error',
5341274Szelenkov@nginx.com            self.conf([{"action": {"proxy": 'http://blah'}}], 'routes'),
5351274Szelenkov@nginx.com            'proxy unix invalid 3',
5361274Szelenkov@nginx.com        )
5371274Szelenkov@nginx.com        self.assertIn(
5381274Szelenkov@nginx.com            'error',
5391274Szelenkov@nginx.com            self.conf([{"action": {"proxy": 'http://127.0.0.1'}}], 'routes'),
5401274Szelenkov@nginx.com            'proxy ipv4 invalid',
5411274Szelenkov@nginx.com        )
5421274Szelenkov@nginx.com        self.assertIn(
5431274Szelenkov@nginx.com            'error',
5441274Szelenkov@nginx.com            self.conf([{"action": {"proxy": 'http://127.0.0.1:'}}], 'routes'),
5451274Szelenkov@nginx.com            'proxy ipv4 invalid 2',
5461274Szelenkov@nginx.com        )
5471274Szelenkov@nginx.com        self.assertIn(
5481274Szelenkov@nginx.com            'error',
5491274Szelenkov@nginx.com            self.conf(
5501274Szelenkov@nginx.com                [{"action": {"proxy": 'http://127.0.0.1:blah'}}], 'routes'
5511274Szelenkov@nginx.com            ),
5521274Szelenkov@nginx.com            'proxy ipv4 invalid 3',
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:-1'}}], 'routes'
5581274Szelenkov@nginx.com            ),
5591274Szelenkov@nginx.com            'proxy ipv4 invalid 4',
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:7080b'}}], 'routes'
5651274Szelenkov@nginx.com            ),
5661274Szelenkov@nginx.com            'proxy ipv4 invalid 5',
5671274Szelenkov@nginx.com        )
5681274Szelenkov@nginx.com        self.assertIn(
5691274Szelenkov@nginx.com            'error',
5701274Szelenkov@nginx.com            self.conf(
5711274Szelenkov@nginx.com                [{"action": {"proxy": 'http://[]'}}], 'routes'
5721274Szelenkov@nginx.com            ),
5731274Szelenkov@nginx.com            'proxy ipv6 invalid',
5741274Szelenkov@nginx.com        )
5751274Szelenkov@nginx.com        self.assertIn(
5761274Szelenkov@nginx.com            'error',
5771274Szelenkov@nginx.com            self.conf(
5781274Szelenkov@nginx.com                [{"action": {"proxy": 'http://[]:7080'}}], 'routes'
5791274Szelenkov@nginx.com            ),
5801274Szelenkov@nginx.com            'proxy ipv6 invalid 2',
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 3',
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 4',
5951274Szelenkov@nginx.com        )
5961274Szelenkov@nginx.com
5971274Szelenkov@nginx.com    @unittest.skip('not yet')
5981274Szelenkov@nginx.com    def test_proxy_loop(self):
5991274Szelenkov@nginx.com        self.conf(
6001274Szelenkov@nginx.com            {
6011274Szelenkov@nginx.com                "listeners": {
6021274Szelenkov@nginx.com                    "*:7080": {"pass": "routes"},
6031274Szelenkov@nginx.com                    "*:7081": {"pass": "applications/mirror"},
6041274Szelenkov@nginx.com                    "*:7082": {"pass": "routes"},
6051274Szelenkov@nginx.com                },
6061274Szelenkov@nginx.com                "routes": [{"action": {"proxy": "http://127.0.0.1:7082"}}],
6071274Szelenkov@nginx.com                "applications": {
6081274Szelenkov@nginx.com                    "mirror": {
6091274Szelenkov@nginx.com                        "type": "python",
6101274Szelenkov@nginx.com                        "processes": {"spare": 0},
6111274Szelenkov@nginx.com                        "path": self.current_dir + "/python/mirror",
6121274Szelenkov@nginx.com                        "working_directory": self.current_dir
6131274Szelenkov@nginx.com                        + "/python/mirror",
6141274Szelenkov@nginx.com                        "module": "wsgi",
6151274Szelenkov@nginx.com                    },
6161274Szelenkov@nginx.com                },
6171274Szelenkov@nginx.com            }
6181274Szelenkov@nginx.com        )
6191274Szelenkov@nginx.com
6201274Szelenkov@nginx.com        self.get_http10(no_recv=True)
6211274Szelenkov@nginx.com
6221274Szelenkov@nginx.comif __name__ == '__main__':
6231274Szelenkov@nginx.com    TestProxy.main()
624