1
from typing import Optional
3
from fastapi import FastAPI, Security
4
from fastapi.security.http import HTTPAuthorizationCredentials, HTTPBase
5
from fastapi.testclient import TestClient
9
security = HTTPBase(scheme="Other", auto_error=False)
14
credentials: Optional[HTTPAuthorizationCredentials] = Security(security),
16
if credentials is None:
17
return {"msg": "Create an account first"}
18
return {"scheme": credentials.scheme, "credentials": credentials.credentials}
21
client = TestClient(app)
24
def test_security_http_base():
25
response = client.get("/users/me", headers={"Authorization": "Other foobar"})
26
assert response.status_code == 200, response.text
27
assert response.json() == {"scheme": "Other", "credentials": "foobar"}
30
def test_security_http_base_no_credentials():
31
response = client.get("/users/me")
32
assert response.status_code == 200, response.text
33
assert response.json() == {"msg": "Create an account first"}
36
def test_openapi_schema():
37
response = client.get("/openapi.json")
38
assert response.status_code == 200, response.text
39
assert response.json() == {
41
"info": {"title": "FastAPI", "version": "0.1.0"},
47
"description": "Successful Response",
48
"content": {"application/json": {"schema": {}}},
51
"summary": "Read Current User",
52
"operationId": "read_current_user_users_me_get",
53
"security": [{"HTTPBase": []}],
58
"securitySchemes": {"HTTPBase": {"type": "http", "scheme": "Other"}}