Don't import the Test::More exports into Catalyst::Test.
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Test.pm
index 012c399..54f8a33 100644 (file)
@@ -1,24 +1,14 @@
 package Catalyst::Test;
 
-use Test::More;
+use strict;
+use warnings;
+use Test::More ();
 
 use Catalyst::Exception;
 use Catalyst::Utils;
-use Class::Inspector;
+use Class::MOP;
 use Sub::Exporter;
 
-{
-    my $import = Sub::Exporter::build_exporter({
-        groups => [ all => \&build_exports ],
-        into_level => 1,
-    });
-
-    sub import {
-        my ($self, $class) = @_;
-        $import->($self, '-all' => { class => $class });
-    }
-}
-
 sub build_exports {
     my ($self, $meth, $args, $defaults) = @_;
 
@@ -30,8 +20,8 @@ sub build_exports {
     } elsif (! $class) {
         $request = sub { Catalyst::Exception->throw("Must specify a test app: use Catalyst::Test 'TestApp'") };
     } else {
-        unless( Class::Inspector->loaded( $class ) ) {
-            require Class::Inspector->filename( $class );
+        unless (Class::MOP::is_class_loaded($class)) {
+            Class::MOP::load_class($class);
         }
         $class->import;
 
@@ -67,6 +57,24 @@ sub build_exports {
     };
 }
 
+use namespace::clean;
+our $default_host;
+
+{
+    my $import = Sub::Exporter::build_exporter({
+        groups => [ all => \&build_exports ],
+        into_level => 1,
+    });
+
+
+    sub import {
+        my ($self, $class, $opts) = @_;
+        $import->($self, '-all' => { class => $class });
+        $opts = {} unless ref $opts eq 'HASH';
+        $default_host = $opts->{default_host} if exists $opts->{default_host};
+    }
+}
+
 =head1 NAME
 
 Catalyst::Test - Test Catalyst Applications
@@ -109,6 +117,15 @@ Catalyst::Test - Test Catalyst Applications
 
     ok( get('/foo') =~ /bar/ );
 
+    # mock virtual hosts
+    use Catalyst::Test 'MyApp', { default_host => 'myapp.com' };
+    like( get('/whichhost'), qr/served by myapp.com/ );
+    like( get( '/whichhost', { host => 'yourapp.com' } ), qr/served by yourapp.com/ );
+    {
+        local $Catalyst::Test::default_host = 'otherapp.com';
+        like( get('/whichhost'), qr/served by otherapp.com/ );
+    }
+
 =head1 DESCRIPTION
 
 This module allows you to make requests to a Catalyst application either without
@@ -141,9 +158,11 @@ method and the L<request> method below:
 
 =head2 request
 
-Returns a C<HTTP::Response> object.
+Returns a C<HTTP::Response> object. Accepts an optional hashref for request
+header configuration; currently only supports setting 'host' value.
 
     my $res = request('foo/bar?test=1');
+    my $virtual_res = request('foo/bar?test=1', {host => 'virtualhost.com'});
 
 =head2 local_request
 
@@ -157,6 +176,7 @@ sub local_request {
     require HTTP::Request::AsCGI;
 
     my $request = Catalyst::Utils::request( shift(@_) );
+    _customize_request($request, @_);
     my $cgi     = HTTP::Request::AsCGI->new( $request, %ENV )->setup;
 
     $class->handle_request;
@@ -179,6 +199,8 @@ sub remote_request {
     my $request = Catalyst::Utils::request( shift(@_) );
     my $server  = URI->new( $ENV{CATALYST_SERVER} );
 
+    _customize_request($request, @_);
+
     if ( $server->path =~ m|^(.+)?/$| ) {
         my $path = $1;
         $server->path("$path") if $path;    # need to be quoted
@@ -218,6 +240,10 @@ sub remote_request {
             keep_alive   => 1,
             max_redirect => 0,
             timeout      => 60,
+            
+            # work around newer LWP max_redirect 0 bug
+            # http://rt.cpan.org/Ticket/Display.html?id=40260
+            requests_redirectable => [],
         );
 
         $agent->env_proxy;
@@ -226,6 +252,15 @@ sub remote_request {
     return $agent->request($request);
 }
 
+sub _customize_request {
+    my $request = shift;
+    my $opts = pop(@_) || {};
+    $opts = {} unless ref($opts) eq 'HASH';
+    if ( my $host = exists $opts->{host} ? $opts->{host} : $default_host  ) {
+        $request->header( 'Host' => $host );
+    }
+}
+
 =head2 action_ok
 
 Fetches the given url and check that the request was successful