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 testapp into a ".t" file
53 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
98 ($res, $c) = ctx_request( ... );
99 Works exactly like request, except it also returns the Catalyst context
100 object, $c. Note that this only works for local requests.
101
102 $res = Catalyst::Test::local_request( $AppClass, $url );
103 Simulate a request using HTTP::Request::AsCGI.
104
105 $res = Catalyst::Test::remote_request( $url );
106 Do an actual remote request using LWP.
107
108 action_ok
109 Fetches the given URL and checks that the request was successful.
110
111 action_redirect
112 Fetches the given URL and checks that the request was a redirect.
113
114 action_notfound
115 Fetches the given URL and checks that the request was not found.
116
117 content_like( $url, $regexp [, $test_name] )
118 Fetches the given URL and returns whether the content matches the
119 regexp.
120
121 contenttype_is
122 Check for given MIME type.
123
125 Catalyst, Test::WWW::Mechanize::Catalyst,
126 Test::WWW::Selenium::Catalyst, Test::More, HTTP::Request::Common
127
129 Catalyst Contributors, see Catalyst.pm
130
132 This library is free software. You can redistribute it and/or modify it
133 under the same terms as Perl itself.
134
135
136
137perl v5.12.1 2010-04-13 Catalyst::Test(3)