62 lines
2.2 KiB
Python
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
|