forked from nginx/nginx-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc_request_buffering.t
196 lines (145 loc) · 5.05 KB
/
grpc_request_buffering.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/perl
# (C) Sergey Kandaurov
# (C) Nginx, Inc.
# Tests for grpc module, request body buffered.
###############################################################################
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 grpc mirror proxy/)->plan(12);
$t->write_file_expand('nginx.conf', <<'EOF');
%%TEST_GLOBALS%%
daemon off;
events {
}
http {
%%TEST_GLOBALS_HTTP%%
server {
listen 127.0.0.1:8080 http2;
listen 127.0.0.1:8082;
server_name localhost;
location /mirror { }
location / {
grpc_pass 127.0.0.1:8081;
add_header X-Body $request_body;
mirror /mirror;
}
location /proxy {
proxy_pass http://127.0.0.1:8082/mirror;
proxy_intercept_errors on;
error_page 404 = @fallback;
}
location @fallback {
grpc_pass 127.0.0.1:8081;
}
}
}
EOF
$t->run();
###############################################################################
my $p = port(8081);
my $f = grpc();
my $frames = $f->{http_start}('/SayHello');
my ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
is($frame->{flags}, 4, 'request - HEADERS flags');
is($frame->{headers}{':method'}, 'POST', 'request - method');
is($frame->{headers}{':scheme'}, 'http', 'request - scheme');
is($frame->{headers}{':path'}, '/SayHello', 'request - path');
is($frame->{headers}{':authority'}, "127.0.0.1:$p", 'request - authority');
($frame) = grep { $_->{type} eq "DATA" } @$frames;
is($frame->{data}, 'Hello', 'request - DATA');
is($frame->{length}, 5, 'request - DATA length');
is($frame->{flags}, 1, 'request - DATA flags');
$frames = $f->{http_end}();
($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
is($frame->{headers}{'x-body'}, 'Hello', 'request body in memory');
# tcp_nopush usage on peer connections
# reopen window for request body after initial window was exhausted
$frames = $f->{http_start}('/proxy');
is(eval(join '+', map { $_->{length} } grep { $_->{type} eq "DATA" } @$frames),
65535, 'preserve_output - first body bytes');
# expect body cleanup is disabled with preserve_output (ticket #1565).
# after request body first bytes were proxied on behalf of initial window size,
# send response header from upstream, this leads to body cleanup code path
$frames = $f->{http_end}();
is(eval(join '+', map { $_->{length} } grep { $_->{type} eq "DATA" } @$frames),
465, 'preserve_output - last body bytes');
like(`grep -F '[crit]' ${\($t->testdir())}/error.log`, qr/^$/s, 'no crits');
###############################################################################
sub grpc {
my ($server, $client, $f, $s, $c, $sid, $uri);
$server = IO::Socket::INET->new(
Proto => 'tcp',
LocalHost => '127.0.0.1',
LocalPort => $p,
Listen => 5,
Reuse => 1
)
or die "Can't create listening socket: $!\n";
$f->{http_start} = sub {
($uri, my %extra) = @_;
$s = Test::Nginx::HTTP2->new() if !defined $s;
my ($body) = $uri eq '/proxy' ? 'Hello' x 13200 : 'Hello';
$s->new_stream({ body => $body, headers => [
{ name => ':method', value => 'POST', mode => 0 },
{ name => ':scheme', value => 'http', mode => 0 },
{ name => ':path', value => $uri },
{ name => ':authority', value => 'localhost' },
{ name => 'content-length', value => length($body) }]});
if (!$extra{reuse}) {
eval {
local $SIG{ALRM} = sub { die "timeout\n" };
alarm(5);
$client = $server->accept() or return;
alarm(0);
};
alarm(0);
if ($@) {
log_in("died: $@");
return undef;
}
log2c("(new connection $client)");
$client->sysread(my $buf, 24) == 24 or return; # preface
$c = Test::Nginx::HTTP2->new(1, socket => $client,
pure => 1, preface => "") or return;
}
my $frames = $uri eq '/proxy'
? $c->read(all => [{ length => 65535 }])
: $c->read(all => [{ fin => 1 }]);
if (!$extra{reuse}) {
$c->h2_settings(0);
$c->h2_settings(1);
}
my ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
$sid = $frame->{sid};
return $frames;
};
$f->{http_end} = sub {
$c->new_stream({ body_more => 1, headers => [
{ name => ':status', value => '200', mode => 0 },
{ name => 'content-type', value => 'application/grpc' },
]}, $sid);
# reopen window for request body after response HEADERS is sent
if ($uri eq '/proxy') {
$c->h2_window(2**16, $sid);
$c->h2_window(2**16);
return $c->read(all => [{ sid => $sid, fin => 1 }]);
}
$c->h2_body('Hello world', { body_more => 1 });
$c->new_stream({ headers => [
{ name => 'grpc-status', value => '0', mode => 2 },
{ name => 'grpc-message', value => '', mode => 2 },
]}, $sid);
return $s->read(all => [{ fin => 1 }]);
};
return $f;
}
sub log2c { Test::Nginx::log_core('||', @_); }
###############################################################################