Author: nick Date: 2006-09-17 16:26:21 +0100 (Sun, 17 Sep 2006) New Revision: 868
Added: trunk/t/18_http_headers.t Modified: trunk/Changes trunk/MANIFEST trunk/lib/OpenGuides/Config.pm trunk/lib/OpenGuides/Template.pm Log: Support the config flag http_charset, which sets a http charset header on content type. (Closes #141)
Modified: trunk/Changes =================================================================== --- trunk/Changes 2006-09-17 12:42:27 UTC (rev 867) +++ trunk/Changes 2006-09-17 15:26:21 UTC (rev 868) @@ -4,6 +4,8 @@ Add RDF autodiscovery link to nodes' <head> section. Add UPGRADING file which summarises inmportant information for people upgrading. + Add an optional new config parameter, http_charset, which will set + an explict charset http header on all responses.
0.57 12 September 2006 New interfaces:
Modified: trunk/MANIFEST =================================================================== --- trunk/MANIFEST 2006-09-17 12:42:27 UTC (rev 867) +++ trunk/MANIFEST 2006-09-17 15:26:21 UTC (rev 868) @@ -72,6 +72,7 @@ t/15_template.t t/16_test_tester.t t/17_commit_node.t +t/18_http_headers.t t/21_rdf.t t/22_feed_recent_changes.t t/23_feed_node_all_versions.t
Modified: trunk/lib/OpenGuides/Config.pm =================================================================== --- trunk/lib/OpenGuides/Config.pm 2006-09-17 12:42:27 UTC (rev 867) +++ trunk/lib/OpenGuides/Config.pm 2006-09-17 15:26:21 UTC (rev 868) @@ -8,7 +8,8 @@ dbtype dbname dbuser dbpass dbhost script_name install_directory script_url custom_lib_path use_plucene indexing_directory enable_page_deletion admin_pass stylesheet_url site_name navbar_on_home_page home_name - site_desc default_city default_country contact_email default_language + site_desc default_city default_country contact_email + default_language http_charset formatting_rules_node formatting_rules_link backlinks_in_title template_path custom_template_path geo_handler ellipsoid gmaps_api_key centre_long centre_lat default_gmaps_zoom default_gmaps_search_zoom force_wgs84 @@ -79,6 +80,7 @@ default_city => "", default_country => "", default_language => "en", + http_charset => "", formatting_rules_node => "Text Formatting Examples", formatting_rules_link => "http://openguides.org/page/text_formatting", backlinks_in_title => 0, @@ -149,6 +151,7 @@ default_country => "What country is the site based in?", contact_email => "Contact email address for the site administrator?", default_language => "What language will the site be in? (Please give an ISO language code.)", + http_charset => "What character set should we put in the http headers? (This won't change the character set internally, just what it's reported as). Leave blank for none to be sent", formatting_rules_node => "What's the name of the node or page to use for the text formatting rules link (this is by default an external document, but if you make formatting_rules_link empty, it will be a wiki node instead", formatting_rules_link => "What URL do you want to use for the text formatting rules (leave blank to use a wiki node instead)?", backlinks_in_title => "Make node titles link to node backlinks (C2 style)?", @@ -238,6 +241,8 @@
=item * default_language (default: C<en>)
+=item * http_charset + =item * contact_email
=item * formatting_rules_node (default: C<Text Formatting Examples>)
Modified: trunk/lib/OpenGuides/Template.pm =================================================================== --- trunk/lib/OpenGuides/Template.pm 2006-09-17 12:42:27 UTC (rev 867) +++ trunk/lib/OpenGuides/Template.pm 2006-09-17 15:26:21 UTC (rev 868) @@ -160,6 +160,7 @@ openguides_version => $OpenGuides::VERSION, enable_page_deletion => $enable_page_deletion, language => $config->default_language, + http_charset => $config->http_charset, default_city => $default_city, gmaps_api_key => $config->gmaps_api_key, licence_name => $config->licence_name, @@ -184,6 +185,9 @@ } else { $content_type = "text/html"; } + if ($tt_vars->{http_charset}) { + $content_type .= "; charset=".$tt_vars->{http_charset}; + } $header = CGI::header( -type => $content_type, -cookie => $args{cookies} ); }
Added: trunk/t/18_http_headers.t =================================================================== --- trunk/t/18_http_headers.t 2006-09-17 12:42:27 UTC (rev 867) +++ trunk/t/18_http_headers.t 2006-09-17 15:26:21 UTC (rev 868) @@ -0,0 +1,78 @@ +use strict; +use Cwd; +use CGI::Cookie; +use Wiki::Toolkit::Formatter::UseMod; +use OpenGuides::Config; +use OpenGuides::Template; +use Test::MockObject; +use Test::More tests => 5; + +my $config = OpenGuides::Config->new( + vars => { + template_path => cwd . '/t/templates', + site_name => 'Wiki::Toolkit Test Site', + script_url => 'http://wiki.example.com/', + script_name => 'mywiki.cgi', + default_country => 'United Kingdom', + default_city => 'London', + contact_email => 'wiki@example.com', + stylesheet_url => 'http://wiki.example.com/styles.css', + home_name => 'Home Page', + formatting_rules_node => 'Rules', + formatting_rules_link => '', + } +); + +# White box testing - we know that OpenGuides::Template only actually uses +# the node_name_to_node_param method of the formatter component of the wiki +# object passed in, and I CBA to make a proper wiki object here. +my $fake_wiki = Test::MockObject->new; +$fake_wiki->mock("formatter", + sub { return Wiki::Toolkit::Formatter::UseMod->new( munge_urls => 1 ); } ); + +eval { + OpenGuides::Template->output( wiki => $fake_wiki, + config => $config, + template => "15_test.tt" ); +}; +is( $@, "", "is happy doing output" ); + +my $output = OpenGuides::Template->output( + wiki => $fake_wiki, + config => $config, + template => "15_test.tt" +); +like( $output, qr/^Content-Type: text/html/, + "Content-Type header included and defaults to text/html" ); + +# Now supply a http charset +$config->{http_charset} = "UTF-8"; + +$output = OpenGuides::Template->output( + wiki => $fake_wiki, + config => $config, + template => "15_test.tt" +); +like( $output, qr/^Content-Type: text/html; charset=UTF-8/, + "Content-Type header included charset" ); + +# Suppy charset and content type +$output = OpenGuides::Template->output( + wiki => $fake_wiki, + config => $config, + content_type => "text/xml", + template => "15_test.tt" +); +like( $output, qr/^Content-Type: text/xml; charset=UTF-8/, + "Content-Type header included charset" ); + +# Content type but no charset +$config->{http_charset} = ""; +$output = OpenGuides::Template->output( + wiki => $fake_wiki, + config => $config, + content_type => "text/xml", + template => "15_test.tt" +); +like( $output, qr/^Content-Type: text/xml/, + "Content-Type header didn't include charset" );