Test::...CGIApp(3pm)

πŸ“¦
version 0.05 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– 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's CGI::Application based), or invokes the ->dispatch() method (when it's CGI::Application::Dispatch based), 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 (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