Coverage for tests / test_security_http_base_description.py: 100%

23 statements  

« 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

5 

6app = FastAPI() 1abcd

7 

8security = HTTPBase(scheme="Other", description="Other Security Scheme") 1abcd

9 

10 

11@app.get("/users/me") 1abcd

12def read_current_user(credentials: HTTPAuthorizationCredentials = Security(security)): 1abcd

13 return {"scheme": credentials.scheme, "credentials": credentials.credentials} 1efg

14 

15 

16client = TestClient(app) 1abcd

17 

18 

19def test_security_http_base(): 1abcd

20 response = client.get("/users/me", headers={"Authorization": "Other foobar"}) 1efg

21 assert response.status_code == 200, response.text 1efg

22 assert response.json() == {"scheme": "Other", "credentials": "foobar"} 1efg

23 

24 

25def test_security_http_base_no_credentials(): 1abcd

26 response = client.get("/users/me") 1hij

27 assert response.status_code == 401, response.text 1hij

28 assert response.json() == {"detail": "Not authenticated"} 1hij

29 assert response.headers["WWW-Authenticate"] == "Other" 1hij

30 

31 

32def test_openapi_schema(): 1abcd

33 response = client.get("/openapi.json") 1klm

34 assert response.status_code == 200, response.text 1klm

35 assert response.json() == snapshot( 1klm

36 { 

37 "openapi": "3.1.0", 

38 "info": {"title": "FastAPI", "version": "0.1.0"}, 

39 "paths": { 

40 "/users/me": { 

41 "get": { 

42 "responses": { 

43 "200": { 

44 "description": "Successful Response", 

45 "content": {"application/json": {"schema": {}}}, 

46 } 

47 }, 

48 "summary": "Read Current User", 

49 "operationId": "read_current_user_users_me_get", 

50 "security": [{"HTTPBase": []}], 

51 } 

52 } 

53 }, 

54 "components": { 

55 "securitySchemes": { 

56 "HTTPBase": { 

57 "type": "http", 

58 "scheme": "Other", 

59 "description": "Other Security Scheme", 

60 } 

61 } 

62 }, 

63 } 

64 )