xref: /unit/test/test_proxy.py (revision 2055)
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": {
75*2055Szelenkov@nginx.com                        "type": self.get_application_type(),
761596Szelenkov@nginx.com                        "processes": {"spare": 0},
771596Szelenkov@nginx.com                        "path": option.test_dir + "/python/mirror",
781596Szelenkov@nginx.com                        "working_directory": option.test_dir
791596Szelenkov@nginx.com                        + "/python/mirror",
801596Szelenkov@nginx.com                        "module": "wsgi",
811274Szelenkov@nginx.com                    },
821596Szelenkov@nginx.com                    "custom_header": {
83*2055Szelenkov@nginx.com                        "type": self.get_application_type(),
841596Szelenkov@nginx.com                        "processes": {"spare": 0},
851596Szelenkov@nginx.com                        "path": option.test_dir + "/python/custom_header",
861596Szelenkov@nginx.com                        "working_directory": option.test_dir
871596Szelenkov@nginx.com                        + "/python/custom_header",
881596Szelenkov@nginx.com                        "module": "wsgi",
891274Szelenkov@nginx.com                    },
901596Szelenkov@nginx.com                    "delayed": {
91*2055Szelenkov@nginx.com                        "type": self.get_application_type(),
921596Szelenkov@nginx.com                        "processes": {"spare": 0},
931596Szelenkov@nginx.com                        "path": option.test_dir + "/python/delayed",
941596Szelenkov@nginx.com                        "working_directory": option.test_dir
951596Szelenkov@nginx.com                        + "/python/delayed",
961596Szelenkov@nginx.com                        "module": "wsgi",
971596Szelenkov@nginx.com                    },
981596Szelenkov@nginx.com                },
991596Szelenkov@nginx.com            }
1001596Szelenkov@nginx.com        ), 'proxy initial configuration'
1011274Szelenkov@nginx.com
1021274Szelenkov@nginx.com    def test_proxy_http10(self):
1031274Szelenkov@nginx.com        for _ in range(10):
1041596Szelenkov@nginx.com            assert self.get_http10()['status'] == 200, 'status'
1051274Szelenkov@nginx.com
1061274Szelenkov@nginx.com    def test_proxy_chain(self):
1071596Szelenkov@nginx.com        assert 'success' in self.conf(
1081596Szelenkov@nginx.com            {
1091596Szelenkov@nginx.com                "listeners": {
1101596Szelenkov@nginx.com                    "*:7080": {"pass": "routes/first"},
1111596Szelenkov@nginx.com                    "*:7081": {"pass": "routes/second"},
1121596Szelenkov@nginx.com                    "*:7082": {"pass": "routes/third"},
1131596Szelenkov@nginx.com                    "*:7083": {"pass": "routes/fourth"},
1141596Szelenkov@nginx.com                    "*:7084": {"pass": "routes/fifth"},
1151596Szelenkov@nginx.com                    "*:7085": {"pass": "applications/mirror"},
1161596Szelenkov@nginx.com                },
1171596Szelenkov@nginx.com                "routes": {
1181596Szelenkov@nginx.com                    "first": [{"action": {"proxy": "http://127.0.0.1:7081"}}],
1191596Szelenkov@nginx.com                    "second": [{"action": {"proxy": "http://127.0.0.1:7082"}}],
1201596Szelenkov@nginx.com                    "third": [{"action": {"proxy": "http://127.0.0.1:7083"}}],
1211596Szelenkov@nginx.com                    "fourth": [{"action": {"proxy": "http://127.0.0.1:7084"}}],
1221596Szelenkov@nginx.com                    "fifth": [{"action": {"proxy": "http://127.0.0.1:7085"}}],
1231596Szelenkov@nginx.com                },
1241596Szelenkov@nginx.com                "applications": {
1251596Szelenkov@nginx.com                    "mirror": {
126*2055Szelenkov@nginx.com                        "type": self.get_application_type(),
1271596Szelenkov@nginx.com                        "processes": {"spare": 0},
1281596Szelenkov@nginx.com                        "path": option.test_dir + "/python/mirror",
1291596Szelenkov@nginx.com                        "working_directory": option.test_dir
1301596Szelenkov@nginx.com                        + "/python/mirror",
1311596Szelenkov@nginx.com                        "module": "wsgi",
1321596Szelenkov@nginx.com                    }
1331596Szelenkov@nginx.com                },
1341596Szelenkov@nginx.com            }
1351596Szelenkov@nginx.com        ), 'proxy chain configuration'
1361274Szelenkov@nginx.com
1371596Szelenkov@nginx.com        assert self.get_http10()['status'] == 200, 'status'
1381274Szelenkov@nginx.com
1391274Szelenkov@nginx.com    def test_proxy_body(self):
1401274Szelenkov@nginx.com        payload = '0123456789'
1411274Szelenkov@nginx.com        for _ in range(10):
1421274Szelenkov@nginx.com            resp = self.post_http10(body=payload)
1431274Szelenkov@nginx.com
1441596Szelenkov@nginx.com            assert resp['status'] == 200, 'status'
1451596Szelenkov@nginx.com            assert resp['body'] == payload, 'body'
1461274Szelenkov@nginx.com
1471274Szelenkov@nginx.com        payload = 'X' * 4096
1481274Szelenkov@nginx.com        for _ in range(10):
1491274Szelenkov@nginx.com            resp = self.post_http10(body=payload)
1501274Szelenkov@nginx.com
1511596Szelenkov@nginx.com            assert resp['status'] == 200, 'status'
1521596Szelenkov@nginx.com            assert resp['body'] == payload, 'body'
1531274Szelenkov@nginx.com
1541274Szelenkov@nginx.com        payload = 'X' * 4097
1551274Szelenkov@nginx.com        for _ in range(10):
1561274Szelenkov@nginx.com            resp = self.post_http10(body=payload)
1571274Szelenkov@nginx.com
1581596Szelenkov@nginx.com            assert resp['status'] == 200, 'status'
1591596Szelenkov@nginx.com            assert resp['body'] == payload, 'body'
1601274Szelenkov@nginx.com
1611274Szelenkov@nginx.com        payload = 'X' * 4096 * 256
1621274Szelenkov@nginx.com        for _ in range(10):
1631274Szelenkov@nginx.com            resp = self.post_http10(body=payload, read_buffer_size=4096 * 128)
1641274Szelenkov@nginx.com
1651596Szelenkov@nginx.com            assert resp['status'] == 200, 'status'
1661596Szelenkov@nginx.com            assert resp['body'] == payload, 'body'
1671274Szelenkov@nginx.com
1681274Szelenkov@nginx.com        payload = 'X' * 4096 * 257
1691274Szelenkov@nginx.com        for _ in range(10):
1701274Szelenkov@nginx.com            resp = self.post_http10(body=payload, read_buffer_size=4096 * 128)
1711274Szelenkov@nginx.com
1721596Szelenkov@nginx.com            assert resp['status'] == 200, 'status'
1731596Szelenkov@nginx.com            assert resp['body'] == payload, 'body'
1741274Szelenkov@nginx.com
1751775Szelenkov@nginx.com        assert 'success' in self.conf(
1761775Szelenkov@nginx.com            {'http': {'max_body_size': 32 * 1024 * 1024}}, 'settings'
1771775Szelenkov@nginx.com        )
1781366Szelenkov@nginx.com
1791366Szelenkov@nginx.com        payload = '0123456789abcdef' * 32 * 64 * 1024
1801366Szelenkov@nginx.com        resp = self.post_http10(body=payload, read_buffer_size=1024 * 1024)
1811596Szelenkov@nginx.com        assert resp['status'] == 200, 'status'
1821596Szelenkov@nginx.com        assert resp['body'] == payload, 'body'
1831366Szelenkov@nginx.com
1841274Szelenkov@nginx.com    def test_proxy_parallel(self):
1851274Szelenkov@nginx.com        payload = 'X' * 4096 * 257
1861274Szelenkov@nginx.com        buff_size = 4096 * 258
1871274Szelenkov@nginx.com
1881274Szelenkov@nginx.com        socks = []
1891274Szelenkov@nginx.com        for i in range(10):
1901274Szelenkov@nginx.com            _, sock = self.post_http10(
1911274Szelenkov@nginx.com                body=payload + str(i),
1921274Szelenkov@nginx.com                start=True,
1931274Szelenkov@nginx.com                no_recv=True,
1941274Szelenkov@nginx.com                read_buffer_size=buff_size,
1951274Szelenkov@nginx.com            )
1961274Szelenkov@nginx.com            socks.append(sock)
1971274Szelenkov@nginx.com
1981274Szelenkov@nginx.com        for i in range(10):
1991274Szelenkov@nginx.com            resp = self.recvall(socks[i], buff_size=buff_size).decode()
2001274Szelenkov@nginx.com            socks[i].close()
2011274Szelenkov@nginx.com
2021274Szelenkov@nginx.com            resp = self._resp_to_dict(resp)
2031274Szelenkov@nginx.com
2041596Szelenkov@nginx.com            assert resp['status'] == 200, 'status'
2051596Szelenkov@nginx.com            assert resp['body'] == payload + str(i), 'body'
2061274Szelenkov@nginx.com
2071274Szelenkov@nginx.com    def test_proxy_header(self):
2081596Szelenkov@nginx.com        assert 'success' in self.conf(
2091596Szelenkov@nginx.com            {"pass": "applications/custom_header"}, 'listeners/*:7081'
2101596Szelenkov@nginx.com        ), 'custom_header configure'
2111274Szelenkov@nginx.com
2121274Szelenkov@nginx.com        header_value = 'blah'
2131596Szelenkov@nginx.com        assert (
2141274Szelenkov@nginx.com            self.get_http10(
2151274Szelenkov@nginx.com                headers={'Host': 'localhost', 'Custom-Header': header_value}
2161596Szelenkov@nginx.com            )['headers']['Custom-Header']
2171596Szelenkov@nginx.com            == header_value
2181596Szelenkov@nginx.com        ), 'custom header'
2191274Szelenkov@nginx.com
2201635Szelenkov@nginx.com        header_value = r'(),/:;<=>?@[\]{}\t !#$%&\'*+-.^_`|~'
2211596Szelenkov@nginx.com        assert (
2221274Szelenkov@nginx.com            self.get_http10(
2231274Szelenkov@nginx.com                headers={'Host': 'localhost', 'Custom-Header': header_value}
2241596Szelenkov@nginx.com            )['headers']['Custom-Header']
2251596Szelenkov@nginx.com            == header_value
2261596Szelenkov@nginx.com        ), 'custom header 2'
2271274Szelenkov@nginx.com
2281274Szelenkov@nginx.com        header_value = 'X' * 4096
2291596Szelenkov@nginx.com        assert (
2301274Szelenkov@nginx.com            self.get_http10(
2311274Szelenkov@nginx.com                headers={'Host': 'localhost', 'Custom-Header': header_value}
2321596Szelenkov@nginx.com            )['headers']['Custom-Header']
2331596Szelenkov@nginx.com            == header_value
2341596Szelenkov@nginx.com        ), 'custom header 3'
2351274Szelenkov@nginx.com
2361274Szelenkov@nginx.com        header_value = 'X' * 8191
2371596Szelenkov@nginx.com        assert (
2381274Szelenkov@nginx.com            self.get_http10(
2391274Szelenkov@nginx.com                headers={'Host': 'localhost', 'Custom-Header': header_value}
2401596Szelenkov@nginx.com            )['headers']['Custom-Header']
2411596Szelenkov@nginx.com            == header_value
2421596Szelenkov@nginx.com        ), 'custom header 4'
2431274Szelenkov@nginx.com
2441274Szelenkov@nginx.com        header_value = 'X' * 8192
2451596Szelenkov@nginx.com        assert (
2461274Szelenkov@nginx.com            self.get_http10(
2471274Szelenkov@nginx.com                headers={'Host': 'localhost', 'Custom-Header': header_value}
2481596Szelenkov@nginx.com            )['status']
2491596Szelenkov@nginx.com            == 431
2501596Szelenkov@nginx.com        ), 'custom header 5'
2511274Szelenkov@nginx.com
2521274Szelenkov@nginx.com    def test_proxy_fragmented(self):
2531274Szelenkov@nginx.com        _, sock = self.http(
2541274Szelenkov@nginx.com            b"""GET / HTT""", raw=True, start=True, no_recv=True
2551274Szelenkov@nginx.com        )
2561274Szelenkov@nginx.com
2571274Szelenkov@nginx.com        time.sleep(1)
2581274Szelenkov@nginx.com
2591274Szelenkov@nginx.com        sock.sendall("P/1.0\r\nHost: localhos".encode())
2601274Szelenkov@nginx.com
2611274Szelenkov@nginx.com        time.sleep(1)
2621274Szelenkov@nginx.com
2631274Szelenkov@nginx.com        sock.sendall("t\r\n\r\n".encode())
2641274Szelenkov@nginx.com
2651596Szelenkov@nginx.com        assert re.search(
2661596Szelenkov@nginx.com            '200 OK', self.recvall(sock).decode()
2671596Szelenkov@nginx.com        ), 'fragmented send'
2681274Szelenkov@nginx.com        sock.close()
2691274Szelenkov@nginx.com
2701274Szelenkov@nginx.com    def test_proxy_fragmented_close(self):
2711274Szelenkov@nginx.com        _, sock = self.http(
2721274Szelenkov@nginx.com            b"""GET / HTT""", raw=True, start=True, no_recv=True
2731274Szelenkov@nginx.com        )
2741274Szelenkov@nginx.com
2751274Szelenkov@nginx.com        time.sleep(1)
2761274Szelenkov@nginx.com
2771274Szelenkov@nginx.com        sock.sendall("P/1.0\r\nHo".encode())
2781274Szelenkov@nginx.com
2791274Szelenkov@nginx.com        sock.close()
2801274Szelenkov@nginx.com
2811274Szelenkov@nginx.com    def test_proxy_fragmented_body(self):
2821274Szelenkov@nginx.com        _, sock = self.http(
2831274Szelenkov@nginx.com            b"""GET / HTT""", raw=True, start=True, no_recv=True
2841274Szelenkov@nginx.com        )
2851274Szelenkov@nginx.com
2861274Szelenkov@nginx.com        time.sleep(1)
2871274Szelenkov@nginx.com
2881274Szelenkov@nginx.com        sock.sendall("P/1.0\r\nHost: localhost\r\n".encode())
2891274Szelenkov@nginx.com        sock.sendall("Content-Length: 30000\r\n".encode())
2901274Szelenkov@nginx.com
2911274Szelenkov@nginx.com        time.sleep(1)
2921274Szelenkov@nginx.com
2931274Szelenkov@nginx.com        sock.sendall("\r\n".encode())
2941274Szelenkov@nginx.com        sock.sendall(("X" * 10000).encode())
2951274Szelenkov@nginx.com
2961274Szelenkov@nginx.com        time.sleep(1)
2971274Szelenkov@nginx.com
2981274Szelenkov@nginx.com        sock.sendall(("X" * 10000).encode())
2991274Szelenkov@nginx.com
3001274Szelenkov@nginx.com        time.sleep(1)
3011274Szelenkov@nginx.com
3021274Szelenkov@nginx.com        sock.sendall(("X" * 10000).encode())
3031274Szelenkov@nginx.com
3041274Szelenkov@nginx.com        resp = self._resp_to_dict(self.recvall(sock).decode())
3051274Szelenkov@nginx.com        sock.close()
3061274Szelenkov@nginx.com
3071596Szelenkov@nginx.com        assert resp['status'] == 200, 'status'
3081596Szelenkov@nginx.com        assert resp['body'] == "X" * 30000, 'body'
3091274Szelenkov@nginx.com
3101274Szelenkov@nginx.com    def test_proxy_fragmented_body_close(self):
3111274Szelenkov@nginx.com        _, sock = self.http(
3121274Szelenkov@nginx.com            b"""GET / HTT""", raw=True, start=True, no_recv=True
3131274Szelenkov@nginx.com        )
3141274Szelenkov@nginx.com
3151274Szelenkov@nginx.com        time.sleep(1)
3161274Szelenkov@nginx.com
3171274Szelenkov@nginx.com        sock.sendall("P/1.0\r\nHost: localhost\r\n".encode())
3181274Szelenkov@nginx.com        sock.sendall("Content-Length: 30000\r\n".encode())
3191274Szelenkov@nginx.com
3201274Szelenkov@nginx.com        time.sleep(1)
3211274Szelenkov@nginx.com
3221274Szelenkov@nginx.com        sock.sendall("\r\n".encode())
3231274Szelenkov@nginx.com        sock.sendall(("X" * 10000).encode())
3241274Szelenkov@nginx.com
3251274Szelenkov@nginx.com        sock.close()
3261274Szelenkov@nginx.com
3271274Szelenkov@nginx.com    def test_proxy_nowhere(self):
3281596Szelenkov@nginx.com        assert 'success' in self.conf(
3291596Szelenkov@nginx.com            [{"action": {"proxy": "http://127.0.0.1:7082"}}], 'routes'
3301596Szelenkov@nginx.com        ), 'proxy path changed'
3311274Szelenkov@nginx.com
3321596Szelenkov@nginx.com        assert self.get_http10()['status'] == 502, 'status'
3331274Szelenkov@nginx.com
3341274Szelenkov@nginx.com    def test_proxy_ipv6(self):
3351596Szelenkov@nginx.com        assert 'success' in self.conf(
3361596Szelenkov@nginx.com            {
3371596Szelenkov@nginx.com                "*:7080": {"pass": "routes"},
3381596Szelenkov@nginx.com                "[::1]:7081": {'application': 'mirror'},
3391596Szelenkov@nginx.com            },
3401596Szelenkov@nginx.com            'listeners',
3411596Szelenkov@nginx.com        ), 'add ipv6 listener configure'
3421274Szelenkov@nginx.com
3431596Szelenkov@nginx.com        assert 'success' in self.conf(
3441596Szelenkov@nginx.com            [{"action": {"proxy": "http://[::1]:7081"}}], 'routes'
3451596Szelenkov@nginx.com        ), 'proxy ipv6 configure'
3461274Szelenkov@nginx.com
3471596Szelenkov@nginx.com        assert self.get_http10()['status'] == 200, 'status'
3481274Szelenkov@nginx.com
3491654Szelenkov@nginx.com    def test_proxy_unix(self, temp_dir):
3501654Szelenkov@nginx.com        addr = temp_dir + '/sock'
3511274Szelenkov@nginx.com
3521596Szelenkov@nginx.com        assert 'success' in self.conf(
3531596Szelenkov@nginx.com            {
3541596Szelenkov@nginx.com                "*:7080": {"pass": "routes"},
3551596Szelenkov@nginx.com                "unix:" + addr: {'application': 'mirror'},
3561596Szelenkov@nginx.com            },
3571596Szelenkov@nginx.com            'listeners',
3581596Szelenkov@nginx.com        ), 'add unix listener configure'
3591274Szelenkov@nginx.com
3601596Szelenkov@nginx.com        assert 'success' in self.conf(
3611596Szelenkov@nginx.com            [{"action": {"proxy": 'http://unix:' + addr}}], 'routes'
3621596Szelenkov@nginx.com        ), 'proxy unix configure'
3631274Szelenkov@nginx.com
3641596Szelenkov@nginx.com        assert self.get_http10()['status'] == 200, 'status'
3651274Szelenkov@nginx.com
3661274Szelenkov@nginx.com    def test_proxy_delayed(self):
3671596Szelenkov@nginx.com        assert 'success' in self.conf(
3681596Szelenkov@nginx.com            {"pass": "applications/delayed"}, 'listeners/*:7081'
3691596Szelenkov@nginx.com        ), 'delayed configure'
3701274Szelenkov@nginx.com
3711274Szelenkov@nginx.com        body = '0123456789' * 1000
3721274Szelenkov@nginx.com        resp = self.post_http10(
3731274Szelenkov@nginx.com            headers={
3741274Szelenkov@nginx.com                'Host': 'localhost',
3751274Szelenkov@nginx.com                'Content-Type': 'text/html',
3761274Szelenkov@nginx.com                'Content-Length': str(len(body)),
3771274Szelenkov@nginx.com                'X-Parts': '2',
3781274Szelenkov@nginx.com                'X-Delay': '1',
3791274Szelenkov@nginx.com            },
3801274Szelenkov@nginx.com            body=body,
3811274Szelenkov@nginx.com        )
3821274Szelenkov@nginx.com
3831596Szelenkov@nginx.com        assert resp['status'] == 200, 'status'
3841596Szelenkov@nginx.com        assert resp['body'] == body, 'body'
3851274Szelenkov@nginx.com
3861274Szelenkov@nginx.com        resp = self.post_http10(
3871274Szelenkov@nginx.com            headers={
3881274Szelenkov@nginx.com                'Host': 'localhost',
3891274Szelenkov@nginx.com                'Content-Type': 'text/html',
3901274Szelenkov@nginx.com                'Content-Length': str(len(body)),
3911274Szelenkov@nginx.com                'X-Parts': '2',
3921274Szelenkov@nginx.com                'X-Delay': '1',
3931274Szelenkov@nginx.com            },
3941274Szelenkov@nginx.com            body=body,
3951274Szelenkov@nginx.com        )
3961274Szelenkov@nginx.com
3971596Szelenkov@nginx.com        assert resp['status'] == 200, 'status'
3981596Szelenkov@nginx.com        assert resp['body'] == body, 'body'
3991274Szelenkov@nginx.com
4001274Szelenkov@nginx.com    def test_proxy_delayed_close(self):
4011596Szelenkov@nginx.com        assert 'success' in self.conf(
4021596Szelenkov@nginx.com            {"pass": "applications/delayed"}, 'listeners/*:7081'
4031596Szelenkov@nginx.com        ), 'delayed configure'
4041274Szelenkov@nginx.com
4051274Szelenkov@nginx.com        _, sock = self.post_http10(
4061274Szelenkov@nginx.com            headers={
4071274Szelenkov@nginx.com                'Host': 'localhost',
4081274Szelenkov@nginx.com                'Content-Type': 'text/html',
4091274Szelenkov@nginx.com                'Content-Length': '10000',
4101274Szelenkov@nginx.com                'X-Parts': '3',
4111274Szelenkov@nginx.com                'X-Delay': '1',
4121274Szelenkov@nginx.com            },
4131274Szelenkov@nginx.com            body='0123456789' * 1000,
4141274Szelenkov@nginx.com            start=True,
4151274Szelenkov@nginx.com            no_recv=True,
4161274Szelenkov@nginx.com        )
4171274Szelenkov@nginx.com
4181596Szelenkov@nginx.com        assert re.search('200 OK', sock.recv(100).decode()), 'first'
4191274Szelenkov@nginx.com        sock.close()
4201274Szelenkov@nginx.com
4211274Szelenkov@nginx.com        _, sock = self.post_http10(
4221274Szelenkov@nginx.com            headers={
4231274Szelenkov@nginx.com                'Host': 'localhost',
4241274Szelenkov@nginx.com                'Content-Type': 'text/html',
4251274Szelenkov@nginx.com                'Content-Length': '10000',
4261274Szelenkov@nginx.com                'X-Parts': '3',
4271274Szelenkov@nginx.com                'X-Delay': '1',
4281274Szelenkov@nginx.com            },
4291274Szelenkov@nginx.com            body='0123456789' * 1000,
4301274Szelenkov@nginx.com            start=True,
4311274Szelenkov@nginx.com            no_recv=True,
4321274Szelenkov@nginx.com        )
4331274Szelenkov@nginx.com
4341596Szelenkov@nginx.com        assert re.search('200 OK', sock.recv(100).decode()), 'second'
4351274Szelenkov@nginx.com        sock.close()
4361274Szelenkov@nginx.com
4371596Szelenkov@nginx.com    @pytest.mark.skip('not yet')
4381274Szelenkov@nginx.com    def test_proxy_content_length(self):
4391596Szelenkov@nginx.com        assert 'success' in self.conf(
4401596Szelenkov@nginx.com            [
4411596Szelenkov@nginx.com                {
4421596Szelenkov@nginx.com                    "action": {
4431596Szelenkov@nginx.com                        "proxy": "http://127.0.0.1:" + str(self.SERVER_PORT)
4441274Szelenkov@nginx.com                    }
4451596Szelenkov@nginx.com                }
4461596Szelenkov@nginx.com            ],
4471596Szelenkov@nginx.com            'routes',
4481596Szelenkov@nginx.com        ), 'proxy backend configure'
4491274Szelenkov@nginx.com
4501274Szelenkov@nginx.com        resp = self.get_http10()
4511596Szelenkov@nginx.com        assert len(resp['body']) == 0, 'body lt Content-Length 0'
4521274Szelenkov@nginx.com
4531274Szelenkov@nginx.com        resp = self.get_http10(headers={'Host': 'localhost', 'X-Len': '5'})
4541596Szelenkov@nginx.com        assert len(resp['body']) == 5, 'body lt Content-Length 5'
4551274Szelenkov@nginx.com
4561274Szelenkov@nginx.com        resp = self.get_http10(headers={'Host': 'localhost', 'X-Len': '9'})
4571596Szelenkov@nginx.com        assert len(resp['body']) == 9, 'body lt Content-Length 9'
4581274Szelenkov@nginx.com
4591274Szelenkov@nginx.com        resp = self.get_http10(headers={'Host': 'localhost', 'X-Len': '11'})
4601596Szelenkov@nginx.com        assert len(resp['body']) == 10, 'body gt Content-Length 11'
4611274Szelenkov@nginx.com
4621274Szelenkov@nginx.com        resp = self.get_http10(headers={'Host': 'localhost', 'X-Len': '15'})
4631596Szelenkov@nginx.com        assert len(resp['body']) == 10, 'body gt Content-Length 15'
4641274Szelenkov@nginx.com
4651274Szelenkov@nginx.com    def test_proxy_invalid(self):
4661476Szelenkov@nginx.com        def check_proxy(proxy):
4671848Szelenkov@nginx.com            assert 'error' in self.conf(
4681848Szelenkov@nginx.com                [{"action": {"proxy": proxy}}], 'routes'
4691848Szelenkov@nginx.com            ), 'proxy invalid'
4701476Szelenkov@nginx.com
4711476Szelenkov@nginx.com        check_proxy('blah')
4721476Szelenkov@nginx.com        check_proxy('/blah')
4731476Szelenkov@nginx.com        check_proxy('unix:/blah')
4741476Szelenkov@nginx.com        check_proxy('http://blah')
4751476Szelenkov@nginx.com        check_proxy('http://127.0.0.1')
4761476Szelenkov@nginx.com        check_proxy('http://127.0.0.1:')
4771476Szelenkov@nginx.com        check_proxy('http://127.0.0.1:blah')
4781476Szelenkov@nginx.com        check_proxy('http://127.0.0.1:-1')
4791476Szelenkov@nginx.com        check_proxy('http://127.0.0.1:7080b')
4801476Szelenkov@nginx.com        check_proxy('http://[]')
4811476Szelenkov@nginx.com        check_proxy('http://[]:7080')
4821476Szelenkov@nginx.com        check_proxy('http://[:]:7080')
4831476Szelenkov@nginx.com        check_proxy('http://[::7080')
4841274Szelenkov@nginx.com
4851803Szelenkov@nginx.com    @pytest.mark.skip('not yet')
4861736Szelenkov@nginx.com    def test_proxy_loop(self, skip_alert):
4871596Szelenkov@nginx.com        skip_alert(
4881596Szelenkov@nginx.com            r'socket.*failed',
4891596Szelenkov@nginx.com            r'accept.*failed',
4901596Szelenkov@nginx.com            r'new connections are not accepted',
4911465Szelenkov@nginx.com        )
4921775Szelenkov@nginx.com        assert 'success' in self.conf(
4931274Szelenkov@nginx.com            {
4941274Szelenkov@nginx.com                "listeners": {
4951274Szelenkov@nginx.com                    "*:7080": {"pass": "routes"},
4961274Szelenkov@nginx.com                    "*:7081": {"pass": "applications/mirror"},
4971274Szelenkov@nginx.com                    "*:7082": {"pass": "routes"},
4981274Szelenkov@nginx.com                },
4991274Szelenkov@nginx.com                "routes": [{"action": {"proxy": "http://127.0.0.1:7082"}}],
5001274Szelenkov@nginx.com                "applications": {
5011274Szelenkov@nginx.com                    "mirror": {
502*2055Szelenkov@nginx.com                        "type": self.get_application_type(),
5031274Szelenkov@nginx.com                        "processes": {"spare": 0},
5041596Szelenkov@nginx.com                        "path": option.test_dir + "/python/mirror",
5051848Szelenkov@nginx.com                        "working_directory": option.test_dir
5061848Szelenkov@nginx.com                        + "/python/mirror",
5071274Szelenkov@nginx.com                        "module": "wsgi",
5081274Szelenkov@nginx.com                    },
5091274Szelenkov@nginx.com                },
5101274Szelenkov@nginx.com            }
5111274Szelenkov@nginx.com        )
5121274Szelenkov@nginx.com
5131274Szelenkov@nginx.com        self.get_http10(no_recv=True)
5141465Szelenkov@nginx.com        self.get_http10(read_timeout=1)
515