Blame t/rt-112313.t

Packit 03f954
BEGIN {
Packit 03f954
  if ( $ENV{NO_NETWORK_TESTING} ) {
Packit 03f954
    print "1..0 # SKIP Live tests disabled due to NO_NETWORK_TESTING\n";
Packit 03f954
    exit;
Packit 03f954
  }
Packit 03f954
  eval {
Packit 03f954
        require IO::Socket::INET;
Packit 03f954
        my $s = IO::Socket::INET->new(
Packit 03f954
            PeerHost => "httpbin.org:80",
Packit 03f954
            Timeout  => 5,
Packit 03f954
        );
Packit 03f954
        die "Can't connect: $@" unless $s;
Packit 03f954
  };
Packit 03f954
  if ($@) {
Packit 03f954
        print "1..0 # SKIP Can't connect to httpbin.org\n";
Packit 03f954
        print $@;
Packit 03f954
        exit;
Packit 03f954
  }
Packit 03f954
}
Packit 03f954
Packit 03f954
use strict;
Packit 03f954
use warnings;
Packit 03f954
use Test::More;
Packit 03f954
use Net::HTTP;
Packit 03f954
Packit 03f954
# Attempt to verify that RT#112313 (Hang in my_readline() when keep-alive => 1 and $reponse_size % 1024 == 0) is fixed
Packit 03f954
Packit 03f954
# To do that, we need responses (headers + body) that are even multiples of 1024 bytes. So we
Packit 03f954
# iterate over the same URL, trying to grow the response size incrementally...
Packit 03f954
Packit 03f954
# There's a chance this test won't work if, for example, the response body grows by one byte while
Packit 03f954
# the Content-Length also rolls over to one more digit, thus increasing the total response by two
Packit 03f954
# bytes.
Packit 03f954
Packit 03f954
# So, we check that the reponse growth is only one byte after each iteration and also test multiple
Packit 03f954
# times across the 1024, 2048 and 3072 boundaries...
Packit 03f954
Packit 03f954
Packit 03f954
sub try
Packit 03f954
{
Packit 03f954
    my $n = shift;
Packit 03f954
Packit 03f954
    # Need a new socket every time because we're testing with Keep-Alive...
Packit 03f954
    my $s = Net::HTTP->new(
Packit 03f954
        Host            => "httpbin.org",
Packit 03f954
        KeepAlive       => 1,
Packit 03f954
        PeerHTTPVersion => "1.1",
Packit 03f954
    ) or die "$@";
Packit 03f954
Packit 03f954
    $s->write_request(GET => '/headers',
Packit 03f954
        'User-Agent' => "Net::HTTP - $0",
Packit 03f954
        'X-Foo'      => ('x' x $n),
Packit 03f954
    );
Packit 03f954
Packit 03f954
    # Wait until all data is probably available on the socket...
Packit 03f954
    sleep 1;
Packit 03f954
Packit 03f954
    my ($code, $mess, @headers) = $s->read_response_headers();
Packit 03f954
Packit 03f954
    # XXX remove X-Processed-Time header
Packit 03f954
    for my $i (0..$#headers) {
Packit 03f954
        if ($headers[$i] eq 'X-Processed-Time') {
Packit 03f954
            splice @headers, $i, 2;
Packit 03f954
            last;
Packit 03f954
        }
Packit 03f954
    }
Packit 03f954
Packit 03f954
    my $body = '';
Packit 03f954
    while ($s->read_entity_body(my $buf, 1024))
Packit 03f954
    {
Packit 03f954
        $body .= $buf;
Packit 03f954
    }
Packit 03f954
Packit 03f954
    # Compute what is probably the total response length...
Packit 03f954
    my $total_len = length(join "\r\n", 'HTTP/1.1', "$code $mess", @headers, '', $body) - 1;
Packit 03f954
Packit 03f954
    # diag("$n - $code $mess => $total_len");
Packit 03f954
    # diag(join "\r\n", 'HTTP/1.1', "$code $mess", @headers, '', $body);
Packit 03f954
Packit 03f954
    $code == 200
Packit 03f954
        or die "$code $mess";
Packit 03f954
Packit 03f954
    return $total_len;
Packit 03f954
}
Packit 03f954
Packit 03f954
my $timeout = 15;
Packit 03f954
my $wiggle_room = 3;
Packit 03f954
Packit 03f954
local $SIG{ALRM} = sub { die 'timeout' };
Packit 03f954
Packit 03f954
my $base_len = try(1);
Packit 03f954
ok($base_len < 1024, "base response length is less than 1024: $base_len");
Packit 03f954
Packit 03f954
for my $kb (1024, 2048, 3072)
Packit 03f954
{
Packit 03f954
    my $last;
Packit 03f954
Packit 03f954
    # Calculate range that will take us across the 1024 boundary...
Packit 03f954
    for my $n (($kb - $base_len - $wiggle_room) .. ($kb - $base_len + $wiggle_room))
Packit 03f954
    {
Packit 03f954
        my $len = -1;
Packit 03f954
Packit 03f954
        eval {
Packit 03f954
            alarm $timeout;
Packit 03f954
            $len = try($n);
Packit 03f954
        };
Packit 03f954
Packit 03f954
        ok(!$@, "ok for n $n -> response length $len")
Packit 03f954
            or diag("error: $@");
Packit 03f954
Packit 03f954
        # Verify that response length only increased by one since the whole test rests on that assumption...
Packit 03f954
        is($len - $last, 1, 'reponse length increased by 1') if $last;
Packit 03f954
Packit 03f954
        $last = $len;
Packit 03f954
    }
Packit 03f954
}
Packit 03f954
Packit 03f954
done_testing();