1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
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(<<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',
'location' => 'https://www.example.com',
]
),
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',
'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(<<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();
__END__
subtest 'decode responses' => sub {
my $decoder = Protocol::HTTP2::HeaderCompression->new;
$decoder->{_max_ht_size} = 256;
is $decoder->headers_decode( \hstr(<<EOF), \my @headers ), 51;
4882 4017 5985 bf06 724b 9763 93d6 dbb2
9884 de2a 7188 0506 2098 5131 09b5 6ba3
7191 adce bf19 8e7e 7cf9 bebe 89b6 fb16
fa9b 6f
EOF
is_deeply \@headers, [
[ ':status' => '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(<<EOF), \@headers ), 84;
8484 4393 d6db b298 84de 2a71 8805 0620
9851 3111 b56b a35e 84ab dd97 ff84 8483
837b b1e0 d6cf 9f6e 8f9f d3e5 f6fa 76fe
fd3c 7edf 9eff 1f2f 0f3c fe9f 6fcf 7f8f
879f 61ad 4f4c c9a9 73a2 200e c372 5e18
b1b7 4e3f
EOF
is_deeply \@headers,
[
[ 'cache-control' => '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;
|