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
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     @INC = '../lib' if -d '../lib';
9 }
10
11 BEGIN {$| = 1; print "1..31\n"; }
12 END {print "not ok 1\n" unless $loaded;}
13 use Config;
14 use CGI ();
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{HTTP_LOVE} = 'true';
37
38 $q = new CGI;
39 test(2,$q,"CGI::new()");
40 test(3,$q->request_method eq 'GET',"CGI::request_method()");
41 test(4,$q->query_string eq 'game=chess&game=checkers&weather=dull',"CGI::query_string()");
42 test(5,$q->param() == 2,"CGI::param()");
43 test(6,join(' ',sort $q->param()) eq 'game weather',"CGI::param()");
44 test(7,$q->param('game') eq 'chess',"CGI::param()");
45 test(8,$q->param('weather') eq 'dull',"CGI::param()");
46 test(9,join(' ',$q->param('game')) eq 'chess checkers',"CGI::param()");
47 test(10,$q->param(-name=>'foo',-value=>'bar'),'CGI::param() put');
48 test(11,$q->param(-name=>'foo') eq 'bar','CGI::param() get');
49 test(12,$q->query_string eq 'game=chess&game=checkers&weather=dull&foo=bar',"CGI::query_string() redux");
50 test(13,$q->http('love') eq 'true',"CGI::http()");
51 test(14,$q->script_name eq '/cgi-bin/foo.cgi',"CGI::script_name()");
52 test(15,$q->url eq 'http://the.good.ship.lollypop.com:8080/cgi-bin/foo.cgi',"CGI::url()");
53 test(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()");
56 test(17,$q->url(-absolute=>1) eq '/cgi-bin/foo.cgi','CGI::url(-absolute=>1)');
57 test(18,$q->url(-relative=>1) eq 'foo.cgi','CGI::url(-relative=>1)');
58 test(19,$q->url(-relative=>1,-path=>1) eq 'foo.cgi/somewhere/else','CGI::url(-relative=>1,-path=>1)');
59 test(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');
63 test(21,!$q->param('foo'),'CGI::delete()');
64
65 $q->_reset_globals;
66 $ENV{QUERY_STRING}='mary+had+a+little+lamb';
67 test(22,$q=new CGI,"CGI::new() redux");
68 test(23,join(' ',$q->keywords) eq 'mary had a little lamb','CGI::keywords');
69 test(24,join(' ',$q->param('keywords')) eq 'mary had a little lamb','CGI::keywords');
70 test(25,$q=new CGI('foo=bar&foo=baz'),"CGI::new() redux");
71 test(26,$q->param('foo') eq 'bar','CGI::param() redux');
72 test(27,$q=new CGI({'foo'=>'bar','bar'=>'froz'}),"CGI::new() redux 2");
73 test(28,$q->param('bar') eq 'froz',"CGI::param() redux 2");
74
75 if (!$Config{d_fork} or $^O eq 'MSWin32' or $^O eq 'VMS') {
76     for (29..31) { print "ok $_ # Skipped: fork n/a\n" }
77 }
78 else {
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()");
93 }