Coverage for tests / test_security_http_base.py: 100%
27 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 fastapi import FastAPI, Security 1abcd
2from fastapi.security.http import HTTPAuthorizationCredentials, HTTPBase 1abcd
3from fastapi.testclient import TestClient 1abcd
4from inline_snapshot import snapshot 1abcd
6app = FastAPI() 1abcd
8security = HTTPBase(scheme="Other") 1abcd
11@app.get("/users/me") 1abcd
12def read_current_user(credentials: HTTPAuthorizationCredentials = Security(security)): 1abcd
13 return {"scheme": credentials.scheme, "credentials": credentials.credentials} 1efghij
16client = TestClient(app) 1abcd
19def test_security_http_base(): 1abcd
20 response = client.get("/users/me", headers={"Authorization": "Other foobar"}) 1egi
21 assert response.status_code == 200, response.text 1egi
22 assert response.json() == {"scheme": "Other", "credentials": "foobar"} 1egi
25def test_security_http_base_with_whitespaces(): 1abcd
26 response = client.get("/users/me", headers={"Authorization": "Other foobar "}) 1fhj
27 assert response.status_code == 200, response.text 1fhj
28 assert response.json() == {"scheme": "Other", "credentials": "foobar"} 1fhj
31def test_security_http_base_no_credentials(): 1abcd
32 response = client.get("/users/me") 1klm
33 assert response.status_code == 401, response.text 1klm
34 assert response.json() == {"detail": "Not authenticated"} 1klm
35 assert response.headers["WWW-Authenticate"] == "Other" 1klm
38def test_openapi_schema(): 1abcd
39 response = client.get("/openapi.json") 1nop
40 assert response.status_code == 200, response.text 1nop
41 assert response.json() == snapshot( 1nop
42 {
43 "openapi": "3.1.0",
44 "info": {"title": "FastAPI", "version": "0.1.0"},
45 "paths": {
46 "/users/me": {
47 "get": {
48 "responses": {
49 "200": {
50 "description": "Successful Response",
51 "content": {"application/json": {"schema": {}}},
52 }
53 },
54 "summary": "Read Current User",
55 "operationId": "read_current_user_users_me_get",
56 "security": [{"HTTPBase": []}],
57 }
58 }
59 },
60 "components": {
61 "securitySchemes": {"HTTPBase": {"type": "http", "scheme": "Other"}}
62 },
63 }
64 )