Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More tests #55

Merged
merged 2 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ jobs:
echo "total=$TOTAL" >> $GITHUB_ENV

# Report again and fail if under the threshold.
python -Im coverage report --fail-under=96
python -Im coverage report --fail-under=97

- name: "Upload HTML report."
uses: "actions/upload-artifact@v3"
Expand Down
14 changes: 10 additions & 4 deletions src/uapi/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,21 +175,27 @@ def openapi_handler() -> Ok[bytes]:

self.route(path, openapi_handler)

def serve_swaggerui(self, path: str = "/swaggerui"):
def serve_swaggerui(
self, path: str = "/swaggerui", openapi_path: str = "/openapi.json"
):
"""Start serving the Swagger UI at the given path."""
from .openapi_ui import swaggerui

fixed_path = swaggerui.replace("$OPENAPIURL", openapi_path)

def swaggerui_handler() -> Ok[str]:
return Ok(swaggerui, {"content-type": "text/html"})
return Ok(fixed_path, {"content-type": "text/html"})

self.route(path, swaggerui_handler)

def serve_redoc(self, path: str = "/redoc"):
def serve_redoc(self, path: str = "/redoc", openapi_path: str = "/openapi.json"):
"""Start serving the ReDoc UI at the given path."""
from .openapi_ui import redoc

fixed_path = redoc.replace("$OPENAPIURL", openapi_path)

def redoc_handler() -> Ok[str]:
return Ok(redoc, {"content-type": "text/html"})
return Ok(fixed_path, {"content-type": "text/html"})

self.route(path, redoc_handler)

Expand Down
2 changes: 1 addition & 1 deletion src/uapi/openapi_ui/redoc.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</head>

<body>
<redoc spec-url='../openapi.json'></redoc>
<redoc spec-url='$OPENAPIURL'></redoc>
<script src="https://cdn.jsdelivr.net/npm/redoc@latest/bundles/redoc.standalone.js"> </script>
</body>

Expand Down
2 changes: 1 addition & 1 deletion src/uapi/openapi_ui/swaggerui.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<script>
window.onload = function () {
const ui = SwaggerUIBundle({
url: "../openapi.json",
url: "$OPENAPIURL",
dom_id: '#swaggerui',
presets: [
SwaggerUIBundle.presets.apis,
Expand Down
20 changes: 20 additions & 0 deletions tests/openapi/test_openapi_uis.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ async def openapi_renderer_app(unused_tcp_port_factory: Callable[..., int]):
app = App()

app.serve_openapi(path="/openapi_test.json")
app.serve_swaggerui(openapi_path="/openapi_test.json")
app.serve_redoc(openapi_path="/openapi_test.json")
app.serve_elements(openapi_path="/openapi_test.json")

shutdown_event = Event()
Expand All @@ -32,3 +34,21 @@ async def test_elements(openapi_renderer_app: int) -> None:

assert resp.status == 200
assert 'apiDescriptionUrl="/openapi_test.json"' in (await resp.text())


async def test_swagger(openapi_renderer_app: int) -> None:
"""SwaggerUI is served properly."""
async with ClientSession() as session:
resp = await session.get(f"http://localhost:{openapi_renderer_app}/swaggerui")

assert resp.status == 200
assert 'url: "/openapi_test.json"' in (await resp.text())


async def test_redoc(openapi_renderer_app: int) -> None:
"""Redoc is served properly."""
async with ClientSession() as session:
resp = await session.get(f"http://localhost:{openapi_renderer_app}/redoc")

assert resp.status == 200
assert "spec-url='/openapi_test.json'" in (await resp.text())
Loading