|
| 1 | +from typing import Any |
| 2 | + |
| 3 | +from lilya.apps import Lilya |
| 4 | +from lilya.contrib.openapi.config import OpenAPIConfig |
| 5 | +from lilya.contrib.openapi.decorator import openapi |
| 6 | +from lilya.routing import Path |
| 7 | +from lilya.testclient import TestClient |
| 8 | + |
| 9 | + |
| 10 | +class CustomOpenAPIConfig(OpenAPIConfig): |
| 11 | + servers: Any = [ |
| 12 | + {"url": "/", "description": "Default server"}, |
| 13 | + { |
| 14 | + "url": "http://staging.esmerald.dev", |
| 15 | + "description": "Staging of Esmerald", |
| 16 | + }, |
| 17 | + {"url": "https://esmerald.dev"}, |
| 18 | + ] |
| 19 | + |
| 20 | + |
| 21 | +@openapi(summary="Bar", description="", operation_id="bar_bar_get") |
| 22 | +async def bar() -> dict[str, str]: |
| 23 | + return {"hello": "world"} |
| 24 | + |
| 25 | + |
| 26 | +app = Lilya( |
| 27 | + routes=[Path("/bar", handler=bar)], |
| 28 | + enable_openapi=True, |
| 29 | + openapi_config=CustomOpenAPIConfig(), |
| 30 | +) |
| 31 | + |
| 32 | + |
| 33 | +client = TestClient(app) |
| 34 | + |
| 35 | + |
| 36 | +def test_application(test_client_factory): |
| 37 | + response = client.get("/bar") |
| 38 | + assert response.status_code == 200, response.json() |
| 39 | + |
| 40 | + |
| 41 | +def test_openapi_schema(test_client_factory): |
| 42 | + response = client.get("/openapi.json") |
| 43 | + |
| 44 | + assert response.status_code == 200, response.text |
| 45 | + |
| 46 | + assert response.json() == { |
| 47 | + "openapi": "3.1.0", |
| 48 | + "info": { |
| 49 | + "title": "Lilya", |
| 50 | + "version": client.app.version, |
| 51 | + "summary": "Lilya application", |
| 52 | + "description": "Yet another framework/toolkit that delivers.", |
| 53 | + "contact": {"name": "Lilya", "url": "https://lilya.dev", "email": "admin@myapp.com"}, |
| 54 | + }, |
| 55 | + "paths": { |
| 56 | + "/bar": { |
| 57 | + "get": { |
| 58 | + "operationId": "bar_bar_get", |
| 59 | + "summary": "Bar", |
| 60 | + "description": "", |
| 61 | + "parameters": [], |
| 62 | + "responses": {"200": {"description": "Successful response"}}, |
| 63 | + } |
| 64 | + } |
| 65 | + }, |
| 66 | + "components": {"schemas": {}, "securitySchemes": {}}, |
| 67 | + "servers": [ |
| 68 | + {"url": "/", "description": "Default server"}, |
| 69 | + {"url": "http://staging.esmerald.dev", "description": "Staging of Esmerald"}, |
| 70 | + {"url": "https://esmerald.dev"}, |
| 71 | + ], |
| 72 | + } |
0 commit comments