Coverage for tests / test_security_http_base_optional.py: 100%
25 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
1from typing import Optional 1abcd
3from fastapi import FastAPI, Security 1abcd
4from fastapi.security.http import HTTPAuthorizationCredentials, HTTPBase 1abcd
5from fastapi.testclient import TestClient 1abcd
6from inline_snapshot import snapshot 1abcd
8app = FastAPI() 1abcd
10security = HTTPBase(scheme="Other", auto_error=False) 1abcd
13@app.get("/users/me") 1abcd
14def read_current_user( 1abcd
15 credentials: Optional[HTTPAuthorizationCredentials] = Security(security),
16):
17 if credentials is None: 1efghij
18 return {"msg": "Create an account first"} 1fhj
19 return {"scheme": credentials.scheme, "credentials": credentials.credentials} 1egi
22client = TestClient(app) 1abcd
25def test_security_http_base(): 1abcd
26 response = client.get("/users/me", headers={"Authorization": "Other foobar"}) 1egi
27 assert response.status_code == 200, response.text 1egi
28 assert response.json() == {"scheme": "Other", "credentials": "foobar"} 1egi
31def test_security_http_base_no_credentials(): 1abcd
32 response = client.get("/users/me") 1fhj
33 assert response.status_code == 200, response.text 1fhj
34 assert response.json() == {"msg": "Create an account first"} 1fhj
37def test_openapi_schema(): 1abcd
38 response = client.get("/openapi.json") 1klm
39 assert response.status_code == 200, response.text 1klm
40 assert response.json() == snapshot( 1klm
41 {
42 "openapi": "3.1.0",
43 "info": {"title": "FastAPI", "version": "0.1.0"},
44 "paths": {
45 "/users/me": {
46 "get": {
47 "responses": {
48 "200": {
49 "description": "Successful Response",
50 "content": {"application/json": {"schema": {}}},
51 }
52 },
53 "summary": "Read Current User",
54 "operationId": "read_current_user_users_me_get",
55 "security": [{"HTTPBase": []}],
56 }
57 }
58 },
59 "components": {
60 "securitySchemes": {"HTTPBase": {"type": "http", "scheme": "Other"}}
61 },
62 }
63 )