Skip to content

Commit

Permalink
Fix urlencoded + test (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
rawleyfowler authored Nov 1, 2023
1 parent 3660216 commit 356f1f6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 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"
}
11 changes: 8 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.5';
our constant $VERSION = '2.1.7';

# Mime type parser from MIME::Types
my constant $mime = MIME::Types.new;
Expand Down Expand Up @@ -99,7 +99,7 @@ class HTTPAction {

my sub parse-urlencoded(Str:D $urlencoded --> Map:D) {
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;
uri_decode_component($urlencoded).split('&', :skip-empty)>>.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 @@ -120,7 +120,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
6 changes: 5 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 5;

my $request = Request.new(body => '{ "foo": "bar" }', path => '/home', method => GET, version => 'HTTP/1.1');
$request.header('Content-Type', 'application/json');
Expand All @@ -22,4 +22,8 @@ $request.body = 'tom=abc&lob=123';

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

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

lives-ok sub { $request.content };

done-testing;

0 comments on commit 356f1f6

Please sign in to comment.