Skip to content

Commit e276751

Browse files
author
Fernando Corrêa de Oliveira
committed
Make http function accept a list of http methods
1 parent 5d74c63 commit e276751

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

Changes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Revision history for Cro::HTTP
22

33
{{NEXT}}
44
- Support link generation
5+
- Make http function accept a list of http methods
56

67
0.8.11
78
- Avoid sending a 0-byte WINDOW_UPDATE frame.

lib/Cro/HTTP/Router.rakumod

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,22 @@ module Cro::HTTP::Router {
13421342
$*CRO-ROUTE-SET.add-around(&cb);
13431343
}
13441344

1345+
#| Add a request handler for a list of HTTP methods. This is useful
1346+
#| when there is the need to add a handler to multiple http methods
1347+
multi http(@methods, &handler --> Nil) is export {
1348+
for @methods -> Str $method {
1349+
http $method, &handler
1350+
}
1351+
}
1352+
1353+
#| Add a request handler for a list of HTTP methods. This is useful
1354+
#| when there is the need to add a handler to multiple http methods
1355+
multi http(&name, @methods, &handler --> Nil) is export {
1356+
for @methods -> Str $method {
1357+
http name($method), $method, &handler
1358+
}
1359+
}
1360+
13451361
#| Add a request handler for the specified HTTP method. This is useful
13461362
#| when there is no shortcut function available for the HTTP method.
13471363
multi http($name, $method, &handler --> Nil) is export {

t/http-router-named-urls.t

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ test-route-urls route {
1919
is abs-link('noqs'), '/baz', 'Non-path related parameters were not counted';
2020
is abs-link('auth'), '/auth', "Auth parameter is ignored when creating uri";
2121
is abs-link('auth-type'), '/auth-type', "Parameter with type that does Auth is ignored when creating uri";
22+
is abs-link('get-multi'), '/multi', "GET option to multi method named endpoint";
23+
is abs-link('post-multi'), '/multi', "POST option to multi method named endpoint";
24+
is abs-link('put-multi'), '/multi', "PUT option to multi method named endpoint";
25+
is abs-link('delete-multi'), '/multi', "DELETE option to multi method named endpoint";
2226
};
2327

2428
get :name<lit>, -> 'foo', 'bar' { }
@@ -28,6 +32,7 @@ test-route-urls route {
2832
get :name<auth>, -> $auth is auth, 'auth' { }
2933
class AuthType does Cro::HTTP::Auth { }
3034
get :name<auth-type>, -> AuthType $a, 'auth-type' { }
35+
http -> $method { "{ $method.lc }-multi" }, <GET POST PUT DELETE>, -> 'multi' { }
3136
}
3237

3338
test-route-urls route {

xt/http-custom-methods.rakutest

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ my $app = route {
1919
http 'CUSTOM', -> {
2020
content 'text/plain', 'CUSTOM';
2121
}
22+
http <GET CUSTOM>, -> "list" {
23+
content 'text/plain', 'GET or CUSTOM';
24+
}
2225
}
2326

2427
{
@@ -42,6 +45,16 @@ my $app = route {
4245
ok $resp ~~ Cro::HTTP::Response, 'CUSTOM using http method works';
4346
is await($resp.body-text), 'CUSTOM', 'Body text is correct';
4447
}
48+
49+
given await $c.get("$base/list") -> $resp {
50+
ok $resp ~~ Cro::HTTP::Response, 'GET using http method works';
51+
is await($resp.body-text), 'GET or CUSTOM', 'Body text is correct';
52+
}
53+
54+
given await $c.request('CUSTOM', "$base/list") -> $resp {
55+
ok $resp ~~ Cro::HTTP::Response, 'CUSTOM using http method works';
56+
is await($resp.body-text), 'GET or CUSTOM', 'Body text is correct';
57+
}
4558
}
4659

4760
if supports-alpn() {
@@ -67,6 +80,16 @@ if supports-alpn() {
6780
ok $resp ~~ Cro::HTTP::Response, 'CUSTOM using http method works';
6881
is await($resp.body-text), 'CUSTOM', 'Body text is correct';
6982
}
83+
84+
given await $c.get("$base/list", :%ca) -> $resp {
85+
ok $resp ~~ Cro::HTTP::Response, 'GET using http method works';
86+
is await($resp.body-text), 'GET or CUSTOM', 'Body text is correct';
87+
}
88+
89+
given await $c.request('CUSTOM', "$base/list", :%ca) -> $resp {
90+
ok $resp ~~ Cro::HTTP::Response, 'CUSTOM using http method works';
91+
is await($resp.body-text), 'GET or CUSTOM', 'Body text is correct';
92+
}
7093
}
7194

7295
done-testing;

0 commit comments

Comments
 (0)