316b5858a6b2636508965103af59eed080434f68
[p5sagit/p5-mst-13.2.git] / cpan / CGI / t / function.t
1 #!/usr/local/bin/perl -w
2
3 BEGIN {$| = 1; print "1..32\n"; }
4 END {print "not ok 1\n" unless $loaded;}
5 use Config;
6 use CGI (':standard','keywords');
7 $loaded = 1;
8 print "ok 1\n";
9
10 ######################### End of black magic.
11
12 # util
13 sub test {
14     local($^W) = 0;
15     my($num, $true,$msg) = @_;
16     print($true ? "ok $num\n" : "not ok $num $msg\n");
17 }
18
19 my $CRLF = "\015\012";
20
21 # A peculiarity of sending "\n" through MBX|Socket|web-server on VMS 
22 # is that a CR character gets inserted automatically in the web server 
23 # case but not internal to perl's double quoted strings "\n".  This
24 # test would need to be modified to use the "\015\012" on VMS if it
25 # were actually run through a web server.
26 # Thanks to Peter Prymmer for this
27
28 if ($^O eq 'VMS') { $CRLF = "\n"; }
29
30 # Web servers on EBCDIC hosts are typically set up to do an EBCDIC -> ASCII
31 # translation hence CRLF is used as \r\n within CGI.pm on such machines.
32
33 if (ord("\t") != 9) { $CRLF = "\r\n"; }
34
35 # Web servers on EBCDIC hosts are typically set up to do an EBCDIC -> ASCII
36 # translation hence CRLF is used as \r\n within CGI.pm on such machines.
37
38 if (ord("\t") != 9) { $CRLF = "\r\n"; }
39  
40 # Set up a CGI environment
41 $ENV{REQUEST_METHOD}='GET';
42 $ENV{QUERY_STRING}  ='game=chess&game=checkers&weather=dull';
43 $ENV{PATH_INFO}     ='/somewhere/else';
44 $ENV{PATH_TRANSLATED} ='/usr/local/somewhere/else';
45 $ENV{SCRIPT_NAME}   ='/cgi-bin/foo.cgi';
46 $ENV{SERVER_PROTOCOL} = 'HTTP/1.0';
47 $ENV{SERVER_PORT} = 8080;
48 $ENV{SERVER_NAME} = 'the.good.ship.lollypop.com';
49 $ENV{HTTP_LOVE} = 'true';
50
51 test(2,request_method() eq 'GET',"CGI::request_method()");
52 test(3,query_string() eq 'game=chess;game=checkers;weather=dull',"CGI::query_string()");
53 test(4,param() == 2,"CGI::param()");
54 test(5,join(' ',sort {$a cmp $b} param()) eq 'game weather',"CGI::param()");
55 test(6,param('game') eq 'chess',"CGI::param()");
56 test(7,param('weather') eq 'dull',"CGI::param()");
57 test(8,join(' ',param('game')) eq 'chess checkers',"CGI::param()");
58 test(9,param(-name=>'foo',-value=>'bar'),'CGI::param() put');
59 test(10,param(-name=>'foo') eq 'bar','CGI::param() get');
60 test(11,query_string() eq 'game=chess;game=checkers;weather=dull;foo=bar',"CGI::query_string() redux");
61 test(12,http('love') eq 'true',"CGI::http()");
62 test(13,script_name() eq '/cgi-bin/foo.cgi',"CGI::script_name()");
63 test(14,url() eq 'http://the.good.ship.lollypop.com:8080/cgi-bin/foo.cgi',"CGI::url()");
64 test(15,self_url() eq 
65      'http://the.good.ship.lollypop.com:8080/cgi-bin/foo.cgi/somewhere/else?game=chess;game=checkers;weather=dull;foo=bar',
66      "CGI::url()");
67 test(16,url(-absolute=>1) eq '/cgi-bin/foo.cgi','CGI::url(-absolute=>1)');
68 test(17,url(-relative=>1) eq 'foo.cgi','CGI::url(-relative=>1)');
69 test(18,url(-relative=>1,-path=>1) eq 'foo.cgi/somewhere/else','CGI::url(-relative=>1,-path=>1)');
70 test(19,url(-relative=>1,-path=>1,-query=>1) eq 
71      'foo.cgi/somewhere/else?game=chess;game=checkers;weather=dull;foo=bar',
72      'CGI::url(-relative=>1,-path=>1,-query=>1)');
73 Delete('foo');
74 test(20,!param('foo'),'CGI::delete()');
75
76 CGI::_reset_globals();
77 $ENV{QUERY_STRING}='mary+had+a+little+lamb';
78 test(21,join(' ',keywords()) eq 'mary had a little lamb','CGI::keywords');
79 test(22,join(' ',param('keywords')) eq 'mary had a little lamb','CGI::keywords');
80
81 CGI::_reset_globals;
82 if ($Config{d_fork}) {
83   $test_string = 'game=soccer&game=baseball&weather=nice';
84   $ENV{REQUEST_METHOD}='POST';
85   $ENV{CONTENT_LENGTH}=length($test_string);
86   $ENV{QUERY_STRING}='big_balls=basketball&small_balls=golf';
87   if (open(CHILD,"|-")) {  # cparent
88     print CHILD $test_string;
89     close CHILD;
90     exit 0;
91   }
92   # at this point, we're in a new (child) process
93   test(23,param('weather') eq 'nice',"CGI::param() from POST");
94   test(24,(url_param('big_balls') eq 'basketball'),"CGI::url_param()");
95 } else {
96   print "ok 23 # Skip\n";
97   print "ok 24 # Skip\n";
98 }
99 test(25,redirect('http://somewhere.else') eq "Status: 302 Found${CRLF}Location: http://somewhere.else${CRLF}${CRLF}","CGI::redirect() 1");
100 my $h = redirect(-Location=>'http://somewhere.else',-Type=>'text/html');
101 test(26,$h eq "Status: 302 Found${CRLF}Location: http://somewhere.else${CRLF}Content-Type: text/html; charset=ISO-8859-1${CRLF}${CRLF}","CGI::redirect() 2");
102 test(27,redirect(-Location=>'http://somewhere.else/bin/foo&bar',-Type=>'text/html') eq "Status: 302 Found${CRLF}Location: http://somewhere.else/bin/foo&bar${CRLF}Content-Type: text/html; charset=ISO-8859-1${CRLF}${CRLF}","CGI::redirect() 2");
103
104 test(28,escapeHTML('CGI') eq 'CGI','escapeHTML(CGI) failing again');
105
106 test(29, charset("UTF-8") && header() eq "Content-Type: text/html; charset=UTF-8${CRLF}${CRLF}", "UTF-8 charset");
107 test(30, !charset("") && header() eq "Content-Type: text/html${CRLF}${CRLF}", "Empty charset");
108
109 test(31, header(-foo=>'bar') eq "Foo: bar${CRLF}Content-Type: text/html${CRLF}${CRLF}", "Custom header");
110
111 test(32, start_form(-action=>'one',name=>'two',onsubmit=>'three') eq qq(<form method="post" action="one" enctype="multipart/form-data" onsubmit="three" name="two">\n), "initial dash followed by undashed arguments");