diff --git a/Changes b/Changes index de6efd6..a4cd806 100644 --- a/Changes +++ b/Changes @@ -2,6 +2,7 @@ Revision history for Cro::HTTP {{NEXT}} - Support link generation + - Make http function accept a list of http methods 0.8.11 - Avoid sending a 0-byte WINDOW_UPDATE frame. diff --git a/lib/Cro/HTTP/Router.rakumod b/lib/Cro/HTTP/Router.rakumod index ae3ec04..5f90e96 100644 --- a/lib/Cro/HTTP/Router.rakumod +++ b/lib/Cro/HTTP/Router.rakumod @@ -1342,6 +1342,22 @@ module Cro::HTTP::Router { $*CRO-ROUTE-SET.add-around(&cb); } + #| Add a request handler for a list of HTTP methods. This is useful + #| when there is the need to add a handler to multiple http methods + multi http(@methods, &handler --> Nil) is export { + for @methods -> Str $method { + http $method, &handler + } + } + + #| Add a request handler for a list of HTTP methods. This is useful + #| when there is the need to add a handler to multiple http methods + multi http(&name, @methods, &handler --> Nil) is export { + for @methods -> Str $method { + http name($method), $method, &handler + } + } + #| Add a request handler for the specified HTTP method. This is useful #| when there is no shortcut function available for the HTTP method. multi http($name, $method, &handler --> Nil) is export { diff --git a/t/http-router-named-urls.t b/t/http-router-named-urls.t index 67c6ecf..f6a4ca5 100644 --- a/t/http-router-named-urls.t +++ b/t/http-router-named-urls.t @@ -19,6 +19,10 @@ test-route-urls route { is abs-link('noqs'), '/baz', 'Non-path related parameters were not counted'; is abs-link('auth'), '/auth', "Auth parameter is ignored when creating uri"; is abs-link('auth-type'), '/auth-type', "Parameter with type that does Auth is ignored when creating uri"; + is abs-link('get-multi'), '/multi', "GET option to multi method named endpoint"; + is abs-link('post-multi'), '/multi', "POST option to multi method named endpoint"; + is abs-link('put-multi'), '/multi', "PUT option to multi method named endpoint"; + is abs-link('delete-multi'), '/multi', "DELETE option to multi method named endpoint"; }; get :name, -> 'foo', 'bar' { } @@ -28,6 +32,7 @@ test-route-urls route { get :name, -> $auth is auth, 'auth' { } class AuthType does Cro::HTTP::Auth { } get :name, -> AuthType $a, 'auth-type' { } + http -> $method { "{ $method.lc }-multi" }, , -> 'multi' { } } test-route-urls route { diff --git a/xt/http-custom-methods.rakutest b/xt/http-custom-methods.rakutest index baaa05b..72e6f79 100644 --- a/xt/http-custom-methods.rakutest +++ b/xt/http-custom-methods.rakutest @@ -19,6 +19,9 @@ my $app = route { http 'CUSTOM', -> { content 'text/plain', 'CUSTOM'; } + http , -> "list" { + content 'text/plain', 'GET or CUSTOM'; + } } { @@ -42,6 +45,16 @@ my $app = route { ok $resp ~~ Cro::HTTP::Response, 'CUSTOM using http method works'; is await($resp.body-text), 'CUSTOM', 'Body text is correct'; } + + given await $c.get("$base/list") -> $resp { + ok $resp ~~ Cro::HTTP::Response, 'GET using http method works'; + is await($resp.body-text), 'GET or CUSTOM', 'Body text is correct'; + } + + given await $c.request('CUSTOM', "$base/list") -> $resp { + ok $resp ~~ Cro::HTTP::Response, 'CUSTOM using http method works'; + is await($resp.body-text), 'GET or CUSTOM', 'Body text is correct'; + } } if supports-alpn() { @@ -67,6 +80,16 @@ if supports-alpn() { ok $resp ~~ Cro::HTTP::Response, 'CUSTOM using http method works'; is await($resp.body-text), 'CUSTOM', 'Body text is correct'; } + + given await $c.get("$base/list", :%ca) -> $resp { + ok $resp ~~ Cro::HTTP::Response, 'GET using http method works'; + is await($resp.body-text), 'GET or CUSTOM', 'Body text is correct'; + } + + given await $c.request('CUSTOM', "$base/list", :%ca) -> $resp { + ok $resp ~~ Cro::HTTP::Response, 'CUSTOM using http method works'; + is await($resp.body-text), 'GET or CUSTOM', 'Body text is correct'; + } } done-testing;