| 1 |
import inspect |
| 2 |
|
| 3 |
from fastapi.openapi.docs import get_redoc_html, get_swagger_ui_html |
| 4 |
|
| 5 |
|
| 6 |
def test_strings_in_generated_swagger(): |
| 7 |
sig = inspect.signature(get_swagger_ui_html) |
| 8 |
swagger_js_url = sig.parameters.get("swagger_js_url").default # type: ignore |
| 9 |
swagger_css_url = sig.parameters.get("swagger_css_url").default # type: ignore |
| 10 |
swagger_favicon_url = sig.parameters.get("swagger_favicon_url").default # type: ignore |
| 11 |
html = get_swagger_ui_html(openapi_url="/docs", title="title") |
| 12 |
body_content = bytes(html.body).decode() |
| 13 |
assert swagger_js_url in body_content |
| 14 |
assert swagger_css_url in body_content |
| 15 |
assert swagger_favicon_url in body_content |
| 16 |
|
| 17 |
|
| 18 |
def test_strings_in_custom_swagger(): |
| 19 |
swagger_js_url = "swagger_fake_file.js" |
| 20 |
swagger_css_url = "swagger_fake_file.css" |
| 21 |
swagger_favicon_url = "swagger_fake_file.png" |
| 22 |
html = get_swagger_ui_html( |
| 23 |
openapi_url="/docs", |
| 24 |
title="title", |
| 25 |
swagger_js_url=swagger_js_url, |
| 26 |
swagger_css_url=swagger_css_url, |
| 27 |
swagger_favicon_url=swagger_favicon_url, |
| 28 |
) |
| 29 |
body_content = bytes(html.body).decode() |
| 30 |
assert swagger_js_url in body_content |
| 31 |
assert swagger_css_url in body_content |
| 32 |
assert swagger_favicon_url in body_content |
| 33 |
|
| 34 |
|
| 35 |
def test_strings_in_generated_redoc(): |
| 36 |
sig = inspect.signature(get_redoc_html) |
| 37 |
redoc_js_url = sig.parameters.get("redoc_js_url").default # type: ignore |
| 38 |
redoc_favicon_url = sig.parameters.get("redoc_favicon_url").default # type: ignore |
| 39 |
html = get_redoc_html(openapi_url="/docs", title="title") |
| 40 |
body_content = bytes(html.body).decode() |
| 41 |
assert redoc_js_url in body_content |
| 42 |
assert redoc_favicon_url in body_content |
| 43 |
|
| 44 |
|
| 45 |
def test_strings_in_custom_redoc(): |
| 46 |
redoc_js_url = "fake_redoc_file.js" |
| 47 |
redoc_favicon_url = "fake_redoc_file.png" |
| 48 |
html = get_redoc_html( |
| 49 |
openapi_url="/docs", |
| 50 |
title="title", |
| 51 |
redoc_js_url=redoc_js_url, |
| 52 |
redoc_favicon_url=redoc_favicon_url, |
| 53 |
) |
| 54 |
body_content = bytes(html.body).decode() |
| 55 |
assert redoc_js_url in body_content |
| 56 |
assert redoc_favicon_url in body_content |
| 57 |
|
| 58 |
|
| 59 |
def test_google_fonts_in_generated_redoc(): |
| 60 |
body_with_google_fonts = bytes( |
| 61 |
get_redoc_html(openapi_url="/docs", title="title").body |
| 62 |
).decode() |
| 63 |
assert "fonts.googleapis.com" in body_with_google_fonts |
| 64 |
body_without_google_fonts = bytes( |
| 65 |
get_redoc_html(openapi_url="/docs", title="title", with_google_fonts=False).body |
| 66 |
).decode() |
| 67 |
assert "fonts.googleapis.com" not in body_without_google_fonts |