X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FTest%2FWWW%2FMechanize%2FCatalyst.pm;h=4248f49c460802a3f34ca315ded30b55c03c6d72;hb=cfd812d6df038fa098c69af4e8a80e0342e7b1ce;hp=f6964ce27c5ed8ffad3a52c68e80d84021517388;hpb=6bc863629dfa3bec1d938eb5a7af7ef68fd3849f;p=catagits%2FTest-WWW-Mechanize-Catalyst.git diff --git a/lib/Test/WWW/Mechanize/Catalyst.pm b/lib/Test/WWW/Mechanize/Catalyst.pm index f6964ce..4248f49 100644 --- a/lib/Test/WWW/Mechanize/Catalyst.pm +++ b/lib/Test/WWW/Mechanize/Catalyst.pm @@ -1,43 +1,63 @@ package Test::WWW::Mechanize::Catalyst; -use strict; -use warnings; + +use Moose; + +use Carp qw/croak/; +require Catalyst::Test; # Do not call import use Encode qw(); use HTML::Entities; use Test::WWW::Mechanize; -use base qw(Test::WWW::Mechanize); -our $VERSION = '0.45'; -my $Test = Test::Builder->new(); -# the reason for the auxiliary package is that both WWW::Mechanize and -# Catalyst::Test have a subroutine named 'request' +extends 'Test::WWW::Mechanize', 'Moose::Object'; + +use namespace::clean -execept => 'meta'; -sub allow_external { - my ( $self, $value ) = @_; - return $self->{allow_external} unless defined $value; - $self->{allow_external} = $value; +our $VERSION = '0.50_2'; +our $APP_CLASS; +my $Test = Test::Builder->new(); + +has catalyst_app => ( + is => 'ro', + predicate => 'has_catalyst_app', +); + +has allow_external => ( + is => 'rw', + isa => 'Bool', + default => 0 +); + +has host => ( + is => 'rw', + isa => 'Str', + clearer => 'clear_host', + predicate => 'has_host', +); + +sub new { + my $class = shift; + + my $obj = $class->SUPER::new(@_); + my $self = $class->meta->new_object( + __INSTANCE__ => $obj, + ($APP_CLASS ? (catalyst_app => $APP_CLASS) : () ), + @_ + ); + + unless ($ENV{CATALYST_SERVER}) { + croak "catalyst_app attribute is required unless CATALYST_SERVER env variable is set" + unless $self->has_catalyst_app; + Class::MOP::load_class($self->catalyst_app) + unless (Class::MOP::is_class_loaded($self->catalyst_app)); + } + + return $self; } sub _make_request { my ( $self, $request ) = @_; - $self->cookie_jar->add_cookie_header($request) if $self->cookie_jar; - if ( $self->{allow_external} ) { - unless ( $request->uri->as_string =~ m{^/} - || $request->uri->host eq 'localhost' ) - { - return $self->SUPER::_make_request($request); - } - } - - $request->authorization_basic( - LWP::UserAgent->get_basic_credentials( - undef, "Basic", $request->uri - ) - ) - if LWP::UserAgent->get_basic_credentials( undef, "Basic", - $request->uri ); - - my $response = Test::WWW::Mechanize::Catalyst::Aux::request($request); + my $response = $self->_do_catalyst_request($request); $response->header( 'Content-Base', $request->uri ); $response->request($request); if ( $request->uri->as_string =~ m{^/} ) { @@ -91,21 +111,57 @@ sub _make_request { return $response; } -sub import { - Test::WWW::Mechanize::Catalyst::Aux::import(@_); -} +sub _do_catalyst_request { + my ($self, $request) = @_; + + $self->cookie_jar->add_cookie_header($request) if $self->cookie_jar; + + # Woe betide anyone who unsets CATALYST_SERVER + return Catalyst::Test::remote_request($request) + if $ENV{CATALYST_SERVER}; + + my $uri = $request->uri; + if ($uri->as_string =~ m{^/}) { + $uri->scheme('http'); + $uri->host('localhost'); + } + + + # If there's no Host header, set one. + unless ($request->header('Host')) { + my $host = $self->has_host + ? $self->host + : $uri->host; -package Test::WWW::Mechanize::Catalyst::Aux; + $request->header('Host', $host); + } + + if ( $self->{allow_external} ) { + unless ( $request->uri->as_string =~ m{^/} + || $request->uri->host eq 'localhost' ) + { + return $self->SUPER::_make_request($request); + } + } + + my @creds = $self->get_basic_credentials( "Basic", $uri ); + $request->authorization_basic( @creds ) if @creds; + + return Catalyst::Test::local_request($self->{catalyst_app}, $request); +} sub import { - my ( $class, @args ) = @_; - eval { - require Catalyst::Test; - Catalyst::Test::import(@_); - }; - warn $@ if $@; + my ($class, $app) = @_; + + if (defined $app) { + Class::MOP::load_class($app) + unless (Class::MOP::is_class_loaded($app)); + $APP_CLASS = $app; + } + } + 1; __END__ @@ -117,16 +173,21 @@ Test::WWW::Mechanize::Catalyst - Test::WWW::Mechanize for Catalyst =head1 SYNOPSIS # We're in a t/*.t test script... + use Test::WWW::Mechanize::Catalyst; + # To test a Catalyst application named 'Catty': - use Test::WWW::Mechanize::Catalyst 'Catty'; + my $mech = Test::WWW::Mechanize::Catalyst->new(catalyst_app => 'Catty'); - my $mech = Test::WWW::Mechanize::Catalyst->new; $mech->get_ok("/"); # no hostname needed is($mech->ct, "text/html"); $mech->title_is("Root", "On the root page"); $mech->content_contains("This is the root page", "Correct content"); $mech->follow_link_ok({text => 'Hello'}, "Click on Hello"); # ... and all other Test::WWW::Mechanize methods + + # White label site testing + $mech->host("foo.com"); + $mech->get_ok("/"); =head1 DESCRIPTION @@ -206,6 +267,24 @@ single sign-on system. You must set allow_external to true for this: $m->allow_external(1); +head2 catalyst_app + +The name of the Catalyst app which we are testing against. Read-only. + +=head2 host + +The host value to set the "Host:" HTTP header to, if none is present already in +the request. If not set (default) then Catalyst::Test will set this to +localhost:80 + +=head2 clear_host + +Unset the host attribute. + +=head2 has_host + +Do we have a value set for the host attribute + =head2 $mech->get_ok($url, [ \%LWP_options ,] $desc) A wrapper around WWW::Mechanize's get(), with similar options, except the @@ -356,11 +435,13 @@ L, L. =head1 AUTHOR -Leon Brocard, C<< >> +Ash Berlin C<< >> (current maintiner) + +Original Author: Leon Brocard, C<< >> =head1 COPYRIGHT -Copyright (C) 2005-7, Leon Brocard +Copyright (C) 2005-8, Leon Brocard =head1 LICENSE