use Catalyst::Utils;
use Class::MOP;
use Sub::Exporter;
-use Carp;
+use Carp 'croak', 'carp';
sub _build_request_export {
my ($self, $args) = @_;
- return sub { remote_request(@_) }
+ return sub { _remote_request(@_) }
if $args->{remote};
my $class = $args->{class};
Class::MOP::load_class($class) unless Class::MOP::is_class_loaded($class);
$class->import;
- return sub { local_request( $class, @_ ) };
+ return sub { _local_request( $class, @_ ) };
}
sub _build_get_export {
},
action_ok => sub {
my $action = shift;
- return Test::More->builder->ok($request->($action)->is_success, @_);
+ my $meth = $request->($action)->request->method;
+ my @args = @_ ? @_ : ("$meth $action returns successfully");
+ return Test::More->builder->ok($request->($action)->is_success,@args);
},
action_redirect => sub {
my $action = shift;
- return Test::More->builder->ok($request->($action)->is_redirect,@_);
+ my $meth = $request->($action)->request->method;
+ my @args = @_ ? @_ : ("$meth $action returns a redirect");
+ return Test::More->builder->ok($request->($action)->is_redirect,@args);
},
action_notfound => sub {
my $action = shift;
- return Test::More->builder->is_eq($request->($action)->code,404,@_);
+ my $meth = $request->($action)->request->method;
+ my @args = @_ ? @_ : ("$meth $action returns a 404");
+ return Test::More->builder->is_eq($request->($action)->code,404,@args);
},
contenttype_is => sub {
my $action = shift;
my $res = request('foo/bar?test=1');
my $virtual_res = request('foo/bar?test=1', {host => 'virtualhost.com'});
-=head1 FUNCTIONS
-
=head2 ($res, $c) = ctx_request( ... );
Works exactly like L<request|/"$res = request( ... );">, except it also returns the Catalyst context object,
C<$c>. Note that this only works for local requests.
-=head2 $res = Catalyst::Test::local_request( $AppClass, $url );
-
-Simulate a request using L<HTTP::Request::AsCGI>.
-
=cut
-sub local_request {
+sub _local_request {
my $class = shift;
my $app = ref($class) eq "CODE" ? $class : $class->_finalized_psgi_app;
my $agent;
-=head2 $res = Catalyst::Test::remote_request( $url );
-
-Do an actual remote request using LWP.
-
-=cut
-
-sub remote_request {
-
+sub _remote_request {
require LWP::UserAgent;
my $request = Catalyst::Utils::request( shift(@_) );
if ($server->path) {
# If request path is '/', we have to add a trailing slash to the
# final request URI
- my $add_trailing = $request->uri->path eq '/';
+ my $add_trailing = ($request->uri->path eq '/' || $request->uri->path eq '') ? 1 : 0;
my @sp = split '/', $server->path;
my @rp = split '/', $request->uri->path;
return $agent->request($request);
}
+for my $name (qw(local_request remote_request)) {
+ my $fun = sub {
+ carp <<"EOW";
+Calling Catalyst::Test::${name}() directly is deprecated.
+
+Please import Catalyst::Test into your namespace and use the provided request()
+function instead.
+EOW
+ return __PACKAGE__->can("_${name}")->(@_);
+ };
+
+ no strict 'refs';
+ *$name = $fun;
+}
+
sub _customize_request {
my $request = shift;
my $extra_env = shift;
}
}
-=head2 action_ok
+=head2 action_ok($url [, $test_name ])
-Fetches the given URL and checks that the request was successful.
+Fetches the given URL and checks that the request was successful. An optional
+second argument can be given to specify the name of the test.
-=head2 action_redirect
+=head2 action_redirect($url [, $test_name ])
-Fetches the given URL and checks that the request was a redirect.
+Fetches the given URL and checks that the request was a redirect. An optional
+second argument can be given to specify the name of the test.
-=head2 action_notfound
+=head2 action_notfound($url [, $test_name ])
-Fetches the given URL and checks that the request was not found.
+Fetches the given URL and checks that the request was not found. An optional
+second argument can be given to specify the name of the test.
-=head2 content_like( $url, $regexp [, $test_name] )
+=head2 content_like( $url, $regexp [, $test_name ] )
-Fetches the given URL and returns whether the content matches the regexp.
+Fetches the given URL and returns whether the content matches the regexp. An
+optional third argument can be given to specify the name of the test.
-=head2 contenttype_is
+=head2 contenttype_is($url, $type [, $test_name ])
-Check for given MIME type.
+Verify the given URL has a content type of $type and optionally specify a test name.
=head1 SEE ALSO
This library is free software. You can redistribute it and/or modify it under
the same terms as Perl itself.
+=begin Pod::Coverage
+
+local_request
+
+remote_request
+
+=end Pod::Coverage
+
=cut
1;