test_api_tool.py 2.0 KB

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