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