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-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';
8 @INC = '../lib' if -d '../lib';
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
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
38$q = new CGI;
39test(2,$q,"CGI::new()");
40test(3,$q->request_method eq 'GET',"CGI::request_method()");
41test(4,$q->query_string eq 'game=chess&game=checkers&weather=dull',"CGI::query_string()");
42test(5,$q->param() == 2,"CGI::param()");
43test(6,join(' ',sort $q->param()) eq 'game weather',"CGI::param()");
44test(7,$q->param('game') eq 'chess',"CGI::param()");
45test(8,$q->param('weather') eq 'dull',"CGI::param()");
46test(9,join(' ',$q->param('game')) eq 'chess checkers',"CGI::param()");
47test(10,$q->param(-name=>'foo',-value=>'bar'),'CGI::param() put');
48test(11,$q->param(-name=>'foo') eq 'bar','CGI::param() get');
49test(12,$q->query_string eq 'game=chess&game=checkers&weather=dull&foo=bar',"CGI::query_string() redux");
50test(13,$q->http('love') eq 'true',"CGI::http()");
51test(14,$q->script_name eq '/cgi-bin/foo.cgi',"CGI::script_name()");
52test(15,$q->url eq 'http://the.good.ship.lollypop.com:8080/cgi-bin/foo.cgi',"CGI::url()");
53test(16,$q->self_url eq
54 'http://the.good.ship.lollypop.com:8080/cgi-bin/foo.cgi/somewhere/else?game=chess&game=checkers&weather=dull&foo=bar',
55 "CGI::url()");
56test(17,$q->url(-absolute=>1) eq '/cgi-bin/foo.cgi','CGI::url(-absolute=>1)');
57test(18,$q->url(-relative=>1) eq 'foo.cgi','CGI::url(-relative=>1)');
58test(19,$q->url(-relative=>1,-path=>1) eq 'foo.cgi/somewhere/else','CGI::url(-relative=>1,-path=>1)');
59test(20,$q->url(-relative=>1,-path=>1,-query=>1) eq
60 'foo.cgi/somewhere/else?game=chess&game=checkers&weather=dull&foo=bar',
61 'CGI::url(-relative=>1,-path=>1,-query=>1)');
62$q->delete('foo');
63test(21,!$q->param('foo'),'CGI::delete()');
64
65$q->_reset_globals;
66$ENV{QUERY_STRING}='mary+had+a+little+lamb';
67test(22,$q=new CGI,"CGI::new() redux");
68test(23,join(' ',$q->keywords) eq 'mary had a little lamb','CGI::keywords');
69test(24,join(' ',$q->param('keywords')) eq 'mary had a little lamb','CGI::keywords');
70test(25,$q=new CGI('foo=bar&foo=baz'),"CGI::new() redux");
71test(26,$q->param('foo') eq 'bar','CGI::param() redux');
72test(27,$q=new CGI({'foo'=>'bar','bar'=>'froz'}),"CGI::new() redux 2");
73test(28,$q->param('bar') eq 'froz',"CGI::param() redux 2");
74
5152d7c7
GS
75if (!$Config{d_fork} or $^O eq 'MSWin32' or $^O eq 'VMS') {
76 for (29..31) { print "ok $_ # Skipped: fork n/a\n" }
77}
78else {
79 $q->_reset_globals;
80 $test_string = 'game=soccer&game=baseball&weather=nice';
81 $ENV{REQUEST_METHOD}='POST';
82 $ENV{CONTENT_LENGTH}=length($test_string);
83 $ENV{QUERY_STRING}='big_balls=basketball&small_balls=golf';
84 if (open(CHILD,"|-")) { # cparent
85 print CHILD $test_string;
86 close CHILD;
87 exit 0;
88 }
89 # at this point, we're in a new (child) process
90 test(29,$q=new CGI,"CGI::new() from POST");
91 test(30,$q->param('weather') eq 'nice',"CGI::param() from POST");
92 test(31,$q->url_param('big_balls') eq 'basketball',"CGI::url_param()");
424ec8fa 93}