5 use lib "$FindBin::Bin/../lib";
7 use Test::More tests => 74;
8 use Catalyst::Test 'TestApp';
13 # test that the path can be changed
15 ok( my $response = request('http://localhost/engine/request/uri/change_path'), 'Request' );
16 ok( $response->is_success, 'Response Successful 2xx' );
17 ok( eval '$creq = ' . $response->content, 'Unserialize Catalyst::Request' );
18 like( $creq->uri, qr{/my/app/lives/here$}, 'URI contains new path' );
21 # test that path properly removes the base location
23 ok( my $response = request('http://localhost/engine/request/uri/change_base'), 'Request' );
24 ok( $response->is_success, 'Response Successful 2xx' );
25 ok( eval '$creq = ' . $response->content, 'Unserialize Catalyst::Request' );
26 like( $creq->base, qr{/new/location}, 'Base URI contains new location' );
27 is( $creq->path, 'engine/request/uri/change_base', 'URI contains correct path' );
30 # test that base + path is correct
32 ok( my $response = request('http://localhost/engine/request/uri'), 'Request' );
33 ok( $response->is_success, 'Response Successful 2xx' );
34 ok( eval '$creq = ' . $response->content, 'Unserialize Catalyst::Request' );
35 is( $creq->base . $creq->path, $creq->uri, 'Base + Path ok' );
38 # test base is correct for HTTPS URLs
41 if ( $ENV{CATALYST_SERVER} ) {
42 skip 'Using remote server', 5;
45 local $ENV{HTTPS} = 'on';
46 ok( my $response = request('https://localhost/engine/request/uri'), 'HTTPS Request' );
47 ok( $response->is_success, 'Response Successful 2xx' );
48 ok( eval '$creq = ' . $response->content, 'Unserialize Catalyst::Request' );
49 is( $creq->base, 'https://localhost/', 'HTTPS base ok' );
50 is( $creq->uri, 'https://localhost/engine/request/uri', 'HTTPS uri ok' );
53 # test that we can use semi-colons as separators
60 ok( my $response = request('http://localhost/engine/request/uri?a=1;a=2;b=3'), 'Request' );
61 ok( $response->is_success, 'Response Successful 2xx' );
62 ok( eval '$creq = ' . $response->content, 'Unserialize Catalyst::Request' );
63 is( $creq->uri->query, 'a=1;a=2;b=3', 'Query string ok' );
64 is_deeply( $creq->parameters, $parameters, 'Parameters ok' );
67 # test that query params are unescaped properly
69 ok( my $response = request('http://localhost/engine/request/uri?text=Catalyst%20Rocks'), 'Request' );
70 ok( $response->is_success, 'Response Successful 2xx' );
71 ok( eval '$creq = ' . $response->content, 'Unserialize Catalyst::Request' );
72 is( $creq->uri->query, 'text=Catalyst%20Rocks', 'Query string ok' );
73 is( $creq->parameters->{text}, 'Catalyst Rocks', 'Unescaped param ok' );
76 # test that uri_with adds params
78 ok( my $response = request('http://localhost/engine/request/uri/uri_with'), 'Request' );
79 ok( $response->is_success, 'Response Successful 2xx' );
80 ok( !defined $response->header( 'X-Catalyst-Param-a' ), 'param "a" ok' );
81 is( $response->header( 'X-Catalyst-Param-b' ), '1', 'param "b" ok' );
82 is( $response->header( 'X-Catalyst-Param-c' ), '--notexists--', 'param "c" ok' );
83 unlike($response->header ('X-Catalyst-query'), qr/c=/, 'no c in return');
86 # test that uri_with adds params (and preserves)
88 ok( my $response = request('http://localhost/engine/request/uri/uri_with?a=1'), 'Request' );
89 ok( $response->is_success, 'Response Successful 2xx' );
90 is( $response->header( 'X-Catalyst-Param-a' ), '1', 'param "a" ok' );
91 is( $response->header( 'X-Catalyst-Param-b' ), '1', 'param "b" ok' );
92 is( $response->header( 'X-Catalyst-Param-c' ), '--notexists--', 'param "c" ok' );
93 unlike($response->header ('X-Catalyst-query'), qr/c=/, 'no c in return');
96 # test that uri_with replaces params (and preserves)
98 ok( my $response = request('http://localhost/engine/request/uri/uri_with?a=1&b=2&c=3'), 'Request' );
99 ok( $response->is_success, 'Response Successful 2xx' );
100 is( $response->header( 'X-Catalyst-Param-a' ), '1', 'param "a" ok' );
101 is( $response->header( 'X-Catalyst-Param-b' ), '1', 'param "b" ok' );
102 is( $response->header( 'X-Catalyst-Param-c' ), '--notexists--', 'param "c" deleted ok' );
103 unlike($response->header ('X-Catalyst-query'), qr/c=/, 'no c in return');
106 # test that uri_with replaces params (and preserves)
108 ok( my $response = request('http://localhost/engine/request/uri/uri_with_object'), 'Request' );
109 ok( $response->is_success, 'Response Successful 2xx' );
110 like( $response->header( 'X-Catalyst-Param-a' ), qr(https?://localhost[^/]*/), 'param "a" ok' );
113 # test that uri_with is utf8 safe
115 ok( my $response = request("http://localhost/engine/request/uri/uri_with_utf8"), 'Request' );
116 ok( $response->is_success, 'Response Successful 2xx' );
117 like( $response->header( 'X-Catalyst-uri-with' ), qr/%E2%98%A0$/, 'uri_with ok' );
120 # test with undef -- no warnings should be thrown
122 ok( my $response = request("http://localhost/engine/request/uri/uri_with_undef"), 'Request' );
123 ok( $response->is_success, 'Response Successful 2xx' );
124 is( $response->header( 'X-Catalyst-warnings' ), 0, 'no warnings emitted' );
127 # more tests with undef - should be ignored
129 my $uri = "http://localhost/engine/request/uri/uri_with_undef_only";
130 my ($check) = $uri =~ m{^http://localhost(.+)}; # needed to work with remote servers
131 ok( my $response = request($uri), 'Request' );
132 ok( $response->is_success, 'Response Successful 2xx' );
133 like( $response->header( 'X-Catalyst-uri-with' ), qr/$check$/, 'uri_with ok' );
135 # try with existing param
137 ($check) = $uri =~ m{^http://localhost(.+)}; # needed to work with remote servers
138 $check =~ s/\?/\\\?/g;
139 ok( $response = request($uri), 'Request' );
140 ok( $response->is_success, 'Response Successful 2xx' );
141 like( $response->header( 'X-Catalyst-uri-with' ), qr/$check$/, 'uri_with ok' );
145 my $uri = "http://localhost/engine/request/uri/uri_with_undef_ignore";
146 my ($check) = $uri =~ m{^http://localhost(.+)}; # needed to work with remote servers
147 ok( my $response = request($uri), 'Request' );
148 ok( $response->is_success, 'Response Successful 2xx' );
149 like( $response->header( 'X-Catalyst-uri-with' ), qr/$check\?a=1/, 'uri_with ok' );
151 # remove an existing param
152 ok( $response = request("${uri}?b=1"), 'Request' );
153 ok( $response->is_success, 'Response Successful 2xx' );
154 like( $response->header( 'X-Catalyst-uri-with' ), qr/$check\?a=1/, 'uri_with ok' );
156 # remove an existing param, leave one, and add a new one
157 ok( $response = request("${uri}?b=1&c=1"), 'Request' );
158 ok( $response->is_success, 'Response Successful 2xx' );
159 is( $response->header( 'X-Catalyst-Param-a' ), '1', 'param "a" ok' );
160 ok( !defined $response->header( 'X-Catalyst-Param-b' ),'param "b" ok' );
161 is( $response->header( 'X-Catalyst-Param-c' ), '1', 'param "c" ok' );
164 # Test an overridden uri method which calls the base method, SmartURI does this.
167 if ( $ENV{CATALYST_SERVER} ) {
168 skip 'Using remote server', 2;
171 require TestApp::RequestBaseBug;
172 TestApp->request_class('TestApp::RequestBaseBug');
173 ok( my $response = request('http://localhost/engine/request/uri'), 'Request' );
174 ok( $response->is_success, 'Response Successful 2xx' );
175 TestApp->request_class('Catalyst::Request');