Mercurial > hg > nginx-tests
annotate grpc.t @ 1361:6c4f9941ae9f
Tests: fixed http_keepalive.t response logging.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Mon, 06 Aug 2018 14:52:00 +0300 |
parents | 351b95be742b |
children | 6874b32dc3d2 |
rev | line source |
---|---|
1303 | 1 #!/usr/bin/perl |
2 | |
3 # (C) Sergey Kandaurov | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Tests for grpc backend. | |
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 | |
21 ############################################################################### | |
22 | |
23 select STDERR; $| = 1; | |
24 select STDOUT; $| = 1; | |
25 | |
1312
6f95c0ed2335
Tests: removed proxy prerequisite from grpc tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1303
diff
changeset
|
26 my $t = Test::Nginx->new()->has(qw/http rewrite http_v2 grpc/) |
1303 | 27 ->has(qw/upstream_keepalive/); |
28 | |
29 $t->write_file_expand('nginx.conf', <<'EOF'); | |
30 | |
31 %%TEST_GLOBALS%% | |
32 | |
33 daemon off; | |
34 | |
35 events { | |
36 } | |
37 | |
38 http { | |
39 %%TEST_GLOBALS_HTTP%% | |
40 | |
41 upstream u { | |
42 server 127.0.0.1:8081; | |
43 keepalive 1; | |
44 } | |
45 | |
46 server { | |
47 listen 127.0.0.1:8080 http2; | |
48 server_name localhost; | |
49 | |
50 http2_max_field_size 128k; | |
51 http2_max_header_size 128k; | |
52 http2_body_preread_size 128k; | |
53 | |
54 location / { | |
55 grpc_pass grpc://127.0.0.1:8081; | |
56 | |
57 if ($arg_if) { | |
58 # nothing | |
59 } | |
60 | |
61 limit_except GET { | |
62 # nothing | |
63 } | |
64 } | |
65 | |
66 location /KeepAlive { | |
67 grpc_pass u; | |
68 } | |
69 | |
70 location /LongHeader { | |
71 grpc_pass 127.0.0.1:8081; | |
72 grpc_set_header X-LongHeader $arg_h; | |
73 } | |
74 | |
75 location /LongField { | |
76 grpc_pass 127.0.0.1:8081; | |
77 grpc_buffer_size 65k; | |
78 } | |
79 | |
80 location /SetHost { | |
81 grpc_pass 127.0.0.1:8081; | |
82 grpc_set_header Host custom; | |
83 } | |
84 | |
85 location /SetArgs { | |
86 grpc_pass 127.0.0.1:8081; | |
87 set $args $arg_c; | |
88 } | |
89 } | |
90 } | |
91 | |
92 EOF | |
93 | |
1318
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
94 $t->try_run('no grpc')->plan(100); |
1303 | 95 |
96 ############################################################################### | |
97 | |
98 my $p = port(8081); | |
99 my $f = grpc(); | |
100 | |
101 my $frames = $f->{http_start}('/SayHello'); | |
102 my ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
103 is($frame->{flags}, 4, 'request - HEADERS flags'); | |
104 ok((my $sid = $frame->{sid}) % 2, 'request - HEADERS sid odd'); | |
105 is($frame->{headers}{':method'}, 'POST', 'request - method'); | |
106 is($frame->{headers}{':scheme'}, 'http', 'request - scheme'); | |
107 is($frame->{headers}{':path'}, '/SayHello', 'request - path'); | |
108 is($frame->{headers}{':authority'}, "127.0.0.1:$p", 'request - authority'); | |
109 is($frame->{headers}{'content-type'}, 'application/grpc', | |
110 'request - content type'); | |
111 is($frame->{headers}{te}, 'trailers', 'request - te'); | |
112 | |
113 $frames = $f->{data}('Hello'); | |
114 ($frame) = grep { $_->{type} eq "SETTINGS" } @$frames; | |
115 is($frame->{flags}, 1, 'request - SETTINGS ack'); | |
116 is($frame->{sid}, 0, 'request - SETTINGS sid'); | |
117 is($frame->{length}, 0, 'request - SETTINGS length'); | |
118 | |
119 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
120 is($frame->{data}, 'Hello', 'request - DATA'); | |
121 is($frame->{length}, 5, 'request - DATA length'); | |
122 is($frame->{flags}, 1, 'request - DATA flags'); | |
123 is($frame->{sid}, $sid, 'request - DATA sid match'); | |
124 | |
125 $frames = $f->{http_end}(); | |
126 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
127 is($frame->{flags}, 4, 'response - HEADERS flags'); | |
128 is($frame->{sid}, 1, 'response - HEADERS sid'); | |
129 is($frame->{headers}{':status'}, '200', 'response - status'); | |
130 is($frame->{headers}{'content-type'}, 'application/grpc', | |
131 'response - content type'); | |
132 ok($frame->{headers}{server}, 'response - server'); | |
133 ok($frame->{headers}{date}, 'response - date'); | |
134 ok(my $c = $frame->{headers}{'x-connection'}, 'response - connection'); | |
135 | |
136 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
137 is($frame->{data}, 'Hello world', 'response - DATA'); | |
138 is($frame->{length}, 11, 'response - DATA length'); | |
139 is($frame->{flags}, 0, 'response - DATA flags'); | |
140 is($frame->{sid}, 1, 'response - DATA sid'); | |
141 | |
142 (undef, $frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
143 is($frame->{flags}, 5, 'response - trailers flags'); | |
144 is($frame->{sid}, 1, 'response - trailers sid'); | |
145 is($frame->{headers}{'grpc-message'}, '', 'response - trailers message'); | |
146 is($frame->{headers}{'grpc-status'}, '0', 'response - trailers status'); | |
147 | |
148 # next request is on a new backend connection, no sid incremented | |
149 | |
150 $frames = $f->{http_start}('/SayHello'); | |
151 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
152 is($frame->{sid}, $sid, 'request 2 - HEADERS sid again'); | |
153 $f->{data}('Hello'); | |
154 $frames = $f->{http_end}(); | |
155 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
156 cmp_ok($frame->{headers}{'x-connection'}, '>', $c, 'response 2 - connection'); | |
157 | |
158 # upstream keepalive | |
159 | |
160 $frames = $f->{http_start}('/KeepAlive'); | |
161 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
162 is($frame->{sid}, $sid, 'keepalive - HEADERS sid'); | |
163 $f->{data}('Hello'); | |
164 $frames = $f->{http_end}(); | |
165 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
166 ok($c = $frame->{headers}{'x-connection'}, 'keepalive - connection'); | |
167 | |
168 $frames = $f->{http_start}('/KeepAlive', reuse => 1); | |
169 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
170 cmp_ok($frame->{sid}, '>', $sid, 'keepalive - HEADERS sid next'); | |
171 $f->{data}('Hello'); | |
172 $frames = $f->{http_end}(); | |
173 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
174 is($frame->{headers}{'x-connection'}, $c, 'keepalive - connection reuse'); | |
175 | |
176 # various header compression formats | |
177 | |
178 $f->{http_start}('/SayHello'); | |
179 $f->{data}('Hello'); | |
180 $frames = $f->{http_end}(mode => 3); | |
181 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
182 is($frame->{headers}{':status'}, '200', 'without indexing'); | |
183 is($frame->{headers}{'content-type'}, 'application/grpc', | |
184 'without indexing 2'); | |
185 | |
186 $f->{http_start}('/SayHello'); | |
187 $f->{data}('Hello'); | |
188 $frames = $f->{http_end}(mode => 4); | |
189 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
190 is($frame->{headers}{':status'}, '200', 'without indexing new'); | |
191 is($frame->{headers}{'content-type'}, 'application/grpc', | |
192 'without indexing new 2'); | |
193 | |
194 $f->{http_start}('/SayHello'); | |
195 $f->{data}('Hello'); | |
196 $frames = $f->{http_end}(mode => 5); | |
197 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
198 is($frame->{headers}{':status'}, '200', 'never indexed'); | |
199 is($frame->{headers}{'content-type'}, 'application/grpc', | |
200 'never indexed 2'); | |
201 | |
202 $f->{http_start}('/SayHello'); | |
203 $f->{data}('Hello'); | |
204 $frames = $f->{http_end}(mode => 6); | |
205 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
206 is($frame->{headers}{':status'}, '200', 'never indexed new'); | |
207 is($frame->{headers}{'content-type'}, 'application/grpc', | |
208 'never indexed new 2'); | |
209 | |
210 # padding & priority | |
211 | |
212 $f->{http_start}('/SayHello'); | |
213 $f->{data}('Hello'); | |
214 $frames = $f->{http_end}(padding => 7); | |
215 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
216 is($frame->{headers}{':status'}, '200', 'padding'); | |
217 | |
218 $f->{http_start}('/SayHello'); | |
219 $f->{data}('Hello'); | |
220 $frames = $f->{http_end}(prio => 137, dep => 0x01020304); | |
221 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
222 is($frame->{headers}{':status'}, '200', 'priority'); | |
223 | |
224 $f->{http_start}('/SayHello'); | |
225 $f->{data}('Hello'); | |
226 $frames = $f->{http_end}(padding => 7, prio => 137, dep => 0x01020304); | |
227 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
228 is($frame->{headers}{':status'}, '200', 'padding priority'); | |
229 | |
230 SKIP: { | |
231 skip 'long test', 1 unless $ENV{TEST_NGINX_UNSAFE}; | |
232 | |
233 $f->{http_start}('/SaySplit'); | |
234 $f->{data}('Hello'); | |
235 $frames = $f->{http_end}(padding => 7, prio => 137, dep => 0x01020304, | |
236 split => [(map{1}(1..20)), 30], split_delay => 0.1); | |
237 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
238 is($frame->{headers}{':status'}, '200', 'padding priority split'); | |
239 | |
240 } | |
241 | |
242 # grpc error, no empty data frame expected | |
243 | |
244 $f->{http_start}('/SayHello'); | |
245 $f->{data}('Hello'); | |
246 $frames = $f->{http_err}(); | |
247 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
248 is($frame->{flags}, 5, 'grpc error - HEADERS flags'); | |
249 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
250 ok(!$frame, 'grpc error - no DATA frame'); | |
251 | |
252 # continuation from backend, expect parts assembled | |
253 | |
254 $f->{http_start}('/SayHello'); | |
255 $f->{data}('Hello'); | |
256 $frames = $f->{continuation}(); | |
257 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
258 is($frame->{flags}, 4, 'continuation - HEADERS flags'); | |
259 is($frame->{headers}{':status'}, '200', 'continuation - status'); | |
260 is($frame->{headers}{'content-type'}, 'application/grpc', | |
261 'continuation - content type'); | |
262 | |
263 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
264 is($frame->{data}, 'Hello world', 'continuation - DATA'); | |
265 is($frame->{length}, 11, 'continuation - DATA length'); | |
266 is($frame->{flags}, 0, 'continuation - DATA flags'); | |
267 | |
268 (undef, $frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
269 is($frame->{flags}, 5, 'continuation - trailers flags'); | |
270 is($frame->{headers}{'grpc-message'}, '', 'continuation - trailers message'); | |
271 is($frame->{headers}{'grpc-status'}, '0', 'continuation - trailers status'); | |
272 | |
273 # continuation from backend, header split | |
274 | |
275 $f->{http_start}('/SayHello'); | |
276 $f->{data}('Hello'); | |
277 $frames = $f->{http_end}(mode => 6, continuation => [map { 1 } (1 .. 42)]); | |
278 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
279 is($frame->{headers}{':status'}, '200', 'continuation - header split'); | |
280 | |
281 # continuation to backend | |
282 | |
283 $frames = $f->{http_start}('/LongHeader?h=' . ('Z' x 31337)); | |
284 @$frames = grep { $_->{type} =~ "HEADERS|CONTINUATION" } @$frames; | |
285 is(@$frames, 4, 'continuation - frames'); | |
286 | |
287 $frame = shift @$frames; | |
288 is($frame->{type}, 'HEADERS', 'continuation - HEADERS'); | |
289 is($frame->{length}, 16384, 'continuation - HEADERS length'); | |
290 is($frame->{flags}, 1, 'continuation - HEADERS flags'); | |
291 ok($frame->{sid}, 'continuation - HEADERS sid'); | |
292 | |
293 $frame = shift @$frames; | |
294 is($frame->{type}, 'CONTINUATION', 'continuation - CONTINUATION'); | |
295 is($frame->{length}, 16384, 'continuation - CONTINUATION length'); | |
296 is($frame->{flags}, 0, 'continuation - CONTINUATION flags'); | |
297 ok($frame->{sid}, 'continuation - CONTINUATION sid'); | |
298 | |
299 $frame = shift @$frames; | |
300 is($frame->{type}, 'CONTINUATION', 'continuation - CONTINUATION 2'); | |
301 is($frame->{length}, 16384, 'continuation - CONTINUATION 2 length'); | |
302 is($frame->{flags}, 0, 'continuation - CONTINUATION 2 flags'); | |
303 | |
304 $frame = shift @$frames; | |
305 is($frame->{type}, 'CONTINUATION', 'continuation - CONTINUATION n'); | |
306 cmp_ok($frame->{length}, '<', 16384, 'continuation - CONTINUATION n length'); | |
307 is($frame->{flags}, 4, 'continuation - CONTINUATION n flags'); | |
308 is($frame->{headers}{':path'}, '/LongHeader?h=' . 'Z' x 31337, | |
309 'continuation - path'); | |
310 is($frame->{headers}{'x-longheader'}, 'Z' x 31337, 'continuation - header'); | |
311 | |
312 $f->{http_end}(); | |
313 | |
314 # long header field | |
315 | |
316 $f->{http_start}('/LongField'); | |
317 $f->{data}('Hello'); | |
318 $frames = $f->{field_len}(2**7); | |
319 ($frame) = grep { $_->{flags} & 0x4 } @$frames; | |
320 is($frame->{headers}{'x' x 2**7}, 'y' x 2**7, 'long header field 1'); | |
321 | |
322 $f->{http_start}('/LongField'); | |
323 $f->{data}('Hello'); | |
324 $frames = $f->{field_len}(2**8); | |
325 ($frame) = grep { $_->{flags} & 0x4 } @$frames; | |
326 is($frame->{headers}{'x' x 2**8}, 'y' x 2**8, 'long header field 2'); | |
327 | |
328 $f->{http_start}('/LongField'); | |
329 $f->{data}('Hello'); | |
330 $frames = $f->{field_len}(2**15); | |
331 ($frame) = grep { $_->{flags} & 0x4 } @$frames; | |
332 is($frame->{headers}{'x' x 2**15}, 'y' x 2**15, 'long header field 3'); | |
333 | |
334 # flow control | |
335 | |
336 $f->{http_start}('/FlowControl'); | |
337 $frames = $f->{data_len}(('Hello' x 13000) . ('x' x 550), 65535); | |
338 my $sum = eval join '+', map { $_->{type} eq "DATA" && $_->{length} } @$frames; | |
339 is($sum, 65535, 'flow control - iws length'); | |
340 | |
341 $f->{update}(10); | |
342 $f->{update_sid}(10); | |
343 | |
344 $frames = $f->{data_len}(undef, 10); | |
345 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
346 is($frame->{length}, 10, 'flow control - update length'); | |
347 is($frame->{flags}, 0, 'flow control - update flags'); | |
348 | |
349 $f->{update_sid}(10); | |
350 $f->{update}(10); | |
351 | |
352 $frames = $f->{data_len}(undef, 5); | |
353 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
354 is($frame->{length}, 5, 'flow control - rest length'); | |
355 is($frame->{flags}, 1, 'flow control - rest flags'); | |
356 | |
357 $f->{http_end}(); | |
358 | |
1318
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
359 # preserve output |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
360 |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
361 $f->{http_start}('/Preserve'); |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
362 $f->{data}('Hello'); |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
363 $frames = $f->{http_pres}(); |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
364 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
365 is($frame->{flags}, 4, 'preserve - HEADERS'); |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
366 |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
367 my @data = grep { $_->{type} eq "DATA" } @$frames; |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
368 $sum = eval join '+', map { $_->{length} } @data; |
1319
3b30e97acdcb
Tests: made grpc preserve output test pass on win32 and Solaris.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1318
diff
changeset
|
369 is($sum, 20480, 'preserve - DATA'); |
1318
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
370 |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
371 (undef, $frame) = grep { $_->{type} eq "HEADERS" } @$frames; |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
372 is($frame->{flags}, 5, 'preserve - trailers'); |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
373 |
1303 | 374 # DATA padding |
375 | |
376 $f->{http_start}('/SayPadding'); | |
377 $f->{data}('Hello'); | |
378 $frames = $f->{http_end}(body_padding => 42); | |
379 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
380 is($frame->{data}, 'Hello world', 'response - DATA'); | |
381 is($frame->{length}, 11, 'response - DATA length'); | |
382 is($frame->{flags}, 0, 'response - DATA flags'); | |
383 | |
384 # :authority inheritance | |
385 | |
386 $frames = $f->{http_start}('/SayHello?if=1'); | |
387 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
388 is($frame->{headers}{':authority'}, "127.0.0.1:$p", 'authority in if'); | |
389 $f->{data}('Hello'); | |
390 $f->{http_end}(); | |
391 | |
392 # misc tests | |
393 | |
394 $frames = $f->{http_start}('/SetHost'); | |
395 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
396 ok(!$frame->{headers}{':authority'}, 'set host - authority'); | |
397 is($frame->{headers}{'host'}, 'custom', 'set host - host'); | |
398 $f->{data}('Hello'); | |
399 $f->{http_end}(); | |
400 | |
401 $frames = $f->{http_start}('/SetArgs?f'); | |
402 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
403 is($frame->{headers}{':path'}, '/SetArgs', 'set args'); | |
404 $f->{data}('Hello'); | |
405 $f->{http_end}(); | |
406 | |
407 $frames = $f->{http_start}('/SetArgs?c=1'); | |
408 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
409 is($frame->{headers}{':path'}, '/SetArgs?1', 'set args len'); | |
410 $f->{data}('Hello'); | |
411 $f->{http_end}(); | |
412 | |
413 $frames = $f->{http_start}('/SetArgs esc'); | |
414 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
415 is($frame->{headers}{':path'}, '/SetArgs%20esc', 'uri escape'); | |
416 $f->{data}('Hello'); | |
417 $f->{http_end}(); | |
418 | |
419 $frames = $f->{http_start}('/'); | |
420 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
421 is($frame->{headers}{':path'}, '/', 'root index'); | |
422 $f->{data}('Hello'); | |
423 $f->{http_end}(); | |
424 | |
425 $frames = $f->{http_start}('/', method => 'GET'); | |
426 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
427 is($frame->{headers}{':method'}, 'GET', 'method get'); | |
428 $f->{data}('Hello'); | |
429 $f->{http_end}(); | |
430 | |
431 $frames = $f->{http_start}('/', method => 'HEAD'); | |
432 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
433 is($frame->{headers}{':method'}, 'HEAD', 'method head'); | |
434 $f->{data}('Hello'); | |
435 $f->{http_end}(); | |
436 | |
437 ############################################################################### | |
438 | |
439 sub grpc { | |
1318
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
440 my ($server, $client, $f, $s, $c, $sid, $csid, $uri); |
1303 | 441 my $n = 0; |
442 | |
443 $server = IO::Socket::INET->new( | |
444 Proto => 'tcp', | |
445 LocalHost => '127.0.0.1', | |
446 LocalPort => $p, | |
447 Listen => 5, | |
448 Reuse => 1 | |
449 ) | |
450 or die "Can't create listening socket: $!\n"; | |
451 | |
452 $f->{http_start} = sub { | |
453 ($uri, my %extra) = @_; | |
454 my $body_more = 1 if $uri !~ /LongHeader/; | |
455 my $meth = $extra{method} || 'POST'; | |
456 $s = Test::Nginx::HTTP2->new() if !defined $s; | |
1318
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
457 $csid = $s->new_stream({ body_more => $body_more, headers => [ |
1303 | 458 { name => ':method', value => $meth, mode => !!$meth }, |
459 { name => ':scheme', value => 'http', mode => 0 }, | |
460 { name => ':path', value => $uri, }, | |
461 { name => ':authority', value => 'localhost' }, | |
462 { name => 'content-type', value => 'application/grpc' }, | |
463 { name => 'te', value => 'trailers', mode => 2 }]}); | |
464 | |
465 if (!$extra{reuse}) { | |
1321
351b95be742b
Tests: fixed grpc tests hang in accept() on internal nginx error.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1319
diff
changeset
|
466 eval { |
351b95be742b
Tests: fixed grpc tests hang in accept() on internal nginx error.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1319
diff
changeset
|
467 local $SIG{ALRM} = sub { die "timeout\n" }; |
351b95be742b
Tests: fixed grpc tests hang in accept() on internal nginx error.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1319
diff
changeset
|
468 alarm(5); |
351b95be742b
Tests: fixed grpc tests hang in accept() on internal nginx error.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1319
diff
changeset
|
469 |
351b95be742b
Tests: fixed grpc tests hang in accept() on internal nginx error.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1319
diff
changeset
|
470 $client = $server->accept() or return; |
351b95be742b
Tests: fixed grpc tests hang in accept() on internal nginx error.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1319
diff
changeset
|
471 |
351b95be742b
Tests: fixed grpc tests hang in accept() on internal nginx error.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1319
diff
changeset
|
472 alarm(0); |
351b95be742b
Tests: fixed grpc tests hang in accept() on internal nginx error.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1319
diff
changeset
|
473 }; |
351b95be742b
Tests: fixed grpc tests hang in accept() on internal nginx error.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1319
diff
changeset
|
474 alarm(0); |
351b95be742b
Tests: fixed grpc tests hang in accept() on internal nginx error.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1319
diff
changeset
|
475 if ($@) { |
351b95be742b
Tests: fixed grpc tests hang in accept() on internal nginx error.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1319
diff
changeset
|
476 log_in("died: $@"); |
351b95be742b
Tests: fixed grpc tests hang in accept() on internal nginx error.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1319
diff
changeset
|
477 return undef; |
351b95be742b
Tests: fixed grpc tests hang in accept() on internal nginx error.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1319
diff
changeset
|
478 } |
351b95be742b
Tests: fixed grpc tests hang in accept() on internal nginx error.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1319
diff
changeset
|
479 |
1303 | 480 log2c("(new connection $client)"); |
481 $n++; | |
482 | |
483 $client->sysread(my $buf, 24) == 24 or return; # preface | |
484 | |
485 $c = Test::Nginx::HTTP2->new(1, socket => $client, | |
486 pure => 1, preface => "") or return; | |
487 } | |
488 | |
489 my $frames = $c->read(all => [{ fin => 4 }]); | |
490 | |
491 if (!$extra{reuse}) { | |
492 $c->h2_settings(0); | |
493 $c->h2_settings(1); | |
494 } | |
495 | |
496 my ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; | |
497 $sid = $frame->{sid}; | |
498 return $frames; | |
499 }; | |
500 $f->{data} = sub { | |
501 my ($body, %extra) = @_; | |
502 $s->h2_body($body, { %extra }); | |
503 return $c->read(all => [{ sid => $sid, | |
504 length => length($body) }]); | |
505 }; | |
506 $f->{data_len} = sub { | |
507 my ($body, $len) = @_; | |
508 $s->h2_body($body) if defined $body; | |
509 return $c->read(all => [{ sid => $sid, length => $len }]); | |
510 }; | |
511 $f->{update} = sub { | |
512 $c->h2_window(shift); | |
513 }; | |
514 $f->{update_sid} = sub { | |
515 $c->h2_window(shift, $sid); | |
516 }; | |
517 $f->{http_end} = sub { | |
518 my (%extra) = @_; | |
519 $c->new_stream({ body_more => 1, %extra, headers => [ | |
520 { name => ':status', value => '200', | |
521 mode => $extra{mode} || 0 }, | |
522 { name => 'content-type', value => 'application/grpc', | |
523 mode => $extra{mode} || 1, huff => 1 }, | |
524 { name => 'x-connection', value => $n, | |
525 mode => 2, huff => 1 }, | |
526 ]}, $sid); | |
527 $c->h2_body('Hello world', { body_more => 1, | |
528 body_padding => $extra{body_padding} }); | |
529 $c->new_stream({ headers => [ | |
530 { name => 'grpc-status', value => '0', | |
531 mode => 2, huff => 1 }, | |
532 { name => 'grpc-message', value => '', | |
533 mode => 2, huff => 1 }, | |
534 ]}, $sid); | |
535 | |
536 return $s->read(all => [{ fin => 1 }]); | |
537 }; | |
1318
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
538 $f->{http_pres} = sub { |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
539 my (%extra) = @_; |
1319
3b30e97acdcb
Tests: made grpc preserve output test pass on win32 and Solaris.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1318
diff
changeset
|
540 $s->h2_settings(0, 0x4 => 8192); |
1318
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
541 $c->new_stream({ body_more => 1, %extra, headers => [ |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
542 { name => ':status', value => '200', |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
543 mode => $extra{mode} || 0 }, |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
544 { name => 'content-type', value => 'application/grpc', |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
545 mode => $extra{mode} || 1, huff => 1 }, |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
546 { name => 'x-connection', value => $n, |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
547 mode => 2, huff => 1 }, |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
548 ]}, $sid); |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
549 for (1 .. 20) { |
1319
3b30e97acdcb
Tests: made grpc preserve output test pass on win32 and Solaris.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1318
diff
changeset
|
550 $c->h2_body(sprintf('Hello %02d', $_) x 128, { |
1318
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
551 body_more => 1, |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
552 body_padding => $extra{body_padding} }); |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
553 $c->h2_ping("PING"); |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
554 } |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
555 # reopen window |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
556 $s->h2_window(2**24); |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
557 $s->h2_window(2**24, $csid); |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
558 $c->new_stream({ headers => [ |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
559 { name => 'grpc-status', value => '0', |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
560 mode => 2, huff => 1 }, |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
561 { name => 'grpc-message', value => '', |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
562 mode => 2, huff => 1 }, |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
563 ]}, $sid); |
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
564 |
1319
3b30e97acdcb
Tests: made grpc preserve output test pass on win32 and Solaris.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1318
diff
changeset
|
565 return $s->read(all => [{ sid => $csid, fin => 1 }]); |
1318
6de2a27af2d3
Tests: grpc preserve output tests added (ticket #1519).
Sergey Kandaurov <pluknet@nginx.com>
parents:
1312
diff
changeset
|
566 }; |
1303 | 567 $f->{http_err} = sub { |
568 $c->new_stream({ headers => [ | |
569 { name => ':status', value => '200', mode => 0 }, | |
570 { name => 'content-type', value => 'application/grpc', | |
571 mode => 1, huff => 1 }, | |
572 { name => 'grpc-status', value => '12', | |
573 mode => 2, huff => 1 }, | |
574 { name => 'grpc-message', value => 'unknown service', | |
575 mode => 2, huff => 1 }, | |
576 ]}, $sid); | |
577 | |
578 return $s->read(all => [{ fin => 1 }]); | |
579 }; | |
580 $f->{continuation} = sub { | |
581 $c->new_stream({ continuation => 1, body_more => 1, headers => [ | |
582 { name => ':status', value => '200', mode => 0 }, | |
583 ]}, $sid); | |
584 $c->h2_continue($sid, { continuation => 1, headers => [ | |
585 { name => 'content-type', value => 'application/grpc', | |
586 mode => 1, huff => 1 }, | |
587 ]}); | |
588 $c->h2_continue($sid, { headers => [ | |
589 # an empty CONTINUATION frame is legitimate | |
590 ]}); | |
591 $c->h2_body('Hello world', { body_more => 1 }); | |
592 $c->new_stream({ continuation => 1, headers => [ | |
593 { name => 'grpc-status', value => '0', | |
594 mode => 2, huff => 1 }, | |
595 ]}, $sid); | |
596 $c->h2_continue($sid, { headers => [ | |
597 { name => 'grpc-message', value => '', | |
598 mode => 2, huff => 1 }, | |
599 ]}); | |
600 | |
601 return $s->read(all => [{ fin => 1 }]); | |
602 }; | |
603 $f->{field_len} = sub { | |
604 my ($len) = @_; | |
605 $c->new_stream({ continuation => [map {2**14} (0..$len/2**13)], | |
606 body_more => 1, headers => [ | |
607 { name => ':status', value => '200', mode => 0 }, | |
608 { name => 'content-type', value => 'application/grpc', | |
609 mode => 1, huff => 1 }, | |
610 { name => 'x' x $len, value => 'y' x $len, mode => 6 }, | |
611 ]}, $sid); | |
612 $c->h2_body('Hello world', { body_more => 1 }); | |
613 $c->new_stream({ headers => [ | |
614 { name => 'grpc-status', value => '0', | |
615 mode => 2, huff => 1 }, | |
616 { name => 'grpc-message', value => '', | |
617 mode => 2, huff => 1 }, | |
618 ]}, $sid); | |
619 | |
620 return $s->read(all => [{ fin => 1 }]); | |
621 }; | |
622 return $f; | |
623 } | |
624 | |
625 sub log2i { Test::Nginx::log_core('|| <<', @_); } | |
626 sub log2o { Test::Nginx::log_core('|| >>', @_); } | |
627 sub log2c { Test::Nginx::log_core('||', @_); } | |
628 | |
629 ############################################################################### |