Skip to content

Commit

Permalink
simplify test
Browse files Browse the repository at this point in the history
  • Loading branch information
jakekaplan committed Dec 12, 2024
1 parent c3c994f commit 885b08d
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 45 deletions.
43 changes: 3 additions & 40 deletions scripts/proxy-test/client.py
Original file line number Diff line number Diff line change
@@ -1,62 +1,25 @@
import asyncio
import os

import httpx
import websockets

from prefect.utilities.proxy import websocket_connect

PROXY_URL = "http://localhost:3128"
HTTP_SERVER_URL = "http://server:8000/http"
WS_SERVER_URL = "ws://server:8000/ws"


async def test_http_proxy():
"""HTTP request through proxy - should work"""
os.environ["HTTP_PROXY"] = PROXY_URL

print("\n\n1. Testing HTTP request through proxy (expected success):")
async with httpx.AsyncClient() as client:
response = await client.get(HTTP_SERVER_URL)
print(f"Response: {response.status_code}")


async def test_websocket_proxy():
"""WebSocket through proxy - should fail"""
os.environ["HTTP_PROXY"] = PROXY_URL

print(
"\n\n2. Testing WebSocket through proxy with plain `websockets` (expected failure):"
)
try:
async with websockets.connect(WS_SERVER_URL) as websocket:
print("Unexpected connection success!")
await websocket.send("Hello!")
response = await websocket.recv()
print(f"Received: {response}")
except Exception as e:
print("Failed as expected.")
print(f"Error details: {str(e)}")
else:
raise Exception("Expected failure, but connection succeeded")


async def test_websocket_proxy_with_compat():
"""WebSocket through proxy with proxy compatibility code - should work"""
os.environ["HTTP_PROXY"] = "http://localhost:3128"

print(
"\n\n3. Testing WebSocket through proxy with custom websocket compatibility (should work):"
)
async with websocket_connect("ws://server:8000/ws") as websocket:
await websocket.send("Hello!")
response = await websocket.recv()
print(f"Received: {response}")
print("Response: ", response)
assert response == "Server received: Hello!"


async def main():
await test_http_proxy()
await test_websocket_proxy()
print("Testing WebSocket through proxy with compatibility code")
await test_websocket_proxy_with_compat()


Expand Down
5 changes: 0 additions & 5 deletions scripts/proxy-test/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
app = FastAPI()


@app.get("/http")
async def http_endpoint():
return {"message": "Hello"}


@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
Expand Down

0 comments on commit 885b08d

Please sign in to comment.