Author: dom Date: 2007-06-10 14:45:21 +0100 (Sun, 10 Jun 2007) New Revision: 1054
Added: trunk/t/50_http_response_codes.t Modified: trunk/MANIFEST trunk/lib/OpenGuides/Template.pm Log: Add support in output routine for specifying HTTP status code (see #102)
Modified: trunk/MANIFEST =================================================================== --- trunk/MANIFEST 2007-06-10 01:50:29 UTC (rev 1053) +++ trunk/MANIFEST 2007-06-10 13:45:21 UTC (rev 1054) @@ -114,6 +114,7 @@ t/47_navbar_placement.t t/48_google_analytics.t t/49_custom_header.t +t/50_http_response_codes.t t/51_display_node.t t/52_display_diffs.t t/53_show_index.t
Modified: trunk/lib/OpenGuides/Template.pm =================================================================== --- trunk/lib/OpenGuides/Template.pm 2007-06-10 01:50:29 UTC (rev 1053) +++ trunk/lib/OpenGuides/Template.pm 2007-06-10 13:45:21 UTC (rev 1054) @@ -109,6 +109,8 @@ Content-Type: defaults to C<text/html> and is omitted if the C<content_type> arg is explicitly set to the blank string.
+The HTTP response code may be explictly set with the C<http_status> arg. + =cut
sub output { @@ -180,19 +182,31 @@ $tt_vars = { %$tt_vars, %{ $args{vars} || {} } };
my $header = ""; - unless ( defined $args{content_type} and $args{content_type} eq "" ) { - my $content_type; - if ($args{content_type}) { - $content_type = $args{content_type}; + my %cgi_header_args; + + if ( defined $args{content_type} and $args{content_type} eq "" ) { + $cgi_header_args{'-type'} = ''; + } else { + if ( $args{content_type} ) { + $cgi_header_args{'-type'} = $args{content_type}; } else { - $content_type = "text/html"; + $cgi_header_args{'-type'} = "text/html"; } - if ($tt_vars->{http_charset}) { - $content_type .= "; charset=".$tt_vars->{http_charset}; + if ( $tt_vars->{http_charset} ) { + $cgi_header_args{'-type'} .= "; charset=".$tt_vars->{http_charset}; } - $header = CGI::header( -type => $content_type, -cookie => $args{cookies} ); + # XXX should possibly not be inside this block, but retaining + # existing functionality for now. See + # http://dev.openguides.org/ticket/220 + $cgi_header_args{'-cookie'} = $args{cookies}; }
+ if ( $args{http_status} ) { + $cgi_header_args{'-status'} = $args{http_status}; + } + + $header = CGI::header( %cgi_header_args ); + # vile hack my %field_vars = OpenGuides::Template->extract_metadata_vars( wiki => $args{wiki},
Added: trunk/t/50_http_response_codes.t =================================================================== --- trunk/t/50_http_response_codes.t (rev 0) +++ trunk/t/50_http_response_codes.t 2007-06-10 13:45:21 UTC (rev 1054) @@ -0,0 +1,39 @@ +use strict; +use Cwd; +use OpenGuides; +use OpenGuides::Template; +use OpenGuides::Test; +use Test::More tests => 3; + +my $config = OpenGuides::Test->make_basic_config; +$config->template_path( cwd . "/t/templates" ); + +my $guide = OpenGuides->new( config => $config ); +my $wiki = $guide->wiki; + +eval { + OpenGuides::Template->output( wiki => $wiki, + config => $config, + template => "15_test.tt" ); +}; +is( $@, "", "is happy doing output" ); + +my $output = OpenGuides::Template->output( + wiki => $wiki, + config => $config, + template => "15_test.tt" +); + +unlike( $output, qr/^Status:/, + "HTTP status not printed when not explicitly specified "); + +# Now supply a http status + +$output = OpenGuides::Template->output( + wiki => $wiki, + config => $config, + template => "15_test.tt", + http_status => '404' +); +like( $output, qr/^Status: 404/, + "Correct HTTP status printed when specified" );
openguides-commits@lists.openguides.org