1 package Test::WWW::Mechanize::Catalyst;
6 use Test::WWW::Mechanize;
7 use base qw(Test::WWW::Mechanize);
9 my $Test = Test::Builder->new();
11 # the reason for the auxiliary package is that both WWW::Mechanize and
12 # Catalyst::Test have a subroutine named 'request'
15 my ( $self, $value ) = @_;
16 return $self->{allow_external} unless defined $value;
17 $self->{allow_external} = $value;
21 my ( $self, $request ) = @_;
22 $self->cookie_jar->add_cookie_header($request) if $self->cookie_jar;
24 if ( $self->{allow_external} ) {
25 unless ( $request->uri->as_string =~ m{^/}
26 || $request->uri->host eq 'localhost' )
28 return $self->SUPER::_make_request($request);
32 my @creds = $self->get_basic_credentials( "Basic", $request->uri );
33 $request->authorization_basic( @creds ) if @creds;
35 my $response = Test::WWW::Mechanize::Catalyst::Aux::request($request);
36 $response->header( 'Content-Base', $request->uri );
37 $response->request($request);
38 if ( $request->uri->as_string =~ m{^/} ) {
40 URI->new( 'http://localhost:80/' . $request->uri->as_string ) );
42 $self->cookie_jar->extract_cookies($response) if $self->cookie_jar;
44 # fail tests under the Catalyst debug screen
45 if ( !$self->{catalyst_debug}
46 && $response->code == 500
47 && $response->content =~ /on Catalyst \d+\.\d+/ )
50 = ( $response->content =~ /<code class="error">(.*?)<\/code>/s );
51 $error ||= "unknown error";
52 decode_entities($error);
53 $Test->diag("Catalyst error screen: $error");
54 $response->content('');
55 $response->content_type('');
58 # check if that was a redirect
59 if ( $response->header('Location')
60 && $self->redirect_ok( $request, $response ) )
63 # remember the old response
64 my $old_response = $response;
66 # *where* do they want us to redirect to?
67 my $location = $old_response->header('Location');
69 # no-one *should* be returning non-absolute URLs, but if they
70 # are then we'd better cope with it. Let's create a new URI, using
71 # our request as the base.
72 my $uri = URI->new_abs( $location, $request->uri )->as_string;
74 # make a new response, and save the old response in it
75 $response = $self->_make_request( HTTP::Request->new( GET => $uri ) );
76 my $end_of_chain = $response;
77 while ( $end_of_chain->previous ) # keep going till the end
79 $end_of_chain = $end_of_chain->previous;
81 $end_of_chain->previous($old_response); # ...and add us to it
83 $response->{_raw_content} = $response->content;
90 Test::WWW::Mechanize::Catalyst::Aux::import(@_);
93 package Test::WWW::Mechanize::Catalyst::Aux;
96 my ( $class, @args ) = @_;
98 require Catalyst::Test;
99 Catalyst::Test::import(@_);
110 Test::WWW::Mechanize::Catalyst - Test::WWW::Mechanize for Catalyst
114 # We're in a t/*.t test script...
115 # To test a Catalyst application named 'Catty':
116 use Test::WWW::Mechanize::Catalyst 'Catty';
118 my $mech = Test::WWW::Mechanize::Catalyst->new;
119 $mech->get_ok("/"); # no hostname needed
120 is($mech->ct, "text/html");
121 $mech->title_is("Root", "On the root page");
122 $mech->content_contains("This is the root page", "Correct content");
123 $mech->follow_link_ok({text => 'Hello'}, "Click on Hello");
124 # ... and all other Test::WWW::Mechanize methods
128 L<Catalyst> is an elegant MVC Web Application
129 Framework. L<Test::WWW::Mechanize> is a subclass of L<WWW::Mechanize> that
130 incorporates features for web application testing. The
131 L<Test::WWW::Mechanize::Catalyst> module meshes the two to allow easy
132 testing of L<Catalyst> applications without starting up a web server.
134 Testing web applications has always been a bit tricky, normally
135 starting a web server for your application and making real HTTP
136 requests to it. This module allows you to test L<Catalyst> web
137 applications but does not start a server or issue HTTP
138 requests. Instead, it passes the HTTP request object directly to
139 L<Catalyst>. Thus you do not need to use a real hostname:
140 "http://localhost/" will do. However, this is optional. The following
141 two lines of code do exactly the same thing:
143 $mech->get_ok('/action');
144 $mech->get_ok('http://localhost/action');
146 Links which do not begin with / or are not for localhost can be handled
147 as normal Web requests - this is handy if you have an external
148 single sign-on system. You must set allow_external to true for this:
150 $m->allow_external(1);
152 You can also test a remote server by setting the environment variable
153 CATALYST_SERVER, for example:
155 $ CATALYST_SERVER=http://example.com/myapp prove -l t
157 will run the same tests on the application running at
158 http://example.com/myapp regardless of whether or not you specify
159 http:://localhost for Test::WWW::Mechanize::Catalyst.
161 This makes testing fast and easy. L<Test::WWW::Mechanize> provides
162 functions for common web testing scenarios. For example:
164 $mech->get_ok( $page );
165 $mech->title_is( "Invoice Status", "Make sure we're on the invoice page" );
166 $mech->content_contains( "Andy Lester", "My name somewhere" );
167 $mech->content_like( qr/(cpan|perl)\.org/, "Link to perl.org or CPAN" );
169 This module supports cookies automatically.
171 To use this module you must pass it the name of the application. See
174 Note that Catalyst has a special developing feature: the debug
175 screen. By default this module will treat responses which are the
176 debug screen as failures. If you actually want to test debug screens,
179 $m->{catalyst_debug} = 1;
181 An alternative to this module is L<Catalyst::Test>.
187 Behaves like, and calls, L<WWW::Mechanize>'s C<new> method. Any parms
188 passed in get passed to WWW::Mechanize's constructor. Note that we
189 need to pass the name of the Catalyst application to the "use":
191 use Test::WWW::Mechanize::Catalyst 'Catty';
192 my $mech = Test::WWW::Mechanize::Catalyst->new;
196 =head2 allow_external
198 Links which do not begin with / or are not for localhost can be handled
199 as normal Web requests - this is handy if you have an external
200 single sign-on system. You must set allow_external to true for this:
202 $m->allow_external(1);
204 =head2 $mech->get_ok($url, [ \%LWP_options ,] $desc)
206 A wrapper around WWW::Mechanize's get(), with similar options, except the
207 second argument needs to be a hash reference, not a hash. Returns true or
210 =head2 $mech->title_is( $str [, $desc ] )
212 Tells if the title of the page is the given string.
214 $mech->title_is( "Invoice Summary" );
216 =head2 $mech->title_like( $regex [, $desc ] )
218 Tells if the title of the page matches the given regex.
220 $mech->title_like( qr/Invoices for (.+)/
222 =head2 $mech->title_unlike( $regex [, $desc ] )
224 Tells if the title of the page matches the given regex.
226 $mech->title_unlike( qr/Invoices for (.+)/
228 =head2 $mech->content_is( $str [, $desc ] )
230 Tells if the content of the page matches the given string
232 =head2 $mech->content_contains( $str [, $desc ] )
234 Tells if the content of the page contains I<$str>.
236 =head2 $mech->content_lacks( $str [, $desc ] )
238 Tells if the content of the page lacks I<$str>.
240 =head2 $mech->content_like( $regex [, $desc ] )
242 Tells if the content of the page matches I<$regex>.
244 =head2 $mech->content_unlike( $regex [, $desc ] )
246 Tells if the content of the page does NOT match I<$regex>.
248 =head2 $mech->page_links_ok( [ $desc ] )
250 Follow all links on the current page and test for HTTP status 200
252 $mech->page_links_ok('Check all links');
254 =head2 $mech->page_links_content_like( $regex,[ $desc ] )
256 Follow all links on the current page and test their contents for I<$regex>.
258 $mech->page_links_content_like( qr/foo/,
259 'Check all links contain "foo"' );
261 =head2 $mech->page_links_content_unlike( $regex,[ $desc ] )
263 Follow all links on the current page and test their contents do not
264 contain the specified regex.
266 $mech->page_links_content_unlike(qr/Restricted/,
267 'Check all links do not contain Restricted');
269 =head2 $mech->links_ok( $links [, $desc ] )
271 Check the current page for specified links and test for HTTP status
272 200. The links may be specified as a reference to an array containing
273 L<WWW::Mechanize::Link> objects, an array of URLs, or a scalar URL
276 my @links = $mech->find_all_links( url_regex => qr/cnn\.com$/ );
277 $mech->links_ok( \@links, 'Check all links for cnn.com' );
279 my @links = qw( index.html search.html about.html );
280 $mech->links_ok( \@links, 'Check main links' );
282 $mech->links_ok( 'index.html', 'Check link to index' );
284 =head2 $mech->link_status_is( $links, $status [, $desc ] )
286 Check the current page for specified links and test for HTTP status
287 passed. The links may be specified as a reference to an array
288 containing L<WWW::Mechanize::Link> objects, an array of URLs, or a
291 my @links = $mech->links();
292 $mech->link_status_is( \@links, 403,
293 'Check all links are restricted' );
295 =head2 $mech->link_status_isnt( $links, $status [, $desc ] )
297 Check the current page for specified links and test for HTTP status
298 passed. The links may be specified as a reference to an array
299 containing L<WWW::Mechanize::Link> objects, an array of URLs, or a
302 my @links = $mech->links();
303 $mech->link_status_isnt( \@links, 404,
304 'Check all links are not 404' );
306 =head2 $mech->link_content_like( $links, $regex [, $desc ] )
308 Check the current page for specified links and test the content of
309 each against I<$regex>. The links may be specified as a reference to
310 an array containing L<WWW::Mechanize::Link> objects, an array of URLs,
311 or a scalar URL name.
313 my @links = $mech->links();
314 $mech->link_content_like( \@links, qr/Restricted/,
315 'Check all links are restricted' );
317 =head2 $mech->link_content_unlike( $links, $regex [, $desc ] )
319 Check the current page for specified links and test the content of each
320 does not match I<$regex>. The links may be specified as a reference to
321 an array containing L<WWW::Mechanize::Link> objects, an array of URLs,
322 or a scalar URL name.
324 my @links = $mech->links();
325 $mech->link_content_like( \@links, qr/Restricted/,
326 'Check all links are restricted' );
328 =head2 follow_link_ok( \%parms [, $comment] )
330 Makes a C<follow_link()> call and executes tests on the results.
331 The link must be found, and then followed successfully. Otherwise,
334 I<%parms> is a hashref containing the parms to pass to C<follow_link()>.
335 Note that the parms to C<follow_link()> are a hash whereas the parms to
336 this function are a hashref. You have to call this function like:
338 $agent->follow_like_ok( {n=>3}, "looking for 3rd link" );
340 As with other test functions, C<$comment> is optional. If it is supplied
341 then it will display when running the test harness in verbose mode.
343 Returns true value if the specified link was found and followed
344 successfully. The HTTP::Response object returned by follow_link()
349 Related modules which may be of interest: L<Catalyst>,
350 L<Test::WWW::Mechanize>, L<WWW::Mechanize>.
354 Leon Brocard, C<< <acme@astray.com> >>
358 Copyright (C) 2005-7, Leon Brocard
362 This module is free software; you can redistribute it or modify it
363 under the same terms as Perl itself.