From: asparkman Date: Fri, 13 May 2016 22:07:25 +0000 (+0100) Subject: Adds test suite assuming orthogonality of query keywords/params X-Git-Tag: 5.90116~10^2~6 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=4910fc25573e604a633a97d47c992f7d0c51b02d Adds test suite assuming orthogonality of query keywords/params https://github.com/perl-catalyst/catalyst-runtime/issues/134 --- diff --git a/t/query_keywords_and_parameters.t b/t/query_keywords_and_parameters.t new file mode 100644 index 0000000..ba8938b --- /dev/null +++ b/t/query_keywords_and_parameters.t @@ -0,0 +1,89 @@ +use warnings; +use strict; +use Test::More; + +# Test case for reported issue when an action consumes JSON but a +# POST sends nothing we get a hard error + +{ + package MyApp::Controller::Root; + $INC{'MyApp/Controller/Root.pm'} = __FILE__; + + use base 'Catalyst::Controller'; + + sub bar :Local Args(0) GET { + my( $self, $c ) = @_; + } + + package MyApp; + use Catalyst; + MyApp->setup; +} + +use HTTP::Request::Common; +use Catalyst::Test 'MyApp'; + +# These tests assume that the decoding that occurs for the query string follows +# the payload decoding algorithm described here: +# https://www.w3.org/TR/html5/forms.html#url-encoded-form-data + +{ + ok my $req = GET 'root/bar', + 'Content-Type' => 'application/x-www-form-urlencoded'; + + my ($res, $c) = ctx_request($req); + + ok !defined($c->req->query_keywords), 'query_keywords is not defined when no ?'; + is_deeply $c->req->query_parameters, {}, 'query_parameters defined, but empty for no ?'; +} + + +{ + ok my $req = GET 'root/bar?', + 'Content-Type' => 'application/x-www-form-urlencoded'; + + my ($res, $c) = ctx_request($req); + + is $c->req->query_keywords, '', 'query_keywords is not defined with empty query string'; + is_deeply $c->req->query_parameters, {}, 'query_parameters defined, but empty with empty query string'; +} + + +{ + ok my $req = GET 'root/bar?a=b', + 'Content-Type' => 'application/x-www-form-urlencoded'; + + my ($res, $c) = ctx_request($req); + + ok !defined($c->req->query_keywords), 'query_keywords undefined when isindex not set'; + is_deeply $c->req->query_parameters, { a => 'b' }, 'query_parameters defined for ?a=b'; +} + + +{ + ok my $req = GET 'root/bar?x', + 'Content-Type' => 'application/x-www-form-urlencoded'; + + my ($res, $c) = ctx_request($req); + + is $c->req->query_keywords, 'x', 'query_keywords defined for ?x'; + # The algorithm reads like 'x' should be treated as a value, not a name. + # Perl does not support undef as a hash key. I feel this would be the best + # alternative as isindex is moving towards complete deprecation. + is_deeply $c->req->query_parameters, { 'x' => undef }, 'query_parameters defined for ?x'; +} + + +{ + ok my $req = GET 'root/bar?x&a=b', + 'Content-Type' => 'application/x-www-form-urlencoded'; + + my ($res, $c) = ctx_request($req); + + is $c->req->query_keywords, 'x', 'query_keywords defined for ?x&a=b'; + # See comment above about the 'query_parameters defined for ?x' test case. + is_deeply $c->req->query_parameters, { 'x' => undef, a => 'b' }, 'query_parameters defined for ?x&a=b'; +} + + +done_testing();