11from unittest import TestCase
22from fastapi import FastAPI , APIRouter
3- from osbot_fast_api .api .routes .Fast_API__Routes import Fast_API__Routes
3+ from osbot_fast_api .api .routes .Fast_API__Routes import Fast_API__Routes
44from osbot_fast_api .schemas .Safe_Str__Fast_API__Route__Prefix import Safe_Str__Fast_API__Route__Prefix
55from osbot_fast_api .schemas .Safe_Str__Fast_API__Route__Tag import Safe_Str__Fast_API__Route__Tag
66from osbot_fast_api .utils .Fast_API_Utils import Fast_API_Utils
@@ -116,4 +116,82 @@ def test__tag_and_prefix__edge_cases(self):
116116 with Fast_API__Routes (tag = Safe_Str__Fast_API__Route__Tag ('USERS' ) ,
117117 prefix = Safe_Str__Fast_API__Route__Prefix ('/API/V2/USERS' )) as _ :
118118 assert _ .tag == 'USERS' # Tag keeps case
119- assert _ .prefix == '/api/v2/users' # Prefix lowercase
119+ assert _ .prefix == '/api/v2/users' # Prefix lowercase
120+
121+ def test_add_route_any (self ): # Test with default path parsing
122+ def handle_any ():
123+ return "any method"
124+
125+
126+ assert self .fast_api_routes .add_route_any (handle_any ) is self .fast_api_routes
127+ routes = self .fast_api_routes .routes ()
128+
129+ assert len (routes ) == 1
130+ assert routes [0 ]['http_path' ] == '/handle-any'
131+ assert routes [0 ]['method_name' ] == 'handle_any'
132+ assert set (routes [0 ]['http_methods' ]) == {'DELETE' , 'GET' , 'HEAD' , 'OPTIONS' , 'PATCH' , 'POST' , 'PUT' }
133+
134+ def test_add_route_any_with_explicit_path (self ): # Test with explicit path - common for proxy routes
135+ def proxy_request (path : str ):
136+ return f"proxied: { path } "
137+
138+ assert self .fast_api_routes .add_route_any (proxy_request , "/{path:path}" ) is self .fast_api_routes
139+ routes = self .fast_api_routes .routes ()
140+ assert len (routes ) == 1
141+ assert routes [0 ]['http_path' ] == '/{path:path}'
142+ assert routes [0 ]['method_name' ] == 'proxy_request'
143+ assert set (routes [0 ]['http_methods' ]) == {'DELETE' , 'GET' , 'HEAD' , 'OPTIONS' , 'PATCH' , 'POST' , 'PUT' }
144+
145+ def test_add_route_any_with_multiple_routes (self ): # Test adding multiple ANY routes
146+ def catch_all (): pass
147+ def api_gateway (): pass
148+ def proxy (path : str ): pass
149+
150+ self .fast_api_routes .add_route_any (catch_all )
151+ self .fast_api_routes .add_route_any (api_gateway )
152+ self .fast_api_routes .add_route_any (proxy , "/api/{path:path}" )
153+
154+ routes = self .fast_api_routes .routes ()
155+ assert len (routes ) == 3
156+
157+ # Check each route
158+ assert routes [0 ]['http_path' ] == '/catch-all'
159+ assert routes [1 ]['http_path' ] == '/api-gateway'
160+ assert routes [2 ]['http_path' ] == '/api/{path:path}'
161+
162+ # All should have same methods
163+ for route in routes :
164+ assert set (route ['http_methods' ]) == {'DELETE' , 'GET' , 'HEAD' , 'OPTIONS' , 'PATCH' , 'POST' , 'PUT' }
165+
166+ def test_add_route_any_with_path_parameters (self ): # Test various path parameter patterns
167+ def get_item (item_id : int ): pass
168+ def get_user_post (user_id : str , post_id : int ): pass
169+
170+ self .fast_api_routes .add_route_any (get_item , "/items/{item_id}" )
171+ self .fast_api_routes .add_route_any (get_user_post , "/users/{user_id}/posts/{post_id}" )
172+
173+ routes = self .fast_api_routes .routes ()
174+ assert routes [0 ]['http_path' ] == '/items/{item_id}'
175+ assert routes [1 ]['http_path' ] == '/users/{user_id}/posts/{post_id}'
176+
177+ def test_add_route_any_edge_cases (self ): # Test with root path
178+ def root_handler (): pass
179+ self .fast_api_routes .add_route_any (root_handler , "/" )
180+
181+ # Test with trailing slash
182+ def api_handler (): pass
183+ self .fast_api_routes .add_route_any (api_handler , "/api/" )
184+
185+ routes = self .fast_api_routes .routes ()
186+ assert routes [0 ]['http_path' ] == '/'
187+ assert routes [1 ]['http_path' ] == '/api/'
188+
189+ def test__bug__any_route_loses_colon (self ):
190+ def root_handler (): pass
191+ with Fast_API__Routes () as _ :
192+ _ .add_route_any (root_handler , "/{path:path" )
193+ assert _ .routes_paths () == ['/{path:path' ]
194+ with self .fast_api_routes as _ :
195+ _ .add_route_any (root_handler , "/{path:path" )
196+ assert _ .routes_paths () == ['/{path:path' ]
197+
0 commit comments