Coverage for tests/test_tutorial/test_header_params/test_tutorial002.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.tutorial002 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, {"strange_header": None}), 

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

15 ( 

16 "/items", 

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

18 200, 

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

20 ), 

21 ( 

22 "/items", 

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

24 200, 

25 {"strange_header": None}, 

26 ), 

27 ], 

28) 

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

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

31 assert response.status_code == expected_status 1abcde

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

33 

34 

35def test_openapi_schema(): 1abcde

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

37 assert response.status_code == 200 1abcde

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

39 "openapi": "3.1.0", 

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

41 "paths": { 

42 "/items/": { 

43 "get": { 

44 "responses": { 

45 "200": { 

46 "description": "Successful Response", 

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

48 }, 

49 "422": { 

50 "description": "Validation Error", 

51 "content": { 

52 "application/json": { 

53 "schema": { 

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

55 } 

56 } 

57 }, 

58 }, 

59 }, 

60 "summary": "Read Items", 

61 "operationId": "read_items_items__get", 

62 "parameters": [ 

63 { 

64 "required": False, 

65 "schema": IsDict( 

66 { 

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

68 "title": "Strange Header", 

69 } 

70 ) 

71 | IsDict( 

72 # TODO: remove when deprecating Pydantic v1 

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

74 ), 

75 "name": "strange_header", 

76 "in": "header", 

77 } 

78 ], 

79 } 

80 } 

81 }, 

82 "components": { 

83 "schemas": { 

84 "ValidationError": { 

85 "title": "ValidationError", 

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

87 "type": "object", 

88 "properties": { 

89 "loc": { 

90 "title": "Location", 

91 "type": "array", 

92 "items": { 

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

94 }, 

95 }, 

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

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

98 }, 

99 }, 

100 "HTTPValidationError": { 

101 "title": "HTTPValidationError", 

102 "type": "object", 

103 "properties": { 

104 "detail": { 

105 "title": "Detail", 

106 "type": "array", 

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

108 } 

109 }, 

110 }, 

111 } 

112 }, 

113 }