-
Notifications
You must be signed in to change notification settings - Fork 76
/
h2_trailers.t
136 lines (95 loc) · 3.53 KB
/
h2_trailers.t
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/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/)->plan(22)
->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;
}
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');
# suppress deprecation warning
open OLDERR, ">&", \*STDERR; close STDERR;
$t->run();
open STDERR, ">&", \*OLDERR;
###############################################################################
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 4000 });
$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');
###############################################################################