6 unless (-f "CAN_TALK_TO_OURSELF") {
7 print "1..0 # Skipped: Can't talk to ourself (misconfigured system)\n";
11 # Hm, this should really use Test.pm, but not worth changing over, really.
16 require IO::Socket; # make sure this work before we try to make a HTTP::Daemon
18 # First we make ourself a daemon in another process
24 my $d = HTTP::Daemon->new(Timeout => 10);
26 print "Please to meet you at: <URL:", $d->url, ">\n";
27 open(STDOUT, $^O eq 'VMS'? ">nl: " : ">/dev/null");
29 while ($c = $d->accept) {
32 my $p = ($r->url->path_segments)[1];
33 my $func = lc("httpd_" . $r->method . "_$p");
41 $c = undef; # close connection
43 print STDERR "HTTP Server terminated\n";
48 my $perl = $Config{'perlpath'};
49 $perl = $^X if $^O eq 'VMS' or -x $^X and $^X =~ m,^([a-z]:)?/,i;
50 open(DAEMON, "$perl local/http-get.t daemon |") or die "Can't exec daemon: $!";
56 my $greeting = <DAEMON>;
57 $greeting =~ /(<[^>]+>)/;
60 my $base = URI->new($1);
63 $u = $u->abs($_[1]) if @_ > 1;
67 print "# Will access HTTP server at $base\n";
69 require LWP::UserAgent;
70 require HTTP::Request;
71 $ua = new LWP::UserAgent;
72 $ua->agent("Mozilla/0.01 " . $ua->agent);
73 $ua->from('gisle@aas.no');
76 #----------------------------------------------------------------
77 print "#------------Testing: Bad request...\n";
79 url("/not_found", $base),
83 print "not " unless $res->is_error
85 and $res->message =~ /not\s+found/i;
87 # we also expect a few headers
88 print "not " if !$res->server and !$res->date;
91 #----------------------------------------------------------------
92 print "#------------Testing: Simple echo...\n";
96 $c->send_basic_header(200);
97 print $c "Content-Type: text/plain\015\012";
99 print $c $req->as_string;
103 url("/echo/path_info?query", $base),
104 Accept => 'text/html',
105 Accept => 'text/plain; q=0.9',
107 Long_text => 'This is a very long header line
108 which is broken between
109 more than one line.',
113 #print $res->as_string;
115 print "not " unless $res->is_success
116 and $res->code == 200 && $res->message eq "OK";
120 @accept = /^Accept:\s*(.*)/mg;
124 print "not " unless /^From:\s*gisle\@aas\.no$/m
127 and /^Accept:\s*text\/html/m
128 and /^Accept:\s*text\/plain/m
129 and /^Accept:\s*image\/\*/m
130 and /^Long-Text:\s*This.*broken between/m
131 and /^X-Foo:\s*Bar$/m
132 and /^User-Agent:\s*Mozilla\/0.01/m;
135 #----------------------------------------------------------------
136 print "#------------Testing: Send file...\n";
138 my $file = "test-$$.html";
140 open(FILE, ">$file") or die "Can't create $file: $!";
141 binmode FILE or die "Can't binmode $file: $!";
143 <html><title>En prøve</title>
144 <h1>Dette er en testfil</h1>
145 Jeg vet ikke hvor stor fila behøver å være heller, men dette
146 er sikkert nok i massevis.
149 print "# ", -s $file, " bytes written to $file\n";
156 my %form = $r->url->query_form;
157 my $file = $form{'name'};
158 $c->send_file_response($file);
163 $res = $ua->get( url("/file?name=$file", $base) );
165 #print $res->as_string;
167 print "not " unless $res->is_success
168 and $res->content_type eq 'text/html'
169 and $res->content_length == 147
170 and $res->title eq 'En prøve'
171 and $res->content =~ /å være/;
180 $res = $ua->get( url("/file?name=$file", $base),
181 ':content_cb' => sub { $content .= $_[0]; return; },
183 #print $res->as_string;
185 print "not " unless $res->is_success
186 and $res->content_type eq 'text/html'
187 and $res->content_length == 147
189 and $res->title eq 'En prøve'
190 and ! $res->content # No content, because callback
191 and $content =~ /å være/;
200 # Then try to list current directory
201 $res = $ua->get( url("/file?name=.", $base) );
202 #print $res->as_string;
203 print "not " unless $res->code == 501; # NYI
207 #----------------------------------------------------------------
208 print "#------------Testing: Check redirect...\n";
209 sub httpd_get_redirect
212 $c->send_redirect("/echo/redirect");
215 $res = $ua->get( url("/redirect/foo", $base) );
216 #print $res->as_string;
218 print "not " unless $res->is_success
219 and $res->content =~ m|/echo/redirect|;
221 print "not " unless $res->previous->is_redirect
222 and $res->previous->code == 301;
225 # Let's test a redirect loop too
226 sub httpd_get_redirect2 { shift->send_redirect("/redirect3/") }
227 sub httpd_get_redirect3 { shift->send_redirect("/redirect2/") }
229 $res = $ua->get(url("/redirect2", $base));
230 #print $res->as_string;
231 print "not " unless $res->is_redirect
232 and $res->header("Client-Warning") =~ /loop detected/i;
235 while ($res->previous) {
237 $res = $res->previous;
239 print "not " unless $i == 7;
242 sub httpd_get_redirect_file { shift->send_redirect("file:/etc/passwd") }
243 $res = $ua->get(url("/redirect_file/", $base));
244 #print $res->as_string;
245 print "not " unless $res->is_redirect
246 and $res->header("Client-Warning") =~ /can't redirect to a file:/i;
250 #----------------------------------------------------------------
251 print "#------------Testing: Check basic authorization...\n";
255 #print STDERR $r->as_string;
256 my($u,$p) = $r->authorization_basic;
257 if (defined($u) && $u eq 'ok 13' && $p eq 'xyzzy') {
258 $c->send_basic_header(200);
259 print $c "Content-Type: text/plain";
265 $c->send_basic_header(401);
266 $c->print("WWW-Authenticate: Basic realm=\"libwww-perl\"\015\012");
272 package MyUA; @ISA=qw(LWP::UserAgent);
273 sub get_basic_credentials {
274 my($self, $realm, $uri, $proxy) = @_;
275 if ($realm eq "libwww-perl" && $uri->rel($base) eq "basic") {
276 return ("ok 13", "xyzzy");
285 my $that_url = url("/basic", $base);
287 $res = MyUA->new->get( $that_url );
288 #print $res->as_string;
290 my $host_port = $res->request->uri->host_port;
292 print "not " unless $res->is_success;
295 # Let's try with a $ua that does not pass out credentials
296 $res = $ua->get( $that_url );
297 print "not " unless $res->code == 401;
301 print "# Host port: $host_port\n";
303 # Let's try to set credentials for this realm
304 $ua->credentials($host_port, "libwww-perl", "ok 13", "xyzzy");
306 $res = $ua->get( $that_url );
308 print "not " unless $res->is_success;
311 # Then illegal credentials
312 $ua->credentials($host_port, "libwww-perl", "user", "passwd");
313 $res = $ua->get( $that_url );
314 print "not " unless $res->code == 401;
318 #----------------------------------------------------------------
319 print "#------------Testing: Check proxy...\n";
323 if ($r->method eq "GET" and
324 $r->url->scheme eq "ftp") {
325 $c->send_basic_header(200);
333 $ua->proxy(ftp => $base);
335 $res = $ua->get( "ftp://ftp.perl.com/proxy" );
336 #print $res->as_string;
337 print "not " unless $res->is_success;
340 #----------------------------------------------------------------
341 print "#------------Testing: Check POSTing...\n";
345 $c->send_basic_header;
346 $c->print("Content-Type: text/plain");
349 $c->print($r->as_string);
353 url("/echo/foo", $base),
354 ['foo' => 'bar', 'bar' => 'test'],
356 #print $res->as_string;
359 print "not " unless $res->is_success
360 and /^Content-Length:\s*16$/mi
361 and /^Content-Type:\s*application\/x-www-form-urlencoded$/mi
362 and /^foo=bar&bar=test/m;
371 url("/echo/foo", $base),
372 ['foo' => 'bar', 'bar' => 'test'],
373 ':content_cb' => sub { $content .= $_[0]; return; },
377 print "not " unless $res->is_success
378 and /^Content-Length:\s*16$/mi
379 and /^Content-Type:\s*application\/x-www-form-urlencoded$/mi
380 and /^foo=bar&bar=test/m
392 url("/echo/foo", $base),
393 Content_Type => 'text/plain',
394 Content => "Plain Text",
395 ':content_cb' => sub { $content .= $_[0]; return; },
399 print "not " unless $res->is_success
400 and /^Content-Length:\s*10$/mi
401 and /^Content-Type:\s*text\/plain$/mi
409 #----------------------------------------------------------------
410 print "#------------Testing: Terminating server...\n";
414 $c->send_error(503, "Bye, bye");
415 exit; # terminate HTTP server
418 $res = $ua->get( url("/quit", $base) );
420 print "not " unless $res->code == 503 and $res->content =~ /Bye, bye/;