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

22 statements  

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

1import pytest 1eabcd

2from dirty_equals import IsDict 1eabcd

3from fastapi.testclient import TestClient 1eabcd

4 

5from ...utils import needs_py39 1eabcd

6 

7 

8@pytest.fixture(name="client") 1eabcd

9def get_client(): 1eabcd

10 from docs_src.header_params.tutorial002_an_py39 import app 1abcd

11 

12 client = TestClient(app) 1abcd

13 return client 1abcd

14 

15 

16@needs_py39 1eabcd

17@pytest.mark.parametrize( 1eabcd

18 "path,headers,expected_status,expected_response", 

19 [ 

20 ("/items", None, 200, {"strange_header": None}), 

21 ("/items", {"X-Header": "notvalid"}, 200, {"strange_header": None}), 

22 ( 

23 "/items", 

24 {"strange_header": "FastAPI test"}, 

25 200, 

26 {"strange_header": "FastAPI test"}, 

27 ), 

28 ( 

29 "/items", 

30 {"strange-header": "Not really underscore"}, 

31 200, 

32 {"strange_header": None}, 

33 ), 

34 ], 

35) 

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

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

38 assert response.status_code == expected_status 1abcd

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

40 

41 

42@needs_py39 1eabcd

43def test_openapi_schema(): 1eabcd

44 from docs_src.header_params.tutorial002_an_py39 import app 1abcd

45 

46 client = TestClient(app) 1abcd

47 response = client.get("/openapi.json") 1abcd

48 assert response.status_code == 200 1abcd

49 assert response.json() == { 1abcd

50 "openapi": "3.1.0", 

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

52 "paths": { 

53 "/items/": { 

54 "get": { 

55 "responses": { 

56 "200": { 

57 "description": "Successful Response", 

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

59 }, 

60 "422": { 

61 "description": "Validation Error", 

62 "content": { 

63 "application/json": { 

64 "schema": { 

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

66 } 

67 } 

68 }, 

69 }, 

70 }, 

71 "summary": "Read Items", 

72 "operationId": "read_items_items__get", 

73 "parameters": [ 

74 { 

75 "required": False, 

76 "schema": IsDict( 

77 { 

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

79 "title": "Strange Header", 

80 } 

81 ) 

82 | IsDict( 

83 # TODO: remove when deprecating Pydantic v1 

84 {"title": "Strange Header", "type": "string"} 

85 ), 

86 "name": "strange_header", 

87 "in": "header", 

88 } 

89 ], 

90 } 

91 } 

92 }, 

93 "components": { 

94 "schemas": { 

95 "ValidationError": { 

96 "title": "ValidationError", 

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

98 "type": "object", 

99 "properties": { 

100 "loc": { 

101 "title": "Location", 

102 "type": "array", 

103 "items": { 

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

105 }, 

106 }, 

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

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

109 }, 

110 }, 

111 "HTTPValidationError": { 

112 "title": "HTTPValidationError", 

113 "type": "object", 

114 "properties": { 

115 "detail": { 

116 "title": "Detail", 

117 "type": "array", 

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

119 } 

120 }, 

121 }, 

122 } 

123 }, 

124 }