Skip to content

Commit 164b9ef

Browse files
author
Fernando Corrêa de Oliveira
committed
feat: Make http function accept a list of http methods
1 parent 35a6c7d commit 164b9ef

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

Changes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
Revision history for Cro::HTTP
22

3+
{{NEXT}}
4+
- Make http function accept a list of http methods
5+
36
0.8.11
47
- Avoid sending a 0-byte WINDOW_UPDATE frame.
58
- Permit use of updated HTTP::Pack module, Samuel Gillespie++

lib/Cro/HTTP/Router.rakumod

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1282,9 +1282,17 @@ module Cro::HTTP::Router {
12821282
$*CRO-ROUTE-SET.add-around(&cb);
12831283
}
12841284

1285+
#| Add a request handler for a list of HTTP methods. This is useful
1286+
#| when there is the need to add a handler to multiple http methods
1287+
multi http(@methods, &handler --> Nil) is export {
1288+
for @methods -> Str $method {
1289+
http $method, &handler
1290+
}
1291+
}
1292+
12851293
#| Add a request handler for the specified HTTP method. This is useful
12861294
#| when there is no shortcut function available for the HTTP method.
1287-
sub http($method, &handler --> Nil) is export {
1295+
multi http($method, &handler --> Nil) is export {
12881296
$*CRO-ROUTE-SET.add-handler($method, &handler);
12891297
}
12901298

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)