|
|
@@ -1,11 +1,12 @@
|
|
|
from __future__ import annotations
|
|
|
|
|
|
+import httpx
|
|
|
import pytest
|
|
|
|
|
|
from unittest.mock import MagicMock
|
|
|
|
|
|
from mcp.client.session import ClientSession
|
|
|
-from conftest import call_tool
|
|
|
+from conftest import call_tool, call_tool_text
|
|
|
from src.services.client_rest import HTTPMethod
|
|
|
|
|
|
@pytest.mark.anyio
|
|
|
@@ -15,7 +16,7 @@ class TestToolsMcpIntegration:
|
|
|
tools_name = {tool.name for tool in result.tools}
|
|
|
assert tools_name == {"createResource", "updateResource", "deleteResource",
|
|
|
"createTemplate", "updateTemplate", "createTv",
|
|
|
- "updateTv", "createCategory", "updateCategory"}
|
|
|
+ "updateTv", "createCategory", "updateCategory", "getPage"}
|
|
|
|
|
|
async def test_create_resource(self, mcp_session: ClientSession, client_rest: MagicMock) -> None:
|
|
|
client_rest.execute.return_value = {
|
|
|
@@ -199,5 +200,60 @@ class TestToolsMcpIntegration:
|
|
|
"id": 1,
|
|
|
},
|
|
|
}
|
|
|
+
|
|
|
+ async def test_get_page_returns_html_slice(self, mcp_session: ClientSession, client_rest: MagicMock) -> None:
|
|
|
+ html = "0123456789" * 20
|
|
|
+ client_rest.execute.return_value = html
|
|
|
+
|
|
|
+ result = await call_tool_text(mcp_session, "getPage", {"url": "accueil/test", "limit": 50})
|
|
|
+
|
|
|
+ assert result == html[:50]
|
|
|
+ client_rest.execute.assert_called_once_with(
|
|
|
+ method=HTTPMethod.GET,
|
|
|
+ endpoint="/accueil/test",
|
|
|
+ )
|
|
|
|
|
|
-
|
|
|
+ async def test_get_page_with_keyword(self, mcp_session: ClientSession, client_rest: MagicMock) -> None:
|
|
|
+ html = "aaaKEYWORDbbb"
|
|
|
+ client_rest.execute.return_value = html
|
|
|
+
|
|
|
+ result = await call_tool_text(
|
|
|
+ mcp_session,
|
|
|
+ "getPage",
|
|
|
+ {"url": "accueil/test", "keyword": "KEYWORD", "offsetTop": 2, "limit": 5},
|
|
|
+ )
|
|
|
+
|
|
|
+ assert result == html[1:15]
|
|
|
+ client_rest.execute.assert_called_once_with(
|
|
|
+ method=HTTPMethod.GET,
|
|
|
+ endpoint="/accueil/test",
|
|
|
+ )
|
|
|
+
|
|
|
+ async def test_get_page_retries_on_301(self, mcp_session: ClientSession, client_rest: MagicMock) -> None:
|
|
|
+ request = httpx.Request("GET", "https://example.com/accueil/test")
|
|
|
+ response = httpx.Response(301, request=request)
|
|
|
+ redirect_error = httpx.HTTPStatusError("Moved", request=request, response=response)
|
|
|
+ html = "<html><body>redirected</body></html>"
|
|
|
+ client_rest.execute.side_effect = [redirect_error, html]
|
|
|
+
|
|
|
+ result = await call_tool_text(mcp_session, "getPage", {"url": "accueil/test"})
|
|
|
+
|
|
|
+ assert result == html[:100]
|
|
|
+ assert client_rest.execute.call_count == 2
|
|
|
+ client_rest.execute.assert_any_call(method=HTTPMethod.GET, endpoint="/accueil/test")
|
|
|
+ client_rest.execute.assert_any_call(method=HTTPMethod.GET, endpoint="/accueil/test/")
|
|
|
+
|
|
|
+ async def test_get_page_propagates_http_error(self, mcp_session: ClientSession, client_rest: MagicMock) -> None:
|
|
|
+ request = httpx.Request("GET", "https://example.com/missing")
|
|
|
+ response = httpx.Response(404, request=request)
|
|
|
+ not_found_error = httpx.HTTPStatusError("Not Found", request=request, response=response)
|
|
|
+ client_rest.execute.side_effect = not_found_error
|
|
|
+
|
|
|
+ result = await mcp_session.call_tool("getPage", {"url": "missing"})
|
|
|
+
|
|
|
+ assert result.isError is True
|
|
|
+ assert "Not Found" in result.content[0].text
|
|
|
+ client_rest.execute.assert_called_once_with(
|
|
|
+ method=HTTPMethod.GET,
|
|
|
+ endpoint="/missing",
|
|
|
+ )
|