]> git.decadent.org.uk Git - maypole.git/blobdiff - t/cgi_maypole.t
+ Use HTTP::Headers for input/output headers. Add appropriate unit tests.
[maypole.git] / t / cgi_maypole.t
index f4d4e82105675caac1846765fa56617506e2a9f2..d8a5ccf7b0a219c1d222f7f63f8178f7a774fac0 100644 (file)
@@ -1,11 +1,12 @@
 #!/usr/bin/perl -w
 use strict;
-use Test::More tests => 26;
+use Test::More tests => 30;
 use Test::MockModule;
 
 require_ok('CGI::Maypole');
 ok($CGI::Maypole::VERSION, 'defines $VERSION');
 ok($INC{'CGI/Simple.pm'}, 'requires CGI::Simple');
+ok($INC{'Maypole/Headers.pm'}, 'requires Maypole::Headers');
 ok(CGI::Maypole->isa('Maypole'), '@ISA = Maypole');
 
 my %calls;
@@ -26,12 +27,16 @@ is_deeply($calls{handler}, ['CGI::Maypole'],
 is($status, 'X', '... and returns its status');
 
 my $r = bless {}, 'CGI::Maypole';
+$r->headers_out(Maypole::Headers->new);
 $ENV{HTTP_HOST}      = 'localhost';
 $ENV{SCRIPT_NAME}    = '/maypole/index.cgi';
 $ENV{PATH_INFO}      = '/';
 $ENV{REQUEST_METHOD} = 'GET';
 $ENV{QUERY_STRING}   = 'beer=1;beer=2;pub=red+lion;handpump';
 $ENV{DOCUMENT_ROOT}  = '/var/tmp/maypole';
+for (keys %ENV) {
+    delete $ENV{$_} if /^HTTPS?/;
+}
 
 # get_request()
 can_ok($r => 'get_request');
@@ -41,7 +46,13 @@ is($cgi, $r->{cgi}, '... and stores it in the "cgi" slot');
 
 # parse_location()
 can_ok($r => 'parse_location');
+$ENV{HTTP_REFERER} = 'http://maypole.perl.org/';
+$ENV{HTTP_USER_AGENT} = 'tty';
 $r->parse_location;
+is($r->headers_in->get('Referer'), 'http://maypole.perl.org/',
+   '... sets headers_in() from HTTP variables');
+is_deeply([$r->headers_in->field_names], [qw(Referer User-Agent)],
+   '... loads only those HTTP variables');
 is($r->path, 'frontpage', '... sets "path" to frontpage if undefined');
 
 #delete $r->{cgi}{'.path_info'};
@@ -79,12 +90,28 @@ SKIP: {
     $r->content_type('text/plain');
     $r->document_encoding('iso8859-1');
     $r->output('Hello World!');
-    my $stdout;
-    IO::CaptureOutput::capture(sub {$r->send_output}, \$stdout);
 
+    my $stdout;
+    eval {
+        IO::CaptureOutput::capture(sub {$r->send_output}, \$stdout);
+    };
+    diag $@ if $@;
     my $compare = join "\cM\cJ", 'Content-length: 12',
         'Content-Type: text/plain; charset=iso8859-1', '', 'Hello World!';
     is($stdout, $compare, '... prints output, including content-type header');
+
+    # test custom output headers
+    $r->headers_out->set(X_Bender => 'kiss my shiny metal ass');
+    eval {
+        IO::CaptureOutput::capture(sub {$r->send_output}, \$stdout);
+    };
+    diag $@ if $@;
+
+    $compare = join "\cM\cJ", 'Content-length: 12',
+        'X-bender: kiss my shiny metal ass',
+        'Content-Type: text/plain; charset=iso8859-1',
+        '', 'Hello World!';
+    is($stdout, $compare, '... prints output, including custom headers');
 }
 
 # get_template_root()