-
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.
feat: auto mount directory of local favicon file
- Loading branch information
1 parent
4074b2c
commit a9b5bbf
Showing
15 changed files
with
181 additions
and
13 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
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
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,21 @@ | ||
#!/usr/bin/env python | ||
|
||
from fastapi import FastAPI, Request | ||
from fastapi.responses import RedirectResponse | ||
|
||
from fastapi_cdn_host import monkey_patch_for_docs_ui | ||
|
||
app = FastAPI(title="FastAPI CDN host test", root_path="/api/v1") | ||
|
||
|
||
@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)} | ||
|
||
|
||
monkey_patch_for_docs_ui(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,36 @@ | ||
# mypy: no-disallow-untyped-decorators | ||
import pytest | ||
from httpx import 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(app=app, base_url="http://test") as c: | ||
yield c | ||
|
||
|
||
@pytest.mark.anyio | ||
async def test_docs(client: AsyncClient): # nosec | ||
response = await client.get("/docs") | ||
text = response.text | ||
assert response.status_code == 200, text | ||
assert default_favicon_url in text | ||
urls = await CdnHostBuilder.sniff_the_fastest() | ||
assert f'"{urls.js}"' in text | ||
assert f'"{urls.css}"' in text | ||
response2 = await client.get("/redoc") | ||
text2 = response2.text | ||
assert response2.status_code == 200, text2 | ||
assert f'"{urls.redoc}"' in text2 | ||
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
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 | ||
|
||
from fastapi_cdn_host import monkey_patch_for_docs_ui | ||
|
||
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)} | ||
|
||
|
||
monkey_patch_for_docs_ui(app, favicon_url="/static/favicon.ico") |
Binary file not shown.
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,39 @@ | ||
# mypy: no-disallow-untyped-decorators | ||
import pytest | ||
from httpx import 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(app=app, base_url="http://test") as c: | ||
yield c | ||
|
||
|
||
@pytest.mark.anyio | ||
async def test_docs(client: AsyncClient): # nosec | ||
response = await client.get("/docs") | ||
text = response.text | ||
assert response.status_code == 200, text | ||
assert default_favicon_url not in text | ||
assert '"/static/favicon.ico"' in text | ||
urls = await CdnHostBuilder.sniff_the_fastest() | ||
assert f'"{urls.js}"' in text | ||
assert f'"{urls.css}"' in text | ||
response2 = await client.get("/redoc") | ||
text2 = response2.text | ||
assert response2.status_code == 200, text2 | ||
assert f'"{urls.redoc}"' in text2 | ||
response = await client.get("/static/favicon.ico") | ||
assert response.status_code == 200 | ||
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