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