summaryrefslogtreecommitdiff
path: root/plugins/security-guidance/tests/test_base_url.py
diff options
context:
space:
mode:
authorClaude <noreply@anthropic.com>2026-11-06 00:54:48 +0000
committerClaude <noreply@anthropic.com>2026-11-06 00:54:48 +0000
commit7aba3deae3ff17d032c37e07d2929d1426b34278 (patch)
tree9c482f9acda1599bd2f9bed95a6a46b75e634e80 /plugins/security-guidance/tests/test_base_url.py
parent5472f5f56e0070feeeb464318b316714fe3e9105 (diff)
parentb8331dd69ab3e132bbf8cd6e89b5d8c1747e21b1 (diff)
Merge remote-tracking branch 'origin/main' into add-plugin/xcodeadd-plugin/xcode
Diffstat (limited to 'plugins/security-guidance/tests/test_base_url.py')
-rw-r--r--plugins/security-guidance/tests/test_base_url.py87
1 files changed, 87 insertions, 0 deletions
diff --git a/plugins/security-guidance/tests/test_base_url.py b/plugins/security-guidance/tests/test_base_url.py
new file mode 100644
index 00000000..26317549
--- /dev/null
+++ b/plugins/security-guidance/tests/test_base_url.py
@@ -0,0 +1,87 @@
+"""A blank ANTHROPIC_BASE_URL means the default endpoint, as it does for the
+Anthropic SDK and Claude Code, for every call the plugin makes."""
+import io
+import json
+
+import pytest
+
+import llm
+import security_reminder_hook as hook
+
+DEFAULT = "https://api.anthropic.com"
+BLANK = ["", " ", "\t\n"]
+
+
+@pytest.mark.parametrize("value", BLANK)
+def test_blank_is_the_default_endpoint(monkeypatch, value):
+ monkeypatch.setenv("ANTHROPIC_BASE_URL", value)
+ assert llm._anthropic_base_url() == DEFAULT
+
+
+@pytest.mark.parametrize("value, expected", [
+ ("https://gw.example.com/", "https://gw.example.com"),
+ (" https://gw.example.com/base/ \n", "https://gw.example.com/base"),
+])
+def test_a_real_value_is_kept(monkeypatch, value, expected):
+ monkeypatch.setenv("ANTHROPIC_BASE_URL", value)
+ assert llm._anthropic_base_url() == expected
+
+
+def test_a_stray_slash_is_not_taken_for_blank(monkeypatch):
+ # A broken gateway setting must not quietly send code to the default host.
+ monkeypatch.setenv("ANTHROPIC_BASE_URL", "/")
+ assert llm._anthropic_base_url() == ""
+
+
+def test_unset_is_the_default_endpoint(monkeypatch):
+ monkeypatch.delenv("ANTHROPIC_BASE_URL", raising=False)
+ assert llm._anthropic_base_url() == DEFAULT
+
+
+class _Reply(io.BytesIO):
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *exc):
+ return False
+
+
+@pytest.mark.parametrize("value", BLANK)
+def test_requests_go_to_the_default_endpoint(monkeypatch, value):
+ urls = []
+
+ def urlopen(request, timeout):
+ urls.append(request.full_url)
+ return _Reply(json.dumps({
+ "data": [{"id": "claude-opus-4-8"}],
+ "content": [{"type": "text", "text": "{}"}],
+ "usage": {},
+ }).encode())
+
+ for var in llm._PROVIDER_ENV_VARS:
+ monkeypatch.delenv(var, raising=False)
+ monkeypatch.setenv("ANTHROPIC_BASE_URL", value)
+ monkeypatch.setattr(llm, "ANTHROPIC_API_KEY", "key")
+ monkeypatch.setattr(llm, "HAS_API_CREDENTIALS", True)
+ monkeypatch.setattr(llm, "_urlopen", urlopen)
+ assert llm._probe_anthropic()
+ llm._fetch_newest_opus()
+ llm._call_claude("prompt", {"type": "object"}, thinking_budget=0, model="claude-x")
+ assert urls == [f"{DEFAULT}/", f"{DEFAULT}/v1/models?limit=100", f"{DEFAULT}/v1/messages"]
+
+
+@pytest.mark.parametrize("value", BLANK)
+def test_blank_is_not_reported_as_a_gateway(monkeypatch, value):
+ for var in llm._PROVIDER_ENV_VARS:
+ monkeypatch.delenv(var, raising=False)
+ monkeypatch.delenv("ANTHROPIC_CUSTOM_HEADERS", raising=False)
+ monkeypatch.setenv("ANTHROPIC_BASE_URL", value)
+ assert llm.gateway_cfg() == 0
+
+
+@pytest.mark.parametrize("value", BLANK)
+def test_blank_gets_no_gateway_hint(monkeypatch, value):
+ for var in llm._PROVIDER_ENV_VARS:
+ monkeypatch.delenv(var, raising=False)
+ monkeypatch.setenv("ANTHROPIC_BASE_URL", value)
+ assert "ANTHROPIC_CUSTOM_HEADERS" not in hook._review_failure_reason(401)[2]