1#!/usr/local/bin/perl -w 2 3# Test ability to retrieve HTTP request info 4######################### We start with some black magic to print on failure. 5use lib '.','../blib/lib','../blib/arch'; 6 7BEGIN {$| = 1; print "1..33\n"; } 8END {print "not ok 1\n" unless $loaded;} 9use CGI (); 10use Config; 11$loaded = 1; 12print "ok 1\n"; 13 14######################### End of black magic. 15 16# util 17sub test { 18 local($^W) = 0; 19 my($num, $true,$msg) = @_; 20 print($true ? "ok $num\n" : "not ok $num $msg\n"); 21} 22 23# Set up a CGI environment 24$ENV{REQUEST_METHOD} = 'GET'; 25$ENV{QUERY_STRING} = 'game=chess&game=checkers&weather=dull'; 26$ENV{PATH_INFO} = '/somewhere/else'; 27$ENV{PATH_TRANSLATED} = '/usr/local/somewhere/else'; 28$ENV{SCRIPT_NAME} = '/cgi-bin/foo.cgi'; 29$ENV{SERVER_PROTOCOL} = 'HTTP/1.0'; 30$ENV{SERVER_PORT} = 8080; 31$ENV{SERVER_NAME} = 'the.good.ship.lollypop.com'; 32$ENV{REQUEST_URI} = "$ENV{SCRIPT_NAME}$ENV{PATH_INFO}?$ENV{QUERY_STRING}"; 33$ENV{HTTP_LOVE} = 'true'; 34 35$q = new CGI; 36test(2,$q,"CGI::new()"); 37test(3,$q->request_method eq 'GET',"CGI::request_method()"); 38test(4,$q->query_string eq 'game=chess;game=checkers;weather=dull',"CGI::query_string()"); 39test(5,$q->param() == 2,"CGI::param()"); 40test(6,join(' ',sort $q->param()) eq 'game weather',"CGI::param()"); 41test(7,$q->param('game') eq 'chess',"CGI::param()"); 42test(8,$q->param('weather') eq 'dull',"CGI::param()"); 43test(9,join(' ',$q->param('game')) eq 'chess checkers',"CGI::param()"); 44test(10,$q->param(-name=>'foo',-value=>'bar'),'CGI::param() put'); 45test(11,$q->param(-name=>'foo') eq 'bar','CGI::param() get'); 46test(12,$q->query_string eq 'game=chess;game=checkers;weather=dull;foo=bar',"CGI::query_string() redux"); 47test(13,$q->http('love') eq 'true',"CGI::http()"); 48test(14,$q->script_name eq '/cgi-bin/foo.cgi',"CGI::script_name()"); 49test(15,$q->url eq 'http://the.good.ship.lollypop.com:8080/cgi-bin/foo.cgi',"CGI::url()"); 50test(16,$q->self_url eq 51 'http://the.good.ship.lollypop.com:8080/cgi-bin/foo.cgi/somewhere/else?game=chess;game=checkers;weather=dull;foo=bar', 52 "CGI::url()"); 53test(17,$q->url(-absolute=>1) eq '/cgi-bin/foo.cgi','CGI::url(-absolute=>1)'); 54test(18,$q->url(-relative=>1) eq 'foo.cgi','CGI::url(-relative=>1)'); 55test(19,$q->url(-relative=>1,-path=>1) eq 'foo.cgi/somewhere/else','CGI::url(-relative=>1,-path=>1)'); 56test(20,$q->url(-relative=>1,-path=>1,-query=>1) eq 57 'foo.cgi/somewhere/else?game=chess;game=checkers;weather=dull;foo=bar', 58 'CGI::url(-relative=>1,-path=>1,-query=>1)'); 59$q->delete('foo'); 60test(21,!$q->param('foo'),'CGI::delete()'); 61 62$q->_reset_globals; 63$ENV{QUERY_STRING}='mary+had+a+little+lamb'; 64test(22,$q=new CGI,"CGI::new() redux"); 65test(23,join(' ',$q->keywords) eq 'mary had a little lamb','CGI::keywords'); 66test(24,join(' ',$q->param('keywords')) eq 'mary had a little lamb','CGI::keywords'); 67test(25,$q=new CGI('foo=bar&foo=baz'),"CGI::new() redux"); 68test(26,$q->param('foo') eq 'bar','CGI::param() redux'); 69test(27,$q=new CGI({'foo'=>'bar','bar'=>'froz'}),"CGI::new() redux 2"); 70test(28,$q->param('bar') eq 'froz',"CGI::param() redux 2"); 71 72# test tied interface 73my $p = $q->Vars; 74test(29,$p->{bar} eq 'froz',"tied interface fetch"); 75$p->{bar} = join("\0",qw(foo bar baz)); 76test(30,join(' ',$q->param('bar')) eq 'foo bar baz','tied interface store'); 77 78# test posting 79$q->_reset_globals; 80if ($Config{d_fork}) { 81 $test_string = 'game=soccer&game=baseball&weather=nice'; 82 $ENV{REQUEST_METHOD}='POST'; 83 $ENV{CONTENT_LENGTH}=length($test_string); 84 $ENV{QUERY_STRING}='big_balls=basketball&small_balls=golf'; 85 if (open(CHILD,"|-")) { # cparent 86 print CHILD $test_string; 87 close CHILD; 88 exit 0; 89 } 90 # at this point, we're in a new (child) process 91 test(31,$q=new CGI,"CGI::new() from POST"); 92 test(32,$q->param('weather') eq 'nice',"CGI::param() from POST"); 93 test(33,$q->url_param('big_balls') eq 'basketball',"CGI::url_param()"); 94} else { 95 print "ok 31 # Skip\n"; 96 print "ok 32 # Skip\n"; 97 print "ok 33 # Skip\n"; 98} 99