Coverage for tests/test_tutorial/test_header_params/test_tutorial001_an.py: 100%

14 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1import pytest 1abcde

2from dirty_equals import IsDict 1abcde

3from fastapi.testclient import TestClient 1abcde

4 

5from docs_src.header_params.tutorial001_an import app 1abcde

6 

7client = TestClient(app) 1abcde

8 

9 

10@pytest.mark.parametrize( 1abcde

11 "path,headers,expected_status,expected_response", 

12 [ 

13 ("/items", None, 200, {"User-Agent": "testclient"}), 

14 ("/items", {"X-Header": "notvalid"}, 200, {"User-Agent": "testclient"}), 

15 ("/items", {"User-Agent": "FastAPI test"}, 200, {"User-Agent": "FastAPI test"}), 

16 ], 

17) 

18def test(path, headers, expected_status, expected_response): 1abcde

19 response = client.get(path, headers=headers) 1abcde

20 assert response.status_code == expected_status 1abcde

21 assert response.json() == expected_response 1abcde

22 

23 

24def test_openapi_schema(): 1abcde

25 response = client.get("/openapi.json") 1abcde

26 assert response.status_code == 200 1abcde

27 assert response.json() == { 1abcde

28 "openapi": "3.1.0", 

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

30 "paths": { 

31 "/items/": { 

32 "get": { 

33 "responses": { 

34 "200": { 

35 "description": "Successful Response", 

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

37 }, 

38 "422": { 

39 "description": "Validation Error", 

40 "content": { 

41 "application/json": { 

42 "schema": { 

43 "$ref": "#/components/schemas/HTTPValidationError" 

44 } 

45 } 

46 }, 

47 }, 

48 }, 

49 "summary": "Read Items", 

50 "operationId": "read_items_items__get", 

51 "parameters": [ 

52 { 

53 "required": False, 

54 "schema": IsDict( 

55 { 

56 "anyOf": [{"type": "string"}, {"type": "null"}], 

57 "title": "User-Agent", 

58 } 

59 ) 

60 | IsDict( 

61 # TODO: remove when deprecating Pydantic v1 

62 {"title": "User-Agent", "type": "string"} 

63 ), 

64 "name": "user-agent", 

65 "in": "header", 

66 } 

67 ], 

68 } 

69 } 

70 }, 

71 "components": { 

72 "schemas": { 

73 "ValidationError": { 

74 "title": "ValidationError", 

75 "required": ["loc", "msg", "type"], 

76 "type": "object", 

77 "properties": { 

78 "loc": { 

79 "title": "Location", 

80 "type": "array", 

81 "items": { 

82 "anyOf": [{"type": "string"}, {"type": "integer"}] 

83 }, 

84 }, 

85 "msg": {"title": "Message", "type": "string"}, 

86 "type": {"title": "Error Type", "type": "string"}, 

87 }, 

88 }, 

89 "HTTPValidationError": { 

90 "title": "HTTPValidationError", 

91 "type": "object", 

92 "properties": { 

93 "detail": { 

94 "title": "Detail", 

95 "type": "array", 

96 "items": {"$ref": "#/components/schemas/ValidationError"}, 

97 } 

98 }, 

99 }, 

100 } 

101 }, 

102 }