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