Coverage for tests/asyncapi/test_asgi.py: 83%

23 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-17 21:20 +0000

1from faststream.asgi import AsgiFastStream, AsgiResponse, get, make_ping_asgi 

2from faststream.kafka import KafkaBroker 

3from faststream.specification import AsyncAPI 

4 

5 

6def test_asgi_v2_6_0() -> None: 

7 broker = KafkaBroker() 

8 

9 @get 

10 async def handler() -> AsgiResponse: 

11 return AsgiResponse() 

12 

13 @get(include_in_schema=False) 

14 async def handler2() -> AsgiResponse: 

15 return AsgiResponse() 

16 

17 app = AsgiFastStream( 

18 broker, 

19 asgi_routes=[ 

20 ( 

21 "/test", 

22 make_ping_asgi( 

23 broker, 

24 description="test description", 

25 tags=[{"name": "test"}], 

26 ), 

27 ), 

28 ("/test2", handler), 

29 ("/test3", handler2), 

30 ], 

31 specification=AsyncAPI(schema_version="2.6.0"), 

32 ) 

33 

34 schema = app.schema.to_specification().to_jsonable() 

35 

36 assert schema["channels"] == { 

37 "/test": { 

38 "description": "test description", 

39 "subscribe": { 

40 "bindings": { 

41 "http": {"method": "GET, HEAD", "bindingVersion": "0.1.0"}, 

42 }, 

43 "tags": [{"name": "test"}], 

44 }, 

45 }, 

46 "/test2": { 

47 "subscribe": { 

48 "bindings": { 

49 "http": {"method": "GET, HEAD", "bindingVersion": "0.1.0"}, 

50 }, 

51 }, 

52 }, 

53 } 

54 

55 

56def test_asgi_v3_0_0() -> None: 

57 broker = KafkaBroker() 

58 

59 @get 

60 async def handler() -> AsgiResponse: 

61 return AsgiResponse() 

62 

63 @get(include_in_schema=False) 

64 async def handler2() -> AsgiResponse: 

65 return AsgiResponse() 

66 

67 app = AsgiFastStream( 

68 broker, 

69 asgi_routes=[ 

70 ( 

71 "/test", 

72 make_ping_asgi( 

73 broker, 

74 description="test description", 

75 tags=[{"name": "test"}], 

76 ), 

77 ), 

78 ("/test2", handler), 

79 ("/test3", handler2), 

80 ], 

81 specification=AsyncAPI(schema_version="3.0.0"), 

82 ) 

83 

84 schema = app.schema.to_specification().to_jsonable() 

85 

86 assert schema["channels"] == { 

87 "test:HttpChannel": { 

88 "address": "/test", 

89 "description": "test description", 

90 "messages": {}, 

91 }, 

92 "test2:HttpChannel": {"address": "/test2", "messages": {}}, 

93 } 

94 assert schema["operations"] == { 

95 "test:HttpChannel": { 

96 "action": "receive", 

97 "channel": {"$ref": "#/channels/test:HttpChannel"}, 

98 "bindings": {"http": {"method": "GET", "bindingVersion": "0.3.0"}}, 

99 "messages": [], 

100 }, 

101 "test2:HttpChannel": { 

102 "action": "receive", 

103 "channel": {"$ref": "#/channels/test2:HttpChannel"}, 

104 "bindings": {"http": {"method": "GET", "bindingVersion": "0.3.0"}}, 

105 "messages": [], 

106 }, 

107 }