-
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
7a53701
commit eca5cfb
Showing
11 changed files
with
106 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "fastapi-cdn-host" | ||
version = "0.3.1" | ||
version = "0.3.2" | ||
description = "" | ||
authors = ["Waket Zheng <[email protected]>"] | ||
readme = "README.md" | ||
|
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,33 @@ | ||
#!/usr/bin/env python | ||
from fastapi import FastAPI, Request | ||
from fastapi.responses import RedirectResponse | ||
|
||
from fastapi_cdn_host import CdnHostEnum, 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, | ||
docs_cdn_host=CdnHostEnum.extend( | ||
( | ||
"https://cdn.bootcdn.net/ajax/libs", | ||
("/swagger-ui/{version}/", ""), | ||
), # BootCDN | ||
("https://cdn.staticfile.org", ("/swagger-ui/{version}/", "")), # 七牛云 | ||
( | ||
"https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M", | ||
("/swagger-ui/{version}/", ""), | ||
), # 字节 | ||
), | ||
) |
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,45 @@ | ||
# mypy: no-disallow-untyped-decorators | ||
import pytest | ||
from httpx import AsyncClient | ||
from main import app | ||
|
||
from fastapi_cdn_host.client import CdnHostBuilder, CdnHostEnum | ||
|
||
|
||
@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 | ||
urls = await CdnHostBuilder.sniff_the_fastest( | ||
choices=CdnHostEnum.extend( | ||
( | ||
"https://cdn.bootcdn.net/ajax/libs", | ||
("/swagger-ui/{version}/", ""), | ||
), | ||
("https://cdn.staticfile.org", ("/swagger-ui/{version}/", "")), | ||
( | ||
"https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M", | ||
("/swagger-ui/{version}/", ""), | ||
), | ||
) | ||
) | ||
response = await client.get("/docs") | ||
text = response.text | ||
assert response.status_code == 200, text | ||
assert urls.js in text | ||
assert urls.css in text | ||
response2 = await client.get("/redoc") | ||
text2 = response2.text | ||
assert response2.status_code == 200, text2 | ||
assert 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
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