Merge remote-tracking branch 'coletdjnz/networking/add-http-proxy-tests' into networking/websockets-http-proxy

# Conflicts:
#	test/test_http_proxy.py
pull/9625/head
coletdjnz 2024-04-06 15:48:12 +13:00
commit eecdc5870c
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 91984263BB39894A
1 zmienionych plików z 15 dodań i 4 usunięć

Wyświetl plik

@ -7,9 +7,8 @@ import os
import random import random
import ssl import ssl
import threading import threading
import time
from http.server import BaseHTTPRequestHandler from http.server import BaseHTTPRequestHandler
from socketserver import BaseRequestHandler, ThreadingTCPServer from socketserver import ThreadingTCPServer
import pytest import pytest
@ -302,6 +301,7 @@ class TestHTTPProxy:
with ctx.http_server(HTTPProxyHandler) as server_address: with ctx.http_server(HTTPProxyHandler) as server_address:
with handler(proxies={ctx.REQUEST_PROTO: f'http://{server_address}'}) as rh: with handler(proxies={ctx.REQUEST_PROTO: f'http://{server_address}'}) as rh:
proxy_info = ctx.proxy_info_request(rh) proxy_info = ctx.proxy_info_request(rh)
assert proxy_info['proxy'] == server_address
assert proxy_info['connect'] is False assert proxy_info['connect'] is False
assert 'Proxy-Authorization' not in proxy_info['headers'] assert 'Proxy-Authorization' not in proxy_info['headers']
assert proxy_info['proxy'] == server_address assert proxy_info['proxy'] == server_address
@ -310,6 +310,7 @@ class TestHTTPProxy:
with ctx.http_server(HTTPProxyHandler, username='test', password='test') as server_address: with ctx.http_server(HTTPProxyHandler, username='test', password='test') as server_address:
with handler(proxies={ctx.REQUEST_PROTO: f'http://test:test@{server_address}'}) as rh: with handler(proxies={ctx.REQUEST_PROTO: f'http://test:test@{server_address}'}) as rh:
proxy_info = ctx.proxy_info_request(rh) proxy_info = ctx.proxy_info_request(rh)
assert proxy_info['proxy'] == server_address
assert 'Proxy-Authorization' in proxy_info['headers'] assert 'Proxy-Authorization' in proxy_info['headers']
def test_http_bad_auth(self, handler, ctx): def test_http_bad_auth(self, handler, ctx):
@ -327,14 +328,15 @@ class TestHTTPProxy:
with handler(proxies={ctx.REQUEST_PROTO: f'http://{server_address}'}, with handler(proxies={ctx.REQUEST_PROTO: f'http://{server_address}'},
source_address=source_address) as rh: source_address=source_address) as rh:
proxy_info = ctx.proxy_info_request(rh) proxy_info = ctx.proxy_info_request(rh)
assert proxy_info['client_address'][0] == source_address
assert proxy_info['proxy'] == server_address assert proxy_info['proxy'] == server_address
assert proxy_info['client_address'][0] == source_address
@pytest.mark.skip_handler('Urllib', 'urllib does not support https proxies') @pytest.mark.skip_handler('Urllib', 'urllib does not support https proxies')
def test_https(self, handler, ctx): def test_https(self, handler, ctx):
with ctx.http_server(HTTPSProxyHandler) as server_address: with ctx.http_server(HTTPSProxyHandler) as server_address:
with handler(verify=False, proxies={ctx.REQUEST_PROTO: f'https://{server_address}'}) as rh: with handler(verify=False, proxies={ctx.REQUEST_PROTO: f'https://{server_address}'}) as rh:
proxy_info = ctx.proxy_info_request(rh) proxy_info = ctx.proxy_info_request(rh)
assert proxy_info['proxy'] == server_address
assert proxy_info['connect'] is False assert proxy_info['connect'] is False
assert 'Proxy-Authorization' not in proxy_info['headers'] assert 'Proxy-Authorization' not in proxy_info['headers']
assert proxy_info['proxy'] == server_address assert proxy_info['proxy'] == server_address
@ -353,6 +355,7 @@ class TestHTTPProxy:
with ctx.http_server(HTTPProxyHandler) as server_address: with ctx.http_server(HTTPProxyHandler) as server_address:
with handler(proxies={ctx.REQUEST_PROTO: f'http://{server_address}'}) as rh: with handler(proxies={ctx.REQUEST_PROTO: f'http://{server_address}'}) as rh:
proxy_info = ctx.proxy_info_request(rh, target_domain='中文.tw') proxy_info = ctx.proxy_info_request(rh, target_domain='中文.tw')
assert proxy_info['proxy'] == server_address
assert proxy_info['path'].startswith('http://xn--fiq228c.tw') assert proxy_info['path'].startswith('http://xn--fiq228c.tw')
assert proxy_info['headers']['Host'].split(':', 1)[0] == 'xn--fiq228c.tw' assert proxy_info['headers']['Host'].split(':', 1)[0] == 'xn--fiq228c.tw'
assert proxy_info['proxy'] == server_address assert proxy_info['proxy'] == server_address
@ -370,6 +373,7 @@ class TestHTTPConnectProxy:
with ctx.http_server(HTTPConnectProxyHandler) as server_address: with ctx.http_server(HTTPConnectProxyHandler) as server_address:
with handler(verify=False, proxies={ctx.REQUEST_PROTO: f'http://{server_address}'}) as rh: with handler(verify=False, proxies={ctx.REQUEST_PROTO: f'http://{server_address}'}) as rh:
proxy_info = ctx.proxy_info_request(rh) proxy_info = ctx.proxy_info_request(rh)
assert proxy_info['proxy'] == server_address
assert proxy_info['connect'] is True assert proxy_info['connect'] is True
assert 'Proxy-Authorization' not in proxy_info['headers'] assert 'Proxy-Authorization' not in proxy_info['headers']
assert proxy_info['proxy'] == server_address assert proxy_info['proxy'] == server_address
@ -378,9 +382,14 @@ class TestHTTPConnectProxy:
with ctx.http_server(HTTPConnectProxyHandler, username='test', password='test') as server_address: with ctx.http_server(HTTPConnectProxyHandler, username='test', password='test') as server_address:
with handler(verify=False, proxies={ctx.REQUEST_PROTO: f'http://test:test@{server_address}'}) as rh: with handler(verify=False, proxies={ctx.REQUEST_PROTO: f'http://test:test@{server_address}'}) as rh:
proxy_info = ctx.proxy_info_request(rh) proxy_info = ctx.proxy_info_request(rh)
assert proxy_info['proxy'] == server_address
assert 'Proxy-Authorization' in proxy_info['headers'] assert 'Proxy-Authorization' in proxy_info['headers']
assert proxy_info['proxy'] == server_address assert proxy_info['proxy'] == server_address
@pytest.mark.skip_handler(
'Requests',
'bug in urllib3 causes unclosed socket: https://github.com/urllib3/urllib3/issues/3374'
)
def test_http_connect_bad_auth(self, handler, ctx): def test_http_connect_bad_auth(self, handler, ctx):
with ctx.http_server(HTTPConnectProxyHandler, username='test', password='test') as server_address: with ctx.http_server(HTTPConnectProxyHandler, username='test', password='test') as server_address:
with handler(verify=False, proxies={ctx.REQUEST_PROTO: f'http://test:bad@{server_address}'}) as rh: with handler(verify=False, proxies={ctx.REQUEST_PROTO: f'http://test:bad@{server_address}'}) as rh:
@ -395,14 +404,15 @@ class TestHTTPConnectProxy:
source_address=source_address, source_address=source_address,
verify=False) as rh: verify=False) as rh:
proxy_info = ctx.proxy_info_request(rh) proxy_info = ctx.proxy_info_request(rh)
assert proxy_info['client_address'][0] == source_address
assert proxy_info['proxy'] == server_address assert proxy_info['proxy'] == server_address
assert proxy_info['client_address'][0] == source_address
@pytest.mark.skipif(urllib3 is None, reason='requires urllib3 to test') @pytest.mark.skipif(urllib3 is None, reason='requires urllib3 to test')
def test_https_connect_proxy(self, handler, ctx): def test_https_connect_proxy(self, handler, ctx):
with ctx.http_server(HTTPSConnectProxyHandler) as server_address: with ctx.http_server(HTTPSConnectProxyHandler) as server_address:
with handler(verify=False, proxies={ctx.REQUEST_PROTO: f'https://{server_address}'}) as rh: with handler(verify=False, proxies={ctx.REQUEST_PROTO: f'https://{server_address}'}) as rh:
proxy_info = ctx.proxy_info_request(rh) proxy_info = ctx.proxy_info_request(rh)
assert proxy_info['proxy'] == server_address
assert proxy_info['connect'] is True assert proxy_info['connect'] is True
assert 'Proxy-Authorization' not in proxy_info['headers'] assert 'Proxy-Authorization' not in proxy_info['headers']
assert proxy_info['proxy'] == server_address assert proxy_info['proxy'] == server_address
@ -422,5 +432,6 @@ class TestHTTPConnectProxy:
with ctx.http_server(HTTPSConnectProxyHandler, username='test', password='test') as server_address: with ctx.http_server(HTTPSConnectProxyHandler, username='test', password='test') as server_address:
with handler(verify=False, proxies={ctx.REQUEST_PROTO: f'https://test:test@{server_address}'}) as rh: with handler(verify=False, proxies={ctx.REQUEST_PROTO: f'https://test:test@{server_address}'}) as rh:
proxy_info = ctx.proxy_info_request(rh) proxy_info = ctx.proxy_info_request(rh)
assert proxy_info['proxy'] == server_address
assert 'Proxy-Authorization' in proxy_info['headers'] assert 'Proxy-Authorization' in proxy_info['headers']
assert proxy_info['proxy'] == server_address assert proxy_info['proxy'] == server_address