]> git.decadent.org.uk Git - maypole.git/blob - t/cgi_maypole.t
f4d4e82105675caac1846765fa56617506e2a9f2
[maypole.git] / t / cgi_maypole.t
1 #!/usr/bin/perl -w
2 use strict;
3 use Test::More tests => 26;
4 use Test::MockModule;
5
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');
10
11 my %calls;
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')
17 });
18
19 # run()
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');
27
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';
35
36 # get_request()
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');
41
42 # parse_location()
43 can_ok($r => 'parse_location');
44 $r->parse_location;
45 is($r->path, 'frontpage', '... sets "path" to frontpage if undefined');
46
47 #delete $r->{cgi}{'.path_info'};
48 $ENV{PATH_INFO} = '/brewery/view/1/2/3';
49 $r->parse_location;
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');
55
56 $mock_maypole->mock(
57     parse_path => sub {$calls{parse_path} = \@_},
58     parse_args => sub {$calls{parse_args} = \@_},
59 );
60 $r->parse_location;
61 is_deeply($calls{parse_path}, [$r], '... calls parse_path');
62 is_deeply($calls{parse_args}, [$r], '... calls parse_args');
63
64
65 # parse_args()
66 $mock_maypole->unmock('parse_args');
67 can_ok($r => 'parse_args');
68 $cgi->parse_query_string;
69 $r->parse_args;
70 is_deeply($r->params, { beer => [1,2], pub => 'red lion', handpump => undef },
71           '... parsed params');
72 is_deeply($r->params, $r->query, '... query and params are identical');
73
74 # send_output()
75 can_ok($r => 'send_output');
76 SKIP: {
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!');
82     my $stdout;
83     IO::CaptureOutput::capture(sub {$r->send_output}, \$stdout);
84
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');
88 }
89
90 # get_template_root()
91 can_ok($r => 'get_template_root');
92 is($r->get_template_root(), '/var/tmp/maypole/index.cgi',
93    '... catdir(document_root, [relative_url])');