Make Moose components collaberate with non-Moose Catalyst
[catagits/Catalyst-Runtime.git] / t / live_engine_request_uri.t
CommitLineData
c7ded7aa 1use strict;
2use warnings;
3
4use FindBin;
5use lib "$FindBin::Bin/lib";
6
83c2a441 7use Test::More tests => 66;
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' );
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
39SKIP:
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' );
63 is( $creq->{uri}->query, 'a=1;a=2;b=3', 'Query string ok' );
64 is_deeply( $creq->{parameters}, $parameters, 'Parameters ok' );
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' );
72 is( $creq->{uri}->query, 'text=Catalyst%20Rocks', 'Query string ok' );
73 is( $creq->{parameters}->{text}, 'Catalyst Rocks', 'Unescaped param ok' );
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' );
82}
83
84# test that uri_with adds params (and preserves)
85{
86 ok( my $response = request('http://localhost/engine/request/uri/uri_with?a=1'), 'Request' );
87 ok( $response->is_success, 'Response Successful 2xx' );
88 is( $response->header( 'X-Catalyst-Param-a' ), '1', 'param "a" ok' );
89 is( $response->header( 'X-Catalyst-Param-b' ), '1', 'param "b" ok' );
90}
91
92# test that uri_with replaces params (and preserves)
93{
94 ok( my $response = request('http://localhost/engine/request/uri/uri_with?a=1&b=2'), 'Request' );
95 ok( $response->is_success, 'Response Successful 2xx' );
96 is( $response->header( 'X-Catalyst-Param-a' ), '1', 'param "a" ok' );
97 is( $response->header( 'X-Catalyst-Param-b' ), '1', 'param "b" ok' );
98}
99
100# test that uri_with replaces params (and preserves)
101{
102 ok( my $response = request('http://localhost/engine/request/uri/uri_with_object'), 'Request' );
103 ok( $response->is_success, 'Response Successful 2xx' );
de19de2e 104 like( $response->header( 'X-Catalyst-Param-a' ), qr(https?://localhost[^/]*/), 'param "a" ok' );
c7ded7aa 105}
106
107# test that uri_with is utf8 safe
108{
109 ok( my $response = request("http://localhost/engine/request/uri/uri_with_utf8"), 'Request' );
110 ok( $response->is_success, 'Response Successful 2xx' );
111 like( $response->header( 'X-Catalyst-uri-with' ), qr/%E2%98%A0$/, 'uri_with ok' );
112}
113
114# test with undef -- no warnings should be thrown
115{
116 ok( my $response = request("http://localhost/engine/request/uri/uri_with_undef"), 'Request' );
117 ok( $response->is_success, 'Response Successful 2xx' );
118 is( $response->header( 'X-Catalyst-warnings' ), 0, 'no warnings emitted' );
119}
0ce22ad4 120
83c2a441 121# more tests with undef - should be ignored
122{
123 my $uri = "http://localhost/engine/request/uri/uri_with_undef_only";
451553f5 124 my ($check) = $uri =~ m{^http://localhost(.+)}; # needed to work with remote servers
83c2a441 125 ok( my $response = request($uri), 'Request' );
126 ok( $response->is_success, 'Response Successful 2xx' );
451553f5 127 like( $response->header( 'X-Catalyst-uri-with' ), qr/$check$/, 'uri_with ok' );
83c2a441 128
129 # try with existing param
130 $uri = "$uri?x=1";
451553f5 131 ($check) = $uri =~ m{^http://localhost(.+)}; # needed to work with remote servers
132 $check =~ s/\?/\\\?/g;
83c2a441 133 ok( $response = request($uri), 'Request' );
134 ok( $response->is_success, 'Response Successful 2xx' );
451553f5 135 like( $response->header( 'X-Catalyst-uri-with' ), qr/$check$/, 'uri_with ok' );
83c2a441 136}
137
138{
139 my $uri = "http://localhost/engine/request/uri/uri_with_undef_ignore";
451553f5 140 my ($check) = $uri =~ m{^http://localhost(.+)}; # needed to work with remote servers
83c2a441 141 ok( my $response = request($uri), 'Request' );
142 ok( $response->is_success, 'Response Successful 2xx' );
451553f5 143 like( $response->header( 'X-Catalyst-uri-with' ), qr/$check\?a=1/, 'uri_with ok' );
83c2a441 144
145 # remove an existing param
146 ok( $response = request("${uri}?b=1"), 'Request' );
147 ok( $response->is_success, 'Response Successful 2xx' );
451553f5 148 like( $response->header( 'X-Catalyst-uri-with' ), qr/$check\?a=1/, 'uri_with ok' );
83c2a441 149
150 # remove an existing param, leave one, and add a new one
151 ok( $response = request("${uri}?b=1&c=1"), 'Request' );
152 ok( $response->is_success, 'Response Successful 2xx' );
153 is( $response->header( 'X-Catalyst-Param-a' ), '1', 'param "a" ok' );
154 ok( !defined $response->header( 'X-Catalyst-Param-b' ),'param "b" ok' );
155 is( $response->header( 'X-Catalyst-Param-c' ), '1', 'param "c" ok' );
156}
157