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

19 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-05-05 00:03 +0000

1import importlib 1abcdef

2 

3import pytest 1abcdef

4from dirty_equals import IsDict 1abcdef

5from fastapi.testclient import TestClient 1abcdef

6 

7from ...utils import needs_py39, needs_py310 1abcdef

8 

9 

10@pytest.fixture( 1abcdef

11 name="client", 

12 params=[ 

13 "tutorial003", 

14 pytest.param("tutorial003_py310", marks=needs_py310), 

15 "tutorial003_an", 

16 pytest.param("tutorial003_an_py39", marks=needs_py39), 

17 pytest.param("tutorial003_an_py310", marks=needs_py310), 

18 ], 

19) 

20def get_client(request: pytest.FixtureRequest): 1abcdef

21 mod = importlib.import_module(f"docs_src.header_params.{request.param}") 1abcdef

22 

23 client = TestClient(mod.app) 1abcdef

24 return client 1abcdef

25 

26 

27@pytest.mark.parametrize( 1abcdef

28 "path,headers,expected_status,expected_response", 

29 [ 

30 ("/items", None, 200, {"X-Token values": None}), 

31 ("/items", {"x-token": "foo"}, 200, {"X-Token values": ["foo"]}), 

32 # TODO: fix this, is it a bug? 

33 # ("/items", [("x-token", "foo"), ("x-token", "bar")], 200, {"X-Token values": ["foo", "bar"]}), 

34 ], 

35) 

36def test(path, headers, expected_status, expected_response, client: TestClient): 1abcdef

37 response = client.get(path, headers=headers) 1ghijkl

38 assert response.status_code == expected_status 1ghijkl

39 assert response.json() == expected_response 1ghijkl

40 

41 

42def test_openapi_schema(client: TestClient): 1abcdef

43 response = client.get("/openapi.json") 1mnopqr

44 assert response.status_code == 200 1mnopqr

45 assert response.json() == { 1mnopqr

46 "openapi": "3.1.0", 

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

48 "paths": { 

49 "/items/": { 

50 "get": { 

51 "summary": "Read Items", 

52 "operationId": "read_items_items__get", 

53 "parameters": [ 

54 { 

55 "required": False, 

56 "schema": IsDict( 

57 { 

58 "title": "X-Token", 

59 "anyOf": [ 

60 {"type": "array", "items": {"type": "string"}}, 

61 {"type": "null"}, 

62 ], 

63 } 

64 ) 

65 | IsDict( 

66 # TODO: remove when deprecating Pydantic v1 

67 { 

68 "title": "X-Token", 

69 "type": "array", 

70 "items": {"type": "string"}, 

71 } 

72 ), 

73 "name": "x-token", 

74 "in": "header", 

75 } 

76 ], 

77 "responses": { 

78 "200": { 

79 "description": "Successful Response", 

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

81 }, 

82 "422": { 

83 "description": "Validation Error", 

84 "content": { 

85 "application/json": { 

86 "schema": { 

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

88 } 

89 } 

90 }, 

91 }, 

92 }, 

93 } 

94 } 

95 }, 

96 "components": { 

97 "schemas": { 

98 "HTTPValidationError": { 

99 "title": "HTTPValidationError", 

100 "type": "object", 

101 "properties": { 

102 "detail": { 

103 "title": "Detail", 

104 "type": "array", 

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

106 } 

107 }, 

108 }, 

109 "ValidationError": { 

110 "title": "ValidationError", 

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

112 "type": "object", 

113 "properties": { 

114 "loc": { 

115 "title": "Location", 

116 "type": "array", 

117 "items": { 

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

119 }, 

120 }, 

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

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

123 }, 

124 }, 

125 } 

126 }, 

127 }