Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/kimi_cli/auth/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def get_device_id() -> str:
def _ascii_header_value(value: str, *, fallback: str = "unknown") -> str:
try:
value.encode("ascii")
return value
return value.strip()
except UnicodeEncodeError:
sanitized = value.encode("ascii", errors="ignore").decode("ascii").strip()
return sanitized or fallback
Expand Down
22 changes: 22 additions & 0 deletions tests/utils/test_oauth_headers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from kimi_cli.auth.oauth import _ascii_header_value


def test_ascii_header_value_strips_trailing_space():
assert _ascii_header_value("test ") == "test"
assert _ascii_header_value(" test") == "test"
assert _ascii_header_value(" test ") == "test"


def test_ascii_header_value_non_ascii_strips_trailing_space():
# Non-ASCII character (Chinese 'test') followed by space
# The non-ASCII characters are ignored by encode('ascii', errors='ignore')
# So "测试 " becomes " " and then .strip() makes it empty, returning fallback
assert _ascii_header_value("测试 ") == "unknown"
# "A测试 " -> "A " -> "A"
assert _ascii_header_value("A测试 ") == "A"


def test_ascii_header_value_fallback():
# Only non-ASCII characters that get ignored, resulting in empty string
assert _ascii_header_value("🚀") == "unknown"
assert _ascii_header_value("🚀", fallback="default") == "default"