10 use HTTP::Request::Common;
13 my $Class = 'Catalyst::Test';
15 my $Pkg = __PACKAGE__;
16 my $Url = 'http://localhost/';
17 my $Content = "root index";
20 $Pkg => [qw|get request ctx_request|], # exported
21 $Class => [qw|local_request remote_request|], # not exported
24 ### make sure we're not trying to connect to a remote host -- these are local tests
25 local $ENV{CATALYST_SERVER};
27 use Catalyst::Test ();
29 ### check available methods
30 { ### turn of redefine warnings, we'll get new subs exported
31 ### XXX 'no warnings' and 'local $^W' wont work as warnings are turned on in
32 ### test.pm, so trap them for now --kane
33 { local $SIG{__WARN__} = sub {};
34 ok( $Class->import, "Argumentless import for methods only" );
37 while( my($class, $meths) = each %Meth ) {
38 for my $meth ( @$meths ) { SKIP: {
41 can_ok( $class, $meth );
43 ### only for exported methods
44 skip "Error tests only for exported methods", 2 unless $class eq $Pkg;
46 ### check error conditions
47 eval { $class->can($meth)->( $Url ) };
48 ok( $@, " $meth without app gives error" );
50 " Error filled with expected content for '$meth'" );
55 ### simple tests for exported methods
56 { ### turn of redefine warnings, we'll get new subs exported
57 ### XXX 'no warnings' and 'local $^W' wont work as warnings are turned on in
58 ### test.pm, so trap them for now --kane
59 { local $SIG{__WARN__} = sub {};
60 ok( $Class->import( $App ),
61 "Loading $Class for App $App" );
64 ### test exported methods again
65 for my $meth ( @{ $Meth{$Pkg} } ) { SKIP: {
67 ### do a call, we should get a result and perhaps a $c if it's 'ctx_request';
68 my ($res, $c) = eval { $Pkg->can($meth)->( $Url ) };
70 ok( 1, " Called $Pkg->$meth( $Url )" );
71 ok( !$@, " No critical error $@" );
72 ok( $res, " Result obtained" );
74 ### get the content as a string, to make sure we got what we expected
75 my $res_as_string = $meth eq 'get' ? $res : $res->content;
76 is( $res_as_string, $Content,
77 " Content as expected: $res_as_string" );
79 ### some tests for 'ctx_request'
80 skip "Context tests skipped for '$meth'", 6 unless $meth eq 'ctx_request';
82 ok( $c, " Context object returned" );
83 isa_ok( $c, $App, " Object" );
84 is( $c->request->uri, $Url,
85 " Url recorded in request" );
86 is( $c->response->body, $Content,
87 " Content recorded in response" );
88 ok( $c->stash, " Stash accessible" );
89 ok( $c->action, " Action object accessible" );
90 ok( $res->request, " Response has request object" );
91 is exception { is( $res->request->uri, $Url) }, undef,
92 " Request object has correct url";
96 ### perl5.8.8 + cat 5.80's Cat::Test->ctx_request didn't return $c the 2nd
97 ### time it was invoked. Without tracking the bug down all the way, it was
98 ### clearly related to the Moose'ification of Cat::Test and a scoping issue
99 ### with a 'my'd variable. Since the same code works fine in 5.10, a bug in
100 ### either Moose or perl 5.8 is suspected.
101 { ok( 1, "Testing consistency of ctx_request()" );
103 my($res, $c) = ctx_request( $Url );
104 ok( $c, " Call $_: Context object returned" );
108 # FIXME - These vhosts in tests tests should be somewhere else...
110 sub customize { Catalyst::Test::_customize_request($_[0], {}, @_[1 .. $#_]) }
113 my $req = Catalyst::Utils::request('/dummy');
115 is( $req->header('Host'), undef, 'normal request is unmodified' );
119 my $req = Catalyst::Utils::request('/dummy');
120 customize( $req, { host => 'customized.com' } );
121 like( $req->header('Host'), qr/customized.com/, 'request is customizable via opts hash' );
125 my $req = Catalyst::Utils::request('/dummy');
126 local $Catalyst::Test::default_host = 'localized.com';
128 like( $req->header('Host'), qr/localized.com/, 'request is customizable via package var' );
132 my $req = Catalyst::Utils::request('/dummy');
133 local $Catalyst::Test::default_host = 'localized.com';
134 customize( $req, { host => 'customized.com' } );
135 like( $req->header('Host'), qr/customized.com/, 'opts hash takes precedence over package var' );
139 my $req = Catalyst::Utils::request('/dummy');
140 local $Catalyst::Test::default_host = 'localized.com';
141 customize( $req, { host => '' } );
142 is( $req->header('Host'), undef, 'default value can be temporarily cleared via opts hash' );
145 # Back compat test, extra args used to be ignored, now a hashref of options.
146 use_ok('Catalyst::Test', 'TestApp', 'foobar');
148 # Back compat test, ensure that request ignores anything which isn't a hash.
150 request(GET('/dummy'), 'foo');
151 }, undef, 'scalar additional param to request method ignored';
153 request(GET('/dummy'), []);
154 }, undef, 'array additional param to request method ignored';
156 my $res = request(GET('/'));
157 is $res->code, 200, 'Response code 200';
158 is $res->headers->{status}, 200, 'Back compat "status" header present';