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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines