Coverage for tests/test_tutorial/test_header_params/test_tutorial003_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.tutorial003_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, {"X-Token values": None}), 

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

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

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

24 ], 

25) 

26def test(path, headers, expected_status, expected_response, client: TestClient): 1deabc

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

28 assert response.status_code == expected_status 1abc

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

30 

31 

32@needs_py310 1deabc

33def test_openapi_schema(client: TestClient): 1deabc

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

35 assert response.status_code == 200 1abc

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

37 "openapi": "3.1.0", 

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

39 "paths": { 

40 "/items/": { 

41 "get": { 

42 "summary": "Read Items", 

43 "operationId": "read_items_items__get", 

44 "parameters": [ 

45 { 

46 "required": False, 

47 "schema": IsDict( 

48 { 

49 "title": "X-Token", 

50 "anyOf": [ 

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

52 {"type": "null"}, 

53 ], 

54 } 

55 ) 

56 | IsDict( 

57 # TODO: remove when deprecating Pydantic v1 

58 { 

59 "title": "X-Token", 

60 "type": "array", 

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

62 } 

63 ), 

64 "name": "x-token", 

65 "in": "header", 

66 } 

67 ], 

68 "responses": { 

69 "200": { 

70 "description": "Successful Response", 

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

72 }, 

73 "422": { 

74 "description": "Validation Error", 

75 "content": { 

76 "application/json": { 

77 "schema": { 

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

79 } 

80 } 

81 }, 

82 }, 

83 }, 

84 } 

85 } 

86 }, 

87 "components": { 

88 "schemas": { 

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 "ValidationError": { 

101 "title": "ValidationError", 

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

103 "type": "object", 

104 "properties": { 

105 "loc": { 

106 "title": "Location", 

107 "type": "array", 

108 "items": { 

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

110 }, 

111 }, 

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

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

114 }, 

115 }, 

116 } 

117 }, 

118 }