test_api_tool.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from core.tools.__base.tool_runtime import ToolRuntime
  2. from core.tools.custom_tool.tool import ApiTool
  3. from core.tools.entities.common_entities import I18nObject
  4. from core.tools.entities.tool_bundle import ApiToolBundle
  5. from core.tools.entities.tool_entities import ToolEntity, ToolIdentity
  6. from tests.integration_tests.tools.__mock.http import setup_http_mock
  7. tool_bundle = {
  8. "server_url": "http://www.example.com/{path_param}",
  9. "method": "post",
  10. "author": "",
  11. "openapi": {
  12. "parameters": [
  13. {"in": "path", "name": "path_param"},
  14. {"in": "query", "name": "query_param"},
  15. {"in": "cookie", "name": "cookie_param"},
  16. {"in": "header", "name": "header_param"},
  17. ],
  18. "requestBody": {
  19. "content": {"application/json": {"schema": {"properties": {"body_param": {"type": "string"}}}}}
  20. },
  21. },
  22. "parameters": [],
  23. }
  24. parameters = {
  25. "path_param": "p_param",
  26. "query_param": "q_param",
  27. "cookie_param": "c_param",
  28. "header_param": "h_param",
  29. "body_param": "b_param",
  30. }
  31. def test_api_tool(setup_http_mock):
  32. tool = ApiTool(
  33. entity=ToolEntity(
  34. identity=ToolIdentity(provider="", author="", name="", label=I18nObject()),
  35. ),
  36. api_bundle=ApiToolBundle(**tool_bundle),
  37. runtime=ToolRuntime(tenant_id="", credentials={"auth_type": "none"}),
  38. )
  39. headers = tool.assembling_request(parameters)
  40. response = tool.do_http_request(tool.api_bundle.server_url, tool.api_bundle.method, headers, parameters)
  41. assert response.status_code == 200
  42. assert "/p_param" == response.request.url.path
  43. assert b"query_param=q_param" == response.request.url.query
  44. assert "h_param" == response.request.headers.get("header_param")
  45. assert "application/json" == response.request.headers.get("content-type")
  46. assert "cookie_param=c_param" == response.request.headers.get("cookie")
  47. assert "b_param" in response.content.decode()