view h2_trailers.t @ 1236:93f749c1d5c5

Tests: fixed parallel tests execution with UDP. Previously, when checking ports availability, a UDP socket was always created first, then a TCP socket was created. On success, one of UDP and TCP sockets was closed (depending on the "udp" option) and the second one was used to busy this port in other scripts. This lead to the following problem: in an attempt to reopen a UDP socket used in a given testing script it could be stolen by another script as part of checking ports availability. To solve this problem, UDP and TCP ports were split into two non-overlapping ranges: TCP ports are only used in the range 8000-8499, and UDP ports - in the range 8500-8999. In addition, the order of creating sockets in UDP tests has been reversed: now a TCP socket used as a lock precedes a UDP socket.
author Andrey Zelenkov <zelenkov@nginx.com>
date Thu, 26 Oct 2017 18:00:21 +0300
parents 39cfdf581253
children 97c8280de681
line wrap: on
line source

#!/usr/bin/perl

# (C) Sergey Kandaurov
# (C) Nginx, Inc.

# Tests for HTTP/2 trailers.

###############################################################################

use warnings;
use strict;

use Test::More;

BEGIN { use FindBin; chdir($FindBin::Bin); }

use lib 'lib';
use Test::Nginx;
use Test::Nginx::HTTP2;

###############################################################################

select STDERR; $| = 1;
select STDOUT; $| = 1;

my $t = Test::Nginx->new()->has(qw/http http_v2/)
	->write_file_expand('nginx.conf', <<'EOF');

%%TEST_GLOBALS%%

daemon off;

events {
}

http {
    %%TEST_GLOBALS_HTTP%%

    server {
        listen       127.0.0.1:8080 http2;
        server_name  localhost;

        location / {
            add_trailer X-Var $host;
        }

        http2_max_field_size 256k;

        location /continuation {
            # many trailers to send in parts
            add_trailer X-LongHeader $arg_h;
            add_trailer X-LongHeader $arg_h;
            add_trailer X-LongHeader $arg_h;
            add_trailer X-LongHeader $arg_h;
            add_trailer X-LongHeader $arg_h;
        }
    }
}

EOF

$t->write_file('index.html', 'SEE-THIS');
$t->write_file('empty', '');
$t->write_file('continuation', 'SEE-THIS');
$t->try_run('no add_trailer')->plan(22);

###############################################################################

my ($s, $sid, $frames, $frame);

$s = Test::Nginx::HTTP2->new();
$sid = $s->new_stream({ path => '/' });
$frames = $s->read(all => [{ sid => $sid, fin => 1 }]);
@$frames = grep { $_->{type} =~ "HEADERS|DATA" } @$frames;

is(@$frames, 3, 'frames');

$frame = shift @$frames;
is($frame->{headers}->{':status'}, 200, 'header');
is($frame->{headers}->{'x-var'}, undef, 'header not trailer');
is($frame->{flags}, 4, 'header flags');

$frame = shift @$frames;
is($frame->{data}, 'SEE-THIS', 'data');
is($frame->{flags}, 0, 'data flags');

$frame = shift @$frames;
is($frame->{headers}->{'x-var'}, 'localhost', 'trailer');
is($frame->{flags}, 5, 'trailer flags');

# with zero content-length

$s = Test::Nginx::HTTP2->new();
$sid = $s->new_stream({ path => '/empty' });
$frames = $s->read(all => [{ sid => $sid, fin => 1 }]);
@$frames = grep { $_->{type} =~ "HEADERS|DATA" } @$frames;

is(@$frames, 2, 'no data - frames');

$frame = shift @$frames;
is($frame->{headers}->{':status'}, 200, 'no data - header');
is($frame->{flags}, 4, 'no data - header flags');

$frame = shift @$frames;
is($frame->{headers}->{'x-var'}, 'localhost', 'no data - trailer');
is($frame->{flags}, 5, 'no data - trailer flags');

# CONTINUATION in response trailers

$s = Test::Nginx::HTTP2->new();
$sid = $s->new_stream({ path => '/continuation?h=' . 'x' x 2**12 });
$frames = $s->read(all => [{ sid => $sid, type => 'CONTINUATION' }]);
@$frames = grep { $_->{type} =~ "HEADERS|CONTINUATION|DATA" } @$frames;

is(@$frames, 4, 'continuation - frames');

$frame = shift @$frames;
is($frame->{headers}->{':status'}, 200, 'continuation - header');
is($frame->{flags}, 4, 'continuation - header flags');

$frame = shift @$frames;
is($frame->{data}, 'SEE-THIS', 'continuation - data');
is($frame->{flags}, 0, 'continuation - data flags');

$frame = shift @$frames;
is($frame->{type}, 'HEADERS', 'continuation - trailer');
is($frame->{flags}, 1, 'continuation - trailer flags');

$frame = shift @$frames;
is($frame->{type}, 'CONTINUATION', 'continuation - trailer continuation');
is($frame->{flags}, 4, 'continuation - trailer continuation flags');

###############################################################################