Add test case from RT#75607
[catagits/Catalyst-Runtime.git] / t / aggregate / live_engine_request_parameters.t
1 #!perl
2
3 use strict;
4 use warnings;
5
6 use FindBin;
7 use lib "$FindBin::Bin/../lib";
8
9 use Test::More tests => 54;
10 use Catalyst::Test 'TestApp';
11
12 use Catalyst::Request;
13 use HTTP::Headers;
14 use HTTP::Request::Common;
15
16 {
17     my $creq;
18
19     my $parameters = { 'a' => [qw(A b C d E f G)], };
20
21     my $query = join( '&', map { 'a=' . $_ } @{ $parameters->{a} } );
22
23     ok( my $response = request("http://localhost/dump/request?$query"),
24         'Request' );
25     ok( $response->is_success, 'Response Successful 2xx' );
26     is( $response->content_type, 'text/plain', 'Response Content-Type' );
27     like(
28         $response->content,
29         qr/^bless\( .* 'Catalyst::Request' \)$/s,
30         'Content is a serialized Catalyst::Request'
31     );
32     ok( eval '$creq = ' . $response->content, 'Unserialize Catalyst::Request' );
33     isa_ok( $creq, 'Catalyst::Request' );
34     is( $creq->method, 'GET', 'Catalyst::Request method' );
35     is_deeply( $creq->parameters, $parameters,
36         'Catalyst::Request parameters' );
37 }
38
39 {
40     my $creq;
41     ok( my $response = request("http://localhost/dump/request?q=foo%2bbar"),
42         'Request' );
43     ok( $response->is_success, 'Response Successful 2xx' );
44     is( $response->content_type, 'text/plain', 'Response Content-Type' );
45     ok( eval '$creq = ' . $response->content );
46     is $creq->parameters->{q}, 'foo+bar', '%2b not double decoded';
47 }
48
49 {
50     my $creq;
51     ok( my $response = request("http://localhost/dump/request?q=foo=bar"),
52         'Request' );
53     ok( $response->is_success, 'Response Successful 2xx' );
54     is( $response->content_type, 'text/plain', 'Response Content-Type' );
55     ok( eval '$creq = ' . $response->content );
56     is $creq->parameters->{q}, 'foo=bar', '= not ignored';
57 }
58
59 {
60     my $creq;
61
62     my $parameters = {
63         'a'     => [qw(A b C d E f G)],
64         '%'     => [ '%', '"', '& - &' ],
65         'blank' => '',
66     };
67
68     my $request = POST(
69         'http://localhost/dump/request/a/b?a=1&a=2&a=3',
70         'Content'      => $parameters,
71         'Content-Type' => 'application/x-www-form-urlencoded'
72     );
73
74     ok( my $response = request($request), 'Request' );
75     ok( $response->is_success, 'Response Successful 2xx' );
76     is( $response->content_type, 'text/plain', 'Response Content-Type' );
77     like(
78         $response->content,
79         qr/^bless\( .* 'Catalyst::Request' \)$/s,
80         'Content is a serialized Catalyst::Request'
81     );
82     ok( eval '$creq = ' . $response->content, 'Unserialize Catalyst::Request' );
83     isa_ok( $creq, 'Catalyst::Request' );
84     is( $creq->method, 'POST', 'Catalyst::Request method' );
85     is_deeply( $creq->body_parameters, $parameters,
86                'Catalyst::Request body_parameters' );
87     unshift( @{ $parameters->{a} }, 1, 2, 3 );
88     is_deeply( $creq->parameters, $parameters,
89         'Catalyst::Request parameters' );
90     is_deeply( $creq->arguments, [qw(a b)], 'Catalyst::Request arguments' );
91     is_deeply( $creq->uploads,   {}, 'Catalyst::Request uploads' );
92     is_deeply( $creq->cookies,   {}, 'Catalyst::Request cookie' );
93 }
94
95 # http://dev.catalyst.perl.org/ticket/37
96 # multipart/form-data parameters that contain 'http://'
97 # was an HTTP::Message bug, but HTTP::Body handles it properly now
98 {
99     my $creq;
100
101     my $parameters = {
102         'url'   => 'http://www.google.com',
103         'blank' => '',
104     };
105
106     my $request = POST( 'http://localhost/dump/request',
107         'Content-Type' => 'multipart/form-data',
108         'Content'      => $parameters,
109     );
110
111     ok( my $response = request($request), 'Request' );
112     ok( eval '$creq = ' . $response->content, 'Unserialize Catalyst::Request' );
113     is_deeply( $creq->parameters, $parameters, 'Catalyst::Request parameters' );
114 }
115
116 # raw query string support
117 {
118     my $creq;
119     
120     my $parameters = {
121         a     => 1,
122         blank => '',
123     };
124
125     my $request = POST(
126         'http://localhost/dump/request/a/b?query+string',
127         'Content'      => $parameters,
128         'Content-Type' => 'application/x-www-form-urlencoded'
129     );
130     
131     ok( my $response = request($request), 'Request' );
132     ok( eval '$creq = ' . $response->content, 'Unserialize Catalyst::Request' );
133     is( $creq->uri->query, 'query+string', 'Catalyst::Request POST query_string' );
134     is( $creq->query_keywords, 'query string', 'Catalyst::Request query_keywords' );
135     is_deeply( $creq->parameters, $parameters, 'Catalyst::Request parameters' );
136     
137     ok( $response = request('http://localhost/dump/request/a/b?x=1&y=1&z=1'), 'Request' );
138     ok( eval '$creq = ' . $response->content, 'Unserialize Catalyst::Request' );
139     is( $creq->uri->query, 'x=1&y=1&z=1', 'Catalyst::Request GET query_string' );
140 }
141
142 {
143     my $creq;
144     ok( my $response = request("http://localhost/dump/request?&&q="),
145         'Request' );
146     ok( $response->is_success, 'Response Successful 2xx' );
147     is( $response->content_type, 'text/plain', 'Response Content-Type' );
148     ok( eval '$creq = ' . $response->content );
149     is( keys %{$creq->{parameters}}, 1, 'remove empty parameter' );
150     is( $creq->{parameters}->{q}, '', 'empty parameter' );
151 }
152
153 {
154     my $creq;
155     ok( my $response = request("http://localhost/dump/request?&0&q="),
156         'Request' );
157     ok( $response->is_success, 'Response Successful 2xx' );
158     is( $response->content_type, 'text/plain', 'Response Content-Type' );
159     ok( eval '$creq = ' . $response->content );
160     is( keys %{$creq->{parameters}}, 2, 'remove empty parameter' );
161     is( $creq->{parameters}->{q}, '', 'empty parameter' );
162     ok( !defined $creq->{parameters}->{0}, 'empty parameter' );
163 }