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