comparison lib/Test/Nginx/HTTP2.pm @ 948:4dc302d8e04f

Tests: changed HTTP2 package to act as a class. Stopped exporting any subroutines. A subset of them now act as class methods.
author Sergey Kandaurov <pluknet@nginx.com>
date Fri, 17 Jun 2016 11:36:33 +0300
parents d73bef563aea
children c657aaffdaa8
comparison
equal deleted inserted replaced
947:b9e42c554ba7 948:4dc302d8e04f
7 7
8 ############################################################################### 8 ###############################################################################
9 9
10 use warnings; 10 use warnings;
11 use strict; 11 use strict;
12
13 use base qw/ Exporter /;
14 our @EXPORT = qw/ new_session new_stream h2_read /;
15 our %EXPORT_TAGS = (
16 io => [ qw/ raw_write raw_read / ],
17 frame => [ qw/ h2_ping h2_rst h2_goaway h2_priority h2_window
18 h2_settings h2_unknown h2_continue h2_body/ ]
19 );
20 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'io'} }, @{ $EXPORT_TAGS{'frame'} } );
21 12
22 use Test::More qw//; 13 use Test::More qw//;
23 use IO::Select; 14 use IO::Select;
24 use IO::Socket; 15 use IO::Socket;
25 use Socket qw/ CRLF /; 16 use Socket qw/ CRLF /;
111 102
112 my $len = length $body; 103 my $len = length $body;
113 my $sid = $sess->{last_stream}; 104 my $sid = $sess->{last_stream};
114 105
115 if ($len > $sess->{conn_window} || $len > $sess->{streams}{$sid}) { 106 if ($len > $sess->{conn_window} || $len > $sess->{streams}{$sid}) {
116 h2_read($sess, all => [{ type => 'WINDOW_UPDATE' }]); 107 $sess->read(all => [{ type => 'WINDOW_UPDATE' }]);
117 } 108 }
118 109
119 if ($len > $sess->{conn_window} || $len > $sess->{streams}{$sid}) { 110 if ($len > $sess->{conn_window} || $len > $sess->{streams}{$sid}) {
120 return; 111 return;
121 } 112 }
258 raw_write($ctx->{socket}, $buf); 249 raw_write($ctx->{socket}, $buf);
259 done: 250 done:
260 return $ctx->{last_stream}; 251 return $ctx->{last_stream};
261 } 252 }
262 253
263 sub h2_read { 254 sub read {
264 my ($sess, %extra) = @_; 255 my ($sess, %extra) = @_;
265 my (@got); 256 my (@got);
266 my $s = $sess->{socket}; 257 my $s = $sess->{socket};
267 my $buf = ''; 258 my $buf = '';
268 my $wait = $extra{wait}; 259 my $wait = $extra{wait};
436 $message = substr($message, $n); 427 $message = substr($message, $n);
437 last unless length $message; 428 last unless length $message;
438 } 429 }
439 } 430 }
440 431
441 sub new_session { 432 sub new {
433 my $class = shift;
442 my ($port, %extra) = @_; 434 my ($port, %extra) = @_;
443 435
444 my $s = $extra{socket} || new_socket($port, %extra); 436 my $s = $extra{socket} || new_socket($port, %extra);
445 my $preface = $extra{preface} 437 my $preface = $extra{preface}
446 || 'PRI * HTTP/2.0' . CRLF . CRLF . 'SM' . CRLF . CRLF; 438 || 'PRI * HTTP/2.0' . CRLF . CRLF . 'SM' . CRLF . CRLF;
456 my $ctx = { socket => $s, last_stream => -1, 448 my $ctx = { socket => $s, last_stream => -1,
457 dynamic_encode => [ static_table() ], 449 dynamic_encode => [ static_table() ],
458 dynamic_decode => [ static_table() ], 450 dynamic_decode => [ static_table() ],
459 static_table_size => scalar @{[static_table()]}, 451 static_table_size => scalar @{[static_table()]},
460 iws => 65535, conn_window => 65535, streams => {}}; 452 iws => 65535, conn_window => 65535, streams => {}};
453 bless $ctx, $class;
461 454
462 return $ctx if $extra{pure}; 455 return $ctx if $extra{pure};
463 456
464 # update windows, if any 457 # update windows, if any
465 458
466 my $frames = h2_read($ctx, all => [ 459 my $frames = $ctx->read(all => [
467 { type => 'WINDOW_UPDATE' }, 460 { type => 'WINDOW_UPDATE' },
468 { type => 'SETTINGS'} 461 { type => 'SETTINGS'}
469 ]); 462 ]);
470 463
471 # 6.5.3. Settings Synchronization 464 # 6.5.3. Settings Synchronization