2 use UNIVERSAL::require;
3 use URI; use URI::QueryParam;
12 die "Couldn't require $package - $@" if $@;
14 unshift @{$package."::ISA"}, "Maypole::CLI";
18 sub get_template_root { $ENV{MAYPOLE_TEMPLATES} || "." }
22 my $url = URI->new(shift @ARGV);
23 my $root = URI->new($self->config->{uri_base})->path;
24 $self->{path} = $url->path;
25 $self->{path} =~ s/^$root//i if $root;
27 $self->{query} = $url->query_form_hash;
30 sub send_output { $buffer = shift->{output} }
33 CHECK { if ((caller(0))[1] eq "-e") {
34 $package->handler() and print $buffer;
41 Maypole::CLI - Command line interface to Maypole for testing and debugging
45 % setenv MAYPOLE_TEMPLATES /var/www/beerdb/
46 % perl -MMaypole::CLI=BeerDB -e1 http://localhost/beerdb/brewery/frontpage
50 This module is used to test Maypole sites without going through a web
51 server or modifying them to use a CGI frontend. To use it, you should
52 first either be in the template root for your Maypole site or set the
53 environment variable C<MAYPOLE_TEMPLATES> to the right value.
55 Next, you import the C<Maypole::CLI> module specifying your base Maypole
56 subclass. The usual way to do this is with the C<-M> flag:
57 C<perl -MMaypole::CLI=MyApp>. This is equivalent to:
59 use Maypole::CLI qw(MyApp);
61 Now Maypole will automatically call your application's handler with the
62 URL specified as the first command line parameter. This should be the
63 full URL, starting from whatever you have defined as the C<uri_base> in
64 your application's configuration, and may include query parameters.
66 The Maypole HTML output should then end up on standard output.
68 =head1 Support for testing
70 The module can also be used as part of a test script.
72 When used programmatically, rather than from the command line, its
73 behaviour is slightly different.
75 Although the URL is taken from C<@ARGV> as normal, your application's
76 C<handler> method is not called automatically, as it is when used on the
77 command line; you need to call it manually. Additionally, when
78 C<handler> is called, the output is not printed to standard output but
79 stored in C<$Maypole::CLI::buffer>, to allow you to check the contents
82 For instance, a test script could look like this:
84 use Test::More tests => 5;
85 use Maypole::CLI qw(BeerDB);
86 $ENV{MAYPOLE_TEMPLATES} = "t/templates";
88 # Hack because isa_ok only supports object isa not class isa
89 isa_ok( (bless {},"BeerDB") , "Maypole");
91 @ARGV = ("http://localhost/beerdb/");
92 is(BeerDB->handler, 200, "OK");
93 like($Maypole::CLI::buffer, qr/frontpage/, "Got the front page");
95 @ARGV = ("http://localhost/beerdb/beer/list");
96 is(BeerDB->handler, 200, "OK");
97 like($Maypole::CLI::buffer, qr/Organic Best/, "Found a beer in the list");