Permalink
Browse files

tests header() more

  • Loading branch information...
1 parent 17290e2 commit cc2eeedfe37328ee298e57ac93a7bba571e1d504 Ryo Anazawa committed Apr 6, 2012
View
1 Makefile.PL
@@ -17,6 +17,7 @@ WriteMakefile(
'File::Spec' => .82,
'FCGI' => 0.67,
},
+ test => { TESTS => 't/*.t t/headers/*.t' },
'linkext' => { LINKTYPE=>'' }, # no link needed
'dist' => {'COMPRESS'=>'gzip -9f', 'SUFFIX' => 'gz',
'ZIP'=>'/usr/bin/zip','ZIPFLAGS'=>'-rl'},
View
13 t/headers/00_default.t
@@ -0,0 +1,13 @@
+use strict;
+use CGI;
+use Test::More;
+
+{
+ my $q = CGI->new;
+ my $got = $q->header();
+ my $expected = 'Content-Type: text/html; charset=ISO-8859-1'
+ . $CGI::CRLF x 2;
+ is $got, $expected, 'default';
+}
+
+done_testing;
View
182 t/headers/10_type.t
@@ -0,0 +1,182 @@
+use strict;
+use CGI;
+use Test::More;
+
+{
+ my $q = CGI->new;
+ my $got = $q->header( -type => 'text/plain' );
+ my $expected = 'Content-Type: text/plain; charset=ISO-8859-1'
+ . $CGI::CRLF x 2;
+ is $got, $expected, 'type';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header( -type => q{} );
+ my $expected = $CGI::CRLF x 2;
+ is $got, $expected, 'type empty string';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header( -type => 'text/plain; charset=utf-8' );
+ my $expected = 'Content-Type: text/plain; charset=utf-8'
+ . $CGI::CRLF x 2;
+ is $got, $expected, 'type defines charset';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header(
+ '-type' => 'text/html',
+ '-Content-Type' => 'text/plain',
+ );
+ my $expected = 'Content-Type: text/plain; charset=ISO-8859-1'
+ . $CGI::CRLF x 2;
+ is $got, $expected, 'type and content-type';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header(
+ '-Content-Type' => 'text/plain',
+ '-type' => 'text/html',
+ );
+ my $expected = "Content-Type: text/html; charset=ISO-8859-1"
+ . $CGI::CRLF x 2;
+ is $got, $expected, 'content-type and type';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header(
+ '-Content-Type' => 'text/plain',
+ '-type' => q{},
+ );
+ my $expected = $CGI::CRLF x 2;
+ is $got, $expected, 'content-type and type, type is empty';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header(
+ '-Content-Type' => q{},
+ '-type' => 'text/plain',
+ );
+ my $expected = 'Content-Type: text/plain; charset=ISO-8859-1'
+ . $CGI::CRLF x 2;
+ is $got, $expected, 'content-type and type, content-type is empty';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header(
+ '-Content-Type' => 'text/plain; charset=utf-8',
+ '-type' => 'text/html',
+ );
+ my $expected = "Content-Type: text/html; charset=ISO-8859-1"
+ . $CGI::CRLF x 2;
+ is $got, $expected, 'content-type and type, content-type defines charset';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header(
+ '-Content-Type' => 'text/plain',
+ '-type' => 'text/html; charset=utf-8',
+ );
+ my $expected = 'Content-Type: text/html; charset=utf-8'
+ . $CGI::CRLF x 2;
+ is $got, $expected, 'content-type and type, type defines charset';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header(
+ '-type' => 'text/plain',
+ '-charset' => 'utf-8',
+ );
+ my $expected = 'Content-Type: text/plain; charset=utf-8'
+ . $CGI::CRLF x 2;
+ is $got, $expected, 'type and charset';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header(
+ '-type' => q{},
+ '-charset' => 'utf-8',
+ );
+ my $expected = $CGI::CRLF x 2;
+ is $got, $expected, 'type and charset, type is empty string';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header(
+ '-type' => 'text/plain',
+ '-charset' => q{},
+ );
+ my $expected = 'Content-Type: text/plain' . $CGI::CRLF x 2;
+ is $got, $expected, 'type and charset, charset is empty string';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header(
+ '-type' => 'text/plain; charset=utf-8',
+ '-charset' => 'EUC-JP',
+ );
+ my $expected = 'Content-Type: text/plain; charset=utf-8'
+ . $CGI::CRLF x 2;
+ is $got, $expected, 'type and charset, type defines charset';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header(
+ '-type' => 'text/plain; charset=utf-8',
+ '-charset' => 'EUC-JP',
+ );
+ my $expected = 'Content-Type: text/plain; charset=utf-8'
+ . $CGI::CRLF x 2;
+ is $got, $expected, 'type and charset, type defines charset';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header(
+ '-type' => 'text/plain',
+ '-charset' => 'EUC-JP',
+ '-Content-Type' => 'text/html; charset=Shift_JIS',
+ );
+ my $expected = 'Content-Type: text/html; charset=Shift_JIS'
+ . $CGI::CRLF x 2;
+ is $got, $expected, 'type and charset and Content-Type';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header(
+ '-type' => 'text/plain; charset=utf-8',
+ '-charset' => 'EUC-JP',
+ '-Content-Type' => 'text/html; charset=Shift_JIS',
+ );
+ my $expected = 'Content-Type: text/html; charset=Shift_JIS'
+ . $CGI::CRLF x 2;
+ is $got, $expected, 'type & charset & Content-Type, type defines charset';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header(
+ '-type' => q{},
+ '-charset' => 'EUC-JP',
+ '-Content-Type' => 'text/html; charset=Shift_JIS',
+ );
+ my $expected = 'Content-Type: text/html; charset=Shift_JIS'
+ . $CGI::CRLF x 2;
+ is $got, $expected, 'type & charset & Content-Type, type is empty string';
+}
+
+done_testing;
View
63 t/headers/11_charset.t
@@ -0,0 +1,63 @@
+use strict;
+use CGI;
+use Test::More;
+
+{
+ my $q = CGI->new;
+ my $got = $q->header( -charset => 'utf-8' );
+ my $expected = 'Content-Type: text/html; charset=utf-8'
+ . $CGI::CRLF x 2;
+ is $got, $expected, 'charset';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header( -charset => q{} );
+ my $expected = 'Content-Type: text/html' . $CGI::CRLF x 2;
+ is $got, $expected, 'charset empty string';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header(
+ '-Content-Type' => 'text/plain; charset=EUC-JP',
+ '-charset' => 'utf-8',
+ );
+ my $expected = "Content-Type: text/plain; charset=EUC-JP"
+ . $CGI::CRLF x 2;
+ is $got, $expected, 'content-type and charset';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header(
+ '-Content-Type' => 'text/plain; charset=EUC-JP',
+ '-charset' => q{},
+ );
+ my $expected = 'Content-Type: text/plain; charset=EUC-JP'
+ . $CGI::CRLF x 2;
+ is $got, $expected, 'content-type & charset, charset is empty string';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header(
+ '-Content-Type' => q{},
+ '-charset' => 'utf-8',
+ );
+ my $expected = $CGI::CRLF x 2;
+ is $got, $expected, 'content-type & charset, content-type is empty string';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header(
+ '-Content-Type' => 'text/plain',
+ '-charset' => 'utf-8',
+ );
+ my $expected = 'Content-Type: text/plain; charset=utf-8'
+ . $CGI::CRLF x 2;
+ is $got, $expected, 'content-type & charset, charset defines charset';
+}
+
+done_testing;
View
35 t/headers/12_content_type.t
@@ -0,0 +1,35 @@
+use strict;
+use CGI;
+use Test::More;
+
+{
+ my $q = CGI->new;
+ my $got = $q->header( '-Content-Type' => 'text/plain; charset=utf-8' );
+ my $expected = 'Content-Type: text/plain; charset=utf-8'
+ . $CGI::CRLF x 2;
+ is $got, $expected, 'content-type';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header( '-Content-Type' => 'text/plain' );
+ my $expected = 'Content-Type: text/plain; charset=ISO-8859-1'
+ . $CGI::CRLF x 2;
+ is $got, $expected, 'content-type without charset';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header( '-Content-Type' => q{} );
+ my $expected = $CGI::CRLF x 2;
+ is $got, $expected, 'content-type empty string';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header( '-Content-Type' => 'charset=utf-8' );
+ my $expected = "Content-Type: charset=utf-8" . $CGI::CRLF x 2;
+ is $got, $expected, 'content-type edge case';
+}
+
+done_testing;
View
39 t/headers/20_attachment.t
@@ -0,0 +1,39 @@
+use strict;
+use CGI;
+use Test::More;
+
+{
+ my $q = CGI->new;
+ my $got = $q->header( -attachment => 'foo.png' );
+ my $expected = qr!^Content-Disposition: attachment; filename="foo.png"!;
+ like $got, $expected, 'attachment';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header( -attachment => q{} );
+ my $expected = "Content-Type: text/html; charset=ISO-8859-1"
+ . $CGI::CRLF x 2;
+ is $got, $expected, 'attachment empty string';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header(
+ '-Content-Disposition' => 'attachment; filename="foo.png"',
+ );
+ my $expected = qr!^Content-disposition: attachment; filename="foo.png"!;
+ like $got, $expected, 'content-disposition';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header(
+ '-Content-Disposition' => 'attachment; filename="foo.png"',
+ '-attachment' => 'bar.png',
+ );
+ my $expected = qr!^Content-Disposition: attachment; filename="bar.png"!;
+ like $got, $expected, 'content-disposition';
+}
+
+done_testing;
View
64 t/headers/30_cookie.t
@@ -0,0 +1,64 @@
+use strict;
+use CGI;
+use Test::More;
+
+{
+ my $q = CGI->new;
+ my $got = $q->header( -cookie => 'foo' );
+ my $expected = qr{^Set-Cookie: foo};
+ like $got, $expected, 'cookie';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header( -cookie => [ 'foo', 'bar' ] );
+ my $expected = qr!^Set-Cookie: foo${CGI::CRLF}Set-Cookie: bar!;
+ like $got, $expected, 'cookie arrayref';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header( -cookie => q{} );
+ my $expected = 'Content-Type: text/html; charset=ISO-8859-1'
+ . $CGI::CRLF x 2;
+ is $got, $expected, 'cookie empty string';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header(
+ '-cookie' => 'foo',
+ '-Set-Cookie' => 'bar',
+ );
+ my $expected = "^Set-Cookie: foo$CGI::CRLF"
+ . "Date: [^$CGI::CRLF]+$CGI::CRLF"
+ . "Set-cookie: bar$CGI::CRLF";
+ like $got, qr($expected), 'cookie & set-cookie';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header(
+ '-cookie' => [ 'foo', 'bar' ],
+ '-Set-Cookie' => 'baz',
+ );
+ my $expected = "^Set-Cookie: foo$CGI::CRLF"
+ . "Set-Cookie: bar$CGI::CRLF"
+ . "Date: [^$CGI::CRLF]+$CGI::CRLF"
+ . "Set-cookie: baz$CGI::CRLF";
+ like $got, qr($expected), 'cookie & set-cookie, cookie is arrayref';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header(
+ '-cookie' => q{},
+ '-Set-Cookie' => 'foo',
+ );
+ my $expected = "Set-cookie: foo$CGI::CRLF"
+ . "Content-Type: text/html; charset=ISO-8859-1"
+ . $CGI::CRLF x 2;
+ is $got, $expected, 'cookie & set-cookie, cookie is empty string';
+}
+
+done_testing;
View
23 t/headers/31_set_cookie.t
@@ -0,0 +1,23 @@
+use strict;
+use CGI;
+use Test::More;
+
+{
+ my $q = CGI->new;
+ my $got = $q->header( '-Set-Cookie' => 'foo' );
+ my $expected = "Set-cookie: foo$CGI::CRLF"
+ . 'Content-Type: text/html; charset=ISO-8859-1'
+ . $CGI::CRLF x 2;
+ is $got, $expected, 'set-cookie';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header( '-Set-Cookie' => q{} );
+ my $expected = "Set-cookie: \"$CGI::CRLF" # looks like a bug
+ . 'Content-Type: text/html; charset=ISO-8859-1'
+ . $CGI::CRLF x 2;
+ is $got, $expected, 'set-cookie empty string';
+}
+
+done_testing;
View
46 t/headers/40_p3p.t
@@ -0,0 +1,46 @@
+use strict;
+use CGI;
+use Test::More;
+
+{
+ my $q = CGI->new;
+ my $got = $q->header( -p3p => "CAO DSP LAW CURa" );
+ my $expected = 'P3P: policyref="/w3c/p3p.xml", CP="CAO DSP LAW CURa"'
+ . $CGI::CRLF
+ . 'Content-Type: text/html; charset=ISO-8859-1'
+ . $CGI::CRLF x 2;
+ is $got, $expected, 'p3p';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header( -p3p => [ qw/CAO DSP LAW CURa/ ] );
+ my $expected = 'P3P: policyref="/w3c/p3p.xml", CP="CAO DSP LAW CURa"'
+ . $CGI::CRLF
+ . 'Content-Type: text/html; charset=ISO-8859-1'
+ . $CGI::CRLF x 2;
+ is $got, $expected, 'p3p arrayref';
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header(
+ -P3P => 'policyref="/w3c/p3p.xml", CP="CAO DSP LAW CURa"',
+ );
+ my $expected = 'P3P: policyref="/w3c/p3p.xml", CP="'
+ . 'policyref="/w3c/p3p.xml", CP="CAO DSP LAW CURa"'
+ . qq{"$CGI::CRLF}
+ . 'Content-Type: text/html; charset=ISO-8859-1'
+ . $CGI::CRLF x 2;
+ is $got, $expected, 'p3p raw'; # doesn't work
+}
+
+{
+ my $q = CGI->new;
+ my $got = $q->header( -p3p => q{} );
+ my $expected = 'Content-Type: text/html; charset=ISO-8859-1'
+ . $CGI::CRLF x 2;
+ is $got, $expected, 'p3p empty string';
+}
+
+done_testing;
View
12 t/headers/50_nph.t
@@ -0,0 +1,12 @@
+use strict;
+use CGI;
+use Test::More;
+
+{
+ my $q = CGI->new;
+ my $got = $q->header( -nph => 1 );
+ my $expected = qr!^HTTP/1.0 200 OK${CGI::CRLF}Server: cmdline!;
+ like $got, $expected, 'nph';
+}
+
+done_testing;
View
14 t/headers/60_target.t
@@ -0,0 +1,14 @@
+use strict;
+use CGI;
+use Test::More;
+
+{
+ my $q = CGI->new;
+ my $got = $q->header( -target => 'ResultsWindow' );
+ my $expected = "Window-Target: ResultsWindow$CGI::CRLF"
+ . 'Content-Type: text/html; charset=ISO-8859-1'
+ . $CGI::CRLF x 2;
+ is $got, $expected, 'target';
+}
+
+done_testing;

0 comments on commit cc2eeed

Please sign in to comment.