This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
d7f3ffb4aa285d50b565a6c6979466df4faca47e
[perl5.git] / t / lib / cgi-html.t
1 #!./perl
2
3 # Test ability to retrieve HTTP request info
4 ######################### We start with some black magic to print on failure.
5
6 BEGIN {
7     chdir 't' if -d 't';
8     @INC = '../lib' if -d '../lib';
9 }
10
11 BEGIN {$| = 1; print "1..17\n"; }
12 BEGIN {$eol = $^O eq 'VMS' ? "\n" : "\cM\cJ";}
13 END {print "not ok 1\n" unless $loaded;}
14 use CGI (':standard','-no_debug');
15 $loaded = 1;
16 print "ok 1\n";
17
18 ######################### End of black magic.
19
20 # util
21 sub test {
22     local($^W) = 0;
23     my($num, $true,$msg) = @_;
24     print($true ? "ok $num\n" : "not ok $num $msg\n");
25 }
26
27 # all the automatic tags
28 test(2,h1() eq '<H1>',"single tag");
29 test(3,h1('fred') eq '<H1>fred</H1>',"open/close tag");
30 test(4,h1('fred','agnes','maura') eq '<H1>fred agnes maura</H1>',"open/close tag multiple");
31 test(5,h1({-align=>'CENTER'},'fred') eq '<H1 ALIGN="CENTER">fred</H1>',"open/close tag with attribute");
32 test(6,h1({-align=>undef},'fred') eq '<H1 ALIGN>fred</H1>',"open/close tag with orphan attribute");
33 test(7,h1({-align=>'CENTER'},['fred','agnes']) eq 
34      '<H1 ALIGN="CENTER">fred</H1> <H1 ALIGN="CENTER">agnes</H1>',
35      "distributive tag with attribute");
36 {
37     local($") = '-'; 
38     test(8,h1('fred','agnes','maura') eq '<H1>fred-agnes-maura</H1>',"open/close tag \$\" interpolation");
39 }
40 test(9,header() eq "Content-Type: text/html${eol}${eol}","header()");
41 test(10,header(-type=>'image/gif') eq "Content-Type: image/gif${eol}${eol}","header()");
42 test(11,header(-type=>'image/gif',-status=>'500 Sucks') eq "Status: 500 Sucks${eol}Content-Type: image/gif${eol}${eol}","header()");
43 test(12,header(-nph=>1) eq "HTTP/1.0 200 OK${eol}Content-Type: text/html${eol}${eol}","header()");
44 test(13,start_html() ."\n" eq <<END,"start_html()");
45 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
46 <HTML><HEAD><TITLE>Untitled Document</TITLE>
47 </HEAD><BODY>
48 END
49     ;
50 test(14,start_html(-dtd=>"-//IETF//DTD HTML 3.2//FR") ."\n" eq <<END,"start_html()");
51 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 3.2//FR">
52 <HTML><HEAD><TITLE>Untitled Document</TITLE>
53 </HEAD><BODY>
54 END
55     ;
56 test(15,start_html(-Title=>'The world of foo') ."\n" eq <<END,"start_html()");
57 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
58 <HTML><HEAD><TITLE>The world of foo</TITLE>
59 </HEAD><BODY>
60 END
61     ;
62 test(16,($cookie=cookie(-name=>'fred',-value=>['chocolate','chip'],-path=>'/')) eq 
63      'fred=chocolate&chip; path=/',"cookie()");
64 test(17,header(-Cookie=>$cookie) =~ m!^Set-Cookie: fred=chocolate&chip\; path=/${eol}Date:.*${eol}Content-Type: text/html${eol}${eol}!s,
65      "header(-cookie)");