include test for failure mode
[catagits/Catalyst-Runtime.git] / t / unit_load_catalyst_test.t
1 #!perl
2
3 use strict;
4 use warnings;
5
6 use FindBin;
7 use lib         "$FindBin::Bin/lib";
8 use Test::More  tests => 56;
9 use FindBin qw/$Bin/;
10 use lib "$Bin/lib";
11 use Catalyst::Utils;
12 use HTTP::Request::Common;
13 use Test::Exception;
14
15 my $Class   = 'Catalyst::Test';
16 my $App     = 'TestApp';
17 my $Pkg     = __PACKAGE__;
18 my $Url     = 'http://localhost/';
19 my $Content = "root index";
20
21 my %Meth    = (
22     $Pkg    => [qw|get request ctx_request|],          # exported
23     $Class  => [qw|local_request remote_request|],  # not exported
24 );
25
26 ### make sure we're not trying to connect to a remote host -- these are local tests
27 local $ENV{CATALYST_SERVER};                
28
29 use_ok( $Class );
30
31 ### check available methods
32 {   ### turn of redefine warnings, we'll get new subs exported
33     ### XXX 'no warnings' and 'local $^W' wont work as warnings are turned on in
34     ### test.pm, so trap them for now --kane
35     {   local $SIG{__WARN__} = sub {};
36         ok( $Class->import,     "Argumentless import for methods only" );
37     }
38
39     while( my($class, $meths) = each %Meth ) {
40         for my $meth ( @$meths ) { SKIP: {
41             
42             ### method available?
43             can_ok( $class,     $meth );
44
45             ### only for exported methods
46             skip "Error tests only for exported methods", 2 unless $class eq $Pkg;
47
48             ### check error conditions
49             eval { $class->can($meth)->( $Url ) };
50             ok( $@,             "   $meth without app gives error" );
51             like( $@, qr/$Class/, 
52                                 "       Error filled with expected content for '$meth'" );
53         } }       
54     }
55 }    
56  
57 ### simple tests for exported methods 
58 {   ### turn of redefine warnings, we'll get new subs exported
59     ### XXX 'no warnings' and 'local $^W' wont work as warnings are turned on in
60     ### test.pm, so trap them for now --kane
61     {   local $SIG{__WARN__} = sub {};
62         ok( $Class->import( $App ), 
63                                 "Loading $Class for App $App" );
64     }
65     
66     ### test exported methods again
67     for my $meth ( @{ $Meth{$Pkg} } ) { SKIP: {
68
69         ### do a call, we should get a result and perhaps a $c if it's 'ctx_request';
70         my ($res, $c) = eval { $Pkg->can($meth)->( $Url ) };
71         
72         ok( 1,                  "   Called $Pkg->$meth( $Url )" );
73         ok( !$@,                "       No critical error $@" );
74         ok( $res,               "       Result obtained" );
75         
76         ### get the content as a string, to make sure we got what we expected
77         my $res_as_string = $meth eq 'get' ? $res : $res->content;
78         is( $res_as_string, $Content,
79                                 "           Content as expected: $res_as_string" );    
80         
81         ### some tests for 'ctx_request'
82         skip "Context tests skipped for '$meth'", 6 unless $meth eq 'ctx_request';
83         
84         ok( $c,                 "           Context object returned" );
85         isa_ok( $c, $App,       "               Object" );
86         is( $c->request->uri, $Url,
87                                 "               Url recorded in request" );
88         is( $c->response->body, $Content,
89                                 "               Content recorded in response" );
90         ok( $c->stash,          "               Stash accessible" );
91         ok( $c->action,         "               Action object accessible" );
92     } }
93 }
94
95 # FIXME - These vhosts in tests tests should be somewhere else...
96
97 sub customize { Catalyst::Test::_customize_request(@_) }
98
99 {
100     my $req = Catalyst::Utils::request('/dummy');
101     customize( $req );
102     is( $req->header('Host'), undef, 'normal request is unmodified' );
103 }
104
105 {
106     my $req = Catalyst::Utils::request('/dummy');
107     customize( $req, { host => 'customized.com' } );
108     like( $req->header('Host'), qr/customized.com/, 'request is customizable via opts hash' );
109 }
110
111 {
112     my $req = Catalyst::Utils::request('/dummy');
113     local $Catalyst::Test::default_host = 'localized.com';
114     customize( $req );
115     like( $req->header('Host'), qr/localized.com/, 'request is customizable via package var' );
116 }
117
118 {
119     my $req = Catalyst::Utils::request('/dummy');
120     local $Catalyst::Test::default_host = 'localized.com';
121     customize( $req, { host => 'customized.com' } );
122     like( $req->header('Host'), qr/customized.com/, 'opts hash takes precedence over package var' );
123 }
124
125 {
126     my $req = Catalyst::Utils::request('/dummy');
127     local $Catalyst::Test::default_host = 'localized.com';
128     customize( $req, { host => '' } );
129     is( $req->header('Host'), undef, 'default value can be temporarily cleared via opts hash' );
130 }
131
132 # Back compat test, extra args used to be ignored, now a hashref of options.
133 use_ok('Catalyst::Test', 'TestApp', 'foobar');
134
135 # Back compat test, ensure that request ignores anything which isn't a hash.
136 lives_ok {
137     request(GET('/dummy'), 'foo');
138 } 'scalar additional param to request method ignored';
139 lives_ok {
140     request(GET('/dummy'), []);
141 } 'array additional param to request method ignored';
142