-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb89b0c
commit a716452
Showing
13 changed files
with
199 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,6 @@ def main() -> int: | |
raise exc | ||
else: | ||
res += rc % 255 # rc may be 256 | ||
print(f"{res = }") | ||
return res | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import calendar | ||
from datetime import datetime | ||
|
||
from fastapi import FastAPI, HTTPException, Request, status | ||
|
||
import fastapi_cdn_host | ||
|
||
app = FastAPI() | ||
app_sync_lock = FastAPI() | ||
|
||
|
||
def sync_lock(request: Request) -> None: | ||
if ( | ||
not (d := request.query_params.get("day")) | ||
or (weekday := getattr(calendar, d.upper(), None)) is None | ||
or (weekday != datetime.now().weekday()) | ||
): | ||
raise HTTPException(status_code=status.HTTP_418_IM_A_TEAPOT) | ||
|
||
|
||
async def lock(request: Request) -> None: | ||
sync_lock(request) | ||
|
||
|
||
fastapi_cdn_host.patch_docs(app, lock=lock) | ||
fastapi_cdn_host.patch_docs(app_sync_lock, lock=sync_lock) | ||
|
||
|
||
@app.get("/") | ||
@app_sync_lock.get("/") | ||
async def index(): | ||
return "homepage" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# mypy: no-disallow-untyped-decorators | ||
from datetime import datetime | ||
|
||
import httpx | ||
import pytest | ||
from main import app, app_sync_lock | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def anyio_backend(): | ||
return "asyncio" | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
async def client(): | ||
async with httpx.AsyncClient( | ||
transport=httpx.ASGITransport(app=app), base_url="http://test.com" | ||
) as c: | ||
yield c | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
async def client_sync_lock(): | ||
async with httpx.AsyncClient( | ||
transport=httpx.ASGITransport(app=app_sync_lock), | ||
base_url="http://sync.test.com", | ||
) as c: | ||
yield c | ||
|
||
|
||
class TestLock: | ||
weekdays = [ | ||
"Monday", | ||
"Tuesday", | ||
"Wednesday", | ||
"Thursday", | ||
"Friday", | ||
"Saturday", | ||
"Sunday", | ||
] | ||
|
||
@pytest.mark.anyio | ||
async def test_lock(self, client: httpx.AsyncClient): | ||
await self.request_locked(client) | ||
|
||
async def request_locked(self, client): | ||
response = await client.get("/") | ||
assert response.status_code == 200 | ||
assert response.text == '"homepage"' | ||
response = await client.get("/docs") | ||
assert response.status_code == 418 | ||
assert response.json()["detail"] == "I'm a Teapot" | ||
response = await client.get("/redoc") | ||
assert response.status_code == 418 | ||
assert response.json()["detail"] == "I'm a Teapot" | ||
day = self.weekdays[datetime.now().weekday()] | ||
response = await client.get(f"/docs?day={day}") | ||
assert response.status_code == 200 | ||
response = await client.get(f"/redoc?day={day}") | ||
assert response.status_code == 200 | ||
response = await client.get("/") | ||
assert response.status_code == 200 | ||
assert response.text == '"homepage"' | ||
|
||
@pytest.mark.anyio | ||
async def test_sync_lock(self, client_sync_lock: httpx.AsyncClient): | ||
await self.request_locked(client_sync_lock) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/usr/bin/env python | ||
|
||
from fastapi import FastAPI, Request | ||
from fastapi.responses import RedirectResponse | ||
|
||
import fastapi_cdn_host | ||
|
||
app = FastAPI(title="FastAPI CDN host test") | ||
|
||
|
||
@app.get("/", include_in_schema=False) | ||
async def to_docs(): | ||
return RedirectResponse("/docs") | ||
|
||
|
||
@app.get("/app") | ||
async def get_app(request: Request) -> dict: | ||
return {"routes": str(request.app.routes)} | ||
|
||
|
||
fastapi_cdn_host.patch_docs(app) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../static_with_favicon/static/swagger-ui.css |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# mypy: no-disallow-untyped-decorators | ||
import pytest | ||
from httpx import ASGITransport, AsyncClient | ||
from main import app | ||
|
||
from fastapi_cdn_host.client import CdnHostBuilder | ||
|
||
default_favicon_url = "https://fastapi.tiangolo.com/img/favicon.png" | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def anyio_backend(): | ||
return "asyncio" | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
async def client(): | ||
async with AsyncClient( | ||
transport=ASGITransport(app=app), base_url="http://test" | ||
) as c: | ||
yield c | ||
|
||
|
||
@pytest.mark.anyio | ||
async def test_missing_js(client: AsyncClient): # nosec | ||
swagger_ui = CdnHostBuilder.swagger_files | ||
js_url = "/static/" + swagger_ui["js"] | ||
css_url = "/static/" + swagger_ui["css"] | ||
response = await client.get(css_url) | ||
assert response.status_code == 200, f"{response.url=};{response.text=}" | ||
response = await client.get(js_url) | ||
assert response.status_code == 404, response.text | ||
response = await client.get("/docs") | ||
text = response.text | ||
assert response.status_code == 200, text | ||
assert default_favicon_url in text | ||
assert js_url in text | ||
assert css_url in text | ||
response = await client.get("/redoc") | ||
text = response.text | ||
assert response.status_code == 200, text | ||
assert "/static/redoc" in text | ||
response = await client.get("/app") | ||
assert response.status_code == 200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters