Commit | Line | Data |
c7ded7aa |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use FindBin; |
42da66a9 |
5 | use lib "$FindBin::Bin/../lib"; |
c7ded7aa |
6 | |
6ebb7818 |
7 | use Test::More tests => 74; |
c7ded7aa |
8 | use Catalyst::Test 'TestApp'; |
9 | use Catalyst::Request; |
10 | |
11 | my $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' ); |
17 | ok( eval '$creq = ' . $response->content, 'Unserialize Catalyst::Request' ); |
18 | like( $creq->uri, qr{/my/app/lives/here$}, 'URI contains new path' ); |
19 | } |
20 | |
21 | # test that path properly removes the base location |
22 | { |
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' ); |
28 | } |
29 | |
30 | # test that base + path is correct |
31 | { |
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' ); |
36 | } |
37 | |
de19de2e |
38 | # test base is correct for HTTPS URLs |
39 | SKIP: |
40 | { |
41 | if ( $ENV{CATALYST_SERVER} ) { |
42 | skip 'Using remote server', 5; |
43 | } |
44 | |
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' ); |
51 | } |
52 | |
c7ded7aa |
53 | # test that we can use semi-colons as separators |
54 | { |
55 | my $parameters = { |
56 | a => [ qw/1 2/ ], |
57 | b => 3, |
58 | }; |
59 | |
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' ); |
8e061fef |
63 | is( $creq->uri->query, 'a=1;a=2;b=3', 'Query string ok' ); |
64 | is_deeply( $creq->parameters, $parameters, 'Parameters ok' ); |
c7ded7aa |
65 | } |
66 | |
67 | # test that query params are unescaped properly |
68 | { |
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' ); |
8e061fef |
72 | is( $creq->uri->query, 'text=Catalyst%20Rocks', 'Query string ok' ); |
73 | is( $creq->parameters->{text}, 'Catalyst Rocks', 'Unescaped param ok' ); |
c7ded7aa |
74 | } |
75 | |
76 | # test that uri_with adds params |
77 | { |
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' ); |
6ebb7818 |
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'); |
c7ded7aa |
84 | } |
85 | |
86 | # test that uri_with adds params (and preserves) |
87 | { |
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' ); |
6ebb7818 |
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'); |
c7ded7aa |
94 | } |
95 | |
96 | # test that uri_with replaces params (and preserves) |
97 | { |
6ebb7818 |
98 | ok( my $response = request('http://localhost/engine/request/uri/uri_with?a=1&b=2&c=3'), 'Request' ); |
c7ded7aa |
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' ); |
6ebb7818 |
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'); |
c7ded7aa |
104 | } |
105 | |
106 | # test that uri_with replaces params (and preserves) |
107 | { |
108 | ok( my $response = request('http://localhost/engine/request/uri/uri_with_object'), 'Request' ); |
109 | ok( $response->is_success, 'Response Successful 2xx' ); |
de19de2e |
110 | like( $response->header( 'X-Catalyst-Param-a' ), qr(https?://localhost[^/]*/), 'param "a" ok' ); |
c7ded7aa |
111 | } |
112 | |
113 | # test that uri_with is utf8 safe |
114 | { |
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' ); |
118 | } |
119 | |
120 | # test with undef -- no warnings should be thrown |
121 | { |
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' ); |
125 | } |
0ce22ad4 |
126 | |
2f381252 |
127 | # more tests with undef - should be ignored |
128 | { |
129 | my $uri = "http://localhost/engine/request/uri/uri_with_undef_only"; |
1cda0e2c |
130 | my ($check) = $uri =~ m{^http://localhost(.+)}; # needed to work with remote servers |
2f381252 |
131 | ok( my $response = request($uri), 'Request' ); |
132 | ok( $response->is_success, 'Response Successful 2xx' ); |
1cda0e2c |
133 | like( $response->header( 'X-Catalyst-uri-with' ), qr/$check$/, 'uri_with ok' ); |
2f381252 |
134 | |
135 | # try with existing param |
136 | $uri = "$uri?x=1"; |
1cda0e2c |
137 | ($check) = $uri =~ m{^http://localhost(.+)}; # needed to work with remote servers |
138 | $check =~ s/\?/\\\?/g; |
2f381252 |
139 | ok( $response = request($uri), 'Request' ); |
140 | ok( $response->is_success, 'Response Successful 2xx' ); |
1cda0e2c |
141 | like( $response->header( 'X-Catalyst-uri-with' ), qr/$check$/, 'uri_with ok' ); |
2f381252 |
142 | } |
143 | |
144 | { |
145 | my $uri = "http://localhost/engine/request/uri/uri_with_undef_ignore"; |
1cda0e2c |
146 | my ($check) = $uri =~ m{^http://localhost(.+)}; # needed to work with remote servers |
2f381252 |
147 | ok( my $response = request($uri), 'Request' ); |
148 | ok( $response->is_success, 'Response Successful 2xx' ); |
1cda0e2c |
149 | like( $response->header( 'X-Catalyst-uri-with' ), qr/$check\?a=1/, 'uri_with ok' ); |
2f381252 |
150 | |
151 | # remove an existing param |
152 | ok( $response = request("${uri}?b=1"), 'Request' ); |
153 | ok( $response->is_success, 'Response Successful 2xx' ); |
1cda0e2c |
154 | like( $response->header( 'X-Catalyst-uri-with' ), qr/$check\?a=1/, 'uri_with ok' ); |
2f381252 |
155 | |
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' ); |
162 | } |
163 | |
6cb9e383 |
164 | # Test an overridden uri method which calls the base method, SmartURI does this. |
f7c7f289 |
165 | SKIP: |
6cb9e383 |
166 | { |
f7c7f289 |
167 | if ( $ENV{CATALYST_SERVER} ) { |
168 | skip 'Using remote server', 2; |
169 | } |
170 | |
dae2b0fa |
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'); |
176 | } |