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(
<<EOF);
93aa 69d2 9ae4 52a9 a74a 6b13 005d a5c0
b5fc 1c7f
EOF
str_decode( \
$s
, 0, \
my
$res
);
is
$res
,
"nghttpd nghttp2/0.4.0-DEV"
,
"str_decode"
;
};
subtest
'encode requests'
=>
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(
<<EOF) );
8286 8441 8cf1 e3c2 e5f2 3a6b a0ab 90f4
ff
EOF
ok binary_eq(
headers_encode(
$ctx
,
[
':method'
=>
'GET'
,
':scheme'
=>
'http'
,
':path'
=>
'/'
,
':authority'
=>
'www.example.com'
,
'cache-control'
=>
'no-cache'
,
]
),
hstr(
<<EOF) );
8286 84be 5886 a8eb 1064 9cbf
EOF
ok binary_eq(
headers_encode(
$ctx
,
[
':method'
=>
'GET'
,
':scheme'
=>
'https'
,
':path'
=>
'/index.html'
,
':authority'
=>
'www.example.com'
,
'custom-key'
=>
'custom-value'
,
]
),
hstr(
<<EOF) );
8287 85bf 4088 25a8 49e9 5ba9 7d7f 8925
a849 e95b b8e8 b4bf
EOF
};
subtest
'encode responses'
=>
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'
,
]
),
hstr(
<<EOF) );
4882 6402 5885 aec3 771a 4b61 96d0 7abe
9410 54d4 44a8 2005 9504 0b81 66e0 82a6
2d1b ff6e 919d 29ad 1718 63c7 8f0b 97c8
e9ae 82ae 43d3
EOF
is
$ctx
->{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'
,
]
),
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'
,
'content-encoding'
=>
'gzip'
,
'set-cookie'
=>
'foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1'
,
]
),
hstr(
<<EOF) );
88c1 6196 d07a be94 1054 d444 a820 0595
040b 8166 e084 a62d 1bff c05a 839b d9ab
77ad 94e7 821d d7f2 e6c7 b335 dfdf cd5b
3960 d5af 2708 7f36 72c1 ab27 0fb5 291f
9587 3160 65c0 03ed 4ee5 b106 3d50 07
EOF
is
$ctx
->{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(
<<EOF);
8286 8441 8cf1 e3c2 e5f2 3a6b a0ab 90f4
ff
EOF
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'
],
"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(
<<EOF);
8287 85bf 4088 25a8 49e9 5ba9 7d7f 8925
a849 e95b b8e8 b4bf
EOF
is headers_decode(
$con
, \
$buf
, 0,
length
$buf
),
length
(
$buf
),
"correct offset"
;
is_deeply
$ctx
->{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();