| 1 | package Catalyst::Test; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 127 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 127 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 127 | ▶ | use Test::More (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 127 | ▶ | use Plack::Test; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 127 | ▶ | use Catalyst::Exception; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 127 | ▶ | use Catalyst::Utils; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 127 | ▶ | use Class::Load qw(load_class is_class_loaded); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 127 | ▶ | use Sub::Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 127 | ▶ | use Moose::Util 'find_meta'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 127 | ▶ | use Carp 'croak', 'carp'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 128 | ▶ | sub _build_request_export { | ||||||||||||||||||||||||||
|
Subroutine
_build_request_export: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 16 | 128 | my ($self, $args) = @_; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 0 | ▶ | return sub { _remote_request(@_) } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 19 | 128 | ▶ | if $args->{remote}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 128 | my $class = $args->{class}; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # Here we should be failing right away, but for some stupid backcompat thing | ||||||||||||||||||||||||||||
| 24 | # I don't quite remember we fail lazily here. Needs a proper deprecation and | ||||||||||||||||||||||||||||
| 25 | # then removal. | ||||||||||||||||||||||||||||
| 26 | 2 | ▶ | return sub { croak "Must specify a test app: use Catalyst::Test 'TestApp'" } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 128 | ▶ | unless $class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 126 | ▶ | load_class($class) unless is_class_loaded($class); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 126 | $class->import; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 126 | ▶ | return sub { _local_request( $class, @_ ) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 128 | ▶ | sub _build_get_export { | ||||||||||||||||||||||||||
|
Subroutine
_build_get_export: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 128 | my ($self, $args) = @_; | |||||||||||||||||||||||||||
| 37 | 128 | my $request = $args->{request}; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 128 | ▶ | return sub { $request->(@_)->content }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 128 | ▶ | sub _build_ctx_request_export { | ||||||||||||||||||||||||||
|
Subroutine
_build_ctx_request_export: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 128 | my ($self, $args) = @_; | |||||||||||||||||||||||||||
| 43 | 128 | my ($class, $request) = @{ $args }{qw(class request)}; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 82 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 46 | 82 | ▶ | my $me = ref $self || $self; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # fail if ctx_request is being used against a remote server | ||||||||||||||||||||||||||||
| 49 | Catalyst::Exception->throw("$me only works with local requests, not remote") | ||||||||||||||||||||||||||||
| 50 | 82 | ▶ | if $ENV{CATALYST_SERVER}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # check explicitly for the class here, or the Cat->meta call will blow | ||||||||||||||||||||||||||||
| 53 | # up in our face | ||||||||||||||||||||||||||||
| 54 | 82 | ▶ | Catalyst::Exception->throw("Must specify a test app: use Catalyst::Test 'TestApp'") unless $class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # place holder for $c after the request finishes; reset every time | ||||||||||||||||||||||||||||
| 57 | # requests are done. | ||||||||||||||||||||||||||||
| 58 | 81 | my $ctx_closed_over; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # hook into 'dispatch' -- the function gets called after all plugins | ||||||||||||||||||||||||||||
| 61 | # have done their work, and it's an easy place to capture $c. | ||||||||||||||||||||||||||||
| 62 | 81 | my $meta = find_meta($class); | |||||||||||||||||||||||||||
| 63 | 81 | $meta->make_mutable; | |||||||||||||||||||||||||||
| 64 | 960 | ▶ | $meta->add_after_method_modifier( "dispatch", sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 65 | 960 | $ctx_closed_over = shift; | |||||||||||||||||||||||||||
| 66 | 81 | }); | |||||||||||||||||||||||||||
| 67 | 81 | $meta->make_immutable( replace_constructor => 1 ); | |||||||||||||||||||||||||||
| 68 | 81 | Class::C3::reinitialize(); # Fixes RT#46459, I've failed to write a test for how/why, but it does. | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # do the request; C::T::request will know about the class name, and | ||||||||||||||||||||||||||||
| 71 | # we've already stopped it from doing remote requests above. | ||||||||||||||||||||||||||||
| 72 | 81 | my $res = $args->{request}->( @_ ); | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # Make sure not to leave a reference $ctx hanging around. | ||||||||||||||||||||||||||||
| 75 | # This means that the context will go out of scope as soon as the | ||||||||||||||||||||||||||||
| 76 | # caller disposes of it, rather than waiting till the next time | ||||||||||||||||||||||||||||
| 77 | # that ctx_request is called. This can be important if your $ctx | ||||||||||||||||||||||||||||
| 78 | # ends up with a reference to a shared resource or lock (for example) | ||||||||||||||||||||||||||||
| 79 | # which you want to clean up in test teardown - if the $ctx is still | ||||||||||||||||||||||||||||
| 80 | # closed over then you're stuffed... | ||||||||||||||||||||||||||||
| 81 | 81 | my $ctx = $ctx_closed_over; | |||||||||||||||||||||||||||
| 82 | 81 | undef $ctx_closed_over; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 81 | return ( $res, $ctx ); | |||||||||||||||||||||||||||
| 85 | 128 | }; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 128 | ▶ | my $build_exports = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 89 | 128 | my ($self, $meth, $args, $defaults) = @_; | |||||||||||||||||||||||||||
| 90 | 128 | my $class = $args->{class}; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | my $request = $self->_build_request_export({ | ||||||||||||||||||||||||||||
| 93 | class => $class, | ||||||||||||||||||||||||||||
| 94 | remote => $ENV{CATALYST_SERVER}, | ||||||||||||||||||||||||||||
| 95 | 128 | }); | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 128 | my $get = $self->_build_get_export({ request => $request }); | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 128 | my $ctx_request = $self->_build_ctx_request_export({ | |||||||||||||||||||||||||||
| 100 | class => $class, | ||||||||||||||||||||||||||||
| 101 | request => $request, | ||||||||||||||||||||||||||||
| 102 | }); | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | return { | ||||||||||||||||||||||||||||
| 105 | request => $request, | ||||||||||||||||||||||||||||
| 106 | get => $get, | ||||||||||||||||||||||||||||
| 107 | ctx_request => $ctx_request, | ||||||||||||||||||||||||||||
| 108 | 9 | ▶ | content_like => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 109 | 9 | my $action = shift; | |||||||||||||||||||||||||||
| 110 | 9 | return Test::More->builder->like($get->($action),@_); | |||||||||||||||||||||||||||
| 111 | }, | ||||||||||||||||||||||||||||
| 112 | 3 | ▶ | action_ok => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 113 | 3 | my $action = shift; | |||||||||||||||||||||||||||
| 114 | 3 | my $meth = $request->($action)->request->method; | |||||||||||||||||||||||||||
| 115 | 3 | ▶ | my @args = @_ ? @_ : ("$meth $action returns successfully"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 3 | return Test::More->builder->ok($request->($action)->is_success,@args); | |||||||||||||||||||||||||||
| 117 | }, | ||||||||||||||||||||||||||||
| 118 | 1 | ▶ | action_redirect => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 119 | 1 | my $action = shift; | |||||||||||||||||||||||||||
| 120 | 1 | my $meth = $request->($action)->request->method; | |||||||||||||||||||||||||||
| 121 | 1 | ▶ | my @args = @_ ? @_ : ("$meth $action returns a redirect"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 1 | return Test::More->builder->ok($request->($action)->is_redirect,@args); | |||||||||||||||||||||||||||
| 123 | }, | ||||||||||||||||||||||||||||
| 124 | 2 | ▶ | action_notfound => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 125 | 2 | my $action = shift; | |||||||||||||||||||||||||||
| 126 | 2 | my $meth = $request->($action)->request->method; | |||||||||||||||||||||||||||
| 127 | 2 | ▶ | my @args = @_ ? @_ : ("$meth $action returns a 404"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 2 | return Test::More->builder->is_eq($request->($action)->code,404,@args); | |||||||||||||||||||||||||||
| 129 | }, | ||||||||||||||||||||||||||||
| 130 | 1 | ▶ | contenttype_is => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 131 | 1 | my $action = shift; | |||||||||||||||||||||||||||
| 132 | 1 | my $res = $request->($action); | |||||||||||||||||||||||||||
| 133 | 1 | return Test::More->builder->is_eq(scalar($res->content_type),@_); | |||||||||||||||||||||||||||
| 134 | }, | ||||||||||||||||||||||||||||
| 135 | 128 | }; | |||||||||||||||||||||||||||
| 136 | 127 | }; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 127 | our $default_host; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | { | ||||||||||||||||||||||||||||
| 141 | 127 | my $import = Sub::Exporter::build_exporter({ | |||||||||||||||||||||||||||
| 142 | groups => [ all => $build_exports ], | ||||||||||||||||||||||||||||
| 143 | into_level => 1, | ||||||||||||||||||||||||||||
| 144 | }); | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 128 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 148 | 128 | my ($self, $class, $opts) = @_; | |||||||||||||||||||||||||||
| 149 | 128 | ▶ | Carp::carp( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | qq{Importing Catalyst::Test without an application name is deprecated:\n | ||||||||||||||||||||||||||||
| 151 | Instead of saying: use Catalyst::Test; | ||||||||||||||||||||||||||||
| 152 | say: use Catalyst::Test (); # If you don't want to import a test app right now. | ||||||||||||||||||||||||||||
| 153 | or say: use Catalyst::Test 'MyApp'; # If you do want to import a test app.\n\n}) | ||||||||||||||||||||||||||||
| 154 | unless $class; | ||||||||||||||||||||||||||||
| 155 | 128 | $import->($self, '-all' => { class => $class }); | |||||||||||||||||||||||||||
| 156 | 128 | ▶ | $opts = {} unless ref $opts eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 128 | ▶ | $default_host = $opts->{default_host} if exists $opts->{default_host}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 128 | return 1; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =head1 NAME | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | Catalyst::Test - Test Catalyst Applications | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | # Helper | ||||||||||||||||||||||||||||
| 169 | script/test.pl | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | # Tests | ||||||||||||||||||||||||||||
| 172 | use Catalyst::Test 'TestApp'; | ||||||||||||||||||||||||||||
| 173 | my $content = get('index.html'); # Content as string | ||||||||||||||||||||||||||||
| 174 | my $response = request('index.html'); # HTTP::Response object | ||||||||||||||||||||||||||||
| 175 | my($res, $c) = ctx_request('index.html'); # HTTP::Response & context object | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | use HTTP::Request::Common; | ||||||||||||||||||||||||||||
| 178 | my $response = request POST '/foo', [ | ||||||||||||||||||||||||||||
| 179 | bar => 'baz', | ||||||||||||||||||||||||||||
| 180 | something => 'else' | ||||||||||||||||||||||||||||
| 181 | ]; | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | # Run tests against a remote server | ||||||||||||||||||||||||||||
| 184 | CATALYST_SERVER='http://localhost:3000/' prove -r -l lib/ t/ | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | use Catalyst::Test 'TestApp'; | ||||||||||||||||||||||||||||
| 187 | use Test::More tests => 1; | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | ok( get('/foo') =~ /bar/ ); | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | # mock virtual hosts | ||||||||||||||||||||||||||||
| 192 | use Catalyst::Test 'MyApp', { default_host => 'myapp.com' }; | ||||||||||||||||||||||||||||
| 193 | like( get('/whichhost'), qr/served by myapp.com/ ); | ||||||||||||||||||||||||||||
| 194 | like( get( '/whichhost', { host => 'yourapp.com' } ), qr/served by yourapp.com/ ); | ||||||||||||||||||||||||||||
| 195 | { | ||||||||||||||||||||||||||||
| 196 | local $Catalyst::Test::default_host = 'otherapp.com'; | ||||||||||||||||||||||||||||
| 197 | like( get('/whichhost'), qr/served by otherapp.com/ ); | ||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | This module allows you to make requests to a Catalyst application either without | ||||||||||||||||||||||||||||
| 203 | a server, by simulating the environment of an HTTP request using | ||||||||||||||||||||||||||||
| 204 | L<HTTP::Request::AsCGI> or remotely if you define the CATALYST_SERVER | ||||||||||||||||||||||||||||
| 205 | environment variable. This module also adds a few Catalyst-specific | ||||||||||||||||||||||||||||
| 206 | testing methods as displayed in the method section. | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | The L<get|/"$content = get( ... )"> and L<request|/"$res = request( ... );"> | ||||||||||||||||||||||||||||
| 209 | functions take either a URI or an L<HTTP::Request> object. | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =head1 INLINE TESTS WILL NO LONGER WORK | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | While it used to be possible to inline a whole test app into a C<.t> file for | ||||||||||||||||||||||||||||
| 214 | a distribution, this will no longer work. | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | The convention is to place your L<Catalyst> test apps into C<t/lib> in your | ||||||||||||||||||||||||||||
| 217 | distribution. E.g.: C<t/lib/TestApp.pm>, C<t/lib/TestApp/Controller/Root.pm>, | ||||||||||||||||||||||||||||
| 218 | etc.. Multiple test apps can be used in this way. | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | Then write your C<.t> files like so: | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | use strict; | ||||||||||||||||||||||||||||
| 223 | use warnings; | ||||||||||||||||||||||||||||
| 224 | use FindBin '$Bin'; | ||||||||||||||||||||||||||||
| 225 | use lib "$Bin/lib"; | ||||||||||||||||||||||||||||
| 226 | use Test::More tests => 6; | ||||||||||||||||||||||||||||
| 227 | use Catalyst::Test 'TestApp'; | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | =head1 METHODS | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | =head2 $content = get( ... ) | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | Returns the content. | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | my $content = get('foo/bar?test=1'); | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | Note that this method doesn't follow redirects, so to test for a | ||||||||||||||||||||||||||||
| 238 | correctly redirecting page you'll need to use a combination of this | ||||||||||||||||||||||||||||
| 239 | method and the L<request|/"$res = request( ... );"> method below: | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | my $res = request('/'); # redirects to /y | ||||||||||||||||||||||||||||
| 242 | warn $res->header('location'); | ||||||||||||||||||||||||||||
| 243 | use URI; | ||||||||||||||||||||||||||||
| 244 | my $uri = URI->new($res->header('location')); | ||||||||||||||||||||||||||||
| 245 | is ( $uri->path , '/y'); | ||||||||||||||||||||||||||||
| 246 | my $content = get($uri->path); | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | Note also that the content is returned as raw bytes, without any attempt | ||||||||||||||||||||||||||||
| 249 | to decode it into characters. | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | =head2 $res = request( ... ); | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | Returns an L<HTTP::Response> object. Accepts an optional hashref for request | ||||||||||||||||||||||||||||
| 254 | header configuration; currently only supports setting 'host' value. | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | my $res = request('foo/bar?test=1'); | ||||||||||||||||||||||||||||
| 257 | my $virtual_res = request('foo/bar?test=1', {host => 'virtualhost.com'}); | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | Alternately, you can pass in an L<HTTP::Request::Common> object to set arbitrary | ||||||||||||||||||||||||||||
| 260 | request headers. | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | my $res = request(GET '/foo/bar', | ||||||||||||||||||||||||||||
| 263 | X-Foo => 'Bar', | ||||||||||||||||||||||||||||
| 264 | Authorization => 'Bearer JWT_HERE', | ||||||||||||||||||||||||||||
| 265 | ... | ||||||||||||||||||||||||||||
| 266 | ); | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | =head2 ($res, $c) = ctx_request( ... ); | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | Works exactly like L<request|/"$res = request( ... );">, except it also returns the Catalyst context object, | ||||||||||||||||||||||||||||
| 271 | C<$c>. Note that this only works for local requests. | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | =cut | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 127 | ▶ | sub _request { | ||||||||||||||||||||||||||
|
Subroutine
_request: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 276 | 917 | my $args = shift; | |||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 917 | my $request = Catalyst::Utils::request(shift); | |||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | 917 | my %extra_env; | |||||||||||||||||||||||||||
| 281 | 917 | _customize_request($request, \%extra_env, @_); | |||||||||||||||||||||||||||
| 282 | 917 | ▶ | $args->{mangle_request}->($request) if $args->{mangle_request}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | 917 | my $ret; | |||||||||||||||||||||||||||
| 285 | test_psgi | ||||||||||||||||||||||||||||
| 286 | 917 | %{ $args }, | |||||||||||||||||||||||||||
| 287 | 917 | ▶ | app => sub { $args->{app}->({ %{ $_[0] }, %extra_env }) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 288 | 917 | ▶ | client => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 289 | 917 | my ($psgi_app) = @_; | |||||||||||||||||||||||||||
| 290 | 917 | my $resp = $psgi_app->($request); | |||||||||||||||||||||||||||
| 291 | 916 | ▶ | $args->{mangle_response}->($resp) if $args->{mangle_response}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 292 | 916 | $ret = $resp; | |||||||||||||||||||||||||||
| 293 | 917 | }; | |||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | 916 | return $ret; | |||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | 917 | ▶ | sub _local_request { | ||||||||||||||||||||||||||
|
Subroutine
_local_request: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 299 | 917 | my $class = shift; | |||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | return _request({ | ||||||||||||||||||||||||||||
| 302 | app => ref($class) eq "CODE" ? $class : $class->_finalized_psgi_app, | ||||||||||||||||||||||||||||
| 303 | 916 | ▶ | mangle_response => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.8
| |||||||||||||||||||||||||||||
| 304 | 916 | my ($resp) = @_; | |||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | # HTML head parsing based on LWP::UserAgent | ||||||||||||||||||||||||||||
| 307 | # | ||||||||||||||||||||||||||||
| 308 | # This is because if you make a remote request with LWP, then the | ||||||||||||||||||||||||||||
| 309 | # <BASE HREF="..."> from the returned HTML document will be used | ||||||||||||||||||||||||||||
| 310 | # to fill in $res->base, as documented in HTTP::Response. We need | ||||||||||||||||||||||||||||
| 311 | # to support this in local test requests so that they work 'the same'. | ||||||||||||||||||||||||||||
| 312 | # | ||||||||||||||||||||||||||||
| 313 | # This is not just horrible and possibly broken, but also really | ||||||||||||||||||||||||||||
| 314 | # doesn't belong here. Whoever wants this should be working on | ||||||||||||||||||||||||||||
| 315 | # getting it into Plack::Test, or make a middleware out of it, or | ||||||||||||||||||||||||||||
| 316 | # whatever. Seriously - horrible. | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | 916 | ▶ | if (!$resp->content_type || $resp->content_is_html) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $resp->content_type or $resp->content_is_html
MC/DC100%2/2
not $resp->content_type or $resp->content_is_html
not $resp->content_type
$resp->content_is_html
Truth table100%3/3
not(A) or B
A= not $resp->content_typeB= $resp->content_is_html
| |||||||||||||||||||||||||||||
| 319 | 712 | require HTML::HeadParser; | |||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 712 | my $parser = HTML::HeadParser->new(); | |||||||||||||||||||||||||||
| 322 | 712 | ▶ | $parser->xml_mode(1) if $resp->content_is_xhtml; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 323 | 712 | ▶ | $parser->utf8_mode(1) if $] >= 5.008 && $HTML::Parser::VERSION >= 3.40; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$] >= "5.008" and $HTML::Parser::VERSION >= "3.4"
MC/DC0%0/2
$] >= "5.008" and $HTML::Parser::VERSION >= "3.4"
$] >= "5.008"
$HTML::Parser::VERSION >= "3.4"
Truth table33%1/3
A and B
A= $] >= "5.008"B= $HTML::Parser::VERSION >= "3.4"
| |||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | 712 | $parser->parse( $resp->content ); | |||||||||||||||||||||||||||
| 326 | 712 | my $h = $parser->header; | |||||||||||||||||||||||||||
| 327 | 712 | for my $f ( $h->header_field_names ) { | |||||||||||||||||||||||||||
| 328 | 26 | $resp->init_header( $f, [ $h->header($f) ] ); | |||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | # Another horrible hack to make the response headers have a | ||||||||||||||||||||||||||||
| 332 | # 'status' field. This is for back-compat, but you should | ||||||||||||||||||||||||||||
| 333 | # call $resp->code instead! | ||||||||||||||||||||||||||||
| 334 | 916 | $resp->init_header('status', [ $resp->code ]); | |||||||||||||||||||||||||||
| 335 | }, | ||||||||||||||||||||||||||||
| 336 | 917 | ▶ | }, @_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 127 | my $agent; | |||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | 0 | ▶ | sub _remote_request { | ||||||||||||||||||||||||||
|
Subroutine
_remote_request: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 342 | 0 | require LWP::UserAgent; | |||||||||||||||||||||||||||
| 343 | 0 | local $Plack::Test::Impl = 'ExternalServer'; | |||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | 0 | ▶ | unless ($agent) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 346 | 0 | $agent = LWP::UserAgent->new( | |||||||||||||||||||||||||||
| 347 | keep_alive => 1, | ||||||||||||||||||||||||||||
| 348 | max_redirect => 0, | ||||||||||||||||||||||||||||
| 349 | timeout => 60, | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | # work around newer LWP max_redirect 0 bug | ||||||||||||||||||||||||||||
| 352 | # http://rt.cpan.org/Ticket/Display.html?id=40260 | ||||||||||||||||||||||||||||
| 353 | requests_redirectable => [], | ||||||||||||||||||||||||||||
| 354 | ); | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | 0 | $agent->env_proxy; | |||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | 0 | my $server = URI->new($ENV{CATALYST_SERVER}); | |||||||||||||||||||||||||||
| 361 | 0 | ▶ | if ( $server->path =~ m|^(.+)?/$| ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 362 | 0 | my $path = $1; | |||||||||||||||||||||||||||
| 363 | 0 | ▶ | $server->path("$path") if $path; # need to be quoted | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | return _request({ | ||||||||||||||||||||||||||||
| 367 | ua => $agent, | ||||||||||||||||||||||||||||
| 368 | uri => $server, | ||||||||||||||||||||||||||||
| 369 | 0 | ▶ | mangle_request => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 370 | 0 | my ($request) = @_; | |||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | # the request path needs to be sanitised if $server is using a | ||||||||||||||||||||||||||||
| 373 | # non-root path due to potential overlap between request path and | ||||||||||||||||||||||||||||
| 374 | # response path. | ||||||||||||||||||||||||||||
| 375 | 0 | ▶ | if ($server->path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 376 | # If request path is '/', we have to add a trailing slash to the | ||||||||||||||||||||||||||||
| 377 | # final request URI | ||||||||||||||||||||||||||||
| 378 | 0 | ▶ | my $add_trailing = ($request->uri->path eq '/' || $request->uri->path eq '') ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$request->uri->path eq "/" or $request->uri->path eq ""
MC/DC0%0/2
$request->uri->path eq "/" or $request->uri->path eq ""
$request->uri->path eq "/"
$request->uri->path eq ""
Truth table0%0/3
A or B
A= $request->uri->path eq "/"B= $request->uri->path eq ""
| |||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | 0 | my @sp = split '/', $server->path; | |||||||||||||||||||||||||||
| 381 | 0 | my @rp = split '/', $request->uri->path; | |||||||||||||||||||||||||||
| 382 | 0 | shift @sp; shift @rp; # leading / | |||||||||||||||||||||||||||
| 383 | 0 | ▶ | if (@rp) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 384 | 0 | foreach my $sp (@sp) { | |||||||||||||||||||||||||||
| 385 | 0 | ▶ | $sp eq $rp[0] ? shift @rp : last | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||
| 388 | 0 | $request->uri->path(join '/', @rp); | |||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | 0 | ▶ | if ( $add_trailing ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 391 | 0 | $request->uri->path( $request->uri->path . '/' ); | |||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||
| 394 | }, | ||||||||||||||||||||||||||||
| 395 | 0 | }, @_); | |||||||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | 127 | for my $name (qw(local_request remote_request)) { | |||||||||||||||||||||||||||
| 399 | 1 | ▶ | my $fun = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 400 | 1 | carp <<"EOW"; | |||||||||||||||||||||||||||
| 401 | Calling Catalyst::Test::${name}() directly is deprecated. | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | Please import Catalyst::Test into your namespace and use the provided request() | ||||||||||||||||||||||||||||
| 404 | function instead. | ||||||||||||||||||||||||||||
| 405 | EOW | ||||||||||||||||||||||||||||
| 406 | 1 | return __PACKAGE__->can("_${name}")->(@_); | |||||||||||||||||||||||||||
| 407 | 254 | }; | |||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | 127 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 410 | 254 | *$name = $fun; | |||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | 922 | ▶ | sub _customize_request { | ||||||||||||||||||||||||||
|
Subroutine
_customize_request: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 414 | 922 | my $request = shift; | |||||||||||||||||||||||||||
| 415 | 922 | my $extra_env = shift; | |||||||||||||||||||||||||||
| 416 | 922 | ▶ | my $opts = pop(@_) || {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
pop @_ || {}
MC/DC100%1/1
pop @_ || {}
pop @_
Truth table100%2/2
A
A= pop @_
| |||||||||||||||||||||||||||||
| 417 | 922 | ▶ | $opts = {} unless ref($opts) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 418 | 922 | ▶ | if ( my $host = exists $opts->{host} ? $opts->{host} : $default_host ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 419 | 27 | $request->header( 'Host' => $host ); | |||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | 922 | ▶ | if (my $extra = $opts->{extra_env}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 423 | 2 | @{ $extra_env }{keys %{ $extra }} = values %{ $extra }; | |||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||
| 425 | } | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | =head2 action_ok($url [, $test_name ]) | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | Fetches the given URL and checks that the request was successful. An optional | ||||||||||||||||||||||||||||
| 430 | second argument can be given to specify the name of the test. | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | =head2 action_redirect($url [, $test_name ]) | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | Fetches the given URL and checks that the request was a redirect. An optional | ||||||||||||||||||||||||||||
| 435 | second argument can be given to specify the name of the test. | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | =head2 action_notfound($url [, $test_name ]) | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | Fetches the given URL and checks that the request was not found. An optional | ||||||||||||||||||||||||||||
| 440 | second argument can be given to specify the name of the test. | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | =head2 content_like( $url, $regexp [, $test_name ] ) | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | Fetches the given URL and returns whether the content matches the regexp. An | ||||||||||||||||||||||||||||
| 445 | optional third argument can be given to specify the name of the test. | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | =head2 contenttype_is($url, $type [, $test_name ]) | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | Verify the given URL has a content type of $type and optionally specify a test name. | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | L<Catalyst>, L<Test::WWW::Mechanize::Catalyst>, | ||||||||||||||||||||||||||||
| 454 | L<Test::WWW::Selenium::Catalyst>, L<Test::More>, L<HTTP::Request::Common> | ||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | Catalyst Contributors, see Catalyst.pm | ||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | This library is free software. You can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 463 | the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | =begin Pod::Coverage | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | local_request | ||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | remote_request | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | =end Pod::Coverage | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | =cut | ||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | 127 | 1; | |||||||||||||||||||||||||||