Some checks failed
CI / test (3.10) (push) Has been cancelled
CI / test (3.11) (push) Has been cancelled
CI / test (3.12) (push) Has been cancelled
CI / test (3.9) (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / type-check (push) Has been cancelled
CI / build (push) Has been cancelled
149 lines
5.4 KiB
Python
149 lines
5.4 KiB
Python
import pytest
|
|
from httpx import AsyncClient, ASGITransport
|
|
from src.core.server import MockServer
|
|
from src.models.config import ServerConfig
|
|
from src.models.endpoint import Endpoint
|
|
|
|
|
|
class TestMockServer:
|
|
@pytest.fixture
|
|
def config(self):
|
|
return ServerConfig(port=8888, host="127.0.0.1", offline=True)
|
|
|
|
@pytest.fixture
|
|
def server(self, config):
|
|
return MockServer(config)
|
|
|
|
@pytest.fixture
|
|
def sample_endpoints(self):
|
|
return [
|
|
Endpoint(
|
|
path="/api/hello",
|
|
method="GET",
|
|
response={"status_code": 200, "body": {"message": "Hello"}}
|
|
),
|
|
Endpoint(
|
|
path="/api/users/{id}",
|
|
method="GET",
|
|
response={
|
|
"status_code": 200,
|
|
"body": {
|
|
"id": "{{request.path.id}}",
|
|
"name": "User {{request.path.id}}"
|
|
}
|
|
}
|
|
),
|
|
Endpoint(
|
|
path="/api/data",
|
|
method="POST",
|
|
response={"status_code": 201, "body": {"received": "{{request.body.key}}"}}
|
|
)
|
|
]
|
|
|
|
def test_register_endpoint(self, server):
|
|
endpoint = Endpoint(
|
|
path="/api/test",
|
|
method="GET",
|
|
response={"status_code": 200}
|
|
)
|
|
server.register_endpoint(endpoint)
|
|
endpoints = server.matcher.get_endpoints()
|
|
assert len(endpoints) == 1
|
|
|
|
def test_register_endpoints(self, server, sample_endpoints):
|
|
server.register_endpoints(sample_endpoints)
|
|
endpoints = server.matcher.get_endpoints()
|
|
assert len(endpoints) == 3
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_handle_simple_get(self, server, sample_endpoints):
|
|
server.register_endpoints(sample_endpoints)
|
|
app = server._create_app()
|
|
transport = ASGITransport(app=app)
|
|
async with AsyncClient(transport=transport, base_url="http://test") as client:
|
|
response = await client.get("/api/hello")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["message"] == "Hello"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_handle_path_params(self, server, sample_endpoints):
|
|
server.register_endpoints(sample_endpoints)
|
|
app = server._create_app()
|
|
transport = ASGITransport(app=app)
|
|
async with AsyncClient(transport=transport, base_url="http://test") as client:
|
|
response = await client.get("/api/users/456")
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["id"] == "456"
|
|
assert data["name"] == "User 456"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_handle_post_with_body(self, server, sample_endpoints):
|
|
server.register_endpoints(sample_endpoints)
|
|
app = server._create_app()
|
|
transport = ASGITransport(app=app)
|
|
async with AsyncClient(transport=transport, base_url="http://test") as client:
|
|
response = await client.post("/api/data", json={"key": "test_value"})
|
|
assert response.status_code == 201
|
|
data = response.json()
|
|
assert data["received"] == "test_value"
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_handle_not_found(self, server, sample_endpoints):
|
|
server.register_endpoints(sample_endpoints)
|
|
app = server._create_app()
|
|
transport = ASGITransport(app=app)
|
|
async with AsyncClient(transport=transport, base_url="http://test") as client:
|
|
response = await client.get("/api/nonexistent")
|
|
assert response.status_code == 404
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_handle_method_not_allowed(self, server, sample_endpoints):
|
|
server.register_endpoints(sample_endpoints)
|
|
app = server._create_app()
|
|
transport = ASGITransport(app=app)
|
|
async with AsyncClient(transport=transport, base_url="http://test") as client:
|
|
response = await client.post("/api/hello")
|
|
assert response.status_code == 404
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_response_headers(self, server):
|
|
endpoint = Endpoint(
|
|
path="/api/headers",
|
|
method="GET",
|
|
response={
|
|
"status_code": 200,
|
|
"body": {"test": "value"},
|
|
"headers": {"X-Custom-Header": "custom-value"}
|
|
}
|
|
)
|
|
server.register_endpoint(endpoint)
|
|
app = server._create_app()
|
|
transport = ASGITransport(app=app)
|
|
async with AsyncClient(transport=transport, base_url="http://test") as client:
|
|
response = await client.get("/api/headers")
|
|
assert response.status_code == 200
|
|
assert response.headers.get("X-Custom-Header") == "custom-value"
|
|
|
|
def test_build_response_with_templating(self, server):
|
|
endpoint = Endpoint(
|
|
path="/api/test",
|
|
method="GET",
|
|
response={
|
|
"status_code": 200,
|
|
"body": {
|
|
"id": "{{request.path.id}}",
|
|
"uuid": "{{uuid}}"
|
|
}
|
|
}
|
|
)
|
|
context = {
|
|
"request": {"path": {"id": "123"}},
|
|
"uuid": "test-uuid"
|
|
}
|
|
response = server._build_response(endpoint, context)
|
|
assert response["status_code"] == 200
|
|
assert response["body"]["id"] == "123"
|
|
assert response["body"]["uuid"] == "test-uuid"
|