Update to CGI 2.70, from Lincoln Stein.
[p5sagit/p5-mst-13.2.git] / t / lib / cgi-function.t
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.
5 use lib '../blib/lib','../blib/arch';
6
7 BEGIN {$| = 1; print "1..27\n"; }
8 END {print "not ok 1\n" unless $loaded;}
9 use Config;
10 use CGI (':standard','keywords');
11 $loaded = 1;
12 print "ok 1\n";
13
14 ######################### End of black magic.
15
16 # util
17 sub test {
18     local($^W) = 0;
19     my($num, $true,$msg) = @_;
20     print($true ? "ok $num\n" : "not ok $num $msg\n");
21 }
22
23 my $CRLF = "\015\012";
24
25 # Set up a CGI environment
26 $ENV{REQUEST_METHOD}='GET';
27 $ENV{QUERY_STRING}  ='game=chess&game=checkers&weather=dull';
28 $ENV{PATH_INFO}     ='/somewhere/else';
29 $ENV{PATH_TRANSLATED} ='/usr/local/somewhere/else';
30 $ENV{SCRIPT_NAME}   ='/cgi-bin/foo.cgi';
31 $ENV{SERVER_PROTOCOL} = 'HTTP/1.0';
32 $ENV{SERVER_PORT} = 8080;
33 $ENV{SERVER_NAME} = 'the.good.ship.lollypop.com';
34 $ENV{HTTP_LOVE} = 'true';
35
36 test(2,request_method() eq 'GET',"CGI::request_method()");
37 test(3,query_string() eq 'game=chess;game=checkers;weather=dull',"CGI::query_string()");
38 test(4,param() == 2,"CGI::param()");
39 test(5,join(' ',sort {$a cmp $b} param()) eq 'game weather',"CGI::param()");
40 test(6,param('game') eq 'chess',"CGI::param()");
41 test(7,param('weather') eq 'dull',"CGI::param()");
42 test(8,join(' ',param('game')) eq 'chess checkers',"CGI::param()");
43 test(9,param(-name=>'foo',-value=>'bar'),'CGI::param() put');
44 test(10,param(-name=>'foo') eq 'bar','CGI::param() get');
45 test(11,query_string() eq 'game=chess;game=checkers;weather=dull;foo=bar',"CGI::query_string() redux");
46 test(12,http('love') eq 'true',"CGI::http()");
47 test(13,script_name() eq '/cgi-bin/foo.cgi',"CGI::script_name()");
48 test(14,url() eq 'http://the.good.ship.lollypop.com:8080/cgi-bin/foo.cgi',"CGI::url()");
49 test(15,self_url() eq 
50      'http://the.good.ship.lollypop.com:8080/cgi-bin/foo.cgi/somewhere/else?game=chess;game=checkers;weather=dull;foo=bar',
51      "CGI::url()");
52 test(16,url(-absolute=>1) eq '/cgi-bin/foo.cgi','CGI::url(-absolute=>1)');
53 test(17,url(-relative=>1) eq 'foo.cgi','CGI::url(-relative=>1)');
54 test(18,url(-relative=>1,-path=>1) eq 'foo.cgi/somewhere/else','CGI::url(-relative=>1,-path=>1)');
55 test(19,url(-relative=>1,-path=>1,-query=>1) eq 
56      'foo.cgi/somewhere/else?game=chess;game=checkers;weather=dull;foo=bar',
57      'CGI::url(-relative=>1,-path=>1,-query=>1)');
58 Delete('foo');
59 test(20,!param('foo'),'CGI::delete()');
60
61 CGI::_reset_globals();
62 $ENV{QUERY_STRING}='mary+had+a+little+lamb';
63 test(21,join(' ',keywords()) eq 'mary had a little lamb','CGI::keywords');
64 test(22,join(' ',param('keywords')) eq 'mary had a little lamb','CGI::keywords');
65
66 CGI::_reset_globals;
67 if ($Config{d_fork}) {
68   $test_string = 'game=soccer&game=baseball&weather=nice';
69   $ENV{REQUEST_METHOD}='POST';
70   $ENV{CONTENT_LENGTH}=length($test_string);
71   $ENV{QUERY_STRING}='big_balls=basketball&small_balls=golf';
72   if (open(CHILD,"|-")) {  # cparent
73     print CHILD $test_string;
74     close CHILD;
75     exit 0;
76   }
77   # at this point, we're in a new (child) process
78   test(23,param('weather') eq 'nice',"CGI::param() from POST");
79   test(24,(url_param('big_balls') eq 'basketball'),"CGI::url_param()");
80 } else {
81   print "ok 23 # Skip\n";
82   print "ok 24 # Skip\n";
83 }
84 test(25,redirect('http://somewhere.else') eq "Status: 302 Moved${CRLF}location: http://somewhere.else${CRLF}${CRLF}","CGI::redirect() 1");
85 my $h = redirect(-Location=>'http://somewhere.else',-Type=>'text/html');
86 test(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");
87 test(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");