Mercurial > hg > nginx-tests
annotate spdy.t @ 406:811fbc213fd8
Tests: sub filter tests with subrequests.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Thu, 22 May 2014 21:07:00 +0400 |
parents | 847ea345becb |
children | 5c25acbc870a |
rev | line source |
---|---|
374 | 1 #!/usr/bin/perl |
2 | |
3 # (C) Sergey Kandaurov | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Tests for SPDY protocol version 3.1. | |
7 | |
8 ############################################################################### | |
9 | |
10 use warnings; | |
11 use strict; | |
12 | |
13 use Test::More; | |
14 | |
15 use IO::Select; | |
16 | |
17 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
18 | |
19 use lib 'lib'; | |
20 use Test::Nginx; | |
21 | |
22 ############################################################################### | |
23 | |
24 select STDERR; $| = 1; | |
25 select STDOUT; $| = 1; | |
26 | |
27 eval { | |
28 require Compress::Raw::Zlib; | |
29 Compress::Raw::Zlib->Z_OK; | |
30 Compress::Raw::Zlib->Z_SYNC_FLUSH; | |
31 Compress::Raw::Zlib->Z_NO_COMPRESSION; | |
32 Compress::Raw::Zlib->WANT_GZIP_OR_ZLIB; | |
33 }; | |
34 plan(skip_all => 'Compress::Raw::Zlib not installed') if $@; | |
35 plan(skip_all => 'win32') if $^O eq 'MSWin32'; | |
36 | |
37 my $t = Test::Nginx->new()->has(qw/http proxy cache limit_conn rewrite spdy/); | |
38 | |
39 $t->plan(72)->write_file_expand('nginx.conf', <<'EOF'); | |
40 | |
41 %%TEST_GLOBALS%% | |
42 | |
43 daemon off; | |
44 | |
45 events { | |
46 } | |
47 | |
48 http { | |
49 %%TEST_GLOBALS_HTTP%% | |
50 | |
51 proxy_cache_path %%TESTDIR%%/cache keys_zone=NAME:10m; | |
52 limit_conn_zone $binary_remote_addr zone=conn:1m; | |
53 | |
54 server { | |
55 listen 127.0.0.1:8080 spdy; | |
56 listen 127.0.0.1:8081; | |
57 server_name localhost; | |
58 | |
59 location /s { | |
60 add_header X-Header X-Foo; | |
61 return 200 'body'; | |
62 } | |
63 location /spdy { | |
64 return 200 $spdy; | |
65 } | |
66 location /prio { | |
67 return 200 $spdy_request_priority; | |
68 } | |
69 location /chunk_size { | |
70 spdy_chunk_size 1; | |
71 return 200 'body'; | |
72 } | |
73 location /redirect { | |
74 error_page 405 /s; | |
75 return 405; | |
76 } | |
77 location /proxy { | |
78 add_header X-Body "$request_body"; | |
79 proxy_pass http://127.0.0.1:8081/; | |
80 proxy_cache NAME; | |
81 proxy_cache_valid 1m; | |
82 } | |
83 location /t3.html { | |
84 limit_conn conn 1; | |
85 } | |
86 } | |
87 } | |
88 | |
89 EOF | |
90 | |
91 $t->run(); | |
92 | |
93 # file size is slightly beyond initial window size: 2**16 + 80 bytes | |
94 | |
95 $t->write_file('t1.html', | |
96 join('', map { sprintf "X%04dXXX", $_ } (1 .. 8202))); | |
97 | |
98 $t->write_file('t2.html', 'SEE-THIS'); | |
99 $t->write_file('t3.html', 'SEE-THIS'); | |
100 | |
101 my %cframe = ( | |
102 2 => \&syn_reply, | |
103 3 => \&rst_stream, | |
104 4 => \&settings, | |
105 6 => \&ping, | |
106 7 => \&goaway, | |
107 9 => \&window_update | |
108 ); | |
109 | |
110 ############################################################################### | |
111 | |
112 # PING | |
113 | |
114 my $sess = new_session(); | |
115 spdy_ping($sess, 0x12345678); | |
116 my $frames = spdy_read($sess); | |
117 | |
118 my ($frame) = grep { $_->{type} eq "PING" } @$frames; | |
119 ok($frame, 'PING frame'); | |
120 is($frame->{value}, 0x12345678, 'PING payload'); | |
121 | |
122 # GET | |
123 | |
124 $sess = new_session(); | |
125 my $sid1 = spdy_stream($sess, { path => '/s' }); | |
126 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
127 | |
128 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; | |
129 ok($frame, 'SYN_REPLAY frame'); | |
130 is($frame->{sid}, $sid1, 'SYN_REPLAY stream'); | |
131 is($frame->{headers}->{':status'}, 200, 'SYN_REPLAY status'); | |
132 is($frame->{headers}->{'x-header'}, 'X-Foo', 'SYN_REPLAY header'); | |
133 | |
134 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
135 ok($frame, 'DATA frame'); | |
136 is($frame->{length}, length 'body', 'DATA length'); | |
137 is($frame->{data}, 'body', 'DATA payload'); | |
138 | |
139 # GET in new SPDY stream in same session | |
140 | |
141 my $sid2 = spdy_stream($sess, { path => '/s' }); | |
142 $frames = spdy_read($sess, all => [{ sid => $sid2, fin => 1 }]); | |
143 | |
144 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; | |
145 is($frame->{sid}, $sid2, 'SYN_REPLAY stream 2'); | |
146 is($frame->{headers}->{':status'}, 200, 'SYN_REPLAY status 2'); | |
147 is($frame->{headers}->{'x-header'}, 'X-Foo', 'SYN_REPLAY header 2'); | |
148 | |
149 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
150 ok($frame, 'DATA frame 2'); | |
151 is($frame->{sid}, $sid2, 'SYN_REPLAY stream 2'); | |
152 is($frame->{length}, length 'body', 'DATA length 2'); | |
153 is($frame->{data}, 'body', 'DATA payload 2'); | |
154 | |
155 # HEAD | |
156 | |
157 $sess = new_session(); | |
158 $sid1 = spdy_stream($sess, { path => '/s', method => 'HEAD' }); | |
159 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
160 | |
161 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; | |
162 is($frame->{sid}, $sid1, 'SYN_REPLAY stream HEAD'); | |
163 is($frame->{headers}->{':status'}, 200, 'SYN_REPLAY status HEAD'); | |
164 is($frame->{headers}->{'x-header'}, 'X-Foo', 'SYN_REPLAY header HEAD'); | |
165 | |
166 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
167 is($frame, undef, 'HEAD no body'); | |
168 | |
169 # request header | |
170 | |
171 $sess = new_session(); | |
172 $sid1 = spdy_stream($sess, { path => '/t1.html', | |
173 headers => { "range" => "bytes=10-19" } | |
174 }); | |
175 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
176 | |
177 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; | |
178 is($frame->{headers}->{':status'}, 206, 'SYN_REPLAY status range'); | |
179 | |
180 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
181 is($frame->{length}, 10, 'DATA length range'); | |
182 is($frame->{data}, '002XXXX000', 'DATA payload range'); | |
183 | |
184 # $spdy | |
185 | |
186 $sess = new_session(); | |
187 $sid1 = spdy_stream($sess, { path => '/spdy' }); | |
188 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
189 | |
190 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
191 is($frame->{data}, '3.1', 'spdy variable'); | |
192 | |
193 # spdy_chunk_size=1 | |
194 | |
195 $sess = new_session(); | |
196 $sid1 = spdy_stream($sess, { path => '/chunk_size' }); | |
197 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
198 | |
199 my @data = grep { $_->{type} eq "DATA" } @$frames; | |
200 is(@data, 4, 'chunk_size body chunks'); | |
201 is($data[0]->{data}, 'b', 'chunk_size body 1'); | |
202 is($data[1]->{data}, 'o', 'chunk_size body 2'); | |
203 is($data[2]->{data}, 'd', 'chunk_size body 3'); | |
204 is($data[3]->{data}, 'y', 'chunk_size body 4'); | |
205 | |
206 # redirect | |
207 | |
208 $sess = new_session(); | |
209 $sid1 = spdy_stream($sess, { path => '/redirect' }); | |
210 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
211 | |
212 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; | |
213 is($frame->{headers}->{':status'}, 405, 'SYN_REPLAY status with redirect'); | |
214 | |
215 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
216 ok($frame, 'DATA frame with redirect'); | |
217 is($frame->{data}, 'body', 'DATA payload with redirect'); | |
218 | |
219 # ensure that HEAD-like requests, i.e., without response body, do not lead to | |
220 # client connection close due to cache filling up with upstream response body | |
221 | |
222 TODO: { | |
223 local $TODO = 'premature client connection close'; | |
224 | |
225 $sess = new_session(); | |
226 $sid1 = spdy_stream($sess, { path => '/proxy/t2.html', method => 'HEAD' }); | |
227 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
228 | |
229 $sid2 = spdy_stream($sess, { path => '/' }); | |
230 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
231 ok(grep ({ $_->{type} eq "SYN_REPLY" } @$frames), 'proxy cache headers only'); | |
232 | |
233 } | |
234 | |
235 # simple proxy cache test | |
236 | |
237 $sess = new_session(); | |
238 $sid1 = spdy_stream($sess, { path => '/proxy/t2.html' }); | |
239 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
240 | |
241 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; | |
242 is($frame->{headers}->{':status'}, '200 OK', 'proxy cache unconditional'); | |
243 | |
376
ab2d8abea393
Tests: skip proxy cache conditional test if we didn't get etag.
Sergey Kandaurov <pluknet@nginx.com>
parents:
374
diff
changeset
|
244 my $etag = $frame->{headers}->{'etag'}; |
ab2d8abea393
Tests: skip proxy cache conditional test if we didn't get etag.
Sergey Kandaurov <pluknet@nginx.com>
parents:
374
diff
changeset
|
245 |
ab2d8abea393
Tests: skip proxy cache conditional test if we didn't get etag.
Sergey Kandaurov <pluknet@nginx.com>
parents:
374
diff
changeset
|
246 SKIP: { |
ab2d8abea393
Tests: skip proxy cache conditional test if we didn't get etag.
Sergey Kandaurov <pluknet@nginx.com>
parents:
374
diff
changeset
|
247 skip 'no etag', 1 unless defined $etag; |
374 | 248 |
249 $sid2 = spdy_stream($sess, { path => '/proxy/t2.html', | |
376
ab2d8abea393
Tests: skip proxy cache conditional test if we didn't get etag.
Sergey Kandaurov <pluknet@nginx.com>
parents:
374
diff
changeset
|
250 headers => { "if-none-match" => $etag } |
374 | 251 }); |
252 $frames = spdy_read($sess, all => [{ sid => $sid2, fin => 1 }]); | |
253 | |
254 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; | |
255 is($frame->{headers}->{':status'}, 304, 'proxy cache conditional'); | |
256 | |
376
ab2d8abea393
Tests: skip proxy cache conditional test if we didn't get etag.
Sergey Kandaurov <pluknet@nginx.com>
parents:
374
diff
changeset
|
257 } |
ab2d8abea393
Tests: skip proxy cache conditional test if we didn't get etag.
Sergey Kandaurov <pluknet@nginx.com>
parents:
374
diff
changeset
|
258 |
374 | 259 # request body (uses proxied response) |
260 | |
261 $sess = new_session(); | |
262 $sid1 = spdy_stream($sess, { path => '/proxy/t2.html', body => 'TEST' }); | |
263 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
264 | |
265 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; | |
266 is($frame->{headers}->{'x-body'}, 'TEST', 'request body'); | |
267 | |
268 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
269 is($frame->{length}, length 'SEE-THIS', 'proxied response length'); | |
270 is($frame->{data}, 'SEE-THIS', 'proxied response'); | |
271 | |
272 # WINDOW_UPDATE (client side) | |
273 | |
274 $sess = new_session(); | |
275 $sid1 = spdy_stream($sess, { path => '/t1.html' }); | |
276 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 0 }]); | |
277 | |
278 @data = grep { $_->{type} eq "DATA" } @$frames; | |
279 my $sum = eval join '+', map { $_->{length} } @data; | |
280 is($sum, 2**16, 'iws - stream blocked on initial window size'); | |
281 | |
282 spdy_ping($sess, 0xf00ff00f); | |
283 $frames = spdy_read($sess); | |
284 | |
285 ($frame) = grep { $_->{type} eq "PING" } @$frames; | |
286 ok($frame, 'iws - PING not blocked'); | |
287 | |
288 spdy_window($sess, 2**16, $sid1); | |
289 $frames = spdy_read($sess); | |
290 is(@$frames, 0, 'iws - updated stream window'); | |
291 | |
292 spdy_window($sess, 2**16); | |
293 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
294 | |
295 @data = grep { $_->{type} eq "DATA" } @$frames; | |
296 $sum = eval join '+', map { $_->{length} } @data; | |
297 is($sum, 80, 'iws - updated connection window'); | |
298 | |
299 # SETTINGS (initial window size, client side) | |
300 | |
301 $sess = new_session(); | |
302 spdy_settings($sess, 7 => 2**17); | |
303 spdy_window($sess, 2**17); | |
304 | |
305 $sid1 = spdy_stream($sess, { path => '/t1.html' }); | |
306 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
307 | |
308 @data = grep { $_->{type} eq "DATA" } @$frames; | |
309 $sum = eval join '+', map { $_->{length} } @data; | |
310 is($sum, 2**16 + 80, 'increased initial window size'); | |
311 | |
312 # probe for negative available space in a flow control window | |
313 | |
314 $sess = new_session(); | |
315 $sid1 = spdy_stream($sess, { path => '/t1.html' }); | |
316 spdy_read($sess, all => [{ sid => $sid1, fin => 0 }]); | |
317 | |
318 spdy_window($sess, 1); | |
319 spdy_settings($sess, 7 => 42); | |
320 spdy_window($sess, 1024, $sid1); | |
321 | |
322 $frames = spdy_read($sess); | |
323 is(@$frames, 0, 'negative window - no data'); | |
324 | |
325 spdy_window($sess, 2**16 - 42 - 1024, $sid1); | |
326 $frames = spdy_read($sess); | |
327 is(@$frames, 0, 'zero window - no data'); | |
328 | |
329 spdy_window($sess, 1, $sid1); | |
330 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 0 }]); | |
331 is(@$frames, 1, 'positive window - data'); | |
332 is(@$frames[0]->{length}, 1, 'positive window - data length'); | |
333 | |
334 # stream multiplexing | |
335 | |
336 $sess = new_session(); | |
337 $sid1 = spdy_stream($sess, { path => '/t1.html' }); | |
338 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 0 }]); | |
339 | |
340 @data = grep { $_->{type} eq "DATA" } @$frames; | |
341 $sum = eval join '+', map { $_->{length} } @data; | |
342 is($sum, 2**16, 'multiple - stream1 data'); | |
343 | |
344 $sid2 = spdy_stream($sess, { path => '/t1.html' }); | |
345 $frames = spdy_read($sess, all => [{ sid => $sid2, fin => 0 }]); | |
346 | |
347 @data = grep { $_->{type} eq "DATA" } @$frames; | |
348 is(@data, 0, 'multiple - stream2 no data'); | |
349 | |
350 spdy_window($sess, 2**17, $sid1); | |
351 spdy_window($sess, 2**17, $sid2); | |
352 spdy_window($sess, 2**17); | |
353 | |
354 $frames = spdy_read($sess, all => [ | |
355 { sid => $sid1, fin => 1 }, | |
356 { sid => $sid2, fin => 1 } | |
357 ]); | |
358 | |
359 @data = grep { $_->{type} eq "DATA" && $_->{sid} == $sid1 } @$frames; | |
360 $sum = eval join '+', map { $_->{length} } @data; | |
361 is($sum, 80, 'multiple - stream1 remain data'); | |
362 | |
363 @data = grep { $_->{type} eq "DATA" && $_->{sid} == $sid2 } @$frames; | |
364 $sum = eval join '+', map { $_->{length} } @data; | |
365 is($sum, 2**16 + 80, 'multiple - stream2 full data'); | |
366 | |
367 # request priority parsing in $spdy_request_priority | |
368 | |
369 $sess = new_session(); | |
370 $sid1 = spdy_stream($sess, { path => '/prio', prio => 0 }); | |
371 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
372 | |
373 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
374 is($frame->{data}, 0, 'priority 0'); | |
375 | |
376 $sid1 = spdy_stream($sess, { path => '/prio', prio => 1 }); | |
377 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
378 | |
379 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
380 is($frame->{data}, 1, 'priority 1'); | |
381 | |
382 $sid1 = spdy_stream($sess, { path => '/prio', prio => 7 }); | |
383 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
384 | |
385 ($frame) = grep { $_->{type} eq "DATA" } @$frames; | |
386 is($frame->{data}, 7, 'priority 7'); | |
387 | |
388 # stream muliplexing + priority | |
389 | |
390 $sess = new_session(); | |
391 $sid1 = spdy_stream($sess, { path => '/t1.html', prio => 7 }); | |
392 $sid2 = spdy_stream($sess, { path => '/t2.html', prio => 0 }); | |
393 spdy_read($sess); | |
394 | |
395 spdy_window($sess, 2**17, $sid1); | |
396 spdy_window($sess, 2**17, $sid2); | |
397 spdy_window($sess, 2**17); | |
398 | |
399 $frames = spdy_read($sess, all => [ | |
400 { sid => $sid1, fin => 1 }, | |
401 { sid => $sid2, fin => 1 } | |
402 ]); | |
403 | |
404 @data = grep { $_->{type} eq "DATA" } @$frames; | |
405 is(join (' ', map { $_->{sid} } @data), "$sid2 $sid1", 'multiple priority 1'); | |
406 | |
407 # and vice versa | |
408 | |
409 $sess = new_session(); | |
410 $sid1 = spdy_stream($sess, { path => '/t1.html', prio => 0 }); | |
411 $sid2 = spdy_stream($sess, { path => '/t2.html', prio => 7 }); | |
412 spdy_read($sess); | |
413 | |
414 spdy_window($sess, 2**17, $sid1); | |
415 spdy_window($sess, 2**17, $sid2); | |
416 spdy_window($sess, 2**17); | |
417 | |
418 $frames = spdy_read($sess, all => [ | |
419 { sid => $sid1, fin => 1 }, | |
420 { sid => $sid2, fin => 1 } | |
421 ]); | |
422 | |
423 @data = grep { $_->{type} eq "DATA" } @$frames; | |
424 is(join (' ', map { $_->{sid} } @data), "$sid1 $sid2", 'multiple priority 2'); | |
425 | |
426 # limit_conn | |
427 | |
428 $sess = new_session(); | |
429 spdy_settings($sess, 7 => 1); | |
430 $sid1 = spdy_stream($sess, { path => '/t3.html' }); | |
431 $sid2 = spdy_stream($sess, { path => '/t3.html' }); | |
432 $frames = spdy_read($sess, all => [ | |
433 { sid => $sid1, fin => 0 }, | |
434 { sid => $sid2, fin => 0 } | |
435 ]); | |
436 | |
437 ($frame) = grep { $_->{type} eq "SYN_REPLY" && $_->{sid} == $sid1 } @$frames; | |
438 is($frame->{headers}->{':status'}, 200, 'conn_limit 1'); | |
439 | |
440 ($frame) = grep { $_->{type} eq "SYN_REPLY" && $_->{sid} == $sid2 } @$frames; | |
441 is($frame->{headers}->{':status'}, 503, 'conn_limit 2'); | |
442 | |
443 # limit_conn + client's RST_STREAM | |
444 | |
445 $sess = new_session(); | |
446 spdy_settings($sess, 7 => 1); | |
447 $sid1 = spdy_stream($sess, { path => '/t3.html' }); | |
448 spdy_rst($sess, $sid1, 5); | |
449 $sid2 = spdy_stream($sess, { path => '/t3.html' }); | |
450 $frames = spdy_read($sess, all => [ | |
451 { sid => $sid1, fin => 0 }, | |
452 { sid => $sid2, fin => 0 } | |
453 ]); | |
454 | |
455 ($frame) = grep { $_->{type} eq "SYN_REPLY" && $_->{sid} == $sid1 } @$frames; | |
456 is($frame->{headers}->{':status'}, 200, 'RST_STREAM 1'); | |
457 | |
458 ($frame) = grep { $_->{type} eq "SYN_REPLY" && $_->{sid} == $sid2 } @$frames; | |
459 is($frame->{headers}->{':status'}, 200, 'RST_STREAM 2'); | |
460 | |
461 # GOAWAY on SYN_STREAM with even StreamID | |
462 | |
463 TODO: { | |
464 local $TODO = 'not yet'; | |
465 | |
466 $sess = new_session(); | |
467 spdy_stream($sess, { path => '/s' }, 2); | |
468 $frames = spdy_read($sess); | |
469 | |
470 ($frame) = grep { $_->{type} eq "GOAWAY" } @$frames; | |
471 ok($frame, 'even stream - GOAWAY frame'); | |
472 is($frame->{code}, 1, 'even stream - error code'); | |
473 is($frame->{sid}, 0, 'even stream - last used stream'); | |
474 | |
475 } | |
476 | |
477 # GOAWAY on SYN_STREAM with backward StreamID | |
478 | |
479 TODO: { | |
480 local $TODO = 'not yet'; | |
481 | |
482 $sess = new_session(); | |
483 $sid1 = spdy_stream($sess, { path => '/s' }, 3); | |
484 spdy_read($sess); | |
485 | |
486 $sid2 = spdy_stream($sess, { path => '/s' }, 1); | |
487 $frames = spdy_read($sess); | |
488 | |
489 ($frame) = grep { $_->{type} eq "GOAWAY" } @$frames; | |
490 ok($frame, 'backward stream - GOAWAY frame'); | |
491 is($frame->{code}, 1, 'backward stream - error code'); | |
492 is($frame->{sid}, $sid1, 'backward stream - last used stream'); | |
493 | |
494 } | |
495 | |
496 # RST_STREAM on the second SYN_STREAM with same StreamID | |
497 | |
498 TODO: { | |
499 local $TODO = 'not yet'; | |
500 | |
501 $sess = new_session(); | |
502 $sid1 = spdy_stream($sess, { path => '/s' }, 3); | |
503 spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
504 $sid2 = spdy_stream($sess, { path => '/s' }, 3); | |
505 $frames = spdy_read($sess); | |
506 | |
507 ($frame) = grep { $_->{type} eq "RST_STREAM" } @$frames; | |
508 ok($frame, 'dup stream - RST_STREAM frame'); | |
509 is($frame->{code}, 1, 'dup stream - error code'); | |
510 is($frame->{sid}, $sid1, 'dup stream - stream'); | |
511 | |
512 } | |
513 | |
514 # awkward protocol version | |
515 | |
516 $sess = new_session(); | |
517 $sid1 = spdy_stream($sess, { path => '/s', version => 'HTTP/1.10' }); | |
518 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
519 | |
520 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; | |
521 is($frame->{headers}->{':status'}, 200, 'awkward version'); | |
522 | |
523 # missing mandatory request header | |
524 | |
525 $sess = new_session(); | |
526 $sid1 = spdy_stream($sess, { path => '/s', version => '' }); | |
527 $frames = spdy_read($sess, all => [{ sid => $sid1, fin => 1 }]); | |
528 | |
529 ($frame) = grep { $_->{type} eq "SYN_REPLY" } @$frames; | |
530 is($frame->{headers}->{':status'}, 400, 'incomplete headers'); | |
531 | |
532 # GOAWAY before closing a connection by server | |
533 | |
534 $t->stop(); | |
535 | |
536 TODO: { | |
537 local $TODO = 'not yet'; | |
538 | |
539 $frames = spdy_read($sess); | |
540 | |
541 ($frame) = grep { $_->{type} eq "GOAWAY" } @$frames; | |
542 ok($frame, 'GOAWAY on connection close'); | |
543 | |
544 } | |
545 | |
546 ############################################################################### | |
547 | |
548 sub spdy_ping { | |
549 my ($sess, $payload) = @_; | |
550 | |
551 raw_write($sess->{socket}, pack("N3", 0x80030006, 0x4, $payload)); | |
552 } | |
553 | |
554 sub spdy_rst { | |
555 my ($sess, $sid, $error) = @_; | |
556 | |
557 raw_write($sess->{socket}, pack("N4", 0x80030003, 0x8, $sid, $error)); | |
558 } | |
559 | |
560 sub spdy_window { | |
561 my ($sess, $win, $stream) = @_; | |
562 | |
563 $stream = 0 unless defined $stream; | |
564 raw_write($sess->{socket}, pack("N4", 0x80030009, 8, $stream, $win)); | |
565 } | |
566 | |
567 sub spdy_settings { | |
568 my ($sess, %extra) = @_; | |
569 | |
570 my $cnt = keys %extra; | |
571 my $len = 4 + 8 * $cnt; | |
572 | |
573 my $buf = pack "N3", 0x80030004, $len, $cnt; | |
574 $buf .= join '', map { pack "N2", $_, $extra{$_} } keys %extra; | |
575 raw_write($sess->{socket}, $buf); | |
576 } | |
577 | |
578 sub spdy_read { | |
579 my ($sess, %extra) = @_; | |
377
ba95a443ff1f
Tests: improved spdy test robustness.
Sergey Kandaurov <pluknet@nginx.com>
parents:
376
diff
changeset
|
580 my ($skip, $length, $buf, @got); |
ba95a443ff1f
Tests: improved spdy test robustness.
Sergey Kandaurov <pluknet@nginx.com>
parents:
376
diff
changeset
|
581 my $tries = 0; |
ba95a443ff1f
Tests: improved spdy test robustness.
Sergey Kandaurov <pluknet@nginx.com>
parents:
376
diff
changeset
|
582 my $maxtried = 3; |
374 | 583 |
584 again: | |
377
ba95a443ff1f
Tests: improved spdy test robustness.
Sergey Kandaurov <pluknet@nginx.com>
parents:
376
diff
changeset
|
585 do { |
ba95a443ff1f
Tests: improved spdy test robustness.
Sergey Kandaurov <pluknet@nginx.com>
parents:
376
diff
changeset
|
586 $buf = raw_read($sess->{socket}); |
ba95a443ff1f
Tests: improved spdy test robustness.
Sergey Kandaurov <pluknet@nginx.com>
parents:
376
diff
changeset
|
587 } until (defined $buf || $tries++ >= $maxtried); |
374 | 588 |
379
f42de3a9fd74
Tests: avoid uninitialized warnings in spdy.t.
Maxim Dounin <mdounin@mdounin.ru>
parents:
377
diff
changeset
|
589 $buf = '' if !defined $buf; |
f42de3a9fd74
Tests: avoid uninitialized warnings in spdy.t.
Maxim Dounin <mdounin@mdounin.ru>
parents:
377
diff
changeset
|
590 |
374 | 591 for ($skip = 0; $skip < length $buf; $skip += $length + 8) { |
592 my $type = unpack("\@$skip B", $buf); | |
593 $length = hex unpack("\@$skip x5 H6", $buf); | |
594 if ($type == 0) { | |
595 push @got, dframe($skip, $buf); | |
596 test_fin($got[-1], $extra{all}); | |
597 next; | |
598 } | |
599 | |
600 my $ctype = unpack("\@$skip x2 n", $buf); | |
601 push @got, $cframe{$ctype}($sess, $skip, $buf); | |
602 test_fin($got[-1], $extra{all}); | |
603 } | |
377
ba95a443ff1f
Tests: improved spdy test robustness.
Sergey Kandaurov <pluknet@nginx.com>
parents:
376
diff
changeset
|
604 goto again if %extra && @{$extra{all}} && $tries < $maxtried; |
374 | 605 return \@got; |
606 } | |
607 | |
608 sub test_fin { | |
609 my ($frame, $all) = @_; | |
610 | |
611 @{$all} = grep { | |
612 !($_->{sid} == $frame->{sid} && $_->{fin} == $frame->{fin}) | |
613 } @{$all} if defined $frame->{fin}; | |
614 } | |
615 | |
616 sub dframe { | |
617 my ($skip, $buf) = @_; | |
618 my %frame; | |
619 | |
620 my $stream = unpack "\@$skip B32", $buf; $skip += 4; | |
621 substr($stream, 0, 1) = 0; | |
622 $stream = unpack("N", pack("B32", $stream)); | |
623 $frame{sid} = $stream; | |
624 | |
625 my $flags = unpack "\@$skip B8", $buf; $skip += 1; | |
626 $frame{fin} = substr($flags, 7, 1); | |
627 | |
628 my $length = hex (unpack "\@$skip H6", $buf); $skip += 3; | |
629 $frame{length} = $length; | |
630 | |
631 $frame{data} = substr($buf, $skip, $length); | |
632 $frame{type} = "DATA"; | |
633 return \%frame; | |
634 } | |
635 | |
636 sub spdy_stream { | |
637 my ($ctx, $uri, $stream) = @_; | |
638 my ($input, $output, $buf); | |
639 my ($d, $status); | |
640 | |
641 my $host = $uri->{host} || '127.0.0.1:8080'; | |
642 my $method = $uri->{method} || 'GET'; | |
643 my $headers = $uri->{headers} || {}; | |
644 my $body = $uri->{body}; | |
645 my $prio = defined $uri->{prio} ? $uri->{prio} : 4; | |
646 my $version = defined $uri->{version} ? $uri->{version} : "HTTP/1.1"; | |
647 | |
648 if ($stream) { | |
649 $ctx->{last_stream} = $stream; | |
650 } else { | |
651 $ctx->{last_stream} += 2; | |
652 } | |
653 | |
654 $buf = pack("NC", 0x80030001, not $body); | |
655 $buf .= pack("xxx"); # Length stub | |
656 $buf .= pack("N", $ctx->{last_stream}); # Stream-ID | |
657 $buf .= pack("N", 0); # Assoc. Stream-ID | |
658 $buf .= pack("n", $prio << 13); | |
659 | |
660 my $ent = 4 + keys %{$headers}; | |
661 $ent++ if $body; | |
662 $ent++ if $version; | |
663 | |
664 $input = pack("N", $ent); | |
665 $input .= hpack(":host", $host); | |
666 $input .= hpack(":method", $method); | |
667 $input .= hpack(":path", $uri->{path}); | |
668 $input .= hpack(":scheme", "http"); | |
669 if ($version) { | |
670 $input .= hpack(":version", $version); | |
671 } | |
672 if ($body) { | |
673 $input .= hpack("content-length", length $body); | |
674 } | |
675 $input .= join '', map { hpack($_, $headers->{$_}) } keys %{$headers}; | |
676 | |
677 $d = $ctx->{zlib}->{d}; | |
678 $status = $d->deflate($input => \my $start); | |
679 $status == Compress::Raw::Zlib->Z_OK or fail "deflate failed"; | |
680 $status = $d->flush(\my $tail => Compress::Raw::Zlib->Z_SYNC_FLUSH); | |
681 $status == Compress::Raw::Zlib->Z_OK or fail "flush failed"; | |
682 $output = $start . $tail; | |
683 | |
684 my $len = ''; | |
685 vec($len, 7, 8) = (length $output) + 10; | |
686 $buf |= $len; | |
687 $buf .= $output; | |
688 | |
689 if (defined $body) { | |
690 $buf .= pack "NCxn", $ctx->{last_stream}, 0x01, length $body; | |
691 $buf .= $body; | |
692 } | |
693 | |
694 raw_write($ctx->{socket}, $buf); | |
695 return $ctx->{last_stream}; | |
696 } | |
697 | |
698 sub syn_reply { | |
699 my ($ctx, $skip, $buf) = @_; | |
700 my ($i, $status); | |
701 my %payload; | |
702 | |
703 $skip += 4; | |
704 my $flags = unpack "\@$skip B8", $buf; $skip += 1; | |
705 $payload{fin} = substr($flags, 7, 1); | |
706 | |
707 my $length = hex unpack "\@$skip H6", $buf; $skip += 3; | |
708 $payload{length} = $length; | |
709 $payload{type} = 'SYN_REPLY'; | |
710 | |
711 my $stream = unpack "\@$skip B32", $buf; $skip += 4; | |
712 substr($stream, 0, 1) = 0; | |
713 $stream = unpack("N", pack("B32", $stream)); | |
714 $payload{sid} = $stream; | |
715 | |
716 my $input = substr($buf, $skip, $length - 4); | |
717 $i = $ctx->{zlib}->{i}; | |
718 | |
719 $status = $i->inflate($input => \my $out); | |
720 fail "Failed: $status" unless $status == Compress::Raw::Zlib->Z_OK; | |
721 $payload{headers} = hunpack($out); | |
722 return \%payload; | |
723 } | |
724 | |
725 sub rst_stream { | |
726 my ($ctx, $skip, $buf) = @_; | |
727 my %payload; | |
728 | |
729 $skip += 5; | |
730 $payload{length} = hex(unpack "\@$skip H6", $buf); $skip += 3; | |
731 $payload{type} = 'RST_STREAM'; | |
732 $payload{sid} = unpack "\@$skip N", $buf; $skip += 4; | |
733 $payload{code} = unpack "\@$skip N", $buf; | |
734 return \%payload; | |
735 } | |
736 | |
737 sub settings { | |
738 my ($ctx, $skip, $buf) = @_; | |
739 my %payload; | |
740 | |
741 $skip += 4; | |
742 $payload{flags} = unpack "\@$skip H", $buf; $skip += 1; | |
743 $payload{length} = hex(unpack "\@$skip H6", $buf); $skip += 3; | |
744 $payload{type} = 'SETTINGS'; | |
745 | |
746 my $nent = unpack "\@$skip N", $buf; $skip += 4; | |
747 for (1 .. $nent) { | |
748 my $flags = hex unpack "\@$skip H2", $buf; $skip += 1; | |
749 my $id = hex unpack "\@$skip H6", $buf; $skip += 3; | |
750 $payload{$id}{flags} = $flags; | |
751 $payload{$id}{value} = unpack "\@$skip N", $buf; $skip += 4; | |
752 } | |
753 return \%payload; | |
754 } | |
755 | |
756 sub ping { | |
757 my ($ctx, $skip, $buf) = @_; | |
758 my %payload; | |
759 | |
760 $skip += 5; | |
761 $payload{length} = hex(unpack "\@$skip H6", $buf); $skip += 3; | |
762 $payload{type} = 'PING'; | |
763 $payload{value} = unpack "\@$skip N", $buf; | |
764 return \%payload; | |
765 } | |
766 | |
767 sub goaway { | |
768 my ($ctx, $skip, $buf) = @_; | |
769 my %payload; | |
770 | |
771 $skip += 5; | |
772 $payload{length} = hex unpack "\@$skip H6", $buf; $skip += 3; | |
773 $payload{type} = 'GOAWAY'; | |
774 $payload{sid} = unpack "\@$skip N", $buf; $skip += 4; | |
775 $payload{code} = unpack "\@$skip N", $buf; | |
776 return \%payload; | |
777 } | |
778 | |
779 sub window_update { | |
780 my ($ctx, $skip, $buf) = @_; | |
781 my %payload; | |
782 | |
783 $skip += 5; | |
784 | |
785 $payload{length} = hex(unpack "\@$skip H6", $buf); $skip += 3; | |
786 $payload{type} = 'WINDOW_UPDATE'; | |
787 | |
788 my $stream = unpack "\@$skip B32", $buf; $skip += 4; | |
789 substr($stream, 0, 1) = 0; | |
790 $stream = unpack("N", pack("B32", $stream)); | |
791 $payload{sid} = $stream; | |
792 | |
793 my $value = unpack "\@$skip B32", $buf; | |
794 substr($value, 0, 1) = 0; | |
795 $payload{wdelta} = unpack("N", pack("B32", $value)); | |
796 return \%payload; | |
797 } | |
798 | |
799 sub hpack { | |
800 my ($name, $value) = @_; | |
801 | |
802 pack("N", length($name)) . $name . pack("N", length($value)) . $value; | |
803 } | |
804 | |
805 sub hunpack { | |
806 my ($data) = @_; | |
807 my %headers; | |
808 my $skip = 0; | |
809 | |
810 my $nent = unpack "\@$skip N", $data; $skip += 4; | |
811 for (1 .. $nent) { | |
812 my $len = unpack("\@$skip N", $data); $skip += 4; | |
813 my $name = unpack("\@$skip A$len", $data); $skip += $len; | |
814 | |
815 $len = unpack("\@$skip N", $data); $skip += 4; | |
816 my $value = unpack("\@$skip A$len", $data); $skip += $len; | |
817 | |
818 $headers{$name} = $value; | |
819 } | |
820 return \%headers; | |
821 } | |
822 | |
823 sub raw_read { | |
824 my ($s) = @_; | |
825 my ($got, $buf); | |
826 | |
827 $s->blocking(0); | |
828 while (IO::Select->new($s)->can_read(0.4)) { | |
829 my $n = $s->sysread($buf, 1024); | |
830 last unless $n; | |
831 $got .= $buf; | |
832 }; | |
833 log_in($got); | |
834 return $got; | |
835 } | |
836 | |
837 sub raw_write { | |
838 my ($s, $message) = @_; | |
839 | |
840 local $SIG{PIPE} = 'IGNORE'; | |
841 | |
842 $s->blocking(0); | |
843 while (IO::Select->new($s)->can_write(0.4)) { | |
844 log_out($message); | |
845 my $n = $s->syswrite($message); | |
846 last unless $n; | |
847 $message = substr($message, $n); | |
848 last unless length $message; | |
849 } | |
850 } | |
851 | |
852 sub new_session { | |
853 my ($d, $i, $status); | |
854 | |
855 ($d, $status) = Compress::Raw::Zlib::Deflate->new( | |
856 -WindowBits => 12, | |
857 -Dictionary => dictionary(), | |
858 -Level => Compress::Raw::Zlib->Z_NO_COMPRESSION | |
859 ); | |
860 fail "Zlib failure: $status" unless $d; | |
861 | |
862 ($i, $status) = Compress::Raw::Zlib::Inflate->new( | |
863 -WindowBits => Compress::Raw::Zlib->WANT_GZIP_OR_ZLIB, | |
864 -Dictionary => dictionary() | |
865 ); | |
866 fail "Zlib failure: $status" unless $i; | |
867 | |
868 return { zlib => { i => $i, d => $d }, | |
869 socket => new_socket(), last_stream => -1 }; | |
870 } | |
871 | |
872 sub new_socket { | |
873 my $s; | |
874 | |
875 eval { | |
876 local $SIG{ALRM} = sub { die "timeout\n" }; | |
877 local $SIG{PIPE} = sub { die "sigpipe\n" }; | |
878 alarm(2); | |
879 $s = IO::Socket::INET->new( | |
880 Proto => 'tcp', | |
881 PeerAddr => '127.0.0.1:8080', | |
882 ); | |
883 alarm(0); | |
884 }; | |
885 alarm(0); | |
886 | |
887 if ($@) { | |
888 log_in("died: $@"); | |
889 return undef; | |
890 } | |
891 | |
892 return $s; | |
893 } | |
894 | |
895 sub dictionary { | |
896 join('', (map pack('N/a*', $_), qw( | |
897 options | |
898 head | |
899 post | |
900 put | |
901 delete | |
902 trace | |
903 accept | |
904 accept-charset | |
905 accept-encoding | |
906 accept-language | |
907 accept-ranges | |
908 age | |
909 allow | |
910 authorization | |
911 cache-control | |
912 connection | |
913 content-base | |
914 content-encoding | |
915 content-language | |
916 content-length | |
917 content-location | |
918 content-md5 | |
919 content-range | |
920 content-type | |
921 date | |
922 etag | |
923 expect | |
924 expires | |
925 from | |
926 host | |
927 if-match | |
928 if-modified-since | |
929 if-none-match | |
930 if-range | |
931 if-unmodified-since | |
932 last-modified | |
933 location | |
934 max-forwards | |
935 pragma | |
936 proxy-authenticate | |
937 proxy-authorization | |
938 range | |
939 referer | |
940 retry-after | |
941 server | |
942 te | |
943 trailer | |
944 transfer-encoding | |
945 upgrade | |
946 user-agent | |
947 vary | |
948 via | |
949 warning | |
950 www-authenticate | |
951 method | |
952 get | |
953 status), "200 OK", | |
954 qw(version HTTP/1.1 url public set-cookie keep-alive origin)), | |
955 "100101201202205206300302303304305306307402405406407408409410", | |
956 "411412413414415416417502504505", | |
957 "203 Non-Authoritative Information", | |
958 "204 No Content", | |
959 "301 Moved Permanently", | |
960 "400 Bad Request", | |
961 "401 Unauthorized", | |
962 "403 Forbidden", | |
963 "404 Not Found", | |
964 "500 Internal Server Error", | |
965 "501 Not Implemented", | |
966 "503 Service Unavailable", | |
967 "Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec", | |
968 " 00:00:00", | |
969 " Mon, Tue, Wed, Thu, Fri, Sat, Sun, GMT", | |
970 "chunked,text/html,image/png,image/jpg,image/gif,", | |
971 "application/xml,application/xhtml+xml,text/plain,", | |
972 "text/javascript,public", "privatemax-age=gzip,deflate,", | |
973 "sdchcharset=utf-8charset=iso-8859-1,utf-,*,enq=0." | |
974 ); | |
975 } | |
976 | |
977 ############################################################################### |