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

20 statements  

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

1import pytest 1deabc

2from dirty_equals import IsDict 1deabc

3from fastapi.testclient import TestClient 1deabc

4 

5from ...utils import needs_py310 1deabc

6 

7 

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

9def get_client(): 1deabc

10 from docs_src.header_params.tutorial002_an_py310 import app 1abc

11 

12 client = TestClient(app) 1abc

13 return client 1abc

14 

15 

16@needs_py310 1deabc

17@pytest.mark.parametrize( 1deabc

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): 1deabc

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

38 assert response.status_code == expected_status 1abc

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

40 

41 

42@needs_py310 1deabc

43def test_openapi_schema(client: TestClient): 1deabc

44 response = client.get("/openapi.json") 1abc

45 assert response.status_code == 200 1abc

46 assert response.json() == { 1abc

47 "openapi": "3.1.0", 

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

49 "paths": { 

50 "/items/": { 

51 "get": { 

52 "responses": { 

53 "200": { 

54 "description": "Successful Response", 

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

56 }, 

57 "422": { 

58 "description": "Validation Error", 

59 "content": { 

60 "application/json": { 

61 "schema": { 

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

63 } 

64 } 

65 }, 

66 }, 

67 }, 

68 "summary": "Read Items", 

69 "operationId": "read_items_items__get", 

70 "parameters": [ 

71 { 

72 "required": False, 

73 "schema": IsDict( 

74 { 

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

76 "title": "Strange Header", 

77 } 

78 ) 

79 | IsDict( 

80 # TODO: remove when deprecating Pydantic v1 

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

82 ), 

83 "name": "strange_header", 

84 "in": "header", 

85 } 

86 ], 

87 } 

88 } 

89 }, 

90 "components": { 

91 "schemas": { 

92 "ValidationError": { 

93 "title": "ValidationError", 

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

95 "type": "object", 

96 "properties": { 

97 "loc": { 

98 "title": "Location", 

99 "type": "array", 

100 "items": { 

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

102 }, 

103 }, 

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

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

106 }, 

107 }, 

108 "HTTPValidationError": { 

109 "title": "HTTPValidationError", 

110 "type": "object", 

111 "properties": { 

112 "detail": { 

113 "title": "Detail", 

114 "type": "array", 

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

116 } 

117 }, 

118 }, 

119 } 

120 }, 

121 }