1 | #!/opt/perl/bin/perl |
1 | #!/opt/perl/bin/perl |
2 | |
2 | |
3 | use strict; |
3 | use strict; |
4 | use Test::More; |
4 | |
5 | use AnyEvent::Impl::Perl; |
5 | use AnyEvent::Impl::Perl; |
6 | use AnyEvent; |
6 | use AnyEvent; |
7 | use AnyEvent::Socket; |
7 | use AnyEvent::Handle; |
8 | |
8 | |
9 | unless ($ENV{PERL_ANYEVENT_NET_TESTS}) { |
9 | unless ($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 | |
14 | plan tests => 2; |
14 | print "1..2\n"; |
15 | |
15 | |
16 | my $cv = AnyEvent->condvar; |
16 | my $cv = AnyEvent->condvar; |
17 | |
17 | |
18 | my $fbytes; |
|
|
19 | my $rbytes; |
18 | my $rbytes; |
20 | |
19 | |
21 | my $ae_sock = |
20 | my $hdl; |
|
|
21 | my $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.de: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 | |
|
|
50 | is (substr ($fbytes, 0, 4), 'HTTP', 'first bytes began with HTTP'); |
|
|
51 | ok ($rbytes =~ /google.*<\/html>\s*$/i, 'content was retrieved successfully'); |
|
|