This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make the test more portable.
[perl5.git] / t / lib / cgi-request.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     unshift @INC, '../lib' if -d '../lib';
9 }
10
11 BEGIN {$| = 1; print "1..33\n"; }
12 END {print "not ok 1\n" unless $loaded;}
13 use CGI ();
14 use Config;
15 $loaded = 1;
16 print "ok 1\n";
17
18 ######################### End of black magic.
19
20 # util
21 sub 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{REQUEST_URI}     = "$ENV{SCRIPT_NAME}$ENV{PATH_INFO}?$ENV{QUERY_STRING}";
37 $ENV{HTTP_LOVE}       = 'true';
38
39 $q = new CGI;
40 test(2,$q,"CGI::new()");
41 test(3,$q->request_method eq 'GET',"CGI::request_method()");
42 test(4,$q->query_string eq 'game=chess;game=checkers;weather=dull',"CGI::query_string()");
43 test(5,$q->param() == 2,"CGI::param()");
44 test(6,join(' ',sort $q->param()) eq 'game weather',"CGI::param()");
45 test(7,$q->param('game') eq 'chess',"CGI::param()");
46 test(8,$q->param('weather') eq 'dull',"CGI::param()");
47 test(9,join(' ',$q->param('game')) eq 'chess checkers',"CGI::param()");
48 test(10,$q->param(-name=>'foo',-value=>'bar'),'CGI::param() put');
49 test(11,$q->param(-name=>'foo') eq 'bar','CGI::param() get');
50 test(12,$q->query_string eq 'game=chess;game=checkers;weather=dull;foo=bar',"CGI::query_string() redux");
51 test(13,$q->http('love') eq 'true',"CGI::http()");
52 test(14,$q->script_name eq '/cgi-bin/foo.cgi',"CGI::script_name()");
53 test(15,$q->url eq 'http://the.good.ship.lollypop.com:8080/cgi-bin/foo.cgi',"CGI::url()");
54 test(16,$q->self_url eq 
55      'http://the.good.ship.lollypop.com:8080/cgi-bin/foo.cgi/somewhere/else?game=chess;game=checkers;weather=dull;foo=bar',
56      "CGI::url()");
57 test(17,$q->url(-absolute=>1) eq '/cgi-bin/foo.cgi','CGI::url(-absolute=>1)');
58 test(18,$q->url(-relative=>1) eq 'foo.cgi','CGI::url(-relative=>1)');
59 test(19,$q->url(-relative=>1,-path=>1) eq 'foo.cgi/somewhere/else','CGI::url(-relative=>1,-path=>1)');
60 test(20,$q->url(-relative=>1,-path=>1,-query=>1) eq 
61      'foo.cgi/somewhere/else?game=chess;game=checkers;weather=dull;foo=bar',
62      'CGI::url(-relative=>1,-path=>1,-query=>1)');
63 $q->delete('foo');
64 test(21,!$q->param('foo'),'CGI::delete()');
65
66 $q->_reset_globals;
67 $ENV{QUERY_STRING}='mary+had+a+little+lamb';
68 test(22,$q=new CGI,"CGI::new() redux");
69 test(23,join(' ',$q->keywords) eq 'mary had a little lamb','CGI::keywords');
70 test(24,join(' ',$q->param('keywords')) eq 'mary had a little lamb','CGI::keywords');
71 test(25,$q=new CGI('foo=bar&foo=baz'),"CGI::new() redux");
72 test(26,$q->param('foo') eq 'bar','CGI::param() redux');
73 test(27,$q=new CGI({'foo'=>'bar','bar'=>'froz'}),"CGI::new() redux 2");
74 test(28,$q->param('bar') eq 'froz',"CGI::param() redux 2");
75
76 # test tied interface
77 my $p = $q->Vars;
78 test(29,$p->{bar} eq 'froz',"tied interface fetch");
79 $p->{bar} = join("\0",qw(foo bar baz));
80 test(30,join(' ',$q->param('bar')) eq 'foo bar baz','tied interface store');
81
82 # test posting
83 $q->_reset_globals;
84 if ($Config{d_fork}) {
85   $test_string = 'game=soccer&game=baseball&weather=nice';
86   $ENV{REQUEST_METHOD}='POST';
87   $ENV{CONTENT_LENGTH}=length($test_string);
88   $ENV{QUERY_STRING}='big_balls=basketball&small_balls=golf';
89   if (open(CHILD,"|-")) {  # cparent
90     print CHILD $test_string;
91     close CHILD;
92     exit 0;
93   }
94   # at this point, we're in a new (child) process
95   test(31,$q=new CGI,"CGI::new() from POST");
96   test(32,$q->param('weather') eq 'nice',"CGI::param() from POST");
97   test(33,$q->url_param('big_balls') eq 'basketball',"CGI::url_param()");
98 } else {
99   print "ok 31 # Skip\n";
100   print "ok 32 # Skip\n";
101   print "ok 33 # Skip\n";
102 }