-
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
148d71b
commit 7b54327
Showing
16 changed files
with
2,022 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# mypy: no-disallow-untyped-decorators | ||
import contextlib | ||
import threading | ||
import time | ||
|
||
import uvicorn | ||
|
||
|
||
class UvicornServer(uvicorn.Server): | ||
def __init__(self, app="main:app", **kw): | ||
super().__init__(config=uvicorn.Config(app, **kw)) | ||
|
||
def install_signal_handlers(self): | ||
pass | ||
|
||
@contextlib.contextmanager | ||
def run_in_thread(self): | ||
thread = threading.Thread(target=self.run) | ||
thread.daemon = True | ||
thread.start() | ||
try: | ||
while not self.started: | ||
time.sleep(1e-3) | ||
yield | ||
finally: | ||
self.should_exit = True | ||
thread.join() |
1,820 changes: 1,820 additions & 0 deletions
1,820
tests/private_cdn/cdn/redoc/next/redoc.standalone.js
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
tests/private_cdn/cdn/swagger-ui@latest/swagger-ui-bundle.js.map
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,2 @@ | ||
PORT = 8617 | ||
MY_CDN = f"http://127.0.0.1:{PORT}/cdn" |
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,25 @@ | ||
#!/usr/bin/env python | ||
from config import MY_CDN | ||
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, | ||
docs_cdn_host=(MY_CDN, ("/swagger-ui@latest/", "/redoc/next/")), | ||
favicon_url=MY_CDN + "/favicon.ico", | ||
) |
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,8 @@ | ||
from pathlib import Path | ||
|
||
from fastapi import FastAPI | ||
from fastapi.staticfiles import StaticFiles | ||
|
||
app = FastAPI() | ||
STATIC_ROOT = Path(__file__).parent.parent / "static_auto" / "static" | ||
app.mount("/cdn", StaticFiles(directory=STATIC_ROOT), name="cdn") |
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,47 @@ | ||
# mypy: no-disallow-untyped-decorators | ||
import sys | ||
from pathlib import Path | ||
|
||
import pytest | ||
from config import MY_CDN, PORT | ||
from httpx import AsyncClient | ||
from main import app | ||
|
||
try: | ||
from tests.http_race.utils import UvicornServer | ||
except ImportError: | ||
_path = Path(__file__).parent.parent / "http_race" | ||
sys.path.append(_path.as_posix()) | ||
from utils import UvicornServer # type: ignore[no-redef] | ||
|
||
|
||
@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 | ||
css_url = MY_CDN + "/swagger-ui@latest/swagger-ui.css" | ||
js_url = MY_CDN + "/swagger-ui@latest/swagger-ui-bundle.js" | ||
redoc_url = MY_CDN + "/redoc/next/redoc.standalone.js" | ||
favicon_url = MY_CDN + "/favicon.ico" | ||
with UvicornServer("media_server:app", port=PORT).run_in_thread(): | ||
response = await client.get("/docs") | ||
text = response.text | ||
assert response.status_code == 200, text | ||
assert f'"{favicon_url}"' in text | ||
assert f'"{css_url}"' in text | ||
assert f'"{js_url}"' in text | ||
response = await client.get("/redoc") | ||
text = response.text | ||
assert response.status_code == 200, text | ||
assert f'"{redoc_url}"' 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
#!/usr/bin/env python | ||
from pathlib import Path | ||
|
||
from fastapi import FastAPI, Request | ||
|