Skip to content

Commit

Permalink
Merge pull request #773 from eaguad1337/feat/any-route
Browse files Browse the repository at this point in the history
feat #772: add Route.any facade
  • Loading branch information
josephmancuso authored Oct 28, 2023
2 parents 77c14bb + 74cb801 commit 029397d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/masonite/routes/Route.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,16 @@ def api(self, base_url, controller):
),
]

@classmethod
def any(self, url, controller, module_location=None, **options):
return self.match(
("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"),
url,
controller,
module_location=module_location,
**options,
)

@classmethod
def compile(self, key, to=""):
self.compilers.update({key: to})
Expand Down
3 changes: 3 additions & 0 deletions tests/integrations/controllers/WelcomeController.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ def show_user(self, request: Request, view: View):
user = User.find_or_fail(request.param("id"))
return view.render("welcome")

def any(self, request: Request):
return "any"

def dd(self, request: Request):
dump({"test": "value"})
dd(request)
Expand Down
2 changes: 2 additions & 0 deletions tests/integrations/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
Route.get("/users/@id", "WelcomeController@show_user").name("users.profile"),
Route.view("/test_view", "test_view", {"show": "111"}),
Route.get("/api/uploads/", "WelcomeController@test").middleware("throttle:api"),

Route.any("/any", "WelcomeController@any"),
]

Broadcast.routes()
Expand Down
12 changes: 11 additions & 1 deletion tests/routes/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def test_route_prefix(self):
Route.group([
Route.get("/route", "WelcomeController@show").name("testparam")
], prefix="params")

)

route = router.find("/params/route", "get")
Expand Down Expand Up @@ -340,3 +340,13 @@ def test_head_method_is_registered_for_get_routes(self):
route = router.find("/home", "HEAD")
self.assertIsNotNone(route)
self.assertEqual(route.request_method, ["get", "head"])

def test_any_method_matches_any_request_method(self):
router = Router(
[
Route.any("/any_method", "WelcomeController@any"),
]
)
route = router.find("/any_method", "GET")
self.assertIsNotNone(route)
self.assertEqual(route.request_method, ['get', 'post', 'put', 'patch', 'delete', 'options', 'head'])

0 comments on commit 029397d

Please sign in to comment.