| 1 |
from fastapi import FastAPI |
| 2 |
from fastapi.testclient import TestClient |
| 3 |
|
| 4 |
swagger_ui_oauth2_redirect_url = "/docs/redirect" |
| 5 |
|
| 6 |
app = FastAPI(swagger_ui_oauth2_redirect_url=swagger_ui_oauth2_redirect_url) |
| 7 |
|
| 8 |
|
| 9 |
@app.get("/items/") |
| 10 |
async def read_items(): |
| 11 |
return {"id": "foo"} |
| 12 |
|
| 13 |
|
| 14 |
client = TestClient(app) |
| 15 |
|
| 16 |
|
| 17 |
def test_swagger_ui(): |
| 18 |
response = client.get("/docs") |
| 19 |
assert response.status_code == 200, response.text |
| 20 |
assert response.headers["content-type"] == "text/html; charset=utf-8" |
| 21 |
assert "swagger-ui-dist" in response.text |
| 22 |
print(client.base_url) |
| 23 |
assert ( |
| 24 |
f"oauth2RedirectUrl: window.location.origin + '{swagger_ui_oauth2_redirect_url}'" |
| 25 |
in response.text |
| 26 |
) |
| 27 |
|
| 28 |
|
| 29 |
def test_swagger_ui_oauth2_redirect(): |
| 30 |
response = client.get(swagger_ui_oauth2_redirect_url) |
| 31 |
assert response.status_code == 200, response.text |
| 32 |
assert response.headers["content-type"] == "text/html; charset=utf-8" |
| 33 |
assert "window.opener.swaggerUIRedirectOauth2" in response.text |
| 34 |
|
| 35 |
|
| 36 |
def test_response(): |
| 37 |
response = client.get("/items/") |
| 38 |
assert response.json() == {"id": "foo"} |