|
7 | 7 | import pytest |
8 | 8 | import labthings_fastapi as lt |
9 | 9 | from fastapi.testclient import TestClient |
| 10 | +from starlette.routing import Route |
10 | 11 |
|
11 | 12 |
|
12 | 13 | def test_server_from_config_non_thing_error(): |
@@ -67,3 +68,31 @@ def test_server_thing_descriptions(): |
67 | 68 | prop = thing_description["properties"][prop_name] |
68 | 69 | expected_href = thing_name + "/" + prop_name |
69 | 70 | assert prop["forms"][0]["href"] == expected_href |
| 71 | + |
| 72 | + |
| 73 | +def test_api_prefix(): |
| 74 | + """Check we can add a prefix to the URLs on a server.""" |
| 75 | + |
| 76 | + class Example(lt.Thing): |
| 77 | + """An example Thing""" |
| 78 | + |
| 79 | + server = lt.ThingServer(things={"example": Example}, api_prefix="/api/v3") |
| 80 | + paths = [route.path for route in server.app.routes if isinstance(route, Route)] |
| 81 | + for expected_path in [ |
| 82 | + "/api/v3/action_invocations", |
| 83 | + "/api/v3/action_invocations/{id}", |
| 84 | + "/api/v3/action_invocations/{id}/output", |
| 85 | + "/api/v3/action_invocations/{id}", |
| 86 | + "/api/v3/blob/{blob_id}", |
| 87 | + "/api/v3/thing_descriptions/", |
| 88 | + "/api/v3/example/", |
| 89 | + ]: |
| 90 | + assert expected_path in paths |
| 91 | + |
| 92 | + unprefixed_paths = {p for p in paths if not p.startswith("/api/v3/")} |
| 93 | + assert unprefixed_paths == { |
| 94 | + "/openapi.json", |
| 95 | + "/docs", |
| 96 | + "/docs/oauth2-redirect", |
| 97 | + "/redoc", |
| 98 | + } |
0 commit comments