Skip to content

Commit 486d33c

Browse files
Add unit tests for x-dashscope-sdk-client header format
1 parent 83db477 commit 486d33c

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

‎tests/unit/test_sdk_headers.py‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright (c) Alibaba, Inc. and its affiliates.
3+
4+
import pytest
5+
6+
from dashscope import __version__ as sdk_version
7+
from dashscope.acli import SDK_SESSION_ID as ACLI_SESSION_ID
8+
from dashscope.acli import __version__ as acli_version
9+
from dashscope.acli.providers.tongyi import TongyiProvider
10+
from dashscope.common.utils import _SDK_SESSION_ID, get_sdk_headers
11+
12+
CLIENT_HEADER = "x-dashscope-sdk-client"
13+
SESSION_HEADER = "x-dashscope-sdk-session-id"
14+
DISABLE_ENV = "DASHSCOPE_DISABLE_SDK_HEADERS"
15+
16+
# module 取值在代码库中的实际使用点:
17+
# - "" (无 module): default_headers / base_request / api_request_factory
18+
# - "agentstudio": agentstudio/transport.py
19+
# - acli 侧由 TongyiProvider(module=...) 构造参数传入(如 "app")
20+
SDK_MODULES = ["", "agentstudio"]
21+
ACLI_MODULES = ["", "app"]
22+
23+
24+
def _check_client_header(value, client, version, module=""):
25+
parts = value.split("/")
26+
assert parts[0] == client
27+
assert parts[1] == version
28+
if module:
29+
assert len(parts) == 3
30+
assert parts[2] == module
31+
else:
32+
assert len(parts) == 2
33+
34+
35+
def test_python_sdk_client_header():
36+
print("\n=== python-sdk: x-dashscope-sdk-client ===")
37+
for module in SDK_MODULES:
38+
headers = get_sdk_headers(module=module)
39+
value = headers[CLIENT_HEADER]
40+
print(f"module={module or '(none)':<15} -> {value}")
41+
_check_client_header(value, "python-sdk", sdk_version, module)
42+
assert headers[SESSION_HEADER] == _SDK_SESSION_ID
43+
44+
45+
def test_acli_client_header():
46+
# pylint: disable=protected-access
47+
print("\n=== acli: x-dashscope-sdk-client ===")
48+
for module in ACLI_MODULES:
49+
provider = TongyiProvider(
50+
model="qwen-plus",
51+
api_key="sk-x",
52+
module=module,
53+
)
54+
headers = provider._get_headers()
55+
value = headers[CLIENT_HEADER]
56+
print(f"module={module or '(none)':<15} -> {value}")
57+
_check_client_header(value, "acli", acli_version, module)
58+
assert headers[SESSION_HEADER] == ACLI_SESSION_ID
59+
60+
61+
def test_sdk_headers_disabled(monkeypatch):
62+
# pylint: disable=protected-access
63+
monkeypatch.setenv(DISABLE_ENV, "1")
64+
assert not get_sdk_headers()
65+
assert not get_sdk_headers(module="agentstudio")
66+
provider = TongyiProvider(model="qwen-plus", api_key="sk-x", module="app")
67+
assert CLIENT_HEADER not in provider._get_headers()
68+
assert SESSION_HEADER not in provider._get_headers()
69+
70+
71+
if __name__ == "__main__":
72+
pytest.main([__file__, "-v", "-s"])

0 commit comments

Comments
 (0)