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