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.7 by root, Sat May 17 21:08:05 2008 UTC

1#!/opt/perl/bin/perl 1#!/opt/perl/bin/perl
2 2
3use strict; 3use strict;
4use Test::More; 4
5use AnyEvent::Impl::Perl; 5use AnyEvent::Impl::Perl;
6use AnyEvent; 6use AnyEvent;
7use AnyEvent::Socket; 7use AnyEvent::Handle;
8 8
9unless ($ENV{PERL_ANYEVENT_NET_TESTS}) { 9unless ($ENV{PERL_ANYEVENT_NET_TESTS}) {
10 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";
11 exit 0; 11 exit 0;
12} 12}
13 13
14plan tests => 2; 14print "1..2\n";
15 15
16my $cv = AnyEvent->condvar; 16my $cv = AnyEvent->condvar;
17 17
18my $fbytes;
19my $rbytes; 18my $rbytes;
20 19
21my $ae_sock = 20my $hdl;
21my $wo = AnyEvent::Util::tcp_connect 'www.google.com', 80, sub {
22 my ($sock) = @_;
23 $hdl =
22 AnyEvent::Socket->new ( 24 AnyEvent::Handle->new (
23 PeerAddr => "www.google.com:80", 25 fh => $sock,
24 on_eof => sub { $cv->broadcast },
25 on_error => sub { 26 on_error => sub {
26 my ($ae_sock) = @_; 27 warn "socket error: $!";
27 diag "error: $!";
28 $cv->broadcast 28 $cv->broadcast;
29 }, 29 },
30 on_connect => sub { 30 on_eof => sub {
31 my ($ae_sock, $error) = @_; 31 my ($hdl) = @_;
32 if ($error) { diag ("connect error: $!"); $cv->broadcast; return }
33 32
34 $ae_sock->read (10, sub { 33 if ($rbytes !~ /<\/html>/i) {
35 my ($ae_sock, $data) = @_; 34 print "not ";
36 $fbytes = $data; 35 }
37 36
38 $ae_sock->on_read (sub { 37 print "ok 2 - received HTML page\n";
39 my ($ae_sock) = @_;
40 $rbytes = $ae_sock->rbuf;
41 });
42 });
43 38
44 $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 ";
45 } 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 });
46 ); 58 });
59
60 $hdl->push_write ("GET http://www.google.com/ HTTP/1.0\015\012\015\012");
61
62};
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