|
13 | 13 | SESSION_HEADER = "x-dashscope-sdk-session-id" |
14 | 14 | DISABLE_ENV = "DASHSCOPE_DISABLE_SDK_HEADERS" |
15 | 15 |
|
| 16 | +# client 取值来源: |
| 17 | +# - "python-sdk": SDK 默认(common/utils.py 的 _SDK_CLIENT) |
| 18 | +# - "python-cli": dashscope/cli/__init__.py 在 CLI 进程启动时调用 |
| 19 | +# set_sdk_client("python-cli") 覆盖 |
| 20 | +# - "acli": acli/providers/tongyi.py 自建 header |
16 | 21 | # module 取值在代码库中的实际使用点: |
17 | 22 | # - "" (无 module): default_headers / base_request / api_request_factory |
18 | 23 | # - "agentstudio": agentstudio/transport.py |
|
21 | 26 | ACLI_MODULES = ["", "app"] |
22 | 27 |
|
23 | 28 |
|
| 29 | +@pytest.fixture(autouse=True) |
| 30 | +def _pin_sdk_client(monkeypatch): |
| 31 | + # CLI 测试会在同进程内把 client 改成 python-cli,这里钉住默认值 |
| 32 | + monkeypatch.setattr("dashscope.common.utils._SDK_CLIENT", "python-sdk") |
| 33 | + |
| 34 | + |
24 | 35 | def _check_client_header(value, client, version, module=""): |
25 | 36 | parts = value.split("/") |
26 | 37 | assert parts[0] == client |
@@ -68,5 +79,46 @@ def test_sdk_headers_disabled(monkeypatch): |
68 | 79 | assert SESSION_HEADER not in provider._get_headers() |
69 | 80 |
|
70 | 81 |
|
| 82 | +def test_python_cli_client_header(): |
| 83 | + from dashscope.common.utils import set_sdk_client |
| 84 | + |
| 85 | + set_sdk_client("python-cli") |
| 86 | + try: |
| 87 | + headers = get_sdk_headers() |
| 88 | + value = headers[CLIENT_HEADER] |
| 89 | + print(f"\npython-cli -> {value}") |
| 90 | + _check_client_header(value, "python-cli", sdk_version) |
| 91 | + # module 段同样适用 |
| 92 | + value = get_sdk_headers(module="agentstudio")[CLIENT_HEADER] |
| 93 | + print(f"python-cli -> {value}") |
| 94 | + _check_client_header(value, "python-cli", sdk_version, "agentstudio") |
| 95 | + finally: |
| 96 | + set_sdk_client("python-sdk") |
| 97 | + |
| 98 | + |
| 99 | +def test_cli_import_marks_process(): |
| 100 | + # 子进程隔离验证:加载 dashscope.cli 后,进程内请求的 |
| 101 | + # client 标识应变为 python-cli(与同进程其他测试的执行顺序无关) |
| 102 | + import subprocess |
| 103 | + import sys |
| 104 | + |
| 105 | + code = ( |
| 106 | + "import dashscope.cli; " |
| 107 | + "from dashscope.common.utils import get_sdk_headers; " |
| 108 | + "h = get_sdk_headers(); " |
| 109 | + "c = h['x-dashscope-sdk-client']; " |
| 110 | + "assert c.startswith('python-cli/'), c; " |
| 111 | + "print(c)" |
| 112 | + ) |
| 113 | + result = subprocess.run( |
| 114 | + [sys.executable, "-c", code], |
| 115 | + capture_output=True, |
| 116 | + text=True, |
| 117 | + check=False, |
| 118 | + ) |
| 119 | + assert result.returncode == 0, result.stderr |
| 120 | + print(f"\nCLI process -> {result.stdout.strip()}") |
| 121 | + |
| 122 | + |
71 | 123 | if __name__ == "__main__": |
72 | 124 | pytest.main([__file__, "-v", "-s"]) |
0 commit comments