1 package Test::WWW::Mechanize::Catalyst;
7 require Catalyst::Test; # Do not call import
10 use Test::WWW::Mechanize;
12 use base qw(Test::WWW::Mechanize);
14 our $VERSION = '0.45';
15 my $Test = Test::Builder->new();
17 # the reason for the auxiliary package is that both WWW::Mechanize and
18 # Catalyst::Test have a subroutine named 'request'
22 my ($class, %args) = @_;
25 if (exists $args{catalyst_app}) {
26 $app = $args{catalyst_app};
28 require Class::Inspector->filename( $app )
29 unless Class::Inspector->loaded( $app );
30 } elsif (!defined $APP_CLASS) {
31 croak 'Please provide a catalyst_app option or import Test::WWW::Mechanize::Catalyst with a class name';
36 my $self = $class->SUPER::new(%args);
37 $self->{catalyst_app} = $app;
42 my ( $self, $value ) = @_;
43 return $self->{allow_external} unless defined $value;
44 $self->{allow_external} = $value;
48 my ( $self, $request ) = @_;
49 $self->cookie_jar->add_cookie_header($request) if $self->cookie_jar;
51 if ( $self->{allow_external} ) {
52 unless ( $request->uri->as_string =~ m{^/}
53 || $request->uri->host eq 'localhost' )
55 return $self->SUPER::_make_request($request);
59 my $uri = $request->uri;
60 if ($uri->as_string =~ m{^/}) {
62 $uri->host('localhost');
64 my @creds = $self->get_basic_credentials( "Basic", $uri );
65 $request->authorization_basic( @creds ) if @creds;
67 my $response = Catalyst::Test::local_request($self->{catalyst_app}, $request);
68 $response->header( 'Content-Base', $request->uri );
69 $response->request($request);
70 if ( $request->uri->as_string =~ m{^/} ) {
72 URI->new( 'http://localhost:80/' . $request->uri->as_string ) );
74 $self->cookie_jar->extract_cookies($response) if $self->cookie_jar;
76 # fail tests under the Catalyst debug screen
77 if ( !$self->{catalyst_debug}
78 && $response->code == 500
79 && $response->content =~ /on Catalyst \d+\.\d+/ )
82 = ( $response->content =~ /<code class="error">(.*?)<\/code>/s );
83 $error ||= "unknown error";
84 decode_entities($error);
85 $Test->diag("Catalyst error screen: $error");
86 $response->content('');
87 $response->content_type('');
90 # check if that was a redirect
91 if ( $response->header('Location')
92 && $self->redirect_ok( $request, $response ) )
95 # remember the old response
96 my $old_response = $response;
98 # *where* do they want us to redirect to?
99 my $location = $old_response->header('Location');
101 # no-one *should* be returning non-absolute URLs, but if they
102 # are then we'd better cope with it. Let's create a new URI, using
103 # our request as the base.
104 my $uri = URI->new_abs( $location, $request->uri )->as_string;
106 # make a new response, and save the old response in it
107 $response = $self->_make_request( HTTP::Request->new( GET => $uri ) );
108 my $end_of_chain = $response;
109 while ( $end_of_chain->previous ) # keep going till the end
111 $end_of_chain = $end_of_chain->previous;
113 $end_of_chain->previous($old_response); # ...and add us to it
115 $response->{_raw_content} = $response->content;
122 my ($class, $app) = @_;
124 require Class::Inspector->filename( $app )
125 unless Class::Inspector->loaded( $app );
137 Test::WWW::Mechanize::Catalyst - Test::WWW::Mechanize for Catalyst
141 # We're in a t/*.t test script...
142 # To test a Catalyst application named 'Catty':
143 use Test::WWW::Mechanize::Catalyst 'Catty';
145 my $mech = Test::WWW::Mechanize::Catalyst->new;
146 $mech->get_ok("/"); # no hostname needed
147 is($mech->ct, "text/html");
148 $mech->title_is("Root", "On the root page");
149 $mech->content_contains("This is the root page", "Correct content");
150 $mech->follow_link_ok({text => 'Hello'}, "Click on Hello");
151 # ... and all other Test::WWW::Mechanize methods
155 L<Catalyst> is an elegant MVC Web Application
156 Framework. L<Test::WWW::Mechanize> is a subclass of L<WWW::Mechanize> that
157 incorporates features for web application testing. The
158 L<Test::WWW::Mechanize::Catalyst> module meshes the two to allow easy
159 testing of L<Catalyst> applications without starting up a web server.
161 Testing web applications has always been a bit tricky, normally
162 starting a web server for your application and making real HTTP
163 requests to it. This module allows you to test L<Catalyst> web
164 applications but does not start a server or issue HTTP
165 requests. Instead, it passes the HTTP request object directly to
166 L<Catalyst>. Thus you do not need to use a real hostname:
167 "http://localhost/" will do. However, this is optional. The following
168 two lines of code do exactly the same thing:
170 $mech->get_ok('/action');
171 $mech->get_ok('http://localhost/action');
173 Links which do not begin with / or are not for localhost can be handled
174 as normal Web requests - this is handy if you have an external
175 single sign-on system. You must set allow_external to true for this:
177 $m->allow_external(1);
179 You can also test a remote server by setting the environment variable
180 CATALYST_SERVER, for example:
182 $ CATALYST_SERVER=http://example.com/myapp prove -l t
184 will run the same tests on the application running at
185 http://example.com/myapp regardless of whether or not you specify
186 http:://localhost for Test::WWW::Mechanize::Catalyst.
188 This makes testing fast and easy. L<Test::WWW::Mechanize> provides
189 functions for common web testing scenarios. For example:
191 $mech->get_ok( $page );
192 $mech->title_is( "Invoice Status", "Make sure we're on the invoice page" );
193 $mech->content_contains( "Andy Lester", "My name somewhere" );
194 $mech->content_like( qr/(cpan|perl)\.org/, "Link to perl.org or CPAN" );
196 This module supports cookies automatically.
198 To use this module you must pass it the name of the application. See
201 Note that Catalyst has a special developing feature: the debug
202 screen. By default this module will treat responses which are the
203 debug screen as failures. If you actually want to test debug screens,
206 $m->{catalyst_debug} = 1;
208 An alternative to this module is L<Catalyst::Test>.
214 Behaves like, and calls, L<WWW::Mechanize>'s C<new> method. Any parms
215 passed in get passed to WWW::Mechanize's constructor. Note that we
216 need to pass the name of the Catalyst application to the "use":
218 use Test::WWW::Mechanize::Catalyst 'Catty';
219 my $mech = Test::WWW::Mechanize::Catalyst->new;
223 =head2 allow_external
225 Links which do not begin with / or are not for localhost can be handled
226 as normal Web requests - this is handy if you have an external
227 single sign-on system. You must set allow_external to true for this:
229 $m->allow_external(1);
231 =head2 $mech->get_ok($url, [ \%LWP_options ,] $desc)
233 A wrapper around WWW::Mechanize's get(), with similar options, except the
234 second argument needs to be a hash reference, not a hash. Returns true or
237 =head2 $mech->title_is( $str [, $desc ] )
239 Tells if the title of the page is the given string.
241 $mech->title_is( "Invoice Summary" );
243 =head2 $mech->title_like( $regex [, $desc ] )
245 Tells if the title of the page matches the given regex.
247 $mech->title_like( qr/Invoices for (.+)/
249 =head2 $mech->title_unlike( $regex [, $desc ] )
251 Tells if the title of the page matches the given regex.
253 $mech->title_unlike( qr/Invoices for (.+)/
255 =head2 $mech->content_is( $str [, $desc ] )
257 Tells if the content of the page matches the given string
259 =head2 $mech->content_contains( $str [, $desc ] )
261 Tells if the content of the page contains I<$str>.
263 =head2 $mech->content_lacks( $str [, $desc ] )
265 Tells if the content of the page lacks I<$str>.
267 =head2 $mech->content_like( $regex [, $desc ] )
269 Tells if the content of the page matches I<$regex>.
271 =head2 $mech->content_unlike( $regex [, $desc ] )
273 Tells if the content of the page does NOT match I<$regex>.
275 =head2 $mech->page_links_ok( [ $desc ] )
277 Follow all links on the current page and test for HTTP status 200
279 $mech->page_links_ok('Check all links');
281 =head2 $mech->page_links_content_like( $regex,[ $desc ] )
283 Follow all links on the current page and test their contents for I<$regex>.
285 $mech->page_links_content_like( qr/foo/,
286 'Check all links contain "foo"' );
288 =head2 $mech->page_links_content_unlike( $regex,[ $desc ] )
290 Follow all links on the current page and test their contents do not
291 contain the specified regex.
293 $mech->page_links_content_unlike(qr/Restricted/,
294 'Check all links do not contain Restricted');
296 =head2 $mech->links_ok( $links [, $desc ] )
298 Check the current page for specified links and test for HTTP status
299 200. The links may be specified as a reference to an array containing
300 L<WWW::Mechanize::Link> objects, an array of URLs, or a scalar URL
303 my @links = $mech->find_all_links( url_regex => qr/cnn\.com$/ );
304 $mech->links_ok( \@links, 'Check all links for cnn.com' );
306 my @links = qw( index.html search.html about.html );
307 $mech->links_ok( \@links, 'Check main links' );
309 $mech->links_ok( 'index.html', 'Check link to index' );
311 =head2 $mech->link_status_is( $links, $status [, $desc ] )
313 Check the current page for specified links and test for HTTP status
314 passed. The links may be specified as a reference to an array
315 containing L<WWW::Mechanize::Link> objects, an array of URLs, or a
318 my @links = $mech->links();
319 $mech->link_status_is( \@links, 403,
320 'Check all links are restricted' );
322 =head2 $mech->link_status_isnt( $links, $status [, $desc ] )
324 Check the current page for specified links and test for HTTP status
325 passed. The links may be specified as a reference to an array
326 containing L<WWW::Mechanize::Link> objects, an array of URLs, or a
329 my @links = $mech->links();
330 $mech->link_status_isnt( \@links, 404,
331 'Check all links are not 404' );
333 =head2 $mech->link_content_like( $links, $regex [, $desc ] )
335 Check the current page for specified links and test the content of
336 each against I<$regex>. The links may be specified as a reference to
337 an array containing L<WWW::Mechanize::Link> objects, an array of URLs,
338 or a scalar URL name.
340 my @links = $mech->links();
341 $mech->link_content_like( \@links, qr/Restricted/,
342 'Check all links are restricted' );
344 =head2 $mech->link_content_unlike( $links, $regex [, $desc ] )
346 Check the current page for specified links and test the content of each
347 does not match I<$regex>. The links may be specified as a reference to
348 an array containing L<WWW::Mechanize::Link> objects, an array of URLs,
349 or a scalar URL name.
351 my @links = $mech->links();
352 $mech->link_content_like( \@links, qr/Restricted/,
353 'Check all links are restricted' );
355 =head2 follow_link_ok( \%parms [, $comment] )
357 Makes a C<follow_link()> call and executes tests on the results.
358 The link must be found, and then followed successfully. Otherwise,
361 I<%parms> is a hashref containing the parms to pass to C<follow_link()>.
362 Note that the parms to C<follow_link()> are a hash whereas the parms to
363 this function are a hashref. You have to call this function like:
365 $agent->follow_like_ok( {n=>3}, "looking for 3rd link" );
367 As with other test functions, C<$comment> is optional. If it is supplied
368 then it will display when running the test harness in verbose mode.
370 Returns true value if the specified link was found and followed
371 successfully. The HTTP::Response object returned by follow_link()
376 Related modules which may be of interest: L<Catalyst>,
377 L<Test::WWW::Mechanize>, L<WWW::Mechanize>.
381 Current Maintainer: Ash Berlin C<< <ash@cpan.org> >>
383 Leon Brocard, C<< <acme@astray.com> >>
387 Copyright (C) 2005-8, Leon Brocard
391 This module is free software; you can redistribute it or modify it
392 under the same terms as Perl itself.