This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
INADDR_ANY INADDR_BROADCAST INADDR_LOOPBACK INADDR_NONE
[perl5.git] / t / lib / cgi-function.t
CommitLineData
6b4ac661 1#!/usr/local/bin/perl -w
424ec8fa 2
36e6c0f5
JH
3BEGIN {
4 chdir('t') if -d 't';
20822f61 5 @INC = '../lib';
36e6c0f5
JH
6}
7
424ec8fa
GS
8# Test ability to retrieve HTTP request info
9######################### We start with some black magic to print on failure.
6b4ac661 10use lib '../blib/lib','../blib/arch';
424ec8fa 11
3d1a2ec4 12BEGIN {$| = 1; print "1..27\n"; }
424ec8fa 13END {print "not ok 1\n" unless $loaded;}
5152d7c7 14use Config;
424ec8fa
GS
15use CGI (':standard','keywords');
16$loaded = 1;
17print "ok 1\n";
18
19######################### End of black magic.
20
21# util
22sub test {
23 local($^W) = 0;
24 my($num, $true,$msg) = @_;
25 print($true ? "ok $num\n" : "not ok $num $msg\n");
26}
27
3d1a2ec4
GS
28my $CRLF = "\015\012";
29
a3b3a725
JH
30# A peculiarity of sending "\n" through MBX|Socket|web-server on VMS
31# is that a CR character gets inserted automatically in the web server
32# case but not internal to perl's double quoted strings "\n". This
33# test would need to be modified to use the "\015\012" on VMS if it
34# were actually run through a web server.
35# Thanks to Peter Prymmer for this
36
37if ($^O eq 'VMS') { $CRLF = "\n"; }
38
fc051c1e
PP
39# Web servers on EBCDIC hosts are typically set up to do an EBCDIC -> ASCII
40# translation hence CRLF is used as \r\n within CGI.pm on such machines.
41
42if (ord("\t") != 9) { $CRLF = "\r\n"; }
43
199d4a26
JH
44# Web servers on EBCDIC hosts are typically set up to do an EBCDIC -> ASCII
45# translation hence CRLF is used as \r\n within CGI.pm on such machines.
46
47if (ord("\t") != 9) { $CRLF = "\r\n"; }
48
424ec8fa
GS
49# Set up a CGI environment
50$ENV{REQUEST_METHOD}='GET';
51$ENV{QUERY_STRING} ='game=chess&game=checkers&weather=dull';
52$ENV{PATH_INFO} ='/somewhere/else';
53$ENV{PATH_TRANSLATED} ='/usr/local/somewhere/else';
54$ENV{SCRIPT_NAME} ='/cgi-bin/foo.cgi';
55$ENV{SERVER_PROTOCOL} = 'HTTP/1.0';
56$ENV{SERVER_PORT} = 8080;
57$ENV{SERVER_NAME} = 'the.good.ship.lollypop.com';
58$ENV{HTTP_LOVE} = 'true';
59
60test(2,request_method() eq 'GET',"CGI::request_method()");
3d1a2ec4 61test(3,query_string() eq 'game=chess;game=checkers;weather=dull',"CGI::query_string()");
424ec8fa
GS
62test(4,param() == 2,"CGI::param()");
63test(5,join(' ',sort {$a cmp $b} param()) eq 'game weather',"CGI::param()");
64test(6,param('game') eq 'chess',"CGI::param()");
65test(7,param('weather') eq 'dull',"CGI::param()");
66test(8,join(' ',param('game')) eq 'chess checkers',"CGI::param()");
67test(9,param(-name=>'foo',-value=>'bar'),'CGI::param() put');
68test(10,param(-name=>'foo') eq 'bar','CGI::param() get');
3d1a2ec4 69test(11,query_string() eq 'game=chess;game=checkers;weather=dull;foo=bar',"CGI::query_string() redux");
424ec8fa
GS
70test(12,http('love') eq 'true',"CGI::http()");
71test(13,script_name() eq '/cgi-bin/foo.cgi',"CGI::script_name()");
72test(14,url() eq 'http://the.good.ship.lollypop.com:8080/cgi-bin/foo.cgi',"CGI::url()");
73test(15,self_url() eq
3d1a2ec4 74 'http://the.good.ship.lollypop.com:8080/cgi-bin/foo.cgi/somewhere/else?game=chess;game=checkers;weather=dull;foo=bar',
424ec8fa
GS
75 "CGI::url()");
76test(16,url(-absolute=>1) eq '/cgi-bin/foo.cgi','CGI::url(-absolute=>1)');
77test(17,url(-relative=>1) eq 'foo.cgi','CGI::url(-relative=>1)');
78test(18,url(-relative=>1,-path=>1) eq 'foo.cgi/somewhere/else','CGI::url(-relative=>1,-path=>1)');
79test(19,url(-relative=>1,-path=>1,-query=>1) eq
3d1a2ec4 80 'foo.cgi/somewhere/else?game=chess;game=checkers;weather=dull;foo=bar',
424ec8fa
GS
81 'CGI::url(-relative=>1,-path=>1,-query=>1)');
82Delete('foo');
83test(20,!param('foo'),'CGI::delete()');
84
85CGI::_reset_globals();
86$ENV{QUERY_STRING}='mary+had+a+little+lamb';
87test(21,join(' ',keywords()) eq 'mary had a little lamb','CGI::keywords');
88test(22,join(' ',param('keywords')) eq 'mary had a little lamb','CGI::keywords');
89
3d1a2ec4
GS
90CGI::_reset_globals;
91if ($Config{d_fork}) {
92 $test_string = 'game=soccer&game=baseball&weather=nice';
93 $ENV{REQUEST_METHOD}='POST';
94 $ENV{CONTENT_LENGTH}=length($test_string);
95 $ENV{QUERY_STRING}='big_balls=basketball&small_balls=golf';
96 if (open(CHILD,"|-")) { # cparent
97 print CHILD $test_string;
98 close CHILD;
99 exit 0;
100 }
101 # at this point, we're in a new (child) process
102 test(23,param('weather') eq 'nice',"CGI::param() from POST");
6b4ac661 103 test(24,(url_param('big_balls') eq 'basketball'),"CGI::url_param()");
3d1a2ec4
GS
104} else {
105 print "ok 23 # Skip\n";
106 print "ok 24 # Skip\n";
424ec8fa 107}
6b4ac661
JH
108test(25,redirect('http://somewhere.else') eq "Status: 302 Moved${CRLF}location: http://somewhere.else${CRLF}${CRLF}","CGI::redirect() 1");
109my $h = redirect(-Location=>'http://somewhere.else',-Type=>'text/html');
110test(26,$h eq "Status: 302 Moved${CRLF}location: http://somewhere.else${CRLF}Content-Type: text/html; charset=ISO-8859-1${CRLF}${CRLF}","CGI::redirect() 2");
111test(27,redirect(-Location=>'http://somewhere.else/bin/foo&bar',-Type=>'text/html') eq "Status: 302 Moved${CRLF}location: http://somewhere.else/bin/foo&bar${CRLF}Content-Type: text/html; charset=ISO-8859-1${CRLF}${CRLF}","CGI::redirect() 2");