MCPcopy Create free account
hub / github.com/CJackHwang/AIstudioProxyAPI / test_handle_connect_intercept

Function test_handle_connect_intercept

tests/api_utils/test_stream.py:75–112  ·  view source on GitHub ↗
(proxy_server)

Source from the content-addressed store, hash-verified

73
74@pytest.mark.asyncio
75async def test_handle_connect_intercept(proxy_server):
76 reader = AsyncMock()
77 writer = AsyncMock()
78 target = "api.test.com:443"
79
80 # Mock cert manager
81 proxy_server.cert_manager.get_domain_cert = MagicMock(
82 return_value=("cert_path", "key_path")
83 )
84
85 # Mock loop and start_tls
86 mock_loop = MagicMock()
87 mock_transport = MagicMock()
88 mock_loop.start_tls = AsyncMock(return_value=mock_transport)
89
90 # Mock ssl context
91 mock_ssl_ctx = MagicMock()
92
93 with (
94 patch("asyncio.get_running_loop", return_value=mock_loop),
95 patch("ssl.create_default_context", return_value=mock_ssl_ctx),
96 patch.object(
97 proxy_server.proxy_connector, "create_connection", AsyncMock()
98 ) as mock_create_conn,
99 patch.object(
100 proxy_server, "_forward_data_with_interception", AsyncMock()
101 ) as mock_forward,
102 patch("asyncio.StreamWriter", MagicMock()),
103 ):
104 mock_create_conn.return_value = (AsyncMock(), AsyncMock())
105
106 await proxy_server._handle_connect(reader, writer, target)
107
108 proxy_server.cert_manager.get_domain_cert.assert_called_with("api.test.com")
109 mock_ssl_ctx.load_cert_chain.assert_called()
110 writer.write.assert_called()
111 mock_create_conn.assert_called()
112 mock_forward.assert_called()
113
114
115@pytest.mark.asyncio

Callers

nothing calls this directly

Calls 1

_handle_connectMethod · 0.80

Tested by

no test coverage detected