Skip to content

Commit 291841b

Browse files
liuxiaopai-aiPangjiping
authored andcommitted
test(server): add endpoint route behavior coverage
1 parent d748a52 commit 291841b

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Copyright 2025 Alibaba Group Holding Ltd.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from fastapi.testclient import TestClient
16+
17+
from src.api import lifecycle
18+
from src.api.schema import Endpoint
19+
20+
21+
def test_get_endpoint_returns_service_result(
22+
client: TestClient,
23+
auth_headers: dict,
24+
monkeypatch,
25+
) -> None:
26+
calls: list[tuple[str, int]] = []
27+
28+
class StubService:
29+
@staticmethod
30+
def get_endpoint(sandbox_id: str, port: int) -> Endpoint:
31+
calls.append((sandbox_id, port))
32+
return Endpoint(endpoint="10.57.1.91:40109/proxy/44772")
33+
34+
monkeypatch.setattr(lifecycle, "sandbox_service", StubService())
35+
36+
response = client.get(
37+
"/v1/sandboxes/sbx-001/endpoints/44772",
38+
headers=auth_headers,
39+
)
40+
41+
assert response.status_code == 200
42+
assert response.json()["endpoint"] == "10.57.1.91:40109/proxy/44772"
43+
assert calls == [("sbx-001", 44772)]
44+
45+
46+
def test_get_endpoint_use_server_proxy_rewrites_url(
47+
client: TestClient,
48+
auth_headers: dict,
49+
monkeypatch,
50+
) -> None:
51+
class StubService:
52+
@staticmethod
53+
def get_endpoint(sandbox_id: str, port: int) -> Endpoint:
54+
return Endpoint(endpoint="10.57.1.91:40109/proxy/44772")
55+
56+
monkeypatch.setattr(lifecycle, "sandbox_service", StubService())
57+
58+
response = client.get(
59+
"/v1/sandboxes/sbx-001/endpoints/44772",
60+
params={"use_server_proxy": "true"},
61+
headers=auth_headers,
62+
)
63+
64+
assert response.status_code == 200
65+
assert response.json()["endpoint"] == "testserver/sandboxes/sbx-001/proxy/44772"
66+
67+
68+
def test_get_endpoint_rejects_non_numeric_port(
69+
client: TestClient,
70+
auth_headers: dict,
71+
) -> None:
72+
response = client.get(
73+
"/v1/sandboxes/sbx-001/endpoints/not-a-port",
74+
headers=auth_headers,
75+
)
76+
77+
assert response.status_code == 422

0 commit comments

Comments
 (0)