1 package Test::WWW::Mechanize::Catalyst;
6 require Catalyst::Test; # Do not call import
9 use Test::WWW::Mechanize;
11 extends 'Test::WWW::Mechanize', 'Moose::Object';
13 use namespace::clean -execept => 'meta';
15 our $VERSION = '0.45';
17 my $Test = Test::Builder->new();
22 default => sub { $APP_CLASS },
25 has allow_external => (
34 clearer => 'clear_host',
35 predicate => 'has_host',
41 my $obj = $class->SUPER::new(@_);
42 my $self = $class->meta->new_object(
47 Class::MOP::load_class($self->catalyst_app)
48 unless (Class::MOP::is_class_loaded($self->catalyst_app));
54 my ( $self, $request ) = @_;
55 $self->cookie_jar->add_cookie_header($request) if $self->cookie_jar;
57 my $uri = $request->uri;
58 if ($uri->as_string =~ m{^/}) {
60 $uri->host('localhost');
63 # If there's no Host header, set one.
64 unless ($request->header('Host')) {
65 my $host = $self->has_host
69 $request->header('Host', $host);
72 if ( $self->{allow_external} ) {
73 unless ( $request->uri->as_string =~ m{^/}
74 || $request->uri->host eq 'localhost' )
76 return $self->SUPER::_make_request($request);
80 my @creds = $self->get_basic_credentials( "Basic", $uri );
81 $request->authorization_basic( @creds ) if @creds;
84 my $response = Catalyst::Test::local_request($self->{catalyst_app}, $request);
85 $response->header( 'Content-Base', $request->uri );
86 $response->request($request);
87 if ( $request->uri->as_string =~ m{^/} ) {
89 URI->new( 'http://localhost:80/' . $request->uri->as_string ) );
91 $self->cookie_jar->extract_cookies($response) if $self->cookie_jar;
93 # fail tests under the Catalyst debug screen
94 if ( !$self->{catalyst_debug}
95 && $response->code == 500
96 && $response->content =~ /on Catalyst \d+\.\d+/ )
99 = ( $response->content =~ /<code class="error">(.*?)<\/code>/s );
100 $error ||= "unknown error";
101 decode_entities($error);
102 $Test->diag("Catalyst error screen: $error");
103 $response->content('');
104 $response->content_type('');
107 # check if that was a redirect
108 if ( $response->header('Location')
109 && $self->redirect_ok( $request, $response ) )
112 # remember the old response
113 my $old_response = $response;
115 # *where* do they want us to redirect to?
116 my $location = $old_response->header('Location');
118 # no-one *should* be returning non-absolute URLs, but if they
119 # are then we'd better cope with it. Let's create a new URI, using
120 # our request as the base.
121 my $uri = URI->new_abs( $location, $request->uri )->as_string;
123 # make a new response, and save the old response in it
124 $response = $self->_make_request( HTTP::Request->new( GET => $uri ) );
125 my $end_of_chain = $response;
126 while ( $end_of_chain->previous ) # keep going till the end
128 $end_of_chain = $end_of_chain->previous;
130 $end_of_chain->previous($old_response); # ...and add us to it
132 $response->{_raw_content} = $response->content;
139 my ($class, $app) = @_;
142 Class::MOP::load_class($app)
143 unless (Class::MOP::is_class_loaded($app));
156 Test::WWW::Mechanize::Catalyst - Test::WWW::Mechanize for Catalyst
160 # We're in a t/*.t test script...
161 # To test a Catalyst application named 'Catty':
162 use Test::WWW::Mechanize::Catalyst 'Catty';
164 my $mech = Test::WWW::Mechanize::Catalyst->new;
165 $mech->get_ok("/"); # no hostname needed
166 is($mech->ct, "text/html");
167 $mech->title_is("Root", "On the root page");
168 $mech->content_contains("This is the root page", "Correct content");
169 $mech->follow_link_ok({text => 'Hello'}, "Click on Hello");
170 # ... and all other Test::WWW::Mechanize methods
174 L<Catalyst> is an elegant MVC Web Application
175 Framework. L<Test::WWW::Mechanize> is a subclass of L<WWW::Mechanize> that
176 incorporates features for web application testing. The
177 L<Test::WWW::Mechanize::Catalyst> module meshes the two to allow easy
178 testing of L<Catalyst> applications without starting up a web server.
180 Testing web applications has always been a bit tricky, normally
181 starting a web server for your application and making real HTTP
182 requests to it. This module allows you to test L<Catalyst> web
183 applications but does not start a server or issue HTTP
184 requests. Instead, it passes the HTTP request object directly to
185 L<Catalyst>. Thus you do not need to use a real hostname:
186 "http://localhost/" will do. However, this is optional. The following
187 two lines of code do exactly the same thing:
189 $mech->get_ok('/action');
190 $mech->get_ok('http://localhost/action');
192 Links which do not begin with / or are not for localhost can be handled
193 as normal Web requests - this is handy if you have an external
194 single sign-on system. You must set allow_external to true for this:
196 $m->allow_external(1);
198 You can also test a remote server by setting the environment variable
199 CATALYST_SERVER, for example:
201 $ CATALYST_SERVER=http://example.com/myapp prove -l t
203 will run the same tests on the application running at
204 http://example.com/myapp regardless of whether or not you specify
205 http:://localhost for Test::WWW::Mechanize::Catalyst.
207 This makes testing fast and easy. L<Test::WWW::Mechanize> provides
208 functions for common web testing scenarios. For example:
210 $mech->get_ok( $page );
211 $mech->title_is( "Invoice Status", "Make sure we're on the invoice page" );
212 $mech->content_contains( "Andy Lester", "My name somewhere" );
213 $mech->content_like( qr/(cpan|perl)\.org/, "Link to perl.org or CPAN" );
215 This module supports cookies automatically.
217 To use this module you must pass it the name of the application. See
220 Note that Catalyst has a special developing feature: the debug
221 screen. By default this module will treat responses which are the
222 debug screen as failures. If you actually want to test debug screens,
225 $m->{catalyst_debug} = 1;
227 An alternative to this module is L<Catalyst::Test>.
233 Behaves like, and calls, L<WWW::Mechanize>'s C<new> method. Any parms
234 passed in get passed to WWW::Mechanize's constructor. Note that we
235 need to pass the name of the Catalyst application to the "use":
237 use Test::WWW::Mechanize::Catalyst 'Catty';
238 my $mech = Test::WWW::Mechanize::Catalyst->new;
242 =head2 allow_external
244 Links which do not begin with / or are not for localhost can be handled
245 as normal Web requests - this is handy if you have an external
246 single sign-on system. You must set allow_external to true for this:
248 $m->allow_external(1);
252 The name of the Catalyst app which we are testing against. Read-only.
256 The host value to set the "Host:" HTTP header to, if none is present already in
257 the request. If not set (default) then Catalyst::Test will set this to
262 Unset the host attribute.
266 Do we have a value set for the host attribute
268 =head2 $mech->get_ok($url, [ \%LWP_options ,] $desc)
270 A wrapper around WWW::Mechanize's get(), with similar options, except the
271 second argument needs to be a hash reference, not a hash. Returns true or
274 =head2 $mech->title_is( $str [, $desc ] )
276 Tells if the title of the page is the given string.
278 $mech->title_is( "Invoice Summary" );
280 =head2 $mech->title_like( $regex [, $desc ] )
282 Tells if the title of the page matches the given regex.
284 $mech->title_like( qr/Invoices for (.+)/
286 =head2 $mech->title_unlike( $regex [, $desc ] )
288 Tells if the title of the page matches the given regex.
290 $mech->title_unlike( qr/Invoices for (.+)/
292 =head2 $mech->content_is( $str [, $desc ] )
294 Tells if the content of the page matches the given string
296 =head2 $mech->content_contains( $str [, $desc ] )
298 Tells if the content of the page contains I<$str>.
300 =head2 $mech->content_lacks( $str [, $desc ] )
302 Tells if the content of the page lacks I<$str>.
304 =head2 $mech->content_like( $regex [, $desc ] )
306 Tells if the content of the page matches I<$regex>.
308 =head2 $mech->content_unlike( $regex [, $desc ] )
310 Tells if the content of the page does NOT match I<$regex>.
312 =head2 $mech->page_links_ok( [ $desc ] )
314 Follow all links on the current page and test for HTTP status 200
316 $mech->page_links_ok('Check all links');
318 =head2 $mech->page_links_content_like( $regex,[ $desc ] )
320 Follow all links on the current page and test their contents for I<$regex>.
322 $mech->page_links_content_like( qr/foo/,
323 'Check all links contain "foo"' );
325 =head2 $mech->page_links_content_unlike( $regex,[ $desc ] )
327 Follow all links on the current page and test their contents do not
328 contain the specified regex.
330 $mech->page_links_content_unlike(qr/Restricted/,
331 'Check all links do not contain Restricted');
333 =head2 $mech->links_ok( $links [, $desc ] )
335 Check the current page for specified links and test for HTTP status
336 200. The links may be specified as a reference to an array containing
337 L<WWW::Mechanize::Link> objects, an array of URLs, or a scalar URL
340 my @links = $mech->find_all_links( url_regex => qr/cnn\.com$/ );
341 $mech->links_ok( \@links, 'Check all links for cnn.com' );
343 my @links = qw( index.html search.html about.html );
344 $mech->links_ok( \@links, 'Check main links' );
346 $mech->links_ok( 'index.html', 'Check link to index' );
348 =head2 $mech->link_status_is( $links, $status [, $desc ] )
350 Check the current page for specified links and test for HTTP status
351 passed. The links may be specified as a reference to an array
352 containing L<WWW::Mechanize::Link> objects, an array of URLs, or a
355 my @links = $mech->links();
356 $mech->link_status_is( \@links, 403,
357 'Check all links are restricted' );
359 =head2 $mech->link_status_isnt( $links, $status [, $desc ] )
361 Check the current page for specified links and test for HTTP status
362 passed. The links may be specified as a reference to an array
363 containing L<WWW::Mechanize::Link> objects, an array of URLs, or a
366 my @links = $mech->links();
367 $mech->link_status_isnt( \@links, 404,
368 'Check all links are not 404' );
370 =head2 $mech->link_content_like( $links, $regex [, $desc ] )
372 Check the current page for specified links and test the content of
373 each against I<$regex>. The links may be specified as a reference to
374 an array containing L<WWW::Mechanize::Link> objects, an array of URLs,
375 or a scalar URL name.
377 my @links = $mech->links();
378 $mech->link_content_like( \@links, qr/Restricted/,
379 'Check all links are restricted' );
381 =head2 $mech->link_content_unlike( $links, $regex [, $desc ] )
383 Check the current page for specified links and test the content of each
384 does not match I<$regex>. The links may be specified as a reference to
385 an array containing L<WWW::Mechanize::Link> objects, an array of URLs,
386 or a scalar URL name.
388 my @links = $mech->links();
389 $mech->link_content_like( \@links, qr/Restricted/,
390 'Check all links are restricted' );
392 =head2 follow_link_ok( \%parms [, $comment] )
394 Makes a C<follow_link()> call and executes tests on the results.
395 The link must be found, and then followed successfully. Otherwise,
398 I<%parms> is a hashref containing the parms to pass to C<follow_link()>.
399 Note that the parms to C<follow_link()> are a hash whereas the parms to
400 this function are a hashref. You have to call this function like:
402 $agent->follow_like_ok( {n=>3}, "looking for 3rd link" );
404 As with other test functions, C<$comment> is optional. If it is supplied
405 then it will display when running the test harness in verbose mode.
407 Returns true value if the specified link was found and followed
408 successfully. The HTTP::Response object returned by follow_link()
413 Related modules which may be of interest: L<Catalyst>,
414 L<Test::WWW::Mechanize>, L<WWW::Mechanize>.
418 Ash Berlin C<< <ash@cpan.org> >> (current maintiner)
420 Original Author: Leon Brocard, C<< <acme@astray.com> >>
424 Copyright (C) 2005-8, Leon Brocard
428 This module is free software; you can redistribute it or modify it
429 under the same terms as Perl itself.