This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make the test more portable.
[perl5.git] / t / lib / cgi-html.t
CommitLineData
424ec8fa
GS
1#!./perl
2
3# Test ability to retrieve HTTP request info
4######################### We start with some black magic to print on failure.
5
6BEGIN {
7 chdir 't' if -d 't';
93430cb4 8 unshift @INC, '../lib' if -d '../lib';
a1a0e61e 9 require Config; import Config;
424ec8fa
GS
10}
11
3d1a2ec4 12BEGIN {$| = 1; print "1..24\n"; }
424ec8fa 13END {print "not ok 1\n" unless $loaded;}
abfd9522 14use CGI (':standard','-no_debug','*h3','start_table');
424ec8fa
GS
15$loaded = 1;
16print "ok 1\n";
17
18######################### End of black magic.
19
53d56f48 20my $Is_EBCDIC = $Config{'ebcdic'} eq 'define';
8d78cc14 21my $crlf = $CGI::CRLF;
53d56f48 22
424ec8fa
GS
23# util
24sub test {
25 local($^W) = 0;
26 my($num, $true,$msg) = @_;
27 print($true ? "ok $num\n" : "not ok $num $msg\n");
28}
29
30# all the automatic tags
31test(2,h1() eq '<H1>',"single tag");
32test(3,h1('fred') eq '<H1>fred</H1>',"open/close tag");
33test(4,h1('fred','agnes','maura') eq '<H1>fred agnes maura</H1>',"open/close tag multiple");
34test(5,h1({-align=>'CENTER'},'fred') eq '<H1 ALIGN="CENTER">fred</H1>',"open/close tag with attribute");
35test(6,h1({-align=>undef},'fred') eq '<H1 ALIGN>fred</H1>',"open/close tag with orphan attribute");
36test(7,h1({-align=>'CENTER'},['fred','agnes']) eq
37 '<H1 ALIGN="CENTER">fred</H1> <H1 ALIGN="CENTER">agnes</H1>',
38 "distributive tag with attribute");
39{
40 local($") = '-';
41 test(8,h1('fred','agnes','maura') eq '<H1>fred-agnes-maura</H1>',"open/close tag \$\" interpolation");
42}
8d78cc14 43
3d1a2ec4
GS
44test(9,header() eq "Content-Type: text/html; charset=ISO-8859-1$crlf$crlf","header()");
45test(10,header(-type=>'image/gif') eq "Content-Type: image/gif; charset=ISO-8859-1$crlf$crlf","header()");
46test(11,header(-type=>'image/gif',-status=>'500 Sucks') eq "Status: 500 Sucks${crlf}Content-Type: image/gif; charset=ISO-8859-1$crlf$crlf","header()");
47test(12,header(-nph=>1) eq "HTTP/1.0 200 OK${crlf}Content-Type: text/html; charset=ISO-8859-1$crlf$crlf","header()");
424ec8fa 48test(13,start_html() ."\n" eq <<END,"start_html()");
3d1a2ec4
GS
49<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
50 "http://www.w3.org/TR/html4/loose.dtd">
424ec8fa
GS
51<HTML><HEAD><TITLE>Untitled Document</TITLE>
52</HEAD><BODY>
53END
54 ;
55test(14,start_html(-dtd=>"-//IETF//DTD HTML 3.2//FR") ."\n" eq <<END,"start_html()");
56<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 3.2//FR">
57<HTML><HEAD><TITLE>Untitled Document</TITLE>
58</HEAD><BODY>
59END
60 ;
61test(15,start_html(-Title=>'The world of foo') ."\n" eq <<END,"start_html()");
3d1a2ec4
GS
62<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
63 "http://www.w3.org/TR/html4/loose.dtd">
424ec8fa
GS
64<HTML><HEAD><TITLE>The world of foo</TITLE>
65</HEAD><BODY>
66END
67 ;
68test(16,($cookie=cookie(-name=>'fred',-value=>['chocolate','chip'],-path=>'/')) eq
cb50131a 69 'fred=chocolate&chip; path=/',"cookie()");
a8e97761 70test(17,header(-Cookie=>$cookie) =~ m!^Set-Cookie: fred=chocolate&chip\; path=/${crlf}Date:.*${crlf}Content-Type: text/html$crlf$crlf!s,
424ec8fa 71 "header(-cookie)");
abfd9522
JH
72test(18,start_h3 eq '<H3>');
73test(19,end_h3 eq '</H3>');
74test(20,start_table({-border=>undef}) eq '<TABLE BORDER>');
3d1a2ec4
GS
75test(21,h1(escapeHTML("this is <not> \x8bright\x9b")) eq '<H1>this is &lt;not&gt; &#139;right&#155;</H1>');
76charset('utf-8');
77test(22,h1(escapeHTML("this is <not> \x8bright\x9b")) eq '<H1>&#116;&#104;&#105;&#115;&#32;&#105;&#115;&#32;&#60;&#110;&#111;&#116;&#62;&#32;&#139;&#114;&#105;&#103;&#104;&#116;&#155;</H1>');
78test(23,i(p('hello there')) eq '<I><P>hello there</P></I>');
79my $q = new CGI;
80test(24,$q->h1('hi') eq '<H1>hi</H1>');