3 use Test::More tests => 26;
6 require_ok('CGI::Maypole');
7 ok($CGI::Maypole::VERSION, 'defines $VERSION');
8 ok($INC{'CGI/Simple.pm'}, 'requires CGI::Simple');
9 ok(CGI::Maypole->isa('Maypole'), '@ISA = Maypole');
12 my $mock_maypole = new Test::MockModule('CGI::Maypole');
13 my $mock_cgi = new Test::MockModule('CGI::Simple');
14 $mock_cgi->mock(path_info => sub {
15 delete $_[0]->{'.path_info'};
16 goto $mock_cgi->original('path_info')
20 can_ok('CGI::Maypole' => 'run');
21 $mock_maypole->mock(handler => sub {$calls{handler} = \@_; 'X'});
22 my $status = CGI::Maypole->run('TEST');
23 ok($calls{handler}, '... calls handler()');
24 is_deeply($calls{handler}, ['CGI::Maypole'],
25 '... as a method, passing 0 arguments');
26 is($status, 'X', '... and returns its status');
28 my $r = bless {}, 'CGI::Maypole';
29 $ENV{HTTP_HOST} = 'localhost';
30 $ENV{SCRIPT_NAME} = '/maypole/index.cgi';
31 $ENV{PATH_INFO} = '/';
32 $ENV{REQUEST_METHOD} = 'GET';
33 $ENV{QUERY_STRING} = 'beer=1;beer=2;pub=red+lion;handpump';
34 $ENV{DOCUMENT_ROOT} = '/var/tmp/maypole';
37 can_ok($r => 'get_request');
38 my $cgi = $r->get_request;
39 isa_ok($cgi, 'CGI::Simple', '... returns a CGI::Simple object');
40 is($cgi, $r->{cgi}, '... and stores it in the "cgi" slot');
43 can_ok($r => 'parse_location');
45 is($r->path, 'frontpage', '... sets "path" to frontpage if undefined');
47 #delete $r->{cgi}{'.path_info'};
48 $ENV{PATH_INFO} = '/brewery/view/1/2/3';
50 is($r->path, 'brewery/view/1/2/3', '... path is PATH_INFO without leading /');
51 is($r->table, 'brewery', '... sets "table" to first part of PATH_INFO');
52 is($r->action, 'view', '... sets "action" to second part of PATH_INFO');
53 is_deeply($r->args, [1,2,3],
54 '... sets "args" to a list of remaining path segments');
57 parse_path => sub {$calls{parse_path} = \@_},
58 parse_args => sub {$calls{parse_args} = \@_},
61 is_deeply($calls{parse_path}, [$r], '... calls parse_path');
62 is_deeply($calls{parse_args}, [$r], '... calls parse_args');
66 $mock_maypole->unmock('parse_args');
67 can_ok($r => 'parse_args');
68 $cgi->parse_query_string;
70 is_deeply($r->params, { beer => [1,2], pub => 'red lion', handpump => undef },
72 is_deeply($r->params, $r->query, '... query and params are identical');
75 can_ok($r => 'send_output');
77 eval "require IO::CaptureOutput";
78 skip "IO::CaptureOutput not installed", 3 if $@;
79 $r->content_type('text/plain');
80 $r->document_encoding('iso8859-1');
81 $r->output('Hello World!');
83 IO::CaptureOutput::capture(sub {$r->send_output}, \$stdout);
85 my $compare = join "\cM\cJ", 'Content-length: 12',
86 'Content-Type: text/plain; charset=iso8859-1', '', 'Hello World!';
87 is($stdout, $compare, '... prints output, including content-type header');
91 can_ok($r => 'get_template_root');
92 is($r->get_template_root(), '/var/tmp/maypole/index.cgi',
93 '... catdir(document_root, [relative_url])');