Skip to content

Commit

Permalink
More tests (#55)
Browse files Browse the repository at this point in the history
* More tests

* Pin coverage to 97
  • Loading branch information
Tinche authored Dec 10, 2023
1 parent f7d151e commit cf55112
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
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())

0 comments on commit cf55112

Please sign in to comment.