Test::...CGIApp(3pm)
π Test::WWW::Mechanize::CGIApp π
ok so bestie, welcome to Test::WWW::Mechanize::CGIApp β it's the whole Test::WWW::Mechanize vibe but for CGI::Application β¨ It ships in the User Contributed Perl Documentation collection and honestly? it's giving "testing web apps but make it easy." π¦
β¨ SYNOPSIS β¨
# We're in a t/*.t test script...
use Test::WWW::Mechanize::CGIApp;
my $mech = Test::WWW::Mechanize::CGIApp->new;
# test a class that uses CGI::Application calling semantics.
# (in this case we'll new up an instance of the app and call
# its ->run() method)
#
$mech->app("My::WebApp");
$mech->get_ok("?rm=my_run_mode&arg1=1&arg2=42");
# test a class that uses CGI::Application::Dispatch
# to locate the run_mode
# (in this case we'll just call the ->dispatch() class method).
#
my $dispatched_mech = Test::WWW::Mechanize::CGIApp->new;
$dispatched_mech->app("My::DispatchApp");
$mech->get_ok("/WebApp/my_run_mode?arg1=1&arg2=42");
# create an anonymous sub that this class will use to
# handle the request.
#
# this could be useful if you need to do something novel
# after creating an instance of your class (e.g. the
# fiddle_with_stuff() below) or maybe you have a unique
# way to get the app to run.
#
my $custom_mech = Test::WWW::Mechanize::CGIApp->new;
$custom_mech->app(
sub {
require "My::WebApp";
my $app = My::WebApp->new();
$app->fiddle_with_stuff();
$app->run();
});
$mech->get_ok("?rm=my_run_mode&arg1=1&arg2=42");
# at this point you can play with all kinds of cool
# Test::WWW::Mechanize testing methods.
is($mech->ct, "text/html");
$mech->title_is("Root", "On the root page");
$mech->content_contains("This is the root page", "Correct content");
$mech->follow_link_ok({text => 'Hello'}, "Click on Hello");
# ... and all other Test::WWW::Mechanize methods
πΈ DESCRIPTION πΈ
So this package makes testing CGIApp-based modules fast and easy, no cap. It rides on Test::WWW::Mechanize to hand you functions for all the classic web-testing scenarios. Like, for example:
$mech->get_ok( $page );
$mech->title_is( "Invoice Status",
"Make sure we're on the invoice page" );
$mech->content_contains( "Andy Lester", "My name somewhere" );
$mech->content_like( qr/(cpan|perl)\.org/,
"Link to perl.org or CPAN" );
For apps that inherit from CGI::Application, it handles requests by making a new instance of the class and calling its "run" method πβοΈ For apps that use CGI::Application::Dispatch, it calls the "dispatch" class method. And if neither of those is the right move for you? You can just set a reference to a sub and that sub will handle the request. iconic, honestly π
This module supports cookies automatically. yes bestie, cookies, handled, you're welcome πͺ
And go check out Test::WWW::Mechanize for all the other cool stuff you can test β it's a whole world π
π CONSTRUCTOR π
new
Behaves like β and literally calls β Test::WWW::Mechanize's "new" method. It optionally takes an "app" parameter (more on that below), and any other parameters you toss in get passed straight to Test::WWW::Mechanize's constructor. And ngl, you've got options: you can either pass the name of the CGI::Application into the constructor with the "app" parameter, or set it later with the "app" method. dealer's choice β¨
use Test::WWW::Mechanize::CGIApp;
my $mech = Test::WWW::Mechanize::CGIApp->new;
# or
my $mech = Test::WWW::Mechanize::CGIApp->new(app => 'TestApp');
π METHODS π
$mech->app($app_handler)
ok so this method is the whole "hey Test::WWW::Mechanize::CGIApp, how exactly are we supposed to run your run_mode?" moment. It's your way of telling it what to do π«Ά
- If you set it to the name of a class, then it loads that class and either makes an instance and
->run()s it (when it'sCGI::Applicationbased), or invokes the->dispatch()method (when it'sCGI::Application::Dispatchbased), or it calls the anonymous subroutine you supplied and lets that do all the heavy lifting. main character energy from your own sub, honestly π
π¦ SEE ALSO π¦
Other modules that might be your vibe: Test::WWW::Mechanize, WWW::Mechanize.
Also, some of the implementation tricks came from Test::WWW::Mechanize::Catalyst β shoutout π«‘
β AUTHOR β
George Hartzell, "<hartzell@alerce.com>"
based on Test::WWW::Mechanize::Catalyst by Leon Brocard, "<acme@astray.com>".
π COPYRIGHT π
Copyright (C) 2007, George Hartzell
This module is free software; you can redistribute it or modify it under the same terms as Perl itself. β¨
aaaand that's the whole page bestie π just so you know, this was perl v5.34.0, dated 2022-06-28, from the Test::...CGIApp(3pm) page β go forth and test, you're gonna slay πππ¦
π¦ where this page comes from
- debian bookworm: libtest-www-mechanize-cgiapp-perl 0.05-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libtest-www-mechanize-cgiapp-perl 0.05-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libtest-www-mechanize-cgiapp-perl 0.05-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libtest-www-mechanize-cgiapp-perl 0.05-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libtest-www-mechanize-cgiapp-perl 0.05-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms