Skip to content

Commit

Permalink
Add .query and .queries to request
Browse files Browse the repository at this point in the history
  • Loading branch information
rawleyfowler committed May 16, 2023
1 parent 2b82a87 commit 3660216
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
2 changes: 1 addition & 1 deletion META6.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@
"Test::Util::ServerPort",
"Cro::HTTP::Client"
],
"version": "2.1.3"
"version": "2.1.6"
}
13 changes: 10 additions & 3 deletions lib/Humming-Bird/Core.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use Humming-Bird::HTTPServer;

unit module Humming-Bird::Core;

our constant $VERSION = '2.1.3';
our constant $VERSION = '2.1.5';

# Mime type parser from MIME::Types
my constant $mime = MIME::Types.new;
Expand Down Expand Up @@ -138,7 +138,14 @@ class Request is HTTPAction is export {
%!params{$param};
}

method query(Str:D $query_param --> Str) {
method queries {
return %!query;
}

multi method query {
return %!query;
}
multi method query(Str:D $query_param --> Str) {
return Nil without %!query{$query_param};
%!query{$query_param};
}
Expand All @@ -153,7 +160,7 @@ class Request is HTTPAction is export {

# Find query params
my %query;
if uri_decode_component(@lines[0]) ~~ m:g /<[a..z A..Z 0..9]>+"="<[a..z A..Z 0..9]>+/ {
if uri_decode_component($path) ~~ m:g /\w+"="(<-[&]>+)/ {
%query = Map.new($<>.map({ .split('=', 2) }).flat);
$path = $path.split('?', 2)[0];
}
Expand Down
28 changes: 28 additions & 0 deletions t/11-advanced-query.rakutest
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use v6;
use strict;
use lib 'lib';

use Test;
use Humming-Bird::Core;

plan 10;

my $simple_raw_request = "GET /?foo=bar%40baz HTTP/1.1\r\nHost: bob.com\r\n";
my $simple_request = Request.decode($simple_raw_request);

ok $simple_request.method === GET, 'Is method OK?';
is $simple_request.version, 'HTTP/1.1', 'Is version OK?';
is $simple_request.path, '/', 'Is path OK?';
is $simple_request.query('foo'), 'bar@baz', 'Is query param correct?';

my $advanced_raw_request = "GET /?foo=bar%40baz&j=123%40abc HTTP/1.1\r\nHost: bob.com\r\n";
my $advanced_request = Request.decode: $advanced_raw_request;

ok $advanced_request.method === GET, 'Is method OK?';
is $advanced_request.version, 'HTTP/1.1', 'Is version OK?';
is $advanced_request.path, '/', 'Is path OK?';
is $advanced_request.query('foo'), 'bar@baz', 'Is first of query params correct?';
is $advanced_request.query('j'), '123@abc', 'Is second of query params correct?';
is $advanced_request.queries, { j => '123@abc', foo => 'bar@baz' }, 'Is queries hash correct?';

done-testing;

0 comments on commit 3660216

Please sign in to comment.