Adds test suite assuming orthogonality of query keywords/params
[catagits/Catalyst-Runtime.git] / t / query_keywords_and_parameters.t
1 use warnings;
2 use strict;
3 use Test::More;
4
5 # Test case for reported issue when an action consumes JSON but a
6 # POST sends nothing we get a hard error
7
8 {
9   package MyApp::Controller::Root;
10   $INC{'MyApp/Controller/Root.pm'} = __FILE__;
11
12   use base 'Catalyst::Controller';
13
14   sub bar :Local Args(0) GET {
15     my( $self, $c ) = @_;
16   }
17
18   package MyApp;
19   use Catalyst;
20   MyApp->setup;
21 }
22
23 use HTTP::Request::Common;
24 use Catalyst::Test 'MyApp';
25
26 # These tests assume that the decoding that occurs for the query string follows
27 # the payload decoding algorithm described here:
28 # https://www.w3.org/TR/html5/forms.html#url-encoded-form-data 
29
30 {
31   ok my $req = GET 'root/bar',
32     'Content-Type' => 'application/x-www-form-urlencoded';
33
34   my ($res, $c) = ctx_request($req);
35
36   ok !defined($c->req->query_keywords), 'query_keywords is not defined when no ?';
37   is_deeply $c->req->query_parameters, {}, 'query_parameters defined, but empty for no ?';
38 }
39
40
41 {
42   ok my $req = GET 'root/bar?',
43     'Content-Type' => 'application/x-www-form-urlencoded';
44
45   my ($res, $c) = ctx_request($req);
46
47   is $c->req->query_keywords, '', 'query_keywords is not defined with empty query string';
48   is_deeply $c->req->query_parameters, {}, 'query_parameters defined, but empty with empty query string';
49 }
50
51
52 {
53   ok my $req = GET 'root/bar?a=b',
54     'Content-Type' => 'application/x-www-form-urlencoded';
55
56   my ($res, $c) = ctx_request($req);
57
58   ok !defined($c->req->query_keywords), 'query_keywords undefined when isindex not set';
59   is_deeply $c->req->query_parameters, { a => 'b' }, 'query_parameters defined for ?a=b';
60 }
61
62
63 {
64   ok my $req = GET 'root/bar?x',
65     'Content-Type' => 'application/x-www-form-urlencoded';
66
67   my ($res, $c) = ctx_request($req);
68
69   is $c->req->query_keywords, 'x', 'query_keywords defined for ?x';
70   # The algorithm reads like 'x' should be treated as a value, not a name.
71   # Perl does not support undef as a hash key.  I feel this would be the best
72   # alternative as isindex is moving towards complete deprecation.
73   is_deeply $c->req->query_parameters, { 'x' => undef }, 'query_parameters defined for ?x';
74 }
75
76
77 {
78   ok my $req = GET 'root/bar?x&a=b',
79     'Content-Type' => 'application/x-www-form-urlencoded';
80
81   my ($res, $c) = ctx_request($req);
82
83   is $c->req->query_keywords, 'x', 'query_keywords defined for ?x&a=b';
84   # See comment above about the 'query_parameters defined for ?x' test case.
85   is_deeply $c->req->query_parameters, { 'x' => undef, a => 'b' }, 'query_parameters defined for ?x&a=b';
86 }
87
88
89 done_testing();