Debian Codegraph @

t/01_HeaderCompression.t

raw
1 use strict;
2 use warnings;
3 use Test::More;
4 use lib 't/lib';
5 use PH2Test;
6 use Protocol::HTTP2::Connection;
7 use Protocol::HTTP2::Constants qw(:endpoints :limits :settings);
8
9 BEGIN {
10 use_ok( 'Protocol::HTTP2::HeaderCompression',
11 qw(int_encode int_decode str_encode str_decode headers_decode headers_encode)
12 );
13 }
14
15 subtest 'int_encode' => sub {
16 ok binary_eq( int_encode( 0, 8 ), pack( "C", 0 ) );
17 ok binary_eq( int_encode( 0xFD, 8 ), pack( "C", 0xFD ) );
18 ok binary_eq( int_encode( 0xFF, 8 ), pack( "C*", 0xFF, 0x00 ) );
19 ok binary_eq( int_encode( 0x100, 8 ), pack( "C*", 0xFF, 0x01 ) );
20 ok binary_eq( int_encode( 1337, 5 ), pack( "C*", 31, 154, 10 ) );
21 };
22
23 subtest 'int_decode' => sub {
24 my $buf = pack( "C*", 31, 154, 10 );
25 my $int = 0;
26 is int_decode( \$buf, 0, \$int, 5 ), 3;
27 is $int, 1337;
28 };
29
30 subtest 'str_encode' => sub {
31
32 ok binary_eq( str_encode('//ee'), hstr("8361 8297") );
33
34 };
35
36 subtest 'str_decode' => sub {
37 my $s = hstr(<<EOF);
38 93aa 69d2 9ae4 52a9 a74a 6b13 005d a5c0
39 b5fc 1c7f
40 EOF
41 str_decode( \$s, 0, \my $res );
42 is $res, "nghttpd nghttp2/0.4.0-DEV", "str_decode";
43 };
44
45 subtest 'encode requests' => sub {
46
47 my $con = Protocol::HTTP2::Connection->new(CLIENT);
48 my $ctx = $con->encode_context;
49
50 ok binary_eq(
51 headers_encode(
52 $ctx,
53 [
54 ':method' => 'GET',
55 ':scheme' => 'http',
56 ':path' => '/',
57 ':authority' => 'www.example.com',
58 ]
59 ),
60 hstr(<<EOF) );
61 8286 8441 8cf1 e3c2 e5f2 3a6b a0ab 90f4
62 ff
63 EOF
64
65 ok binary_eq(
66 headers_encode(
67 $ctx,
68 [
69 ':method' => 'GET',
70 ':scheme' => 'http',
71 ':path' => '/',
72 ':authority' => 'www.example.com',
73 'cache-control' => 'no-cache',
74 ]
75 ),
76 hstr(<<EOF) );
77 8286 84be 5886 a8eb 1064 9cbf
78 EOF
79
80 ok binary_eq(
81 headers_encode(
82 $ctx,
83 [
84 ':method' => 'GET',
85 ':scheme' => 'https',
86 ':path' => '/index.html',
87 ':authority' => 'www.example.com',
88 'custom-key' => 'custom-value',
89 ]
90 ),
91 hstr(<<EOF) );
92 8287 85bf 4088 25a8 49e9 5ba9 7d7f 8925
93 a849 e95b b8e8 b4bf
94 EOF
95
96 };
97
98 subtest 'encode responses' => sub {
99
100 my $con = Protocol::HTTP2::Connection->new(SERVER);
101 my $ctx = $con->encode_context;
102 $ctx->{max_ht_size} = $ctx->{settings}->{&SETTINGS_HEADER_TABLE_SIZE} = 256;
103
104 ok binary_eq(
105 headers_encode(
106 $ctx,
107 [
108 ':status' => '302',
109 'cache-control' => 'private',
110 'date' => 'Mon, 21 Oct 2013 20:13:21 GMT',
111 'location' => 'https://www.example.com',
112 ]
113 ),
114 hstr(<<EOF) );
115 4882 6402 5885 aec3 771a 4b61 96d0 7abe
116 9410 54d4 44a8 2005 9504 0b81 66e0 82a6
117 2d1b ff6e 919d 29ad 1718 63c7 8f0b 97c8
118 e9ae 82ae 43d3
119 EOF
120
121 is $ctx->{ht_size} => 222, 'ht_size ok';
122
123 ok binary_eq(
124 headers_encode(
125 $ctx,
126 [
127 ':status' => 307,
128 'cache-control' => 'private',
129 'date' => 'Mon, 21 Oct 2013 20:13:21 GMT',
130 'location' => 'https://www.example.com',
131 ]
132 ),
133 hstr("4803 3330 37c1 c0bf")
134 );
135
136 is $ctx->{ht_size} => 222, 'ht_size ok';
137
138 ok binary_eq(
139 headers_encode(
140 $ctx,
141 [
142 ':status' => 200,
143 'cache-control' => 'private',
144 'date' => 'Mon, 21 Oct 2013 20:13:22 GMT',
145 'location' => 'https://www.example.com',
146 'content-encoding' => 'gzip',
147 'set-cookie' =>
148 'foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1',
149 ]
150 ),
151 hstr(<<EOF) );
152 88c1 6196 d07a be94 1054 d444 a820 0595
153 040b 8166 e084 a62d 1bff c05a 839b d9ab
154 77ad 94e7 821d d7f2 e6c7 b335 dfdf cd5b
155 3960 d5af 2708 7f36 72c1 ab27 0fb5 291f
156 9587 3160 65c0 03ed 4ee5 b106 3d50 07
157 EOF
158 is $ctx->{ht_size} => 215, 'ht_size ok';
159
160 };
161
162 subtest 'decode requests' => sub {
163
164 my $con = Protocol::HTTP2::Connection->new(SERVER);
165 my $ctx = $con->decode_context;
166
167 my $buf = hstr(<<EOF);
168 8286 8441 8cf1 e3c2 e5f2 3a6b a0ab 90f4
169 ff
170 EOF
171 is headers_decode( $con, \$buf, 0, length $buf ), length($buf),
172 "correct offset";
173
174 is_deeply $ctx->{emitted_headers},
175 [
176 ':method' => 'GET',
177 ':scheme' => 'http',
178 ':path' => '/',
179 ':authority' => 'www.example.com'
180 ],
181 "emitted headers";
182 $ctx->{emitted_headers} = [];
183 is_deeply $ctx->{header_table}, [ [ ':authority' => 'www.example.com' ] ],
184 "dynamic table";
185 is $ctx->{ht_size}, 57, "correct table size";
186
187 $buf = hstr('8286 84be 5886 a8eb 1064 9cbf');
188 is headers_decode( $con, \$buf, 0, length $buf ), length($buf),
189 "correct offset";
190 is_deeply $ctx->{emitted_headers},
191 [
192 ':method' => 'GET',
193 ':scheme' => 'http',
194 ':path' => '/',
195 ':authority' => 'www.example.com',
196 'cache-control' => 'no-cache',
197 ],
198 "emitted headers";
199 $ctx->{emitted_headers} = [];
200 is_deeply $ctx->{header_table}, [
201 [ 'cache-control' => 'no-cache' ],
202
203 [ ':authority' => 'www.example.com' ]
204 ],
205 "dynamic table";
206 is $ctx->{ht_size}, 110, "correct table size";
207
208 $buf = hstr(<<EOF);
209 8287 85bf 4088 25a8 49e9 5ba9 7d7f 8925
210 a849 e95b b8e8 b4bf
211 EOF
212 is headers_decode( $con, \$buf, 0, length $buf ), length($buf),
213 "correct offset";
214 is_deeply $ctx->{emitted_headers},
215 [
216 ':method' => 'GET',
217 ':scheme' => 'https',
218 ':path' => '/index.html',
219 ':authority' => 'www.example.com',
220 'custom-key' => 'custom-value',
221 ],
222 "emitted headers";
223 is_deeply $ctx->{header_table}, [
224 [ 'custom-key' => 'custom-value' ],
225 [
226 'cache-control' => 'no-cache'
227 ],
228
229 [ ':authority' => 'www.example.com' ]
230 ],
231 "dynamic table";
232 is $ctx->{ht_size}, 164, "correct table size";
233
234 };
235
236 done_testing();
237 __END__
238
239
240 subtest 'decode responses' => sub {
241 my $decoder = Protocol::HTTP2::HeaderCompression->new;
242
243 $decoder->{_max_ht_size} = 256;
244
245 is $decoder->headers_decode( \hstr(<<EOF), \my @headers ), 51;
246 4882 4017 5985 bf06 724b 9763 93d6 dbb2
247 9884 de2a 7188 0506 2098 5131 09b5 6ba3
248 7191 adce bf19 8e7e 7cf9 bebe 89b6 fb16
249 fa9b 6f
250 EOF
251
252 is_deeply \@headers, [
253 [ ':status' => '302' ],
254 [ 'cache-control' => 'private' ],
255 [ 'date' => 'Mon, 21 Oct 2013 20:13:21 GMT' ],
256 [ 'location' => 'https://www.example.com' ],
257
258 ] or diag explain \@headers;
259
260 @headers = ();
261
262 is $decoder->headers_decode( \hstr("8c"), \@headers ), 1;
263
264 is_deeply \@headers, [ [ ':status' => '200' ], ] or diag explain \@headers;
265
266 @headers = ();
267
268 is $decoder->headers_decode( \hstr(<<EOF), \@headers ), 84;
269 8484 4393 d6db b298 84de 2a71 8805 0620
270 9851 3111 b56b a35e 84ab dd97 ff84 8483
271 837b b1e0 d6cf 9f6e 8f9f d3e5 f6fa 76fe
272 fd3c 7edf 9eff 1f2f 0f3c fe9f 6fcf 7f8f
273 879f 61ad 4f4c c9a9 73a2 200e c372 5e18
274 b1b7 4e3f
275 EOF
276
277 is_deeply \@headers,
278 [
279 [ 'cache-control' => 'private' ],
280 [ 'date' => 'Mon, 21 Oct 2013 20:13:22 GMT' ],
281 [ 'content-encoding' => 'gzip' ],
282 [ 'location' => 'https://www.example.com' ],
283 [ ':status' => '200' ],
284 [
285 'set-cookie' =>
286 'foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1'
287 ],
288 ]
289 or diag explain \@headers;
290
291 };
292
293 done_testing;
294