File: 03_keep_alive.t

package info (click to toggle)
libanyevent-httpd-perl 0.93-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 244 kB
  • sloc: perl: 1,247; makefile: 4
file content (50 lines) | stat: -rw-r--r-- 1,003 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!perl
use common::sense;
use Test::More tests => 1;
use AnyEvent::Impl::Perl;
use AnyEvent;
use AnyEvent::HTTPD;
use AnyEvent::Socket;

my $c = AnyEvent->condvar;

my $h = AnyEvent::HTTPD->new;

my $cnt = 0;

$h->reg_cb (
   '/test' => sub {
      my ($httpd, $req) = @_;
      $cnt++;
      $req->respond ({ content => ['text/plain', "Test response ($cnt)"] });
   },
);

my $hdl;
my $buf;
tcp_connect '127.0.0.1', $h->port, sub {
   my ($fh) = @_
      or die "couldn't connect: $!";

   $hdl =
      AnyEvent::Handle->new (
         fh => $fh, on_eof => sub { $c->send },
         on_read => sub {
            $buf .= $hdl->rbuf;
            $hdl->rbuf = '';
            if ($buf =~ /Test response \(2\)/) {
               $c->send;
            }
         });

   for (1..2) {
      $hdl->push_write (
         "GET\040http://localhost:19090/test\040HTTP/1.0\015\012"
         . "Connection: Keep-Alive\015\012\015\012"
      );
   }
};

$c->recv;

is ($cnt, 2, 'two requests over one connection');