This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Enhance lfs tests: check every seek and sysseek
[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
11BEGIN {$| = 1; print "1..31\n"; }
12END {print "not ok 1\n" unless $loaded;}
5152d7c7 13use Config;
424ec8fa
GS
14use CGI ();
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
f6b3007c
JH
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';
f6b3007c
JH
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()");
42test(4,$q->query_string eq 'game=chess&game=checkers&weather=dull',"CGI::query_string()");
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');
50test(12,$q->query_string eq 'game=chess&game=checkers&weather=dull&foo=bar',"CGI::query_string() redux");
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
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()");
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
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');
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
5152d7c7
GS
76if (!$Config{d_fork} or $^O eq 'MSWin32' or $^O eq 'VMS') {
77 for (29..31) { print "ok $_ # Skipped: fork n/a\n" }
78}
79else {
80 $q->_reset_globals;
81 $test_string = 'game=soccer&game=baseball&weather=nice';
82 $ENV{REQUEST_METHOD}='POST';
83 $ENV{CONTENT_LENGTH}=length($test_string);
84 $ENV{QUERY_STRING}='big_balls=basketball&small_balls=golf';
85 if (open(CHILD,"|-")) { # cparent
86 print CHILD $test_string;
87 close CHILD;
88 exit 0;
89 }
90 # at this point, we're in a new (child) process
91 test(29,$q=new CGI,"CGI::new() from POST");
92 test(30,$q->param('weather') eq 'nice',"CGI::param() from POST");
93 test(31,$q->url_param('big_balls') eq 'basketball',"CGI::url_param()");
424ec8fa 94}