Path: blob/main/external/curl/tests/http/test_13_proxy_auth.py
2066 views
#!/usr/bin/env python31# -*- coding: utf-8 -*-2#***************************************************************************3# _ _ ____ _4# Project ___| | | | _ \| |5# / __| | | | |_) | |6# | (__| |_| | _ <| |___7# \___|\___/|_| \_\_____|8#9# Copyright (C) Daniel Stenberg, <[email protected]>, et al.10#11# This software is licensed as described in the file COPYING, which12# you should have received as part of this distribution. The terms13# are also available at https://curl.se/docs/copyright.html.14#15# You may opt to use, copy, modify, merge, publish, distribute and/or sell16# copies of the Software, and permit persons to whom the Software is17# furnished to do so, under the terms of the COPYING file.18#19# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY20# KIND, either express or implied.21#22# SPDX-License-Identifier: curl23#24###########################################################################25#26import logging27import os28import re29import pytest3031from testenv import Env, CurlClient, ExecResult323334log = logging.getLogger(__name__)353637@pytest.mark.skipif(condition=Env.setup_incomplete(),38reason=f"missing: {Env.incomplete_reason()}")39class TestProxyAuth:4041def httpd_configure(self, env, httpd):42httpd.set_proxy_auth(True)43httpd.reload_if_config_changed()4445def get_tunnel_proto_used(self, r: ExecResult):46for line in r.trace_lines:47m = re.match(r'.* CONNECT tunnel: (\S+) negotiated$', line)48if m:49return m.group(1)50assert False, f'tunnel protocol not found in:\n{"".join(r.trace_lines)}'51return None5253# download via http: proxy (no tunnel), no auth54def test_13_01_proxy_no_auth(self, env: Env, httpd, configures_httpd):55self.httpd_configure(env, httpd)56curl = CurlClient(env=env)57url = f'http://localhost:{env.http_port}/data.json'58r = curl.http_download(urls=[url], alpn_proto='http/1.1', with_stats=True,59extra_args=curl.get_proxy_args(proxys=False))60r.check_response(count=1, http_status=407)6162# download via http: proxy (no tunnel), auth63def test_13_02_proxy_auth(self, env: Env, httpd, configures_httpd):64self.httpd_configure(env, httpd)65curl = CurlClient(env=env)66url = f'http://localhost:{env.http_port}/data.json'67xargs = curl.get_proxy_args(proxys=False)68xargs.extend(['--proxy-user', 'proxy:proxy'])69r = curl.http_download(urls=[url], alpn_proto='http/1.1', with_stats=True,70extra_args=xargs)71r.check_response(count=1, http_status=200)7273@pytest.mark.skipif(condition=not Env.curl_has_feature('HTTPS-proxy'),74reason='curl lacks HTTPS-proxy support')75@pytest.mark.skipif(condition=not Env.have_nghttpx(), reason="no nghttpx available")76def test_13_03_proxys_no_auth(self, env: Env, httpd, configures_httpd, nghttpx_fwd):77self.httpd_configure(env, httpd)78curl = CurlClient(env=env)79url = f'http://localhost:{env.http_port}/data.json'80xargs = curl.get_proxy_args(proxys=True)81r = curl.http_download(urls=[url], alpn_proto='http/1.1', with_stats=True,82extra_args=xargs)83r.check_response(count=1, http_status=407)8485@pytest.mark.skipif(condition=not Env.curl_has_feature('HTTPS-proxy'),86reason='curl lacks HTTPS-proxy support')87@pytest.mark.skipif(condition=not Env.have_nghttpx(), reason="no nghttpx available")88def test_13_04_proxys_auth(self, env: Env, httpd, configures_httpd, nghttpx_fwd):89self.httpd_configure(env, httpd)90curl = CurlClient(env=env)91url = f'http://localhost:{env.http_port}/data.json'92xargs = curl.get_proxy_args(proxys=True)93xargs.extend(['--proxy-user', 'proxy:proxy'])94r = curl.http_download(urls=[url], alpn_proto='http/1.1', with_stats=True,95extra_args=xargs)96r.check_response(count=1, http_status=200)9798def test_13_05_tunnel_http_no_auth(self, env: Env, httpd, configures_httpd, nghttpx_fwd):99self.httpd_configure(env, httpd)100curl = CurlClient(env=env)101url = f'http://localhost:{env.http_port}/data.json'102xargs = curl.get_proxy_args(proxys=False, tunnel=True)103r = curl.http_download(urls=[url], alpn_proto='http/1.1', with_stats=True,104extra_args=xargs)105# expect "COULD_NOT_CONNECT"106r.check_response(exitcode=56, http_status=None)107108def test_13_06_tunnel_http_auth(self, env: Env, httpd, configures_httpd):109self.httpd_configure(env, httpd)110curl = CurlClient(env=env)111url = f'http://localhost:{env.http_port}/data.json'112xargs = curl.get_proxy_args(proxys=False, tunnel=True)113xargs.extend(['--proxy-user', 'proxy:proxy'])114r = curl.http_download(urls=[url], alpn_proto='http/1.1', with_stats=True,115extra_args=xargs)116r.check_response(count=1, http_status=200)117118@pytest.mark.skipif(condition=not Env.have_nghttpx(), reason="no nghttpx available")119@pytest.mark.skipif(condition=not Env.curl_has_feature('HTTPS-proxy'),120reason='curl lacks HTTPS-proxy support')121@pytest.mark.parametrize("proto", ['http/1.1', 'h2'])122@pytest.mark.parametrize("tunnel", ['http/1.1', 'h2'])123def test_13_07_tunnels_no_auth(self, env: Env, httpd, configures_httpd, nghttpx_fwd, proto, tunnel):124self.httpd_configure(env, httpd)125if tunnel == 'h2' and not env.curl_uses_lib('nghttp2'):126pytest.skip('only supported with nghttp2')127curl = CurlClient(env=env)128url = f'https://localhost:{env.https_port}/data.json'129xargs = curl.get_proxy_args(proxys=True, tunnel=True, proto=tunnel)130r = curl.http_download(urls=[url], alpn_proto=proto, with_stats=True,131extra_args=xargs)132# expect "COULD_NOT_CONNECT"133r.check_response(exitcode=56, http_status=None)134assert self.get_tunnel_proto_used(r) == 'HTTP/2' \135if tunnel == 'h2' else 'HTTP/1.1'136137@pytest.mark.skipif(condition=not Env.have_nghttpx(), reason="no nghttpx available")138@pytest.mark.skipif(condition=not Env.curl_has_feature('HTTPS-proxy'),139reason='curl lacks HTTPS-proxy support')140@pytest.mark.parametrize("proto", ['http/1.1', 'h2'])141@pytest.mark.parametrize("tunnel", ['http/1.1', 'h2'])142def test_13_08_tunnels_auth(self, env: Env, httpd, configures_httpd, nghttpx_fwd, proto, tunnel):143self.httpd_configure(env, httpd)144if tunnel == 'h2' and not env.curl_uses_lib('nghttp2'):145pytest.skip('only supported with nghttp2')146curl = CurlClient(env=env)147url = f'https://localhost:{env.https_port}/data.json'148xargs = curl.get_proxy_args(proxys=True, tunnel=True, proto=tunnel)149xargs.extend(['--proxy-user', 'proxy:proxy'])150r = curl.http_download(urls=[url], alpn_proto=proto, with_stats=True,151extra_args=xargs)152r.check_response(count=1, http_status=200,153protocol='HTTP/2' if proto == 'h2' else 'HTTP/1.1')154assert self.get_tunnel_proto_used(r) == 'HTTP/2' \155if tunnel == 'h2' else 'HTTP/1.1'156157@pytest.mark.skipif(condition=not Env.curl_has_feature('SPNEGO'),158reason='curl lacks SPNEGO support')159def test_13_09_negotiate_http(self, env: Env, httpd, configures_httpd):160self.httpd_configure(env, httpd)161run_env = os.environ.copy()162run_env['https_proxy'] = f'http://127.0.0.1:{env.proxy_port}'163curl = CurlClient(env=env, run_env=run_env)164url = f'https://localhost:{env.https_port}/data.json'165r1 = curl.http_download(urls=[url], alpn_proto='http/1.1', with_stats=True, extra_args=[166'--negotiate', '--proxy-user', 'proxy:proxy'167])168r1.check_response(count=1, http_status=200)169170171