This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
bump patchlevel to 69, various little tweaks (tested on win32, Solaris
[perl5.git] / t / lib / cgi-function.t
CommitLineData
424ec8fa
GS
1#!./perl
2
3# Test ability to retrieve HTTP request info
4######################### We start with some black magic to print on failure.
5
6BEGIN {
7 chdir 't' if -d 't';
8 @INC = '../lib' if -d '../lib';
9}
10
11BEGIN {$| = 1; print "1..24\n"; }
12END {print "not ok 1\n" unless $loaded;}
5152d7c7 13use Config;
424ec8fa
GS
14use CGI (':standard','keywords');
15$loaded = 1;
16print "ok 1\n";
17
18######################### End of black magic.
19
20# util
21sub test {
22 local($^W) = 0;
23 my($num, $true,$msg) = @_;
24 print($true ? "ok $num\n" : "not ok $num $msg\n");
25}
26
27# Set up a CGI environment
28$ENV{REQUEST_METHOD}='GET';
29$ENV{QUERY_STRING} ='game=chess&game=checkers&weather=dull';
30$ENV{PATH_INFO} ='/somewhere/else';
31$ENV{PATH_TRANSLATED} ='/usr/local/somewhere/else';
32$ENV{SCRIPT_NAME} ='/cgi-bin/foo.cgi';
33$ENV{SERVER_PROTOCOL} = 'HTTP/1.0';
34$ENV{SERVER_PORT} = 8080;
35$ENV{SERVER_NAME} = 'the.good.ship.lollypop.com';
36$ENV{HTTP_LOVE} = 'true';
37
38test(2,request_method() eq 'GET',"CGI::request_method()");
39test(3,query_string() eq 'game=chess&game=checkers&weather=dull',"CGI::query_string()");
40test(4,param() == 2,"CGI::param()");
41test(5,join(' ',sort {$a cmp $b} param()) eq 'game weather',"CGI::param()");
42test(6,param('game') eq 'chess',"CGI::param()");
43test(7,param('weather') eq 'dull',"CGI::param()");
44test(8,join(' ',param('game')) eq 'chess checkers',"CGI::param()");
45test(9,param(-name=>'foo',-value=>'bar'),'CGI::param() put');
46test(10,param(-name=>'foo') eq 'bar','CGI::param() get');
47test(11,query_string() eq 'game=chess&game=checkers&weather=dull&foo=bar',"CGI::query_string() redux");
48test(12,http('love') eq 'true',"CGI::http()");
49test(13,script_name() eq '/cgi-bin/foo.cgi',"CGI::script_name()");
50test(14,url() eq 'http://the.good.ship.lollypop.com:8080/cgi-bin/foo.cgi',"CGI::url()");
51test(15,self_url() eq
52 'http://the.good.ship.lollypop.com:8080/cgi-bin/foo.cgi/somewhere/else?game=chess&game=checkers&weather=dull&foo=bar',
53 "CGI::url()");
54test(16,url(-absolute=>1) eq '/cgi-bin/foo.cgi','CGI::url(-absolute=>1)');
55test(17,url(-relative=>1) eq 'foo.cgi','CGI::url(-relative=>1)');
56test(18,url(-relative=>1,-path=>1) eq 'foo.cgi/somewhere/else','CGI::url(-relative=>1,-path=>1)');
57test(19,url(-relative=>1,-path=>1,-query=>1) eq
58 'foo.cgi/somewhere/else?game=chess&game=checkers&weather=dull&foo=bar',
59 'CGI::url(-relative=>1,-path=>1,-query=>1)');
60Delete('foo');
61test(20,!param('foo'),'CGI::delete()');
62
63CGI::_reset_globals();
64$ENV{QUERY_STRING}='mary+had+a+little+lamb';
65test(21,join(' ',keywords()) eq 'mary had a little lamb','CGI::keywords');
66test(22,join(' ',param('keywords')) eq 'mary had a little lamb','CGI::keywords');
67
5152d7c7
GS
68if (!$Config{d_fork} or $^O eq 'MSWin32' or $^O eq 'VMS') {
69 for (23,24) { print "ok $_ # Skipped: fork n/a\n" }
70}
71else {
72 CGI::_reset_globals;
73 $test_string = 'game=soccer&game=baseball&weather=nice';
74 $ENV{REQUEST_METHOD}='POST';
75 $ENV{CONTENT_LENGTH}=length($test_string);
76 $ENV{QUERY_STRING}='big_balls=basketball&small_balls=golf';
77 if (open(CHILD,"|-")) { # cparent
78 print CHILD $test_string;
79 close CHILD;
80 exit 0;
81 }
82 # at this point, we're in a new (child) process
83 test(23,param('weather') eq 'nice',"CGI::param() from POST");
84 test(24,url_param('big_balls') eq 'basketball',"CGI::url_param()");
424ec8fa 85}