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