Skip to content

Commit

Permalink
improve test-coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
rawleyfowler committed Nov 1, 2023
2 parents 032fc25 + 356f1f6 commit ba156f1
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 36 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.6"
"version": "2.1.7"
}
66 changes: 34 additions & 32 deletions lib/Humming-Bird/Core.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use Humming-Bird::HTTPServer;

unit module Humming-Bird::Core;

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

# Mime type parser from MIME::Types
my constant $mime = MIME::Types.new;
Expand Down Expand Up @@ -39,7 +39,7 @@ sub http-method-of-str(Str:D $method --> HTTPMethod:D) {

# Converts a string of headers "KEY: VALUE\r\nKEY: VALUE\r\n..." to a map of headers.
sub decode_headers(Str:D $header_block --> Map:D) {
Map.new($header_block.lines.map({ .split(": ", :skip-empty) }).flat);
Map.new($header_block.lines.map({ .split(": ", 2, :skip-empty) }).flat);
}

subset SameSite of Str where 'Strict' | 'Lax';
Expand Down Expand Up @@ -99,12 +99,9 @@ class HTTPAction {
}

my sub parse-urlencoded(Str:D $urlencoded --> Map:D) {
<<<<<<< Updated upstream
use URI::Encode;
uri_decode_component($urlencoded).split('&', :skip-empty)>>.split('=', :skip-empty)>>.map(-> $a, $b { $b.contains(',') ?? slip $a => $b.split(',', :skip-empty) !! slip $a => $b }).flat.Map;
=======
$urlencoded.split('&', :skip-empty)>>.split('=', 2, :skip-empty)>>.map(-> $a, $b { $b.contains(',') ?? slip $a => $b.split(',', :skip-empty) !! slip $a => $b }).flat.Map;
>>>>>>> Stashed changes
$urlencoded.split('&', :skip-empty).map(&uri_decode_component)>>.split('=', 2, :skip-empty)>>.map(-> $a, $b { $b.contains(',') ?? slip $a => $b.split(',', :skip-empty) !! slip $a => $b })
.flat
.Map;
}

class Request is HTTPAction is export {
Expand All @@ -125,7 +122,12 @@ class Request is HTTPAction is export {
return $!content = Map.new unless self.header('Content-Type');

try {
CATCH { default { warn "Failed trying to parse a body of type { self.header('Content-Type') }"; return ($!content = Map.new) } }
CATCH {
default {
warn "Encountered Error: $_;\n\n Failed trying to parse a body of type { self.header('Content-Type') }"; return ($!content = Map.new)
}
}

if self.header('Content-Type').ends-with: 'json' {
$!content = from-json(self.body).Map;
} elsif self.header('Content-Type').ends-with: 'urlencoded' {
Expand Down Expand Up @@ -487,18 +489,18 @@ sub dispatch-request(Request:D $request --> Response:D) {

return NOT-FOUND($request);
} elsif $possible-param && !%loc{$uri} {
$request.params{~$possible-param.match(/<[A..Z a..z 0..9 \- \_]>+/)} = $uri;
%loc := %loc{$possible-param};
} else {
$request.params{~$possible-param.match(/<[A..Z a..z 0..9 \- \_]>+/)} = $uri;
%loc := %loc{$possible-param};
} else {
%loc := %loc{$uri};
}

# If the route could possibly be static
with %loc{$request.method} {
if %loc{$request.method}.static {
return %loc{$request.method}($request);
}
}
if %loc{$request.method}.static {
return %loc{$request.method}($request);
}
}
}

# For HEAD requests we should return a GET request. The decoder will delete the body
Expand All @@ -512,8 +514,8 @@ sub dispatch-request(Request:D $request --> Response:D) {

# If we don't support the request method on this route.
without %loc{$request.method} {
return METHOD-NOT-ALLOWED($request);
}
return METHOD-NOT-ALLOWED($request);
}

try {
# This is how we pass to error handlers.
Expand Down Expand Up @@ -667,40 +669,40 @@ requests at the moment.
=head3 group
=for code
# Add middleware to a few routes
group([
&get.assuming('/', -> $request, $response {
$response.html('Index');
}),
# Add middleware to a few routes
group([
&get.assuming('/', -> $request, $response {
$response.html('Index');
}),
&get.assuming('/other', -> $request, $response {
$response.html('Other');
})
], [ &m_logger, &my_middleware ]);
&get.assuming('/other', -> $request, $response {
$response.html('Other');
})
], [ &m_logger, &my_middleware ]);
Group registers multiple routes functionally via partial application. This allows you to
group as many different routes together and feed them a C<List> of middleware in the last parameter.
Group takes a C<List> of route functions partially applied to their route and callback, then a C<List>
of middleware to apply to the routes.
of middleware to apply to the routes.
=head3 listen
=for code
listen(8080);
listen(8080);
Start the server, after you've declared your routes. It will listen in a given port.
=head3 routes
=for code
routes();
routes();
Returns a read-only version of the currently stored routes.
=head3 HTTPMethod
Simply an ENUM that contains the major HTTP methods allowed by Humming-Bird.
Simply an ENUM that contains the major HTTP methods allowed by Humming-Bird.
=end pod

# vim: expandtab shiftwidth=4
# vim: expandtab shiftwidth=4
2 changes: 1 addition & 1 deletion lib/Humming-Bird/HTTPServer.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Humming-Bird::HTTPServer is export {

my $content-length = $data.elems - $index;
for @header-lines -> $header {
my ($key, $value) = $header.split(': ', :skip-empty);
my ($key, $value) = $header.split(': ', 2, :skip-empty);
given $key.lc {
when 'content-length' {
$content-length = +$value // ($data.elems - $index);
Expand Down
8 changes: 7 additions & 1 deletion t/10-content-guessing.rakutest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use v6.d;
use Humming-Bird::Core;
use Test;

plan 4;
plan 7;

my $request = Request.new(body => '{ "foo": "bar" }', path => '/home', method => GET, version => 'HTTP/1.1');
$request.header('Content-Type', 'application/json');
Expand All @@ -21,5 +21,11 @@ is $request.content, Map.new('tom', 'abc', 'bob' => (123,456,789), 'john', 'abc'
$request.body = 'tom=abc&lob=123';

is $request.content<lob>, '123', 'Is urlencoded re-evaluated on change?';
is $request.content<tom>, 'abc', 'Is urlencoded re-evaluated on change?';

$request.body = 'hyperlink=https%3A%2F%2Fyoutube.com%2Fwatch%3Fv%3DxvFZjo5PgG0';

lives-ok sub { $request.content };
is $request.content<hyperlink>, 'https://youtube.com/watch?v=xvFZjo5PgG0';

done-testing;
2 changes: 1 addition & 1 deletion t/11-advanced-query.rakutest
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use lib 'lib';
use Test;
use Humming-Bird::Core;

plan 10;
plan 12;

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);
Expand Down

0 comments on commit ba156f1

Please sign in to comment.