Commit | Line | Data |
c7ded7aa |
1 | #!perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
26dd6d9f |
6 | use FindBin; |
02fee6c6 |
7 | use lib "$FindBin::Bin/../lib"; |
12755afc |
8 | use Test::More tests => 61; |
d258fcb2 |
9 | use FindBin qw/$Bin/; |
b1ead5a2 |
10 | use lib "$Bin/../lib"; |
d9d04ded |
11 | use Catalyst::Utils; |
4348c28b |
12 | use HTTP::Request::Common; |
6f8be318 |
13 | use Test::Exception; |
c7ded7aa |
14 | |
26dd6d9f |
15 | my $Class = 'Catalyst::Test'; |
16 | my $App = 'TestApp'; |
17 | my $Pkg = __PACKAGE__; |
18 | my $Url = 'http://localhost/'; |
19 | my $Content = "root index"; |
c7ded7aa |
20 | |
26dd6d9f |
21 | my %Meth = ( |
4fbc0e85 |
22 | $Pkg => [qw|get request ctx_request|], # exported |
26dd6d9f |
23 | $Class => [qw|local_request remote_request|], # not exported |
24 | ); |
c7ded7aa |
25 | |
26dd6d9f |
26 | ### make sure we're not trying to connect to a remote host -- these are local tests |
702729f5 |
27 | local $ENV{CATALYST_SERVER}; |
c7ded7aa |
28 | |
26dd6d9f |
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: { |
702729f5 |
41 | |
26dd6d9f |
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" ); |
702729f5 |
51 | like( $@, qr/$Class/, |
26dd6d9f |
52 | " Error filled with expected content for '$meth'" ); |
702729f5 |
53 | } } |
26dd6d9f |
54 | } |
702729f5 |
55 | } |
56 | |
57 | ### simple tests for exported methods |
26dd6d9f |
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 {}; |
702729f5 |
62 | ok( $Class->import( $App ), |
26dd6d9f |
63 | "Loading $Class for App $App" ); |
64 | } |
702729f5 |
65 | |
26dd6d9f |
66 | ### test exported methods again |
67 | for my $meth ( @{ $Meth{$Pkg} } ) { SKIP: { |
68 | |
f2e13bbd |
69 | ### do a call, we should get a result and perhaps a $c if it's 'ctx_request'; |
26dd6d9f |
70 | my ($res, $c) = eval { $Pkg->can($meth)->( $Url ) }; |
702729f5 |
71 | |
26dd6d9f |
72 | ok( 1, " Called $Pkg->$meth( $Url )" ); |
73 | ok( !$@, " No critical error $@" ); |
74 | ok( $res, " Result obtained" ); |
702729f5 |
75 | |
26dd6d9f |
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, |
702729f5 |
79 | " Content as expected: $res_as_string" ); |
80 | |
f2e13bbd |
81 | ### some tests for 'ctx_request' |
82 | skip "Context tests skipped for '$meth'", 6 unless $meth eq 'ctx_request'; |
702729f5 |
83 | |
26dd6d9f |
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" ); |
12755afc |
92 | ok( $res->request, " Response has request object" ); |
93 | lives_and { is( $res->request->uri, $Url) } |
94 | " Request object has correct url"; |
26dd6d9f |
95 | } } |
96 | } |
d9d04ded |
97 | |
702729f5 |
98 | ### perl5.8.8 + cat 5.80's Cat::Test->ctx_request didn't return $c the 2nd |
ba151d0d |
99 | ### time it was invoked. Without tracking the bug down all the way, it was |
100 | ### clearly related to the Moose'ification of Cat::Test and a scoping issue |
101 | ### with a 'my'd variable. Since the same code works fine in 5.10, a bug in |
102 | ### either Moose or perl 5.8 is suspected. |
103 | { ok( 1, "Testing consistency of ctx_request()" ); |
104 | for( 1..2 ) { |
105 | my($res, $c) = ctx_request( $Url ); |
106 | ok( $c, " Call $_: Context object returned" ); |
107 | } |
702729f5 |
108 | } |
ba151d0d |
109 | |
4348c28b |
110 | # FIXME - These vhosts in tests tests should be somewhere else... |
111 | |
d9d04ded |
112 | sub customize { Catalyst::Test::_customize_request(@_) } |
113 | |
114 | { |
115 | my $req = Catalyst::Utils::request('/dummy'); |
116 | customize( $req ); |
117 | is( $req->header('Host'), undef, 'normal request is unmodified' ); |
118 | } |
119 | |
120 | { |
121 | my $req = Catalyst::Utils::request('/dummy'); |
122 | customize( $req, { host => 'customized.com' } ); |
123 | like( $req->header('Host'), qr/customized.com/, 'request is customizable via opts hash' ); |
124 | } |
125 | |
126 | { |
127 | my $req = Catalyst::Utils::request('/dummy'); |
128 | local $Catalyst::Test::default_host = 'localized.com'; |
129 | customize( $req ); |
130 | like( $req->header('Host'), qr/localized.com/, 'request is customizable via package var' ); |
131 | } |
132 | |
133 | { |
134 | my $req = Catalyst::Utils::request('/dummy'); |
135 | local $Catalyst::Test::default_host = 'localized.com'; |
136 | customize( $req, { host => 'customized.com' } ); |
137 | like( $req->header('Host'), qr/customized.com/, 'opts hash takes precedence over package var' ); |
138 | } |
139 | |
140 | { |
141 | my $req = Catalyst::Utils::request('/dummy'); |
142 | local $Catalyst::Test::default_host = 'localized.com'; |
143 | customize( $req, { host => '' } ); |
144 | is( $req->header('Host'), undef, 'default value can be temporarily cleared via opts hash' ); |
145 | } |
d258fcb2 |
146 | |
147 | # Back compat test, extra args used to be ignored, now a hashref of options. |
148 | use_ok('Catalyst::Test', 'TestApp', 'foobar'); |
4348c28b |
149 | |
150 | # Back compat test, ensure that request ignores anything which isn't a hash. |
151 | lives_ok { |
152 | request(GET('/dummy'), 'foo'); |
153 | } 'scalar additional param to request method ignored'; |
154 | lives_ok { |
155 | request(GET('/dummy'), []); |
156 | } 'array additional param to request method ignored'; |
269194b4 |
157 | |