Commit | Line | Data |
c7ded7aa |
1 | use strict; |
2 | use warnings; |
3 | |
ec6213a0 |
4 | use Test::More; |
d258fcb2 |
5 | use FindBin qw/$Bin/; |
b1ead5a2 |
6 | use lib "$Bin/../lib"; |
d9d04ded |
7 | use Catalyst::Utils; |
4348c28b |
8 | use HTTP::Request::Common; |
2a56ace9 |
9 | use Test::Fatal; |
c7ded7aa |
10 | |
26dd6d9f |
11 | my $Class = 'Catalyst::Test'; |
12 | my $App = 'TestApp'; |
13 | my $Pkg = __PACKAGE__; |
14 | my $Url = 'http://localhost/'; |
15 | my $Content = "root index"; |
c7ded7aa |
16 | |
26dd6d9f |
17 | my %Meth = ( |
4fbc0e85 |
18 | $Pkg => [qw|get request ctx_request|], # exported |
26dd6d9f |
19 | $Class => [qw|local_request remote_request|], # not exported |
20 | ); |
c7ded7aa |
21 | |
26dd6d9f |
22 | ### make sure we're not trying to connect to a remote host -- these are local tests |
702729f5 |
23 | local $ENV{CATALYST_SERVER}; |
c7ded7aa |
24 | |
ec6213a0 |
25 | use Catalyst::Test (); |
26dd6d9f |
26 | |
27 | ### check available methods |
28 | { ### turn of redefine warnings, we'll get new subs exported |
29 | ### XXX 'no warnings' and 'local $^W' wont work as warnings are turned on in |
30 | ### test.pm, so trap them for now --kane |
31 | { local $SIG{__WARN__} = sub {}; |
32 | ok( $Class->import, "Argumentless import for methods only" ); |
33 | } |
34 | |
35 | while( my($class, $meths) = each %Meth ) { |
36 | for my $meth ( @$meths ) { SKIP: { |
702729f5 |
37 | |
26dd6d9f |
38 | ### method available? |
39 | can_ok( $class, $meth ); |
40 | |
41 | ### only for exported methods |
42 | skip "Error tests only for exported methods", 2 unless $class eq $Pkg; |
43 | |
44 | ### check error conditions |
45 | eval { $class->can($meth)->( $Url ) }; |
46 | ok( $@, " $meth without app gives error" ); |
702729f5 |
47 | like( $@, qr/$Class/, |
26dd6d9f |
48 | " Error filled with expected content for '$meth'" ); |
702729f5 |
49 | } } |
26dd6d9f |
50 | } |
702729f5 |
51 | } |
52 | |
53 | ### simple tests for exported methods |
26dd6d9f |
54 | { ### turn of redefine warnings, we'll get new subs exported |
55 | ### XXX 'no warnings' and 'local $^W' wont work as warnings are turned on in |
56 | ### test.pm, so trap them for now --kane |
57 | { local $SIG{__WARN__} = sub {}; |
702729f5 |
58 | ok( $Class->import( $App ), |
26dd6d9f |
59 | "Loading $Class for App $App" ); |
60 | } |
702729f5 |
61 | |
26dd6d9f |
62 | ### test exported methods again |
63 | for my $meth ( @{ $Meth{$Pkg} } ) { SKIP: { |
64 | |
f2e13bbd |
65 | ### do a call, we should get a result and perhaps a $c if it's 'ctx_request'; |
26dd6d9f |
66 | my ($res, $c) = eval { $Pkg->can($meth)->( $Url ) }; |
702729f5 |
67 | |
26dd6d9f |
68 | ok( 1, " Called $Pkg->$meth( $Url )" ); |
69 | ok( !$@, " No critical error $@" ); |
70 | ok( $res, " Result obtained" ); |
702729f5 |
71 | |
26dd6d9f |
72 | ### get the content as a string, to make sure we got what we expected |
73 | my $res_as_string = $meth eq 'get' ? $res : $res->content; |
74 | is( $res_as_string, $Content, |
702729f5 |
75 | " Content as expected: $res_as_string" ); |
76 | |
f2e13bbd |
77 | ### some tests for 'ctx_request' |
78 | skip "Context tests skipped for '$meth'", 6 unless $meth eq 'ctx_request'; |
702729f5 |
79 | |
26dd6d9f |
80 | ok( $c, " Context object returned" ); |
81 | isa_ok( $c, $App, " Object" ); |
82 | is( $c->request->uri, $Url, |
83 | " Url recorded in request" ); |
84 | is( $c->response->body, $Content, |
85 | " Content recorded in response" ); |
86 | ok( $c->stash, " Stash accessible" ); |
87 | ok( $c->action, " Action object accessible" ); |
12755afc |
88 | ok( $res->request, " Response has request object" ); |
2a56ace9 |
89 | is exception { is( $res->request->uri, $Url) }, undef, |
12755afc |
90 | " Request object has correct url"; |
26dd6d9f |
91 | } } |
92 | } |
d9d04ded |
93 | |
702729f5 |
94 | ### perl5.8.8 + cat 5.80's Cat::Test->ctx_request didn't return $c the 2nd |
ba151d0d |
95 | ### time it was invoked. Without tracking the bug down all the way, it was |
96 | ### clearly related to the Moose'ification of Cat::Test and a scoping issue |
97 | ### with a 'my'd variable. Since the same code works fine in 5.10, a bug in |
98 | ### either Moose or perl 5.8 is suspected. |
99 | { ok( 1, "Testing consistency of ctx_request()" ); |
100 | for( 1..2 ) { |
101 | my($res, $c) = ctx_request( $Url ); |
102 | ok( $c, " Call $_: Context object returned" ); |
103 | } |
702729f5 |
104 | } |
ba151d0d |
105 | |
4348c28b |
106 | # FIXME - These vhosts in tests tests should be somewhere else... |
107 | |
759d380e |
108 | sub customize { Catalyst::Test::_customize_request($_[0], {}, @_[1 .. $#_]) } |
d9d04ded |
109 | |
110 | { |
111 | my $req = Catalyst::Utils::request('/dummy'); |
112 | customize( $req ); |
113 | is( $req->header('Host'), undef, 'normal request is unmodified' ); |
114 | } |
115 | |
116 | { |
117 | my $req = Catalyst::Utils::request('/dummy'); |
118 | customize( $req, { host => 'customized.com' } ); |
119 | like( $req->header('Host'), qr/customized.com/, 'request is customizable via opts hash' ); |
120 | } |
121 | |
122 | { |
123 | my $req = Catalyst::Utils::request('/dummy'); |
124 | local $Catalyst::Test::default_host = 'localized.com'; |
125 | customize( $req ); |
126 | like( $req->header('Host'), qr/localized.com/, 'request is customizable via package var' ); |
127 | } |
128 | |
129 | { |
130 | my $req = Catalyst::Utils::request('/dummy'); |
131 | local $Catalyst::Test::default_host = 'localized.com'; |
132 | customize( $req, { host => 'customized.com' } ); |
133 | like( $req->header('Host'), qr/customized.com/, 'opts hash takes precedence over package var' ); |
134 | } |
135 | |
136 | { |
137 | my $req = Catalyst::Utils::request('/dummy'); |
138 | local $Catalyst::Test::default_host = 'localized.com'; |
139 | customize( $req, { host => '' } ); |
140 | is( $req->header('Host'), undef, 'default value can be temporarily cleared via opts hash' ); |
141 | } |
d258fcb2 |
142 | |
143 | # Back compat test, extra args used to be ignored, now a hashref of options. |
144 | use_ok('Catalyst::Test', 'TestApp', 'foobar'); |
4348c28b |
145 | |
146 | # Back compat test, ensure that request ignores anything which isn't a hash. |
2a56ace9 |
147 | is exception { |
4348c28b |
148 | request(GET('/dummy'), 'foo'); |
2a56ace9 |
149 | }, undef, 'scalar additional param to request method ignored'; |
150 | is exception { |
4348c28b |
151 | request(GET('/dummy'), []); |
2a56ace9 |
152 | }, undef, 'array additional param to request method ignored'; |
269194b4 |
153 | |
89222c2a |
154 | my $res = request(GET('/')); |
155 | is $res->code, 200, 'Response code 200'; |
156 | is $res->headers->{status}, 200, 'Back compat "status" header present'; |
157 | |
ec6213a0 |
158 | done_testing; |