test_http_client.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import pytest
  2. from cq2_sdk.transport.http import HttpClient
  3. @pytest.mark.asyncio
  4. async def test_sign_injected_into_params(monkeypatch):
  5. captured = {}
  6. class FakeResp:
  7. status_code = 200
  8. headers = {"content-type": "application/json"}
  9. text = '{"ok": true}'
  10. def raise_for_status(self):
  11. pass
  12. def json(self):
  13. return {"ok": True}
  14. class FakeAsyncClient:
  15. def __init__(self, **kw):
  16. pass
  17. async def __aenter__(self):
  18. return self
  19. async def __aexit__(self, *a):
  20. return False
  21. async def request(self, method, url, params=None, data=None, headers=None):
  22. captured["url"] = url
  23. captured["params"] = params
  24. captured["method"] = method
  25. return FakeResp()
  26. import httpx
  27. monkeypatch.setattr(httpx, "AsyncClient", FakeAsyncClient)
  28. cli = HttpClient("https://example.com", signing_secret="SECRET", sign_version=1)
  29. result = await cli.request("GET", "/api/foo", params={"a": "1"})
  30. assert "sign" in captured["params"]
  31. assert captured["params"]["a"] == "1"
  32. assert result["ok"] is True
  33. assert result["_status"] == 200
  34. @pytest.mark.asyncio
  35. async def test_no_sign_when_version_unknown(monkeypatch):
  36. captured = {}
  37. class FakeResp:
  38. status_code = 200
  39. headers = {"content-type": "application/json"}
  40. def raise_for_status(self):
  41. pass
  42. def json(self):
  43. return {}
  44. class FakeAsyncClient:
  45. def __init__(self, **kw):
  46. pass
  47. async def __aenter__(self):
  48. return self
  49. async def __aexit__(self, *a):
  50. return False
  51. async def request(self, method, url, params=None, **kw):
  52. captured["params"] = params
  53. return FakeResp()
  54. import httpx
  55. monkeypatch.setattr(httpx, "AsyncClient", FakeAsyncClient)
  56. cli = HttpClient("https://example.com", signing_secret="SECRET", sign_version=99)
  57. await cli.request("GET", "/api/foo", params={"a": "1"})
  58. assert "sign" not in captured["params"]
  59. @pytest.mark.asyncio
  60. async def test_request_passes_method_and_path(monkeypatch):
  61. captured = {}
  62. class FakeResp:
  63. status_code = 200
  64. headers = {"content-type": "application/json"}
  65. def raise_for_status(self):
  66. pass
  67. def json(self):
  68. return {}
  69. class FakeAsyncClient:
  70. def __init__(self, **kw):
  71. pass
  72. async def __aenter__(self):
  73. return self
  74. async def __aexit__(self, *a):
  75. return False
  76. async def request(self, method, url, params=None, **kw):
  77. captured["method"] = method
  78. captured["url"] = url
  79. return FakeResp()
  80. import httpx
  81. monkeypatch.setattr(httpx, "AsyncClient", FakeAsyncClient)
  82. cli = HttpClient("https://api.996hezi.com", signing_secret="X")
  83. await cli.request("POST", "/userApi/v1/Login/mobile", data={"phone": "13800138000"})
  84. assert captured["method"] == "POST"
  85. assert captured["url"] == "https://api.996hezi.com/userApi/v1/Login/mobile"