xref: /unit/test/test_proxy.py (revision 2073)
11274Szelenkov@nginx.comimport re
21477Szelenkov@nginx.comimport socket
31274Szelenkov@nginx.comimport time
41477Szelenkov@nginx.com
51635Szelenkov@nginx.comimport pytest
61654Szelenkov@nginx.comfrom conftest import run_process
71274Szelenkov@nginx.comfrom unit.applications.lang.python import TestApplicationPython
81730Szelenkov@nginx.comfrom unit.option import option
91735Szelenkov@nginx.comfrom unit.utils import waitforsocket
101274Szelenkov@nginx.com
111274Szelenkov@nginx.com
121274Szelenkov@nginx.comclass TestProxy(TestApplicationPython):
131467Szelenkov@nginx.com    prerequisites = {'modules': {'python': 'any'}}
141274Szelenkov@nginx.com
151274Szelenkov@nginx.com    SERVER_PORT = 7999
161274Szelenkov@nginx.com
171353Smax.romanov@nginx.com    @staticmethod
181353Smax.romanov@nginx.com    def run_server(server_port):
191274Szelenkov@nginx.com        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
201274Szelenkov@nginx.com        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
211274Szelenkov@nginx.com
221353Smax.romanov@nginx.com        server_address = ('', server_port)
231274Szelenkov@nginx.com        sock.bind(server_address)
241274Szelenkov@nginx.com        sock.listen(5)
251274Szelenkov@nginx.com
261274Szelenkov@nginx.com        def recvall(sock):
271274Szelenkov@nginx.com            buff_size = 4096
281274Szelenkov@nginx.com            data = b''
291274Szelenkov@nginx.com            while True:
301274Szelenkov@nginx.com                part = sock.recv(buff_size)
311274Szelenkov@nginx.com                data += part
321274Szelenkov@nginx.com                if len(part) < buff_size:
331274Szelenkov@nginx.com                    break
341274Szelenkov@nginx.com            return data
351274Szelenkov@nginx.com
361274Szelenkov@nginx.com        req = b"""HTTP/1.1 200 OK
371274Szelenkov@nginx.comContent-Length: 10
381274Szelenkov@nginx.com
391274Szelenkov@nginx.com"""
401274Szelenkov@nginx.com
411274Szelenkov@nginx.com        while True:
421274Szelenkov@nginx.com            connection, client_address = sock.accept()
431274Szelenkov@nginx.com
441274Szelenkov@nginx.com            data = recvall(connection).decode()
451274Szelenkov@nginx.com
461274Szelenkov@nginx.com            to_send = req
471274Szelenkov@nginx.com
481596Szelenkov@nginx.com            m = re.search(r'X-Len: (\d+)', data)
491274Szelenkov@nginx.com            if m:
501274Szelenkov@nginx.com                to_send += b'X' * int(m.group(1))
511274Szelenkov@nginx.com
521274Szelenkov@nginx.com            connection.sendall(to_send)
531274Szelenkov@nginx.com
541274Szelenkov@nginx.com            connection.close()
551274Szelenkov@nginx.com
561274Szelenkov@nginx.com    def get_http10(self, *args, **kwargs):
571274Szelenkov@nginx.com        return self.get(*args, http_10=True, **kwargs)
581274Szelenkov@nginx.com
591274Szelenkov@nginx.com    def post_http10(self, *args, **kwargs):
601274Szelenkov@nginx.com        return self.post(*args, http_10=True, **kwargs)
611274Szelenkov@nginx.com
621596Szelenkov@nginx.com    def setup_method(self):
631654Szelenkov@nginx.com        run_process(self.run_server, self.SERVER_PORT)
641654Szelenkov@nginx.com        waitforsocket(self.SERVER_PORT)
651274Szelenkov@nginx.com
661596Szelenkov@nginx.com        assert 'success' in self.conf(
671596Szelenkov@nginx.com            {
681596Szelenkov@nginx.com                "listeners": {
691596Szelenkov@nginx.com                    "*:7080": {"pass": "routes"},
701596Szelenkov@nginx.com                    "*:7081": {"pass": "applications/mirror"},
711596Szelenkov@nginx.com                },
721596Szelenkov@nginx.com                "routes": [{"action": {"proxy": "http://127.0.0.1:7081"}}],
731596Szelenkov@nginx.com                "applications": {
741596Szelenkov@nginx.com                    "mirror": {
752055Szelenkov@nginx.com                        "type": self.get_application_type(),
761596Szelenkov@nginx.com                        "processes": {"spare": 0},
771596Szelenkov@nginx.com                        "path": option.test_dir + "/python/mirror",
78*2073Szelenkov@nginx.com                        "working_directory": option.test_dir + "/python/mirror",
791596Szelenkov@nginx.com                        "module": "wsgi",
801274Szelenkov@nginx.com                    },
811596Szelenkov@nginx.com                    "custom_header": {
822055Szelenkov@nginx.com                        "type": self.get_application_type(),
831596Szelenkov@nginx.com                        "processes": {"spare": 0},
841596Szelenkov@nginx.com                        "path": option.test_dir + "/python/custom_header",
851596Szelenkov@nginx.com                        "working_directory": option.test_dir
861596Szelenkov@nginx.com                        + "/python/custom_header",
871596Szelenkov@nginx.com                        "module": "wsgi",
881274Szelenkov@nginx.com                    },
891596Szelenkov@nginx.com                    "delayed": {
902055Szelenkov@nginx.com                        "type": self.get_application_type(),
911596Szelenkov@nginx.com                        "processes": {"spare": 0},
921596Szelenkov@nginx.com                        "path": option.test_dir + "/python/delayed",
931596Szelenkov@nginx.com                        "working_directory": option.test_dir
941596Szelenkov@nginx.com                        + "/python/delayed",
951596Szelenkov@nginx.com                        "module": "wsgi",
961596Szelenkov@nginx.com                    },
971596Szelenkov@nginx.com                },
981596Szelenkov@nginx.com            }
991596Szelenkov@nginx.com        ), 'proxy initial configuration'
1001274Szelenkov@nginx.com
1011274Szelenkov@nginx.com    def test_proxy_http10(self):
1021274Szelenkov@nginx.com        for _ in range(10):
1031596Szelenkov@nginx.com            assert self.get_http10()['status'] == 200, 'status'
1041274Szelenkov@nginx.com
1051274Szelenkov@nginx.com    def test_proxy_chain(self):
1061596Szelenkov@nginx.com        assert 'success' in self.conf(
1071596Szelenkov@nginx.com            {
1081596Szelenkov@nginx.com                "listeners": {
1091596Szelenkov@nginx.com                    "*:7080": {"pass": "routes/first"},
1101596Szelenkov@nginx.com                    "*:7081": {"pass": "routes/second"},
1111596Szelenkov@nginx.com                    "*:7082": {"pass": "routes/third"},
1121596Szelenkov@nginx.com                    "*:7083": {"pass": "routes/fourth"},
1131596Szelenkov@nginx.com                    "*:7084": {"pass": "routes/fifth"},
1141596Szelenkov@nginx.com                    "*:7085": {"pass": "applications/mirror"},
1151596Szelenkov@nginx.com                },
1161596Szelenkov@nginx.com                "routes": {
1171596Szelenkov@nginx.com                    "first": [{"action": {"proxy": "http://127.0.0.1:7081"}}],
1181596Szelenkov@nginx.com                    "second": [{"action": {"proxy": "http://127.0.0.1:7082"}}],
1191596Szelenkov@nginx.com                    "third": [{"action": {"proxy": "http://127.0.0.1:7083"}}],
1201596Szelenkov@nginx.com                    "fourth": [{"action": {"proxy": "http://127.0.0.1:7084"}}],
1211596Szelenkov@nginx.com                    "fifth": [{"action": {"proxy": "http://127.0.0.1:7085"}}],
1221596Szelenkov@nginx.com                },
1231596Szelenkov@nginx.com                "applications": {
1241596Szelenkov@nginx.com                    "mirror": {
1252055Szelenkov@nginx.com                        "type": self.get_application_type(),
1261596Szelenkov@nginx.com                        "processes": {"spare": 0},
1271596Szelenkov@nginx.com                        "path": option.test_dir + "/python/mirror",
128*2073Szelenkov@nginx.com                        "working_directory": option.test_dir + "/python/mirror",
1291596Szelenkov@nginx.com                        "module": "wsgi",
1301596Szelenkov@nginx.com                    }
1311596Szelenkov@nginx.com                },
1321596Szelenkov@nginx.com            }
1331596Szelenkov@nginx.com        ), 'proxy chain configuration'
1341274Szelenkov@nginx.com
1351596Szelenkov@nginx.com        assert self.get_http10()['status'] == 200, 'status'
1361274Szelenkov@nginx.com
1371274Szelenkov@nginx.com    def test_proxy_body(self):
1381274Szelenkov@nginx.com        payload = '0123456789'
1391274Szelenkov@nginx.com        for _ in range(10):
1401274Szelenkov@nginx.com            resp = self.post_http10(body=payload)
1411274Szelenkov@nginx.com
1421596Szelenkov@nginx.com            assert resp['status'] == 200, 'status'
1431596Szelenkov@nginx.com            assert resp['body'] == payload, 'body'
1441274Szelenkov@nginx.com
1451274Szelenkov@nginx.com        payload = 'X' * 4096
1461274Szelenkov@nginx.com        for _ in range(10):
1471274Szelenkov@nginx.com            resp = self.post_http10(body=payload)
1481274Szelenkov@nginx.com
1491596Szelenkov@nginx.com            assert resp['status'] == 200, 'status'
1501596Szelenkov@nginx.com            assert resp['body'] == payload, 'body'
1511274Szelenkov@nginx.com
1521274Szelenkov@nginx.com        payload = 'X' * 4097
1531274Szelenkov@nginx.com        for _ in range(10):
1541274Szelenkov@nginx.com            resp = self.post_http10(body=payload)
1551274Szelenkov@nginx.com
1561596Szelenkov@nginx.com            assert resp['status'] == 200, 'status'
1571596Szelenkov@nginx.com            assert resp['body'] == payload, 'body'
1581274Szelenkov@nginx.com
1591274Szelenkov@nginx.com        payload = 'X' * 4096 * 256
1601274Szelenkov@nginx.com        for _ in range(10):
1611274Szelenkov@nginx.com            resp = self.post_http10(body=payload, read_buffer_size=4096 * 128)
1621274Szelenkov@nginx.com
1631596Szelenkov@nginx.com            assert resp['status'] == 200, 'status'
1641596Szelenkov@nginx.com            assert resp['body'] == payload, 'body'
1651274Szelenkov@nginx.com
1661274Szelenkov@nginx.com        payload = 'X' * 4096 * 257
1671274Szelenkov@nginx.com        for _ in range(10):
1681274Szelenkov@nginx.com            resp = self.post_http10(body=payload, read_buffer_size=4096 * 128)
1691274Szelenkov@nginx.com
1701596Szelenkov@nginx.com            assert resp['status'] == 200, 'status'
1711596Szelenkov@nginx.com            assert resp['body'] == payload, 'body'
1721274Szelenkov@nginx.com
1731775Szelenkov@nginx.com        assert 'success' in self.conf(
1741775Szelenkov@nginx.com            {'http': {'max_body_size': 32 * 1024 * 1024}}, 'settings'
1751775Szelenkov@nginx.com        )
1761366Szelenkov@nginx.com
1771366Szelenkov@nginx.com        payload = '0123456789abcdef' * 32 * 64 * 1024
1781366Szelenkov@nginx.com        resp = self.post_http10(body=payload, read_buffer_size=1024 * 1024)
1791596Szelenkov@nginx.com        assert resp['status'] == 200, 'status'
1801596Szelenkov@nginx.com        assert resp['body'] == payload, 'body'
1811366Szelenkov@nginx.com
1821274Szelenkov@nginx.com    def test_proxy_parallel(self):
1831274Szelenkov@nginx.com        payload = 'X' * 4096 * 257
1841274Szelenkov@nginx.com        buff_size = 4096 * 258
1851274Szelenkov@nginx.com
1861274Szelenkov@nginx.com        socks = []
1871274Szelenkov@nginx.com        for i in range(10):
1881274Szelenkov@nginx.com            _, sock = self.post_http10(
1891274Szelenkov@nginx.com                body=payload + str(i),
1901274Szelenkov@nginx.com                start=True,
1911274Szelenkov@nginx.com                no_recv=True,
1921274Szelenkov@nginx.com                read_buffer_size=buff_size,
1931274Szelenkov@nginx.com            )
1941274Szelenkov@nginx.com            socks.append(sock)
1951274Szelenkov@nginx.com
1961274Szelenkov@nginx.com        for i in range(10):
1971274Szelenkov@nginx.com            resp = self.recvall(socks[i], buff_size=buff_size).decode()
1981274Szelenkov@nginx.com            socks[i].close()
1991274Szelenkov@nginx.com
2001274Szelenkov@nginx.com            resp = self._resp_to_dict(resp)
2011274Szelenkov@nginx.com
2021596Szelenkov@nginx.com            assert resp['status'] == 200, 'status'
2031596Szelenkov@nginx.com            assert resp['body'] == payload + str(i), 'body'
2041274Szelenkov@nginx.com
2051274Szelenkov@nginx.com    def test_proxy_header(self):
2061596Szelenkov@nginx.com        assert 'success' in self.conf(
2071596Szelenkov@nginx.com            {"pass": "applications/custom_header"}, 'listeners/*:7081'
2081596Szelenkov@nginx.com        ), 'custom_header configure'
2091274Szelenkov@nginx.com
2101274Szelenkov@nginx.com        header_value = 'blah'
2111596Szelenkov@nginx.com        assert (
2121274Szelenkov@nginx.com            self.get_http10(
2131274Szelenkov@nginx.com                headers={'Host': 'localhost', 'Custom-Header': header_value}
2141596Szelenkov@nginx.com            )['headers']['Custom-Header']
2151596Szelenkov@nginx.com            == header_value
2161596Szelenkov@nginx.com        ), 'custom header'
2171274Szelenkov@nginx.com
2181635Szelenkov@nginx.com        header_value = r'(),/:;<=>?@[\]{}\t !#$%&\'*+-.^_`|~'
2191596Szelenkov@nginx.com        assert (
2201274Szelenkov@nginx.com            self.get_http10(
2211274Szelenkov@nginx.com                headers={'Host': 'localhost', 'Custom-Header': header_value}
2221596Szelenkov@nginx.com            )['headers']['Custom-Header']
2231596Szelenkov@nginx.com            == header_value
2241596Szelenkov@nginx.com        ), 'custom header 2'
2251274Szelenkov@nginx.com
2261274Szelenkov@nginx.com        header_value = 'X' * 4096
2271596Szelenkov@nginx.com        assert (
2281274Szelenkov@nginx.com            self.get_http10(
2291274Szelenkov@nginx.com                headers={'Host': 'localhost', 'Custom-Header': header_value}
2301596Szelenkov@nginx.com            )['headers']['Custom-Header']
2311596Szelenkov@nginx.com            == header_value
2321596Szelenkov@nginx.com        ), 'custom header 3'
2331274Szelenkov@nginx.com
2341274Szelenkov@nginx.com        header_value = 'X' * 8191
2351596Szelenkov@nginx.com        assert (
2361274Szelenkov@nginx.com            self.get_http10(
2371274Szelenkov@nginx.com                headers={'Host': 'localhost', 'Custom-Header': header_value}
2381596Szelenkov@nginx.com            )['headers']['Custom-Header']
2391596Szelenkov@nginx.com            == header_value
2401596Szelenkov@nginx.com        ), 'custom header 4'
2411274Szelenkov@nginx.com
2421274Szelenkov@nginx.com        header_value = 'X' * 8192
2431596Szelenkov@nginx.com        assert (
2441274Szelenkov@nginx.com            self.get_http10(
2451274Szelenkov@nginx.com                headers={'Host': 'localhost', 'Custom-Header': header_value}
2461596Szelenkov@nginx.com            )['status']
2471596Szelenkov@nginx.com            == 431
2481596Szelenkov@nginx.com        ), 'custom header 5'
2491274Szelenkov@nginx.com
2501274Szelenkov@nginx.com    def test_proxy_fragmented(self):
2511274Szelenkov@nginx.com        _, sock = self.http(
2521274Szelenkov@nginx.com            b"""GET / HTT""", raw=True, start=True, no_recv=True
2531274Szelenkov@nginx.com        )
2541274Szelenkov@nginx.com
2551274Szelenkov@nginx.com        time.sleep(1)
2561274Szelenkov@nginx.com
2571274Szelenkov@nginx.com        sock.sendall("P/1.0\r\nHost: localhos".encode())
2581274Szelenkov@nginx.com
2591274Szelenkov@nginx.com        time.sleep(1)
2601274Szelenkov@nginx.com
2611274Szelenkov@nginx.com        sock.sendall("t\r\n\r\n".encode())
2621274Szelenkov@nginx.com
2631596Szelenkov@nginx.com        assert re.search(
2641596Szelenkov@nginx.com            '200 OK', self.recvall(sock).decode()
2651596Szelenkov@nginx.com        ), 'fragmented send'
2661274Szelenkov@nginx.com        sock.close()
2671274Szelenkov@nginx.com
2681274Szelenkov@nginx.com    def test_proxy_fragmented_close(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\nHo".encode())
2761274Szelenkov@nginx.com
2771274Szelenkov@nginx.com        sock.close()
2781274Szelenkov@nginx.com
2791274Szelenkov@nginx.com    def test_proxy_fragmented_body(self):
2801274Szelenkov@nginx.com        _, sock = self.http(
2811274Szelenkov@nginx.com            b"""GET / HTT""", raw=True, start=True, no_recv=True
2821274Szelenkov@nginx.com        )
2831274Szelenkov@nginx.com
2841274Szelenkov@nginx.com        time.sleep(1)
2851274Szelenkov@nginx.com
2861274Szelenkov@nginx.com        sock.sendall("P/1.0\r\nHost: localhost\r\n".encode())
2871274Szelenkov@nginx.com        sock.sendall("Content-Length: 30000\r\n".encode())
2881274Szelenkov@nginx.com
2891274Szelenkov@nginx.com        time.sleep(1)
2901274Szelenkov@nginx.com
2911274Szelenkov@nginx.com        sock.sendall("\r\n".encode())
2921274Szelenkov@nginx.com        sock.sendall(("X" * 10000).encode())
2931274Szelenkov@nginx.com
2941274Szelenkov@nginx.com        time.sleep(1)
2951274Szelenkov@nginx.com
2961274Szelenkov@nginx.com        sock.sendall(("X" * 10000).encode())
2971274Szelenkov@nginx.com
2981274Szelenkov@nginx.com        time.sleep(1)
2991274Szelenkov@nginx.com
3001274Szelenkov@nginx.com        sock.sendall(("X" * 10000).encode())
3011274Szelenkov@nginx.com
3021274Szelenkov@nginx.com        resp = self._resp_to_dict(self.recvall(sock).decode())
3031274Szelenkov@nginx.com        sock.close()
3041274Szelenkov@nginx.com
3051596Szelenkov@nginx.com        assert resp['status'] == 200, 'status'
3061596Szelenkov@nginx.com        assert resp['body'] == "X" * 30000, 'body'
3071274Szelenkov@nginx.com
3081274Szelenkov@nginx.com    def test_proxy_fragmented_body_close(self):
3091274Szelenkov@nginx.com        _, sock = self.http(
3101274Szelenkov@nginx.com            b"""GET / HTT""", raw=True, start=True, no_recv=True
3111274Szelenkov@nginx.com        )
3121274Szelenkov@nginx.com
3131274Szelenkov@nginx.com        time.sleep(1)
3141274Szelenkov@nginx.com
3151274Szelenkov@nginx.com        sock.sendall("P/1.0\r\nHost: localhost\r\n".encode())
3161274Szelenkov@nginx.com        sock.sendall("Content-Length: 30000\r\n".encode())
3171274Szelenkov@nginx.com
3181274Szelenkov@nginx.com        time.sleep(1)
3191274Szelenkov@nginx.com
3201274Szelenkov@nginx.com        sock.sendall("\r\n".encode())
3211274Szelenkov@nginx.com        sock.sendall(("X" * 10000).encode())
3221274Szelenkov@nginx.com
3231274Szelenkov@nginx.com        sock.close()
3241274Szelenkov@nginx.com
3251274Szelenkov@nginx.com    def test_proxy_nowhere(self):
3261596Szelenkov@nginx.com        assert 'success' in self.conf(
3271596Szelenkov@nginx.com            [{"action": {"proxy": "http://127.0.0.1:7082"}}], 'routes'
3281596Szelenkov@nginx.com        ), 'proxy path changed'
3291274Szelenkov@nginx.com
3301596Szelenkov@nginx.com        assert self.get_http10()['status'] == 502, 'status'
3311274Szelenkov@nginx.com
3321274Szelenkov@nginx.com    def test_proxy_ipv6(self):
3331596Szelenkov@nginx.com        assert 'success' in self.conf(
3341596Szelenkov@nginx.com            {
3351596Szelenkov@nginx.com                "*:7080": {"pass": "routes"},
3361596Szelenkov@nginx.com                "[::1]:7081": {'application': 'mirror'},
3371596Szelenkov@nginx.com            },
3381596Szelenkov@nginx.com            'listeners',
3391596Szelenkov@nginx.com        ), 'add ipv6 listener configure'
3401274Szelenkov@nginx.com
3411596Szelenkov@nginx.com        assert 'success' in self.conf(
3421596Szelenkov@nginx.com            [{"action": {"proxy": "http://[::1]:7081"}}], 'routes'
3431596Szelenkov@nginx.com        ), 'proxy ipv6 configure'
3441274Szelenkov@nginx.com
3451596Szelenkov@nginx.com        assert self.get_http10()['status'] == 200, 'status'
3461274Szelenkov@nginx.com
3471654Szelenkov@nginx.com    def test_proxy_unix(self, temp_dir):
3481654Szelenkov@nginx.com        addr = temp_dir + '/sock'
3491274Szelenkov@nginx.com
3501596Szelenkov@nginx.com        assert 'success' in self.conf(
3511596Szelenkov@nginx.com            {
3521596Szelenkov@nginx.com                "*:7080": {"pass": "routes"},
3531596Szelenkov@nginx.com                "unix:" + addr: {'application': 'mirror'},
3541596Szelenkov@nginx.com            },
3551596Szelenkov@nginx.com            'listeners',
3561596Szelenkov@nginx.com        ), 'add unix listener configure'
3571274Szelenkov@nginx.com
3581596Szelenkov@nginx.com        assert 'success' in self.conf(
3591596Szelenkov@nginx.com            [{"action": {"proxy": 'http://unix:' + addr}}], 'routes'
3601596Szelenkov@nginx.com        ), 'proxy unix configure'
3611274Szelenkov@nginx.com
3621596Szelenkov@nginx.com        assert self.get_http10()['status'] == 200, 'status'
3631274Szelenkov@nginx.com
3641274Szelenkov@nginx.com    def test_proxy_delayed(self):
3651596Szelenkov@nginx.com        assert 'success' in self.conf(
3661596Szelenkov@nginx.com            {"pass": "applications/delayed"}, 'listeners/*:7081'
3671596Szelenkov@nginx.com        ), 'delayed configure'
3681274Szelenkov@nginx.com
3691274Szelenkov@nginx.com        body = '0123456789' * 1000
3701274Szelenkov@nginx.com        resp = self.post_http10(
3711274Szelenkov@nginx.com            headers={
3721274Szelenkov@nginx.com                'Host': 'localhost',
3731274Szelenkov@nginx.com                'Content-Type': 'text/html',
3741274Szelenkov@nginx.com                'Content-Length': str(len(body)),
3751274Szelenkov@nginx.com                'X-Parts': '2',
3761274Szelenkov@nginx.com                'X-Delay': '1',
3771274Szelenkov@nginx.com            },
3781274Szelenkov@nginx.com            body=body,
3791274Szelenkov@nginx.com        )
3801274Szelenkov@nginx.com
3811596Szelenkov@nginx.com        assert resp['status'] == 200, 'status'
3821596Szelenkov@nginx.com        assert resp['body'] == body, 'body'
3831274Szelenkov@nginx.com
3841274Szelenkov@nginx.com        resp = self.post_http10(
3851274Szelenkov@nginx.com            headers={
3861274Szelenkov@nginx.com                'Host': 'localhost',
3871274Szelenkov@nginx.com                'Content-Type': 'text/html',
3881274Szelenkov@nginx.com                'Content-Length': str(len(body)),
3891274Szelenkov@nginx.com                'X-Parts': '2',
3901274Szelenkov@nginx.com                'X-Delay': '1',
3911274Szelenkov@nginx.com            },
3921274Szelenkov@nginx.com            body=body,
3931274Szelenkov@nginx.com        )
3941274Szelenkov@nginx.com
3951596Szelenkov@nginx.com        assert resp['status'] == 200, 'status'
3961596Szelenkov@nginx.com        assert resp['body'] == body, 'body'
3971274Szelenkov@nginx.com
3981274Szelenkov@nginx.com    def test_proxy_delayed_close(self):
3991596Szelenkov@nginx.com        assert 'success' in self.conf(
4001596Szelenkov@nginx.com            {"pass": "applications/delayed"}, 'listeners/*:7081'
4011596Szelenkov@nginx.com        ), 'delayed configure'
4021274Szelenkov@nginx.com
4031274Szelenkov@nginx.com        _, sock = self.post_http10(
4041274Szelenkov@nginx.com            headers={
4051274Szelenkov@nginx.com                'Host': 'localhost',
4061274Szelenkov@nginx.com                'Content-Type': 'text/html',
4071274Szelenkov@nginx.com                'Content-Length': '10000',
4081274Szelenkov@nginx.com                'X-Parts': '3',
4091274Szelenkov@nginx.com                'X-Delay': '1',
4101274Szelenkov@nginx.com            },
4111274Szelenkov@nginx.com            body='0123456789' * 1000,
4121274Szelenkov@nginx.com            start=True,
4131274Szelenkov@nginx.com            no_recv=True,
4141274Szelenkov@nginx.com        )
4151274Szelenkov@nginx.com
4161596Szelenkov@nginx.com        assert re.search('200 OK', sock.recv(100).decode()), 'first'
4171274Szelenkov@nginx.com        sock.close()
4181274Szelenkov@nginx.com
4191274Szelenkov@nginx.com        _, sock = self.post_http10(
4201274Szelenkov@nginx.com            headers={
4211274Szelenkov@nginx.com                'Host': 'localhost',
4221274Szelenkov@nginx.com                'Content-Type': 'text/html',
4231274Szelenkov@nginx.com                'Content-Length': '10000',
4241274Szelenkov@nginx.com                'X-Parts': '3',
4251274Szelenkov@nginx.com                'X-Delay': '1',
4261274Szelenkov@nginx.com            },
4271274Szelenkov@nginx.com            body='0123456789' * 1000,
4281274Szelenkov@nginx.com            start=True,
4291274Szelenkov@nginx.com            no_recv=True,
4301274Szelenkov@nginx.com        )
4311274Szelenkov@nginx.com
4321596Szelenkov@nginx.com        assert re.search('200 OK', sock.recv(100).decode()), 'second'
4331274Szelenkov@nginx.com        sock.close()
4341274Szelenkov@nginx.com
4351596Szelenkov@nginx.com    @pytest.mark.skip('not yet')
4361274Szelenkov@nginx.com    def test_proxy_content_length(self):
4371596Szelenkov@nginx.com        assert 'success' in self.conf(
4381596Szelenkov@nginx.com            [
4391596Szelenkov@nginx.com                {
4401596Szelenkov@nginx.com                    "action": {
4411596Szelenkov@nginx.com                        "proxy": "http://127.0.0.1:" + str(self.SERVER_PORT)
4421274Szelenkov@nginx.com                    }
4431596Szelenkov@nginx.com                }
4441596Szelenkov@nginx.com            ],
4451596Szelenkov@nginx.com            'routes',
4461596Szelenkov@nginx.com        ), 'proxy backend configure'
4471274Szelenkov@nginx.com
4481274Szelenkov@nginx.com        resp = self.get_http10()
4491596Szelenkov@nginx.com        assert len(resp['body']) == 0, 'body lt Content-Length 0'
4501274Szelenkov@nginx.com
4511274Szelenkov@nginx.com        resp = self.get_http10(headers={'Host': 'localhost', 'X-Len': '5'})
4521596Szelenkov@nginx.com        assert len(resp['body']) == 5, 'body lt Content-Length 5'
4531274Szelenkov@nginx.com
4541274Szelenkov@nginx.com        resp = self.get_http10(headers={'Host': 'localhost', 'X-Len': '9'})
4551596Szelenkov@nginx.com        assert len(resp['body']) == 9, 'body lt Content-Length 9'
4561274Szelenkov@nginx.com
4571274Szelenkov@nginx.com        resp = self.get_http10(headers={'Host': 'localhost', 'X-Len': '11'})
4581596Szelenkov@nginx.com        assert len(resp['body']) == 10, 'body gt Content-Length 11'
4591274Szelenkov@nginx.com
4601274Szelenkov@nginx.com        resp = self.get_http10(headers={'Host': 'localhost', 'X-Len': '15'})
4611596Szelenkov@nginx.com        assert len(resp['body']) == 10, 'body gt Content-Length 15'
4621274Szelenkov@nginx.com
4631274Szelenkov@nginx.com    def test_proxy_invalid(self):
4641476Szelenkov@nginx.com        def check_proxy(proxy):
4651848Szelenkov@nginx.com            assert 'error' in self.conf(
4661848Szelenkov@nginx.com                [{"action": {"proxy": proxy}}], 'routes'
4671848Szelenkov@nginx.com            ), 'proxy invalid'
4681476Szelenkov@nginx.com
4691476Szelenkov@nginx.com        check_proxy('blah')
4701476Szelenkov@nginx.com        check_proxy('/blah')
4711476Szelenkov@nginx.com        check_proxy('unix:/blah')
4721476Szelenkov@nginx.com        check_proxy('http://blah')
4731476Szelenkov@nginx.com        check_proxy('http://127.0.0.1')
4741476Szelenkov@nginx.com        check_proxy('http://127.0.0.1:')
4751476Szelenkov@nginx.com        check_proxy('http://127.0.0.1:blah')
4761476Szelenkov@nginx.com        check_proxy('http://127.0.0.1:-1')
4771476Szelenkov@nginx.com        check_proxy('http://127.0.0.1:7080b')
4781476Szelenkov@nginx.com        check_proxy('http://[]')
4791476Szelenkov@nginx.com        check_proxy('http://[]:7080')
4801476Szelenkov@nginx.com        check_proxy('http://[:]:7080')
4811476Szelenkov@nginx.com        check_proxy('http://[::7080')
4821274Szelenkov@nginx.com
4831803Szelenkov@nginx.com    @pytest.mark.skip('not yet')
4841736Szelenkov@nginx.com    def test_proxy_loop(self, skip_alert):
4851596Szelenkov@nginx.com        skip_alert(
4861596Szelenkov@nginx.com            r'socket.*failed',
4871596Szelenkov@nginx.com            r'accept.*failed',
4881596Szelenkov@nginx.com            r'new connections are not accepted',
4891465Szelenkov@nginx.com        )
4901775Szelenkov@nginx.com        assert 'success' in self.conf(
4911274Szelenkov@nginx.com            {
4921274Szelenkov@nginx.com                "listeners": {
4931274Szelenkov@nginx.com                    "*:7080": {"pass": "routes"},
4941274Szelenkov@nginx.com                    "*:7081": {"pass": "applications/mirror"},
4951274Szelenkov@nginx.com                    "*:7082": {"pass": "routes"},
4961274Szelenkov@nginx.com                },
4971274Szelenkov@nginx.com                "routes": [{"action": {"proxy": "http://127.0.0.1:7082"}}],
4981274Szelenkov@nginx.com                "applications": {
4991274Szelenkov@nginx.com                    "mirror": {
5002055Szelenkov@nginx.com                        "type": self.get_application_type(),
5011274Szelenkov@nginx.com                        "processes": {"spare": 0},
5021596Szelenkov@nginx.com                        "path": option.test_dir + "/python/mirror",
503*2073Szelenkov@nginx.com                        "working_directory": option.test_dir + "/python/mirror",
5041274Szelenkov@nginx.com                        "module": "wsgi",
5051274Szelenkov@nginx.com                    },
5061274Szelenkov@nginx.com                },
5071274Szelenkov@nginx.com            }
5081274Szelenkov@nginx.com        )
5091274Szelenkov@nginx.com
5101274Szelenkov@nginx.com        self.get_http10(no_recv=True)
5111465Szelenkov@nginx.com        self.get_http10(read_timeout=1)
512