whitespace cleanup
[catagits/Catalyst-Runtime.git] / t / aggregate / live_engine_request_uri.t
CommitLineData
c7ded7aa 1use strict;
2use warnings;
3
4use FindBin;
42da66a9 5use lib "$FindBin::Bin/../lib";
c7ded7aa 6
6ebb7818 7use Test::More tests => 74;
c7ded7aa 8use Catalyst::Test 'TestApp';
9use Catalyst::Request;
10
11my $creq;
12
13# test that the path can be changed
14{
15 ok( my $response = request('http://localhost/engine/request/uri/change_path'), 'Request' );
16 ok( $response->is_success, 'Response Successful 2xx' );
faa02805 17 ok( eval '$creq = ' . $response->content, 'Unserialize Catalyst::Request' )
18 or diag("Exception '$@', content " . $response->content);
c7ded7aa 19 like( $creq->uri, qr{/my/app/lives/here$}, 'URI contains new path' );
20}
21
22# test that path properly removes the base location
23{
24 ok( my $response = request('http://localhost/engine/request/uri/change_base'), 'Request' );
25 ok( $response->is_success, 'Response Successful 2xx' );
26 ok( eval '$creq = ' . $response->content, 'Unserialize Catalyst::Request' );
27 like( $creq->base, qr{/new/location}, 'Base URI contains new location' );
28 is( $creq->path, 'engine/request/uri/change_base', 'URI contains correct path' );
29}
30
31# test that base + path is correct
32{
33 ok( my $response = request('http://localhost/engine/request/uri'), 'Request' );
34 ok( $response->is_success, 'Response Successful 2xx' );
35 ok( eval '$creq = ' . $response->content, 'Unserialize Catalyst::Request' );
36 is( $creq->base . $creq->path, $creq->uri, 'Base + Path ok' );
37}
38
de19de2e 39# test base is correct for HTTPS URLs
40SKIP:
41{
42 if ( $ENV{CATALYST_SERVER} ) {
43 skip 'Using remote server', 5;
44 }
88e5a8b0 45
de19de2e 46 local $ENV{HTTPS} = 'on';
47 ok( my $response = request('https://localhost/engine/request/uri'), 'HTTPS Request' );
48 ok( $response->is_success, 'Response Successful 2xx' );
49 ok( eval '$creq = ' . $response->content, 'Unserialize Catalyst::Request' );
50 is( $creq->base, 'https://localhost/', 'HTTPS base ok' );
51 is( $creq->uri, 'https://localhost/engine/request/uri', 'HTTPS uri ok' );
52}
53
c7ded7aa 54# test that we can use semi-colons as separators
55{
56 my $parameters = {
57 a => [ qw/1 2/ ],
58 b => 3,
59 };
88e5a8b0 60
c7ded7aa 61 ok( my $response = request('http://localhost/engine/request/uri?a=1;a=2;b=3'), 'Request' );
62 ok( $response->is_success, 'Response Successful 2xx' );
63 ok( eval '$creq = ' . $response->content, 'Unserialize Catalyst::Request' );
8e061fef 64 is( $creq->uri->query, 'a=1;a=2;b=3', 'Query string ok' );
65 is_deeply( $creq->parameters, $parameters, 'Parameters ok' );
c7ded7aa 66}
67
68# test that query params are unescaped properly
69{
70 ok( my $response = request('http://localhost/engine/request/uri?text=Catalyst%20Rocks'), 'Request' );
71 ok( $response->is_success, 'Response Successful 2xx' );
72 ok( eval '$creq = ' . $response->content, 'Unserialize Catalyst::Request' );
8e061fef 73 is( $creq->uri->query, 'text=Catalyst%20Rocks', 'Query string ok' );
74 is( $creq->parameters->{text}, 'Catalyst Rocks', 'Unescaped param ok' );
c7ded7aa 75}
76
77# test that uri_with adds params
78{
79 ok( my $response = request('http://localhost/engine/request/uri/uri_with'), 'Request' );
80 ok( $response->is_success, 'Response Successful 2xx' );
81 ok( !defined $response->header( 'X-Catalyst-Param-a' ), 'param "a" ok' );
82 is( $response->header( 'X-Catalyst-Param-b' ), '1', 'param "b" ok' );
6ebb7818 83 is( $response->header( 'X-Catalyst-Param-c' ), '--notexists--', 'param "c" ok' );
84 unlike($response->header ('X-Catalyst-query'), qr/c=/, 'no c in return');
c7ded7aa 85}
86
87# test that uri_with adds params (and preserves)
88{
89 ok( my $response = request('http://localhost/engine/request/uri/uri_with?a=1'), 'Request' );
90 ok( $response->is_success, 'Response Successful 2xx' );
91 is( $response->header( 'X-Catalyst-Param-a' ), '1', 'param "a" ok' );
92 is( $response->header( 'X-Catalyst-Param-b' ), '1', 'param "b" ok' );
6ebb7818 93 is( $response->header( 'X-Catalyst-Param-c' ), '--notexists--', 'param "c" ok' );
94 unlike($response->header ('X-Catalyst-query'), qr/c=/, 'no c in return');
c7ded7aa 95}
96
97# test that uri_with replaces params (and preserves)
98{
6ebb7818 99 ok( my $response = request('http://localhost/engine/request/uri/uri_with?a=1&b=2&c=3'), 'Request' );
c7ded7aa 100 ok( $response->is_success, 'Response Successful 2xx' );
101 is( $response->header( 'X-Catalyst-Param-a' ), '1', 'param "a" ok' );
102 is( $response->header( 'X-Catalyst-Param-b' ), '1', 'param "b" ok' );
6ebb7818 103 is( $response->header( 'X-Catalyst-Param-c' ), '--notexists--', 'param "c" deleted ok' );
104 unlike($response->header ('X-Catalyst-query'), qr/c=/, 'no c in return');
c7ded7aa 105}
106
107# test that uri_with replaces params (and preserves)
108{
109 ok( my $response = request('http://localhost/engine/request/uri/uri_with_object'), 'Request' );
110 ok( $response->is_success, 'Response Successful 2xx' );
de19de2e 111 like( $response->header( 'X-Catalyst-Param-a' ), qr(https?://localhost[^/]*/), 'param "a" ok' );
c7ded7aa 112}
113
114# test that uri_with is utf8 safe
115{
116 ok( my $response = request("http://localhost/engine/request/uri/uri_with_utf8"), 'Request' );
117 ok( $response->is_success, 'Response Successful 2xx' );
118 like( $response->header( 'X-Catalyst-uri-with' ), qr/%E2%98%A0$/, 'uri_with ok' );
119}
120
121# test with undef -- no warnings should be thrown
122{
123 ok( my $response = request("http://localhost/engine/request/uri/uri_with_undef"), 'Request' );
124 ok( $response->is_success, 'Response Successful 2xx' );
125 is( $response->header( 'X-Catalyst-warnings' ), 0, 'no warnings emitted' );
126}
0ce22ad4 127
2f381252 128# more tests with undef - should be ignored
129{
130 my $uri = "http://localhost/engine/request/uri/uri_with_undef_only";
1cda0e2c 131 my ($check) = $uri =~ m{^http://localhost(.+)}; # needed to work with remote servers
2f381252 132 ok( my $response = request($uri), 'Request' );
133 ok( $response->is_success, 'Response Successful 2xx' );
1cda0e2c 134 like( $response->header( 'X-Catalyst-uri-with' ), qr/$check$/, 'uri_with ok' );
2f381252 135
136 # try with existing param
137 $uri = "$uri?x=1";
1cda0e2c 138 ($check) = $uri =~ m{^http://localhost(.+)}; # needed to work with remote servers
139 $check =~ s/\?/\\\?/g;
2f381252 140 ok( $response = request($uri), 'Request' );
141 ok( $response->is_success, 'Response Successful 2xx' );
1cda0e2c 142 like( $response->header( 'X-Catalyst-uri-with' ), qr/$check$/, 'uri_with ok' );
2f381252 143}
144
145{
146 my $uri = "http://localhost/engine/request/uri/uri_with_undef_ignore";
1cda0e2c 147 my ($check) = $uri =~ m{^http://localhost(.+)}; # needed to work with remote servers
2f381252 148 ok( my $response = request($uri), 'Request' );
149 ok( $response->is_success, 'Response Successful 2xx' );
1cda0e2c 150 like( $response->header( 'X-Catalyst-uri-with' ), qr/$check\?a=1/, 'uri_with ok' );
2f381252 151
152 # remove an existing param
153 ok( $response = request("${uri}?b=1"), 'Request' );
154 ok( $response->is_success, 'Response Successful 2xx' );
1cda0e2c 155 like( $response->header( 'X-Catalyst-uri-with' ), qr/$check\?a=1/, 'uri_with ok' );
2f381252 156
157 # remove an existing param, leave one, and add a new one
158 ok( $response = request("${uri}?b=1&c=1"), 'Request' );
159 ok( $response->is_success, 'Response Successful 2xx' );
160 is( $response->header( 'X-Catalyst-Param-a' ), '1', 'param "a" ok' );
161 ok( !defined $response->header( 'X-Catalyst-Param-b' ),'param "b" ok' );
162 is( $response->header( 'X-Catalyst-Param-c' ), '1', 'param "c" ok' );
163}
164
6cb9e383 165# Test an overridden uri method which calls the base method, SmartURI does this.
f7c7f289 166SKIP:
6cb9e383 167{
f7c7f289 168 if ( $ENV{CATALYST_SERVER} ) {
169 skip 'Using remote server', 2;
170 }
88e5a8b0 171
dae2b0fa 172 require TestApp::RequestBaseBug;
173 TestApp->request_class('TestApp::RequestBaseBug');
174 ok( my $response = request('http://localhost/engine/request/uri'), 'Request' );
175 ok( $response->is_success, 'Response Successful 2xx' );
176 TestApp->request_class('Catalyst::Request');
177}