use strict; use warnings; use Test::More; use lib 't/lib'; use PH2Test; use Protocol::HTTP2::Connection; use Protocol::HTTP2::Constants qw(:endpoints :limits :settings); BEGIN { use_ok( 'Protocol::HTTP2::HeaderCompression', qw(int_encode int_decode str_encode str_decode headers_decode headers_encode) ); } subtest 'int_encode' => sub { ok binary_eq( int_encode( 0, 8 ), pack( "C", 0 ) ); ok binary_eq( int_encode( 0xFD, 8 ), pack( "C", 0xFD ) ); ok binary_eq( int_encode( 0xFF, 8 ), pack( "C*", 0xFF, 0x00 ) ); ok binary_eq( int_encode( 0x100, 8 ), pack( "C*", 0xFF, 0x01 ) ); ok binary_eq( int_encode( 1337, 5 ), pack( "C*", 31, 154, 10 ) ); }; subtest 'int_decode' => sub { my $buf = pack( "C*", 31, 154, 10 ); my $int = 0; is int_decode( \$buf, 0, \$int, 5 ), 3; is $int, 1337; }; subtest 'str_encode' => sub { ok binary_eq( str_encode('//ee'), hstr("8361 8297") ); }; subtest 'str_decode' => sub { my $s = hstr(< sub { my $con = Protocol::HTTP2::Connection->new(CLIENT); my $ctx = $con->encode_context; ok binary_eq( headers_encode( $ctx, [ ':method' => 'GET', ':scheme' => 'http', ':path' => '/', ':authority' => 'www.example.com', ] ), hstr(< 'GET', ':scheme' => 'http', ':path' => '/', ':authority' => 'www.example.com', 'cache-control' => 'no-cache', ] ), hstr(< 'GET', ':scheme' => 'https', ':path' => '/index.html', ':authority' => 'www.example.com', 'custom-key' => 'custom-value', ] ), hstr(< sub { my $con = Protocol::HTTP2::Connection->new(SERVER); my $ctx = $con->encode_context; $ctx->{max_ht_size} = $ctx->{settings}->{&SETTINGS_HEADER_TABLE_SIZE} = 256; ok binary_eq( headers_encode( $ctx, [ ':status' => '302', 'cache-control' => 'private', 'date' => 'Mon, 21 Oct 2013 20:13:21 GMT', 'location' => 'https://www.example.com', ] ), hstr(<{ht_size} => 222, 'ht_size ok'; ok binary_eq( headers_encode( $ctx, [ ':status' => 307, 'cache-control' => 'private', 'date' => 'Mon, 21 Oct 2013 20:13:21 GMT', 'location' => 'https://www.example.com', ] ), hstr("4803 3330 37c1 c0bf") ); is $ctx->{ht_size} => 222, 'ht_size ok'; ok binary_eq( headers_encode( $ctx, [ ':status' => 200, 'cache-control' => 'private', 'date' => 'Mon, 21 Oct 2013 20:13:22 GMT', 'location' => 'https://www.example.com', 'content-encoding' => 'gzip', 'set-cookie' => 'foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1', ] ), hstr(<{ht_size} => 215, 'ht_size ok'; }; subtest 'decode requests' => sub { my $con = Protocol::HTTP2::Connection->new(SERVER); my $ctx = $con->decode_context; my $buf = hstr(<{emitted_headers}, [ ':method' => 'GET', ':scheme' => 'http', ':path' => '/', ':authority' => 'www.example.com' ], "emitted headers"; $ctx->{emitted_headers} = []; is_deeply $ctx->{header_table}, [ [ ':authority' => 'www.example.com' ] ], "dynamic table"; is $ctx->{ht_size}, 57, "correct table size"; $buf = hstr('8286 84be 5886 a8eb 1064 9cbf'); is headers_decode( $con, \$buf, 0, length $buf ), length($buf), "correct offset"; is_deeply $ctx->{emitted_headers}, [ ':method' => 'GET', ':scheme' => 'http', ':path' => '/', ':authority' => 'www.example.com', 'cache-control' => 'no-cache', ], "emitted headers"; $ctx->{emitted_headers} = []; is_deeply $ctx->{header_table}, [ [ 'cache-control' => 'no-cache' ], [ ':authority' => 'www.example.com' ] ], "dynamic table"; is $ctx->{ht_size}, 110, "correct table size"; $buf = hstr(<{emitted_headers}, [ ':method' => 'GET', ':scheme' => 'https', ':path' => '/index.html', ':authority' => 'www.example.com', 'custom-key' => 'custom-value', ], "emitted headers"; is_deeply $ctx->{header_table}, [ [ 'custom-key' => 'custom-value' ], [ 'cache-control' => 'no-cache' ], [ ':authority' => 'www.example.com' ] ], "dynamic table"; is $ctx->{ht_size}, 164, "correct table size"; }; done_testing(); __END__ subtest 'decode responses' => sub { my $decoder = Protocol::HTTP2::HeaderCompression->new; $decoder->{_max_ht_size} = 256; is $decoder->headers_decode( \hstr(< '302' ], [ 'cache-control' => 'private' ], [ 'date' => 'Mon, 21 Oct 2013 20:13:21 GMT' ], [ 'location' => 'https://www.example.com' ], ] or diag explain \@headers; @headers = (); is $decoder->headers_decode( \hstr("8c"), \@headers ), 1; is_deeply \@headers, [ [ ':status' => '200' ], ] or diag explain \@headers; @headers = (); is $decoder->headers_decode( \hstr(< 'private' ], [ 'date' => 'Mon, 21 Oct 2013 20:13:22 GMT' ], [ 'content-encoding' => 'gzip' ], [ 'location' => 'https://www.example.com' ], [ ':status' => '200' ], [ 'set-cookie' => 'foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1' ], ] or diag explain \@headers; }; done_testing;