Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 16 additions & 0 deletions lib/Cro/HTTP/Router.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions t/http-router-named-urls.t
Original file line number Diff line number Diff line change
Expand Up @@ -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<lit>, -> 'foo', 'bar' { }
Expand All @@ -28,6 +32,7 @@ test-route-urls route {
get :name<auth>, -> $auth is auth, 'auth' { }
class AuthType does Cro::HTTP::Auth { }
get :name<auth-type>, -> AuthType $a, 'auth-type' { }
http -> $method { "{ $method.lc }-multi" }, <GET POST PUT DELETE>, -> 'multi' { }
}

test-route-urls route {
Expand Down
23 changes: 23 additions & 0 deletions xt/http-custom-methods.rakutest
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ my $app = route {
http 'CUSTOM', -> {
content 'text/plain', 'CUSTOM';
}
http <GET CUSTOM>, -> "list" {
content 'text/plain', 'GET or CUSTOM';
}
}

{
Expand All @@ -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() {
Expand All @@ -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;