Skip to content

Commit

Permalink
Integration test for HTTPS proxy handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
Albertas Agejevas authored and Albertas Agejevas committed Jan 23, 2024
1 parent 3a5ff1c commit 11d3866
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions tests/integration/test_proxy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Test using a proxy."""

import asyncio
import http.server
import socketserver
import threading
Expand Down Expand Up @@ -36,6 +37,35 @@ def do_GET(self):
self.end_headers()
self.copyfile(upstream_response, self.wfile)

def do_CONNECT(self):
host, port = self.path.split(":")

asyncio.run(self._tunnel(host, port, self.connection))

async def _tunnel(self, host, port, client_sock):
target_r, target_w = await asyncio.open_connection(host=host, port=port)

self.send_response(http.HTTPStatus.OK)
self.end_headers()

source_r, source_w = await asyncio.open_connection(sock=client_sock)

async def channel(reader, writer):
while True:
data = await reader.read(1024)
if not data:
break
writer.write(data)
await writer.drain()

writer.close()
await writer.wait_closed()

await asyncio.gather(
channel(target_r, source_w),
channel(source_r, target_w),
)


@pytest.fixture(scope="session")
def proxy_server():
Expand All @@ -59,3 +89,23 @@ def test_use_proxy(tmpdir, httpbin, proxy_server):
assert cassette_response.headers[key] == response.headers[key]
assert cassette_response.headers == response.headers
assert cassette.play_count == 1


def test_use_https_proxy(tmpdir, httpbin_secure, proxy_server):
"""Ensure that it works with an HTTPS proxy."""
with vcr.use_cassette(str(tmpdir.join("proxy.yaml"))):
response = requests.get(httpbin_secure.url, proxies={"https": proxy_server})

with vcr.use_cassette(str(tmpdir.join("proxy.yaml")), mode="once") as cassette:
cassette_response = requests.get(
httpbin_secure.url,
proxies={"https": proxy_server},
)

for key in set(cassette_response.headers.keys()) & set(response.headers.keys()):
assert cassette_response.headers[key] == response.headers[key]
assert cassette_response.headers == response.headers
assert cassette.play_count == 1

# The cassette url points to httpbin, not the proxy
assert cassette.requests[0].url == httpbin_secure.url + "/"

0 comments on commit 11d3866

Please sign in to comment.