This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
3fbea1fb0dc99005f4d3f498d2975bda93d9e3e3
[perl5.git] / t / lib / cgi-function.t
1 #!./perl
2
3 # Test ability to retrieve HTTP request info
4 ######################### We start with some black magic to print on failure.
5
6 BEGIN {
7     chdir 't' if -d 't';
8     @INC = '../lib' if -d '../lib';
9 }
10
11 BEGIN {$| = 1; print "1..24\n"; }
12 END {print "not ok 1\n" unless $loaded;}
13 use CGI (':standard','keywords');
14 $loaded = 1;
15 print "ok 1\n";
16
17 ######################### End of black magic.
18
19 # util
20 sub test {
21     local($^W) = 0;
22     my($num, $true,$msg) = @_;
23     print($true ? "ok $num\n" : "not ok $num $msg\n");
24 }
25
26 # Set up a CGI environment
27 $ENV{REQUEST_METHOD}='GET';
28 $ENV{QUERY_STRING}  ='game=chess&game=checkers&weather=dull';
29 $ENV{PATH_INFO}     ='/somewhere/else';
30 $ENV{PATH_TRANSLATED} ='/usr/local/somewhere/else';
31 $ENV{SCRIPT_NAME}   ='/cgi-bin/foo.cgi';
32 $ENV{SERVER_PROTOCOL} = 'HTTP/1.0';
33 $ENV{SERVER_PORT} = 8080;
34 $ENV{SERVER_NAME} = 'the.good.ship.lollypop.com';
35 $ENV{HTTP_LOVE} = 'true';
36
37 test(2,request_method() eq 'GET',"CGI::request_method()");
38 test(3,query_string() eq 'game=chess&game=checkers&weather=dull',"CGI::query_string()");
39 test(4,param() == 2,"CGI::param()");
40 test(5,join(' ',sort {$a cmp $b} param()) eq 'game weather',"CGI::param()");
41 test(6,param('game') eq 'chess',"CGI::param()");
42 test(7,param('weather') eq 'dull',"CGI::param()");
43 test(8,join(' ',param('game')) eq 'chess checkers',"CGI::param()");
44 test(9,param(-name=>'foo',-value=>'bar'),'CGI::param() put');
45 test(10,param(-name=>'foo') eq 'bar','CGI::param() get');
46 test(11,query_string() eq 'game=chess&game=checkers&weather=dull&foo=bar',"CGI::query_string() redux");
47 test(12,http('love') eq 'true',"CGI::http()");
48 test(13,script_name() eq '/cgi-bin/foo.cgi',"CGI::script_name()");
49 test(14,url() eq 'http://the.good.ship.lollypop.com:8080/cgi-bin/foo.cgi',"CGI::url()");
50 test(15,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()");
53 test(16,url(-absolute=>1) eq '/cgi-bin/foo.cgi','CGI::url(-absolute=>1)');
54 test(17,url(-relative=>1) eq 'foo.cgi','CGI::url(-relative=>1)');
55 test(18,url(-relative=>1,-path=>1) eq 'foo.cgi/somewhere/else','CGI::url(-relative=>1,-path=>1)');
56 test(19,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 Delete('foo');
60 test(20,!param('foo'),'CGI::delete()');
61
62 CGI::_reset_globals();
63 $ENV{QUERY_STRING}='mary+had+a+little+lamb';
64 test(21,join(' ',keywords()) eq 'mary had a little lamb','CGI::keywords');
65 test(22,join(' ',param('keywords')) eq 'mary had a little lamb','CGI::keywords');
66
67 CGI::_reset_globals;
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()");