|
20 | 20 | from unittest.mock import AsyncMock, MagicMock, Mock
|
21 | 21 |
|
22 | 22 | # Third-Party
|
| 23 | +import httpx |
23 | 24 | import pytest
|
24 | 25 |
|
25 | 26 | # First-Party
|
@@ -144,7 +145,6 @@ class TestGatewayService:
|
144 | 145 | # ────────────────────────────────────────────────────────────────────
|
145 | 146 | # REGISTER
|
146 | 147 | # ────────────────────────────────────────────────────────────────────
|
147 |
| - |
148 | 148 | @pytest.mark.asyncio
|
149 | 149 | async def test_register_gateway(self, gateway_service, test_db):
|
150 | 150 | """Successful gateway registration populates DB and returns data."""
|
@@ -231,6 +231,146 @@ async def test_register_gateway_connection_error(self, gateway_service, test_db)
|
231 | 231 |
|
232 | 232 | assert "Failed to connect" in str(exc_info.value)
|
233 | 233 |
|
| 234 | + # ──────────────────────────────────────────────────────────────────── |
| 235 | + # Validate Gateway URL Timeout |
| 236 | + # ──────────────────────────────────────────────────────────────────── |
| 237 | + @pytest.mark.asyncio |
| 238 | + async def test_gateway_validate_timeout(self, gateway_service, monkeypatch): |
| 239 | + # creating a mock with a timeout error |
| 240 | + mock_stream = AsyncMock(side_effect=httpx.ReadTimeout("Timeout")) |
| 241 | + |
| 242 | + mock_aclose = AsyncMock() |
| 243 | + |
| 244 | + # Step 3: Mock client with .stream and .aclose |
| 245 | + mock_client_instance = MagicMock() |
| 246 | + mock_client_instance.stream = mock_stream |
| 247 | + mock_client_instance.aclose = mock_aclose |
| 248 | + |
| 249 | + mock_http_client = MagicMock() |
| 250 | + mock_http_client.client = mock_client_instance |
| 251 | + mock_http_client.aclose = mock_aclose |
| 252 | + |
| 253 | + monkeypatch.setattr("mcpgateway.services.gateway_service.ResilientHttpClient", MagicMock(return_value=mock_http_client)) |
| 254 | + |
| 255 | + result = await gateway_service._validate_gateway_url(url="http://example.com", headers={}, transport_type="SSE", timeout=2) |
| 256 | + |
| 257 | + assert result is False |
| 258 | + |
| 259 | + # ──────────────────────────────────────────────────────────────────── |
| 260 | + # Validate Gateway URL SSL Verification |
| 261 | + # ──────────────────────────────────────────────────────────────────── |
| 262 | + @pytest.mark.asyncio |
| 263 | + async def test_ssl_verification_bypass(self, gateway_service, monkeypatch): |
| 264 | + # TODO |
| 265 | + pass |
| 266 | + |
| 267 | + # ──────────────────────────────────────────────────────────────────── |
| 268 | + # Validate Gateway URL Auth Failure |
| 269 | + # ──────────────────────────────────────────────────────────────────── |
| 270 | + @pytest.mark.asyncio |
| 271 | + async def test_validate_auth_failure(self, gateway_service, monkeypatch): |
| 272 | + # Mock the response object to be returned inside the async with block |
| 273 | + response_mock = MagicMock() |
| 274 | + response_mock.status_code = 401 |
| 275 | + response_mock.headers = {"content-type": "text/event-stream"} |
| 276 | + |
| 277 | + # Create an async context manager mock that returns response_mock |
| 278 | + stream_context = MagicMock() |
| 279 | + stream_context.__aenter__ = AsyncMock(return_value=response_mock) |
| 280 | + stream_context.__aexit__ = AsyncMock(return_value=None) |
| 281 | + |
| 282 | + # Mock the AsyncClient to return this context manager from .stream() |
| 283 | + client_mock = MagicMock() |
| 284 | + client_mock.stream = AsyncMock(return_value=stream_context) |
| 285 | + client_mock.aclose = AsyncMock() |
| 286 | + |
| 287 | + # Mock ResilientHttpClient to return this client |
| 288 | + resilient_client_mock = MagicMock() |
| 289 | + resilient_client_mock.client = client_mock |
| 290 | + resilient_client_mock.aclose = AsyncMock() |
| 291 | + |
| 292 | + monkeypatch.setattr("mcpgateway.services.gateway_service.ResilientHttpClient", MagicMock(return_value=resilient_client_mock)) |
| 293 | + |
| 294 | + # Run the method |
| 295 | + result = await gateway_service._validate_gateway_url(url="http://example.com", headers={}, transport_type="SSE") |
| 296 | + |
| 297 | + # Expect False due to 401 |
| 298 | + assert result is False |
| 299 | + |
| 300 | + # ──────────────────────────────────────────────────────────────────── |
| 301 | + # Validate Gateway URL Connection Error |
| 302 | + # ──────────────────────────────────────────────────────────────────── |
| 303 | + @pytest.mark.asyncio |
| 304 | + async def test_validate_connectivity_failure(self, gateway_service, monkeypatch): |
| 305 | + # Create an async context manager mock that raises ConnectError |
| 306 | + stream_context = AsyncMock() |
| 307 | + stream_context.__aenter__.side_effect = httpx.ConnectError("connection error") |
| 308 | + stream_context.__aexit__.return_value = AsyncMock() |
| 309 | + |
| 310 | + # Mock client with .stream() and .aclose() |
| 311 | + mock_client = MagicMock() |
| 312 | + mock_client.stream.return_value = stream_context |
| 313 | + mock_client.aclose = AsyncMock() |
| 314 | + |
| 315 | + # Patch ResilientHttpClient to return this mock client |
| 316 | + resilient_client_mock = MagicMock() |
| 317 | + resilient_client_mock.client = mock_client |
| 318 | + resilient_client_mock.aclose = AsyncMock() |
| 319 | + |
| 320 | + monkeypatch.setattr("mcpgateway.services.gateway_service.ResilientHttpClient", MagicMock(return_value=resilient_client_mock)) |
| 321 | + |
| 322 | + # Call the method and assert result |
| 323 | + result = await gateway_service._validate_gateway_url(url="http://example.com", headers={}, transport_type="SSE") |
| 324 | + |
| 325 | + assert result is False |
| 326 | + |
| 327 | + # ──────────────────────────────────────────────────────────────────── |
| 328 | + # Validate Gateway URL Bulk Connections Validation |
| 329 | + # ──────────────────────────────────────────────────────────────────── |
| 330 | + @pytest.mark.asyncio |
| 331 | + async def test_bulk_concurrent_validation(self, gateway_service, monkeypatch): |
| 332 | + # TODO |
| 333 | + pass |
| 334 | + |
| 335 | + # ─────────────────────────────────────────────────────────────────────────── |
| 336 | + # Validate Gateway - StreamableHTTP with mcp-session-id & redirected-url |
| 337 | + # ─────────────────────────────────────────────────────────────────────────── |
| 338 | + @pytest.mark.asyncio |
| 339 | + async def test_streamablehttp_redirect(self, gateway_service, monkeypatch): |
| 340 | + # Mock first response (redirect) |
| 341 | + first_response = MagicMock() |
| 342 | + first_response.status_code = 200 |
| 343 | + first_response.headers = {"Location": "http://sampleredirected.com"} |
| 344 | + |
| 345 | + first_cm = AsyncMock() |
| 346 | + first_cm.__aenter__.return_value = first_response |
| 347 | + first_cm.__aexit__.return_value = None |
| 348 | + |
| 349 | + # Mock redirected response (final) |
| 350 | + redirected_response = MagicMock() |
| 351 | + redirected_response.status_code = 200 |
| 352 | + redirected_response.headers = {"Mcp-Session-Id": "sample123", "Content-Type": "application/json"} |
| 353 | + |
| 354 | + second_cm = AsyncMock() |
| 355 | + second_cm.__aenter__.return_value = redirected_response |
| 356 | + second_cm.__aexit__.return_value = None |
| 357 | + |
| 358 | + # Mock ResilientHttpClient client.stream to return redirect chain |
| 359 | + client_mock = MagicMock() |
| 360 | + client_mock.stream = AsyncMock(side_effect=[first_cm, second_cm]) |
| 361 | + client_mock.aclose = AsyncMock() |
| 362 | + |
| 363 | + resilient_http_mock = MagicMock() |
| 364 | + resilient_http_mock.client = client_mock |
| 365 | + resilient_http_mock.aclose = AsyncMock() |
| 366 | + |
| 367 | + monkeypatch.setattr("mcpgateway.services.gateway_service.ResilientHttpClient", MagicMock(return_value=resilient_http_mock)) |
| 368 | + |
| 369 | + result = await gateway_service._validate_gateway_url(url="http://example.com", headers={}, transport_type="STREAMABLEHTTP") |
| 370 | + # TODO |
| 371 | + # assert result is True |
| 372 | + pass |
| 373 | + |
234 | 374 | # ────────────────────────────────────────────────────────────────────
|
235 | 375 | # LIST / GET
|
236 | 376 | # ────────────────────────────────────────────────────────────────────
|
|
0 commit comments