Coverage for tests/test_tutorial/test_header_params/test_tutorial001_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.tutorial001_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, {"User-Agent": "testclient"}), 

21 ("/items", {"X-Header": "notvalid"}, 200, {"User-Agent": "testclient"}), 

22 ("/items", {"User-Agent": "FastAPI test"}, 200, {"User-Agent": "FastAPI test"}), 

23 ], 

24) 

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

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

27 assert response.status_code == expected_status 1abc

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

29 

30 

31@needs_py310 1deabc

32def test_openapi_schema(client: TestClient): 1deabc

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

34 assert response.status_code == 200 1abc

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

36 "openapi": "3.1.0", 

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

38 "paths": { 

39 "/items/": { 

40 "get": { 

41 "responses": { 

42 "200": { 

43 "description": "Successful Response", 

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

45 }, 

46 "422": { 

47 "description": "Validation Error", 

48 "content": { 

49 "application/json": { 

50 "schema": { 

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

52 } 

53 } 

54 }, 

55 }, 

56 }, 

57 "summary": "Read Items", 

58 "operationId": "read_items_items__get", 

59 "parameters": [ 

60 { 

61 "required": False, 

62 "schema": IsDict( 

63 { 

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

65 "title": "User-Agent", 

66 } 

67 ) 

68 | IsDict( 

69 # TODO: remove when deprecating Pydantic v1 

70 {"title": "User-Agent", "type": "string"} 

71 ), 

72 "name": "user-agent", 

73 "in": "header", 

74 } 

75 ], 

76 } 

77 } 

78 }, 

79 "components": { 

80 "schemas": { 

81 "ValidationError": { 

82 "title": "ValidationError", 

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

84 "type": "object", 

85 "properties": { 

86 "loc": { 

87 "title": "Location", 

88 "type": "array", 

89 "items": { 

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

91 }, 

92 }, 

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

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

95 }, 

96 }, 

97 "HTTPValidationError": { 

98 "title": "HTTPValidationError", 

99 "type": "object", 

100 "properties": { 

101 "detail": { 

102 "title": "Detail", 

103 "type": "array", 

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

105 } 

106 }, 

107 }, 

108 } 

109 }, 

110 }