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.10 by root, Sat May 24 23:15:14 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;
20 AnyEvent::Socket->new ( 22my $wo = tcp_connect 'www.google.com', 80, sub {
21 PeerAddr => "www.google.de:80", 23 my ($fh) = @_;
22 on_eof => sub { $cv->broadcast },
23 on_error => sub {
24 my ($ae_sock) = @_;
25 diag "error: $!";
26 $cv->broadcast
27 },
28 on_connect => sub {
29 my ($ae_sock, $error) = @_;
30 if ($error) { diag ("connect error: $!"); return }
31 24
32 $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 {
33 my ($ae_sock, $data) = @_; 33 my ($hdl) = @_;
34 $fbytes = $data;
35 34
36 $ae_sock->on_read (sub { 35 if ($rbytes !~ /<\/html>/i) {
37 my ($ae_sock) = @_; 36 print "not ";
38 $rbytes = $ae_sock->rbuf;
39 }); 37 }
40 });
41 38
42 $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 ";
43 } 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 });
44 ); 60 });
61
62 $hdl->push_write ("GET http://www.google.com/ HTTP/1.0\015\012\015\012");
63
64};
45 65
46$cv->wait; 66$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