ForcePilot/backend/test/unit/channel/common/test_fake_request.py
Kris bab30f2715
Some checks failed
Deploy VitePress site to Pages / build (push) Has been cancelled
Ruff Format Check / Ruff Format & Lint (push) Has been cancelled
Deploy VitePress site to Pages / Deploy (push) Has been cancelled
feat:0715
2026-07-15 12:30:58 +08:00

62 lines
2.2 KiB
Python

from __future__ import annotations
import json
from yuxi.channel.common.fake_request import FakeRequest
class TestFakeRequest:
async def test_json_returns_body(self):
body = {"event": "message"}
request = FakeRequest(config={}, body=body)
assert await request.json() == body
async def test_body_returns_serialized_bytes(self):
body = {"event": "message"}
request = FakeRequest(config={}, body=body)
assert await request.body() == json.dumps(body).encode("utf-8")
def test_state_carries_config_and_raw_body(self):
config = {"account_id": "acc-1"}
body = {"event": "message"}
request = FakeRequest(config=config, body=body)
assert request.state.channel_config is config
assert request.state.channel_raw_body is body
async def test_state_uses_explicit_channel_raw_body(self):
config = {"account_id": "acc-1"}
body = {"normalized": True}
raw = {"original": True}
request = FakeRequest(config=config, body=body, channel_raw_body=raw)
assert request.state.channel_raw_body is raw
assert await request.json() == body
def test_default_params_are_empty_dicts(self):
request = FakeRequest(config={}, body={})
assert request.headers == {}
assert request.query_params == {}
assert request.path_params == {}
def test_custom_headers_query_and_path_params(self):
request = FakeRequest(
config={},
body={},
headers={"X-Token": "token"},
query_params={"foo": "bar"},
path_params={"id": "123"},
)
assert request.headers == {"X-Token": "token"}
assert request.query_params == {"foo": "bar"}
assert request.path_params == {"id": "123"}
async def test_json_and_body_independence(self):
body = {"key": "value"}
request = FakeRequest(config={}, body=body)
assert await request.json() == body
assert await request.body() == json.dumps(body).encode("utf-8")
async def test_body_with_non_ascii_content(self):
body = {"text": "中文"}
request = FakeRequest(config={}, body=body)
assert json.loads(await request.body()) == body