Skip to content

Commit 345f9b7

Browse files
committed
Tests: grpc tests for passing trailers from backend.
1 parent 9184319 commit 345f9b7

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed

grpc_trailers.t

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
#!/usr/bin/perl
2+
3+
# (C) Sergey Kandaurov
4+
# (C) Nginx, Inc.
5+
6+
# Tests for grpc backend returning response with trailers.
7+
8+
###############################################################################
9+
10+
use warnings;
11+
use strict;
12+
13+
use Test::More;
14+
15+
BEGIN { use FindBin; chdir($FindBin::Bin); }
16+
17+
use lib 'lib';
18+
use Test::Nginx;
19+
use Test::Nginx::HTTP2;
20+
use Test::Nginx::HTTP3;
21+
22+
###############################################################################
23+
24+
select STDERR; $| = 1;
25+
select STDOUT; $| = 1;
26+
27+
my $t = Test::Nginx->new()->has(qw/http http_v2 http_v3 cryptx grpc/)
28+
->has_daemon('openssl')->plan(12);
29+
30+
$t->write_file_expand('nginx.conf', <<'EOF');
31+
32+
%%TEST_GLOBALS%%
33+
34+
daemon off;
35+
36+
events {
37+
}
38+
39+
http {
40+
%%TEST_GLOBALS_HTTP%%
41+
42+
server {
43+
listen 127.0.0.1:8080;
44+
listen 127.0.0.1:%%PORT_8980_UDP%% quic;
45+
server_name localhost;
46+
47+
ssl_certificate_key localhost.key;
48+
ssl_certificate localhost.crt;
49+
50+
http2 on;
51+
52+
add_trailer X ""; # force chunked encoding
53+
54+
location / {
55+
grpc_pass 127.0.0.1:8081;
56+
}
57+
}
58+
59+
server {
60+
listen 127.0.0.1:8081;
61+
server_name localhost;
62+
63+
http2 on;
64+
65+
add_header Trailer "X-Trailer, X-Another";
66+
add_trailer X-Trailer foo;
67+
add_trailer X-Another bar;
68+
69+
location / { }
70+
}
71+
}
72+
73+
EOF
74+
75+
$t->write_file('openssl.conf', <<EOF);
76+
[ req ]
77+
default_bits = 2048
78+
encrypt_key = no
79+
distinguished_name = req_distinguished_name
80+
[ req_distinguished_name ]
81+
EOF
82+
83+
my $d = $t->testdir();
84+
85+
foreach my $name ('localhost') {
86+
system('openssl req -x509 -new '
87+
. "-config $d/openssl.conf -subj /CN=$name/ "
88+
. "-out $d/$name.crt -keyout $d/$name.key "
89+
. ">>$d/openssl.out 2>&1") == 0
90+
or die "Can't create certificate for $name: $!\n";
91+
}
92+
93+
$t->write_file('index.html', 'SEE-THIS');
94+
$t->run();
95+
96+
###############################################################################
97+
98+
like(get('/'), qr/SEE-THIS.*X-Trailer: foo.*bar/si, 'trailers');
99+
100+
# HTTP/2
101+
102+
my ($s, $sid, $frames, $frame);
103+
$s = Test::Nginx::HTTP2->new();
104+
$sid = $s->new_stream({ headers => [
105+
{ name => ':method', value => 'GET' },
106+
{ name => ':scheme', value => 'http' },
107+
{ name => ':path', value => '/', },
108+
{ name => ':authority', value => 'localhost' },
109+
{ name => 'te', value => 'trailers', mode => 2 }]});
110+
111+
$frames = $s->read(all => [{ sid => $sid, fin => 1 }]);
112+
@$frames = grep { $_->{type} =~ "HEADERS|DATA" } @$frames;
113+
114+
$frame = shift @$frames;
115+
is($frame->{headers}->{':status'}, 200, 'h2 header');
116+
is($frame->{flags}, 4, 'h2 header flags');
117+
118+
$frame = shift @$frames;
119+
is($frame->{data}, 'SEE-THIS', 'h2 data');
120+
is($frame->{flags}, 0, 'h2 data flags');
121+
122+
$frame = shift @$frames;
123+
is($frame->{headers}->{'x-trailer'}, 'foo', 'h2 trailer');
124+
is($frame->{headers}->{'x-another'}, 'bar', 'h2 trailer 2');
125+
is($frame->{flags}, 5, 'h2 trailer flags');
126+
127+
# HTTP/3
128+
129+
$s = Test::Nginx::HTTP3->new();
130+
$sid = $s->new_stream({ headers => [
131+
{ name => ':method', value => 'GET' },
132+
{ name => ':scheme', value => 'http' },
133+
{ name => ':path', value => '/', },
134+
{ name => ':authority', value => 'localhost' },
135+
{ name => 'te', value => 'trailers' }]});
136+
137+
$frames = $s->read(all => [{ sid => $sid, fin => 1 }]);
138+
@$frames = grep { $_->{type} =~ "HEADERS|DATA" } @$frames;
139+
140+
$frame = shift @$frames;
141+
is($frame->{headers}->{':status'}, 200, 'h3 header');
142+
143+
$frame = shift @$frames;
144+
is($frame->{data}, 'SEE-THIS', 'h3 data');
145+
146+
$frame = shift @$frames;
147+
is($frame->{headers}->{'x-trailer'}, 'foo', 'h3 trailer');
148+
is($frame->{headers}->{'x-another'}, 'bar', 'h3 trailer 2');
149+
150+
###############################################################################
151+
152+
sub get {
153+
my ($uri) = @_;
154+
http(<<EOF);
155+
GET $uri HTTP/1.1
156+
Host: localhost
157+
Connection: te, close
158+
TE: trailers
159+
160+
EOF
161+
}
162+
163+
###############################################################################

0 commit comments

Comments
 (0)