Coverage for tests/test_forms_single_param.py: 100%

16 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-01-13 13:38 +0000

1from fastapi import FastAPI, Form 1abcde

2from fastapi.testclient import TestClient 1abcde

3from typing_extensions import Annotated 1abcde

4 

5app = FastAPI() 1abcde

6 

7 

8@app.post("/form/") 1abcde

9def post_form(username: Annotated[str, Form()]): 1abcde

10 return username 1fghij

11 

12 

13client = TestClient(app) 1abcde

14 

15 

16def test_single_form_field(): 1abcde

17 response = client.post("/form/", data={"username": "Rick"}) 1fghij

18 assert response.status_code == 200, response.text 1fghij

19 assert response.json() == "Rick" 1fghij

20 

21 

22def test_openapi_schema(): 1abcde

23 response = client.get("/openapi.json") 1klmno

24 assert response.status_code == 200, response.text 1klmno

25 assert response.json() == { 1klmno

26 "openapi": "3.1.0", 

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

28 "paths": { 

29 "/form/": { 

30 "post": { 

31 "summary": "Post Form", 

32 "operationId": "post_form_form__post", 

33 "requestBody": { 

34 "content": { 

35 "application/x-www-form-urlencoded": { 

36 "schema": { 

37 "$ref": "#/components/schemas/Body_post_form_form__post" 

38 } 

39 } 

40 }, 

41 "required": True, 

42 }, 

43 "responses": { 

44 "200": { 

45 "description": "Successful Response", 

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

47 }, 

48 "422": { 

49 "description": "Validation Error", 

50 "content": { 

51 "application/json": { 

52 "schema": { 

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

54 } 

55 } 

56 }, 

57 }, 

58 }, 

59 } 

60 } 

61 }, 

62 "components": { 

63 "schemas": { 

64 "Body_post_form_form__post": { 

65 "properties": {"username": {"type": "string", "title": "Username"}}, 

66 "type": "object", 

67 "required": ["username"], 

68 "title": "Body_post_form_form__post", 

69 }, 

70 "HTTPValidationError": { 

71 "properties": { 

72 "detail": { 

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

74 "type": "array", 

75 "title": "Detail", 

76 } 

77 }, 

78 "type": "object", 

79 "title": "HTTPValidationError", 

80 }, 

81 "ValidationError": { 

82 "properties": { 

83 "loc": { 

84 "items": { 

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

86 }, 

87 "type": "array", 

88 "title": "Location", 

89 }, 

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

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

92 }, 

93 "type": "object", 

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

95 "title": "ValidationError", 

96 }, 

97 } 

98 }, 

99 }