Skip to content

Commit 9184319

Browse files
committed
Tests: proxy tests for passing trailers from backend.
1 parent e6e16c4 commit 9184319

File tree

1 file changed

+170
-0
lines changed

1 file changed

+170
-0
lines changed

proxy_trailers.t

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
#!/usr/bin/perl
2+
3+
# (C) Sergey Kandaurov
4+
# (C) Nginx, Inc.
5+
6+
# Tests for http 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 proxy/)
28+
->has_daemon('openssl');
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+
location / {
53+
proxy_pass http://127.0.0.1:8081;
54+
proxy_http_version 1.1;
55+
proxy_pass_trailers on;
56+
}
57+
58+
location /nobuffering {
59+
proxy_pass http://127.0.0.1:8081/;
60+
proxy_buffering off;
61+
proxy_http_version 1.1;
62+
proxy_pass_trailers on;
63+
}
64+
}
65+
66+
server {
67+
listen 127.0.0.1:8081;
68+
server_name localhost;
69+
70+
add_header Trailer "X-Trailer, X-Another";
71+
add_trailer X-Trailer foo;
72+
add_trailer X-Another bar;
73+
74+
location / { }
75+
}
76+
}
77+
78+
EOF
79+
80+
$t->write_file('openssl.conf', <<EOF);
81+
[ req ]
82+
default_bits = 2048
83+
encrypt_key = no
84+
distinguished_name = req_distinguished_name
85+
[ req_distinguished_name ]
86+
EOF
87+
88+
my $d = $t->testdir();
89+
90+
foreach my $name ('localhost') {
91+
system('openssl req -x509 -new '
92+
. "-config $d/openssl.conf -subj /CN=$name/ "
93+
. "-out $d/$name.crt -keyout $d/$name.key "
94+
. ">>$d/openssl.out 2>&1") == 0
95+
or die "Can't create certificate for $name: $!\n";
96+
}
97+
98+
$t->write_file('index.html', 'SEE-THIS');
99+
$t->try_run('no proxy_pass_trailers')->plan(13);
100+
101+
###############################################################################
102+
103+
like(get('/'), qr/SEE-THIS.*X-Trailer: foo.*bar/si, 'trailers');
104+
like(get('/nobuffering'), qr/SEE-THIS.*X-Trailer: foo.*bar/si,
105+
'trailers nobuffering');
106+
107+
# HTTP/2
108+
109+
my ($s, $sid, $frames, $frame);
110+
$s = Test::Nginx::HTTP2->new();
111+
$sid = $s->new_stream({ headers => [
112+
{ name => ':method', value => 'GET' },
113+
{ name => ':scheme', value => 'http' },
114+
{ name => ':path', value => '/', },
115+
{ name => ':authority', value => 'localhost' },
116+
{ name => 'te', value => 'trailers', mode => 2 }]});
117+
118+
$frames = $s->read(all => [{ sid => $sid, fin => 1 }]);
119+
@$frames = grep { $_->{type} =~ "HEADERS|DATA" } @$frames;
120+
121+
$frame = shift @$frames;
122+
is($frame->{headers}->{':status'}, 200, 'h2 header');
123+
is($frame->{flags}, 4, 'h2 header flags');
124+
125+
$frame = shift @$frames;
126+
is($frame->{data}, 'SEE-THIS', 'h2 data');
127+
is($frame->{flags}, 0, 'h2 data flags');
128+
129+
$frame = shift @$frames;
130+
is($frame->{headers}->{'x-trailer'}, 'foo', 'h2 trailer');
131+
is($frame->{headers}->{'x-another'}, 'bar', 'h2 trailer 2');
132+
is($frame->{flags}, 5, 'h2 trailer flags');
133+
134+
# HTTP/3
135+
136+
$s = Test::Nginx::HTTP3->new();
137+
$sid = $s->new_stream({ headers => [
138+
{ name => ':method', value => 'GET' },
139+
{ name => ':scheme', value => 'http' },
140+
{ name => ':path', value => '/', },
141+
{ name => ':authority', value => 'localhost' },
142+
{ name => 'te', value => 'trailers' }]});
143+
144+
$frames = $s->read(all => [{ sid => $sid, fin => 1 }]);
145+
@$frames = grep { $_->{type} =~ "HEADERS|DATA" } @$frames;
146+
147+
$frame = shift @$frames;
148+
is($frame->{headers}->{':status'}, 200, 'h3 header');
149+
150+
$frame = shift @$frames;
151+
is($frame->{data}, 'SEE-THIS', 'h3 data');
152+
153+
$frame = shift @$frames;
154+
is($frame->{headers}->{'x-trailer'}, 'foo', 'h3 trailer');
155+
is($frame->{headers}->{'x-another'}, 'bar', 'h3 trailer 2');
156+
157+
###############################################################################
158+
159+
sub get {
160+
my ($uri) = @_;
161+
http(<<EOF);
162+
GET $uri HTTP/1.1
163+
Host: localhost
164+
Connection: te, close
165+
TE: trailers
166+
167+
EOF
168+
}
169+
170+
###############################################################################

0 commit comments

Comments
 (0)