10Sstevel@tonic-gate#!/usr/local/bin/perl -w 20Sstevel@tonic-gate 30Sstevel@tonic-gateuse lib qw(t/lib); 40Sstevel@tonic-gate 50Sstevel@tonic-gate# Test ability to retrieve HTTP request info 60Sstevel@tonic-gate######################### We start with some black magic to print on failure. 7*6287Sps156622use lib '.','..','../blib/lib','../blib/arch'; 80Sstevel@tonic-gate 9*6287Sps156622BEGIN {$| = 1; print "1..32\n"; } 100Sstevel@tonic-gateEND {print "not ok 1\n" unless $loaded;} 110Sstevel@tonic-gateuse Config; 120Sstevel@tonic-gateuse CGI (':standard','keywords'); 130Sstevel@tonic-gate$loaded = 1; 140Sstevel@tonic-gateprint "ok 1\n"; 150Sstevel@tonic-gate 160Sstevel@tonic-gate######################### End of black magic. 170Sstevel@tonic-gate 180Sstevel@tonic-gate# util 190Sstevel@tonic-gatesub test { 200Sstevel@tonic-gate local($^W) = 0; 210Sstevel@tonic-gate my($num, $true,$msg) = @_; 220Sstevel@tonic-gate print($true ? "ok $num\n" : "not ok $num $msg\n"); 230Sstevel@tonic-gate} 240Sstevel@tonic-gate 250Sstevel@tonic-gatemy $CRLF = "\015\012"; 260Sstevel@tonic-gate 270Sstevel@tonic-gate# A peculiarity of sending "\n" through MBX|Socket|web-server on VMS 280Sstevel@tonic-gate# is that a CR character gets inserted automatically in the web server 290Sstevel@tonic-gate# case but not internal to perl's double quoted strings "\n". This 300Sstevel@tonic-gate# test would need to be modified to use the "\015\012" on VMS if it 310Sstevel@tonic-gate# were actually run through a web server. 320Sstevel@tonic-gate# Thanks to Peter Prymmer for this 330Sstevel@tonic-gate 340Sstevel@tonic-gateif ($^O eq 'VMS') { $CRLF = "\n"; } 350Sstevel@tonic-gate 360Sstevel@tonic-gate# Web servers on EBCDIC hosts are typically set up to do an EBCDIC -> ASCII 370Sstevel@tonic-gate# translation hence CRLF is used as \r\n within CGI.pm on such machines. 380Sstevel@tonic-gate 390Sstevel@tonic-gateif (ord("\t") != 9) { $CRLF = "\r\n"; } 400Sstevel@tonic-gate 410Sstevel@tonic-gate# Web servers on EBCDIC hosts are typically set up to do an EBCDIC -> ASCII 420Sstevel@tonic-gate# translation hence CRLF is used as \r\n within CGI.pm on such machines. 430Sstevel@tonic-gate 440Sstevel@tonic-gateif (ord("\t") != 9) { $CRLF = "\r\n"; } 450Sstevel@tonic-gate 460Sstevel@tonic-gate# Set up a CGI environment 470Sstevel@tonic-gate$ENV{REQUEST_METHOD}='GET'; 480Sstevel@tonic-gate$ENV{QUERY_STRING} ='game=chess&game=checkers&weather=dull'; 490Sstevel@tonic-gate$ENV{PATH_INFO} ='/somewhere/else'; 500Sstevel@tonic-gate$ENV{PATH_TRANSLATED} ='/usr/local/somewhere/else'; 510Sstevel@tonic-gate$ENV{SCRIPT_NAME} ='/cgi-bin/foo.cgi'; 520Sstevel@tonic-gate$ENV{SERVER_PROTOCOL} = 'HTTP/1.0'; 530Sstevel@tonic-gate$ENV{SERVER_PORT} = 8080; 540Sstevel@tonic-gate$ENV{SERVER_NAME} = 'the.good.ship.lollypop.com'; 550Sstevel@tonic-gate$ENV{HTTP_LOVE} = 'true'; 560Sstevel@tonic-gate 570Sstevel@tonic-gatetest(2,request_method() eq 'GET',"CGI::request_method()"); 580Sstevel@tonic-gatetest(3,query_string() eq 'game=chess;game=checkers;weather=dull',"CGI::query_string()"); 590Sstevel@tonic-gatetest(4,param() == 2,"CGI::param()"); 600Sstevel@tonic-gatetest(5,join(' ',sort {$a cmp $b} param()) eq 'game weather',"CGI::param()"); 610Sstevel@tonic-gatetest(6,param('game') eq 'chess',"CGI::param()"); 620Sstevel@tonic-gatetest(7,param('weather') eq 'dull',"CGI::param()"); 630Sstevel@tonic-gatetest(8,join(' ',param('game')) eq 'chess checkers',"CGI::param()"); 640Sstevel@tonic-gatetest(9,param(-name=>'foo',-value=>'bar'),'CGI::param() put'); 650Sstevel@tonic-gatetest(10,param(-name=>'foo') eq 'bar','CGI::param() get'); 660Sstevel@tonic-gatetest(11,query_string() eq 'game=chess;game=checkers;weather=dull;foo=bar',"CGI::query_string() redux"); 670Sstevel@tonic-gatetest(12,http('love') eq 'true',"CGI::http()"); 680Sstevel@tonic-gatetest(13,script_name() eq '/cgi-bin/foo.cgi',"CGI::script_name()"); 690Sstevel@tonic-gatetest(14,url() eq 'http://the.good.ship.lollypop.com:8080/cgi-bin/foo.cgi',"CGI::url()"); 700Sstevel@tonic-gatetest(15,self_url() eq 710Sstevel@tonic-gate 'http://the.good.ship.lollypop.com:8080/cgi-bin/foo.cgi/somewhere/else?game=chess;game=checkers;weather=dull;foo=bar', 720Sstevel@tonic-gate "CGI::url()"); 730Sstevel@tonic-gatetest(16,url(-absolute=>1) eq '/cgi-bin/foo.cgi','CGI::url(-absolute=>1)'); 740Sstevel@tonic-gatetest(17,url(-relative=>1) eq 'foo.cgi','CGI::url(-relative=>1)'); 750Sstevel@tonic-gatetest(18,url(-relative=>1,-path=>1) eq 'foo.cgi/somewhere/else','CGI::url(-relative=>1,-path=>1)'); 760Sstevel@tonic-gatetest(19,url(-relative=>1,-path=>1,-query=>1) eq 770Sstevel@tonic-gate 'foo.cgi/somewhere/else?game=chess;game=checkers;weather=dull;foo=bar', 780Sstevel@tonic-gate 'CGI::url(-relative=>1,-path=>1,-query=>1)'); 790Sstevel@tonic-gateDelete('foo'); 800Sstevel@tonic-gatetest(20,!param('foo'),'CGI::delete()'); 810Sstevel@tonic-gate 820Sstevel@tonic-gateCGI::_reset_globals(); 830Sstevel@tonic-gate$ENV{QUERY_STRING}='mary+had+a+little+lamb'; 840Sstevel@tonic-gatetest(21,join(' ',keywords()) eq 'mary had a little lamb','CGI::keywords'); 850Sstevel@tonic-gatetest(22,join(' ',param('keywords')) eq 'mary had a little lamb','CGI::keywords'); 860Sstevel@tonic-gate 870Sstevel@tonic-gateCGI::_reset_globals; 880Sstevel@tonic-gateif ($Config{d_fork}) { 890Sstevel@tonic-gate $test_string = 'game=soccer&game=baseball&weather=nice'; 900Sstevel@tonic-gate $ENV{REQUEST_METHOD}='POST'; 910Sstevel@tonic-gate $ENV{CONTENT_LENGTH}=length($test_string); 920Sstevel@tonic-gate $ENV{QUERY_STRING}='big_balls=basketball&small_balls=golf'; 930Sstevel@tonic-gate if (open(CHILD,"|-")) { # cparent 940Sstevel@tonic-gate print CHILD $test_string; 950Sstevel@tonic-gate close CHILD; 960Sstevel@tonic-gate exit 0; 970Sstevel@tonic-gate } 980Sstevel@tonic-gate # at this point, we're in a new (child) process 990Sstevel@tonic-gate test(23,param('weather') eq 'nice',"CGI::param() from POST"); 1000Sstevel@tonic-gate test(24,(url_param('big_balls') eq 'basketball'),"CGI::url_param()"); 1010Sstevel@tonic-gate} else { 1020Sstevel@tonic-gate print "ok 23 # Skip\n"; 1030Sstevel@tonic-gate print "ok 24 # Skip\n"; 1040Sstevel@tonic-gate} 105*6287Sps156622test(25,redirect('http://somewhere.else') eq "Status: 302 Found${CRLF}Location: http://somewhere.else${CRLF}${CRLF}","CGI::redirect() 1"); 1060Sstevel@tonic-gatemy $h = redirect(-Location=>'http://somewhere.else',-Type=>'text/html'); 107*6287Sps156622test(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"); 108*6287Sps156622test(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"); 1090Sstevel@tonic-gate 1100Sstevel@tonic-gatetest(28,escapeHTML('CGI') eq 'CGI','escapeHTML(CGI) failing again'); 1110Sstevel@tonic-gate 1120Sstevel@tonic-gatetest(29, charset("UTF-8") && header() eq "Content-Type: text/html; charset=UTF-8${CRLF}${CRLF}", "UTF-8 charset"); 1130Sstevel@tonic-gatetest(30, !charset("") && header() eq "Content-Type: text/html${CRLF}${CRLF}", "Empty charset"); 1140Sstevel@tonic-gate 1150Sstevel@tonic-gatetest(31, header(-foo=>'bar') eq "Foo: bar${CRLF}Content-Type: text/html${CRLF}${CRLF}", "Custom header"); 116*6287Sps156622 117*6287Sps156622test(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"); 118