ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/t/handle/03_http_req.t
(Generate patch)

Comparing AnyEvent/t/handle/03_http_req.t (file contents):
Revision 1.5 by root, Sun Apr 27 20:05:21 2008 UTC vs.
Revision 1.6 by elmex, Thu May 15 14:12:24 2008 UTC

1#!/opt/perl/bin/perl 1#!/opt/perl/bin/perl
2
3use strict; 2use strict;
4use Test::More;
5use AnyEvent::Impl::Perl; 3use AnyEvent::Impl::Perl;
6use AnyEvent; 4use AnyEvent;
7use AnyEvent::Socket; 5use AnyEvent::Socket;
8 6
9unless ($ENV{PERL_ANYEVENT_NET_TESTS}) { 7unless ($ENV{PERL_ANYEVENT_NET_TESTS}) {
10 plan skip_all => "PERL_ANYEVENT_NET_TESTS environment variable not set"; 8 print "1..0 # Skip PERL_ANYEVENT_NET_TESTS environment variable not set\n";
11 exit 0; 9 exit 0;
12} 10}
13 11
14plan tests => 2; 12print "1..2\n";
15 13
16my $cv = AnyEvent->condvar; 14my $cv = AnyEvent->condvar;
17 15
18my $fbytes;
19my $rbytes; 16my $rbytes;
20 17
21my $ae_sock = 18my $hdl;
19my $wo = AnyEvent::Util::tcp_connect ('www.google.com', 80, sub {
20 my ($sock) = @_;
21 $hdl =
22 AnyEvent::Socket->new ( 22 AnyEvent::Handle->new (
23 PeerAddr => "www.google.com:80", 23 fh => $sock,
24 on_eof => sub { $cv->broadcast },
25 on_error => sub { 24 on_error => sub {
26 my ($ae_sock) = @_; 25 warn "socket error: $!";
27 diag "error: $!";
28 $cv->broadcast 26 $cv->broadcast;
29 }, 27 },
30 on_connect => sub { 28 on_eof => sub {
31 my ($ae_sock, $error) = @_; 29 my ($hdl) = @_;
32 if ($error) { diag ("connect error: $!"); $cv->broadcast; return }
33 30
34 $ae_sock->read (10, sub { 31 if ($rbytes !~ /<\/html>/i) {
35 my ($ae_sock, $data) = @_; 32 print "not ";
36 $fbytes = $data; 33 }
37 34
38 $ae_sock->on_read (sub { 35 print "ok 2 - received HTML page\n";
39 my ($ae_sock) = @_;
40 $rbytes = $ae_sock->rbuf;
41 });
42 });
43 36
44 $ae_sock->write ("GET http://www.google.de/ HTTP/1.0\015\012\015\012"); 37 $cv->broadcast
38 }
39 );
40
41 $hdl->push_read_chunk (10, sub {
42 my ($hdl, $data) = @_;
43
44 unless (substr ($data, 0, 4) eq 'HTTP') {
45 print "not ";
45 } 46 }
47
48 print "ok 1 - received 'HTTP'\n";
49
50 $hdl->on_read (sub {
51 my ($hdl) = @_;
52 $rbytes .= $hdl->rbuf;
53 $hdl->rbuf = '';
54 return 1;
55 });
46 ); 56 });
57
58 $hdl->push_write ("GET http://www.google.com/ HTTP/1.0\015\012\015\012");
59
60}, sub {
61 warn "error on connect: $!";
62}, 10);
47 63
48$cv->wait; 64$cv->wait;
49
50is (substr ($fbytes, 0, 4), 'HTTP', 'first bytes began with HTTP');
51ok ($rbytes =~ /google.*<\/html>\s*$/i, 'content was retrieved successfully');

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines