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.2 by elmex, Sun Apr 27 19:15:43 2008 UTC vs.
Revision 1.7 by root, Sat May 17 21:08:05 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines