Coverage for tests / test_forms_single_param.py: 100%

17 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1from typing import Annotated 1abcd

2 

3from fastapi import FastAPI, Form 1abcd

4from fastapi.testclient import TestClient 1abcd

5from inline_snapshot import snapshot 1abcd

6 

7app = FastAPI() 1abcd

8 

9 

10@app.post("/form/") 1abcd

11def post_form(username: Annotated[str, Form()]): 1abcd

12 return username 1efg

13 

14 

15client = TestClient(app) 1abcd

16 

17 

18def test_single_form_field(): 1abcd

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

20 assert response.status_code == 200, response.text 1efg

21 assert response.json() == "Rick" 1efg

22 

23 

24def test_openapi_schema(): 1abcd

25 response = client.get("/openapi.json") 1hij

26 assert response.status_code == 200, response.text 1hij

27 assert response.json() == snapshot( 1hij

28 { 

29 "openapi": "3.1.0", 

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

31 "paths": { 

32 "/form/": { 

33 "post": { 

34 "summary": "Post Form", 

35 "operationId": "post_form_form__post", 

36 "requestBody": { 

37 "content": { 

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

39 "schema": { 

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

41 } 

42 } 

43 }, 

44 "required": True, 

45 }, 

46 "responses": { 

47 "200": { 

48 "description": "Successful Response", 

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

50 }, 

51 "422": { 

52 "description": "Validation Error", 

53 "content": { 

54 "application/json": { 

55 "schema": { 

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

57 } 

58 } 

59 }, 

60 }, 

61 }, 

62 } 

63 } 

64 }, 

65 "components": { 

66 "schemas": { 

67 "Body_post_form_form__post": { 

68 "properties": { 

69 "username": {"type": "string", "title": "Username"} 

70 }, 

71 "type": "object", 

72 "required": ["username"], 

73 "title": "Body_post_form_form__post", 

74 }, 

75 "HTTPValidationError": { 

76 "properties": { 

77 "detail": { 

78 "items": { 

79 "$ref": "#/components/schemas/ValidationError" 

80 }, 

81 "type": "array", 

82 "title": "Detail", 

83 } 

84 }, 

85 "type": "object", 

86 "title": "HTTPValidationError", 

87 }, 

88 "ValidationError": { 

89 "properties": { 

90 "ctx": {"title": "Context", "type": "object"}, 

91 "input": {"title": "Input"}, 

92 "loc": { 

93 "items": { 

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

95 }, 

96 "type": "array", 

97 "title": "Location", 

98 }, 

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

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

101 }, 

102 "type": "object", 

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

104 "title": "ValidationError", 

105 }, 

106 } 

107 }, 

108 } 

109 )