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