pythonasyncioapiasyncfastapiframeworkjsonjson-schemaopenapiopenapi3pydanticpython-typespython3redocreststarletteswaggerswagger-uiuvicornweb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
879 B
31 lines
879 B
from fastapi import Depends, FastAPI
|
|
from fastapi.security import HTTPBasic
|
|
from fastapi.testclient import TestClient
|
|
|
|
|
|
def test_default_realm_is_included():
|
|
app = FastAPI()
|
|
security = HTTPBasic()
|
|
|
|
@app.get("/protected")
|
|
def protected(_: str = Depends(security)):
|
|
return {"ok": True}
|
|
|
|
client = TestClient(app)
|
|
response = client.get("/protected")
|
|
assert response.status_code == 401
|
|
assert response.headers["www-authenticate"] == 'Basic realm="fastapi"'
|
|
|
|
|
|
def test_custom_realm_is_respected():
|
|
app = FastAPI()
|
|
security = HTTPBasic(realm="custom")
|
|
|
|
@app.get("/protected")
|
|
def protected(_: str = Depends(security)):
|
|
return {"ok": True}
|
|
|
|
client = TestClient(app)
|
|
response = client.get("/protected")
|
|
assert response.status_code == 401
|
|
assert response.headers["www-authenticate"] == 'Basic realm="custom"'
|
|
|