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
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';
93430cb4 8 unshift @INC, '../lib' if -d '../lib';
424ec8fa
GS
9}
10
3d1a2ec4 11BEGIN {$| = 1; print "1..33\n"; }
424ec8fa
GS
12END {print "not ok 1\n" unless $loaded;}
13use CGI ();
3d1a2ec4 14use Config;
424ec8fa
GS
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
3538e1d5
GS
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';
424ec8fa 33$ENV{SERVER_PROTOCOL} = 'HTTP/1.0';
3538e1d5
GS
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';
424ec8fa
GS
38
39$q = new CGI;
40test(2,$q,"CGI::new()");
41test(3,$q->request_method eq 'GET',"CGI::request_method()");
3d1a2ec4 42test(4,$q->query_string eq 'game=chess;game=checkers;weather=dull',"CGI::query_string()");
424ec8fa
GS
43test(5,$q->param() == 2,"CGI::param()");
44test(6,join(' ',sort $q->param()) eq 'game weather',"CGI::param()");
45test(7,$q->param('game') eq 'chess',"CGI::param()");
46test(8,$q->param('weather') eq 'dull',"CGI::param()");
47test(9,join(' ',$q->param('game')) eq 'chess checkers',"CGI::param()");
48test(10,$q->param(-name=>'foo',-value=>'bar'),'CGI::param() put');
49test(11,$q->param(-name=>'foo') eq 'bar','CGI::param() get');
3d1a2ec4 50test(12,$q->query_string eq 'game=chess;game=checkers;weather=dull;foo=bar',"CGI::query_string() redux");
424ec8fa
GS
51test(13,$q->http('love') eq 'true',"CGI::http()");
52test(14,$q->script_name eq '/cgi-bin/foo.cgi',"CGI::script_name()");
53test(15,$q->url eq 'http://the.good.ship.lollypop.com:8080/cgi-bin/foo.cgi',"CGI::url()");
54test(16,$q->self_url eq
3d1a2ec4 55 'http://the.good.ship.lollypop.com:8080/cgi-bin/foo.cgi/somewhere/else?game=chess;game=checkers;weather=dull;foo=bar',
424ec8fa
GS
56 "CGI::url()");
57test(17,$q->url(-absolute=>1) eq '/cgi-bin/foo.cgi','CGI::url(-absolute=>1)');
58test(18,$q->url(-relative=>1) eq 'foo.cgi','CGI::url(-relative=>1)');
59test(19,$q->url(-relative=>1,-path=>1) eq 'foo.cgi/somewhere/else','CGI::url(-relative=>1,-path=>1)');
60test(20,$q->url(-relative=>1,-path=>1,-query=>1) eq
3d1a2ec4 61 'foo.cgi/somewhere/else?game=chess;game=checkers;weather=dull;foo=bar',
424ec8fa
GS
62 'CGI::url(-relative=>1,-path=>1,-query=>1)');
63$q->delete('foo');
64test(21,!$q->param('foo'),'CGI::delete()');
65
66$q->_reset_globals;
67$ENV{QUERY_STRING}='mary+had+a+little+lamb';
68test(22,$q=new CGI,"CGI::new() redux");
69test(23,join(' ',$q->keywords) eq 'mary had a little lamb','CGI::keywords');
70test(24,join(' ',$q->param('keywords')) eq 'mary had a little lamb','CGI::keywords');
71test(25,$q=new CGI('foo=bar&foo=baz'),"CGI::new() redux");
72test(26,$q->param('foo') eq 'bar','CGI::param() redux');
73test(27,$q=new CGI({'foo'=>'bar','bar'=>'froz'}),"CGI::new() redux 2");
74test(28,$q->param('bar') eq 'froz',"CGI::param() redux 2");
75
3d1a2ec4
GS
76# test tied interface
77my $p = $q->Vars;
78test(29,$p->{bar} eq 'froz',"tied interface fetch");
79$p->{bar} = join("\0",qw(foo bar baz));
80test(30,join(' ',$q->param('bar')) eq 'foo bar baz','tied interface store');
81
82# test posting
83$q->_reset_globals;
84if ($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";
424ec8fa 102}