1Catalyst::Test(3) User Contributed Perl Documentation Catalyst::Test(3)
2
3
4
6 Catalyst::Test - Test Catalyst Applications
7
9 # Helper
10 script/test.pl
11
12 # Tests
13 use Catalyst::Test 'TestApp';
14 my $content = get('index.html'); # Content as string
15 my $response = request('index.html'); # HTTP::Response object
16 my($res, $c) = ctx_request('index.html'); # HTTP::Response & context object
17
18 use HTTP::Request::Common;
19 my $response = request POST '/foo', [
20 bar => 'baz',
21 something => 'else'
22 ];
23
24 # Run tests against a remote server
25 CATALYST_SERVER='http://localhost:3000/' prove -r -l lib/ t/
26
27 use Catalyst::Test 'TestApp';
28 use Test::More tests => 1;
29
30 ok( get('/foo') =~ /bar/ );
31
32 # mock virtual hosts
33 use Catalyst::Test 'MyApp', { default_host => 'myapp.com' };
34 like( get('/whichhost'), qr/served by myapp.com/ );
35 like( get( '/whichhost', { host => 'yourapp.com' } ), qr/served by yourapp.com/ );
36 {
37 local $Catalyst::Test::default_host = 'otherapp.com';
38 like( get('/whichhost'), qr/served by otherapp.com/ );
39 }
40
42 This module allows you to make requests to a Catalyst application
43 either without a server, by simulating the environment of an HTTP
44 request using HTTP::Request::AsCGI or remotely if you define the
45 CATALYST_SERVER environment variable. This module also adds a few
46 Catalyst-specific testing methods as displayed in the method section.
47
48 The get and request functions take either a URI or an HTTP::Request
49 object.
50
52 While it used to be possible to inline a whole test app into a ".t"
53 file for a distribution, this will no longer work.
54
55 The convention is to place your Catalyst test apps into "t/lib" in your
56 distribution. E.g.: "t/lib/TestApp.pm",
57 "t/lib/TestApp/Controller/Root.pm", etc.. Multiple test apps can be
58 used in this way.
59
60 Then write your ".t" files like so:
61
62 use strict;
63 use warnings;
64 use FindBin '$Bin';
65 use lib "$Bin/lib";
66 use Test::More tests => 6;
67 use Catalyst::Test 'TestApp';
68
70 $content = get( ... )
71 Returns the content.
72
73 my $content = get('foo/bar?test=1');
74
75 Note that this method doesn't follow redirects, so to test for a
76 correctly redirecting page you'll need to use a combination of this
77 method and the request method below:
78
79 my $res = request('/'); # redirects to /y
80 warn $res->header('location');
81 use URI;
82 my $uri = URI->new($res->header('location'));
83 is ( $uri->path , '/y');
84 my $content = get($uri->path);
85
86 Note also that the content is returned as raw bytes, without any
87 attempt to decode it into characters.
88
89 $res = request( ... );
90 Returns an HTTP::Response object. Accepts an optional hashref for
91 request header configuration; currently only supports setting 'host'
92 value.
93
94 my $res = request('foo/bar?test=1');
95 my $virtual_res = request('foo/bar?test=1', {host => 'virtualhost.com'});
96
97 Alternately, you can pass in an HTTP::Request::Common object to set
98 arbitrary request headers.
99
100 my $res = request(GET '/foo/bar',
101 X-Foo => 'Bar',
102 Authorization => 'Bearer JWT_HERE',
103 ...
104 );
105
106 ($res, $c) = ctx_request( ... );
107 Works exactly like request, except it also returns the Catalyst context
108 object, $c. Note that this only works for local requests.
109
110 action_ok($url [, $test_name ])
111 Fetches the given URL and checks that the request was successful. An
112 optional second argument can be given to specify the name of the test.
113
114 action_redirect($url [, $test_name ])
115 Fetches the given URL and checks that the request was a redirect. An
116 optional second argument can be given to specify the name of the test.
117
118 action_notfound($url [, $test_name ])
119 Fetches the given URL and checks that the request was not found. An
120 optional second argument can be given to specify the name of the test.
121
122 content_like( $url, $regexp [, $test_name ] )
123 Fetches the given URL and returns whether the content matches the
124 regexp. An optional third argument can be given to specify the name of
125 the test.
126
127 contenttype_is($url, $type [, $test_name ])
128 Verify the given URL has a content type of $type and optionally specify
129 a test name.
130
132 Catalyst, Test::WWW::Mechanize::Catalyst,
133 Test::WWW::Selenium::Catalyst, Test::More, HTTP::Request::Common
134
136 Catalyst Contributors, see Catalyst.pm
137
139 This library is free software. You can redistribute it and/or modify it
140 under the same terms as Perl itself.
141
142
143
144perl v5.36.0 2023-01-20 Catalyst::Test(3)