| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
use Protocol::HTTP2::Constants qw(const_name :endpoints :states); |
| 5 |
use lib 't/lib'; |
| 6 |
use PH2Test; |
| 7 |
|
| 8 |
BEGIN { |
| 9 |
use_ok('Protocol::HTTP2::Connection'); |
| 10 |
} |
| 11 |
|
| 12 |
done_testing(); |
| 13 |
__END__ |
| 14 |
|
| 15 |
subtest 'decode_request' => sub { |
| 16 |
|
| 17 |
my $data = hstr(<<EOF); |
| 18 |
5052 4920 2a20 4854 5450 2f32 2e30 0d0a |
| 19 |
0d0a 534d 0d0a 0d0a 000a 0400 0000 0000 |
| 20 |
0300 0000 6404 0000 ffff 0038 0105 0000 |
| 21 |
0001 4189 128d f07c 1f19 a400 0f83 0608 |
| 22 |
2f4c 4943 454e 5345 8856 032a 2f2a 548a |
| 23 |
abdd 97e5 352b e162 5cbf 7f00 8dba aadc |
| 24 |
ebc8 e0fc 0f86 6d1d ff8f |
| 25 |
EOF |
| 26 |
|
| 27 |
my $run_test = sub { }; |
| 28 |
my $con = Protocol::HTTP2::Connection->new( SERVER, |
| 29 |
on_change_state => sub { |
| 30 |
my ( $stream_id, $previous_state, $current_state ) = @_; |
| 31 |
printf "Stream %i changed state from %s to %s\n", |
| 32 |
$stream_id, const_name( "states", $previous_state ), |
| 33 |
const_name( "states", $current_state ); |
| 34 |
|
| 35 |
if ( $current_state == HALF_CLOSED ) { |
| 36 |
$run_test->($stream_id); |
| 37 |
} |
| 38 |
}, |
| 39 |
on_error => sub { |
| 40 |
fail("Error occurred"); |
| 41 |
} |
| 42 |
); |
| 43 |
|
| 44 |
my $run_test_flag = 0; |
| 45 |
|
| 46 |
$run_test = sub { |
| 47 |
my $stream_id = shift; |
| 48 |
is_deeply( |
| 49 |
$con->stream_headers($stream_id), |
| 50 |
[ |
| 51 |
':authority' => '127.0.0.1:8000', |
| 52 |
':method' => 'GET', |
| 53 |
':path' => '/LICENSE', |
| 54 |
':scheme' => 'http', |
| 55 |
'accept' => '*/*', |
| 56 |
'accept-encoding' => 'gzip, deflate', |
| 57 |
'user-agent' => 'nghttp2/0.4.0-DEV', |
| 58 |
], |
| 59 |
"correct request headers" |
| 60 |
) and $run_test_flag = 1; |
| 61 |
}; |
| 62 |
|
| 63 |
my $offset = $con->preface_decode( \$data, 0 ); |
| 64 |
is( $offset, 24, "Preface exists" ) or BAIL_OUT "preface?"; |
| 65 |
while ( my $size = $con->frame_decode( \$data, $offset ) ) { |
| 66 |
$offset += $size; |
| 67 |
} |
| 68 |
ok( $con->error == 0 && $run_test_flag, "decode headers" ); |
| 69 |
$data = hstr("0000 0401 0000 0000"); |
| 70 |
$offset = 0; |
| 71 |
while ( my $size = $con->frame_decode( \$data, $offset ) ) { |
| 72 |
$offset += $size; |
| 73 |
} |
| 74 |
ok( $con->error == 0 ); |
| 75 |
|
| 76 |
$data = hstr("0008 0700 0000 0000 0000 0000 0000 0000"); |
| 77 |
$offset = 0; |
| 78 |
while ( my $size = $con->frame_decode( \$data, $offset ) ) { |
| 79 |
$offset += $size; |
| 80 |
} |
| 81 |
ok( $con->error == 0 ); |
| 82 |
}; |
| 83 |
|
| 84 |
subtest 'decode_response' => sub { |
| 85 |
|
| 86 |
my $data = hstr(<<EOF); |
| 87 |
0005 0400 0000 0000 0300 0000 64 |
| 88 |
EOF |
| 89 |
|
| 90 |
my $con = Protocol::HTTP2::Connection->new(CLIENT); |
| 91 |
|
| 92 |
# Emulate request |
| 93 |
my $sid = $con->new_stream; |
| 94 |
$con->stream_state( $sid, HALF_CLOSED ); |
| 95 |
|
| 96 |
my $run_test_flag = 0; |
| 97 |
$con->stream_cb( |
| 98 |
$sid, CLOSED, |
| 99 |
sub { |
| 100 |
|
| 101 |
is_deeply( |
| 102 |
$con->stream_headers($sid), |
| 103 |
[ |
| 104 |
':status' => 200, |
| 105 |
'server' => 'nghttpd nghttp2/0.4.0-DEV', |
| 106 |
'content-length' => 46, |
| 107 |
'cache-control' => 'max-age=3600', |
| 108 |
'date' => 'Fri, 18 Apr 2014 07:27:11 GMT', |
| 109 |
'last-modified' => 'Thu, 27 Feb 2014 10:30:37 GMT', |
| 110 |
], |
| 111 |
"correct response headers" |
| 112 |
) and $run_test_flag = 1; |
| 113 |
} |
| 114 |
); |
| 115 |
|
| 116 |
my $offset = 0; |
| 117 |
while ( my $size = $con->frame_decode( \$data, $offset ) ) { |
| 118 |
$offset += $size; |
| 119 |
} |
| 120 |
ok( $con->error == 0 ); |
| 121 |
|
| 122 |
$data = hstr(<<EOF); |
| 123 |
0000 0401 0000 0000 0052 0104 0000 0001 |
| 124 |
8877 93ba aadc ebe9 35d5 56e7 5e47 07e0 |
| 125 |
7c33 68ef fc7f 0f0f 0234 365a 89b5 3d33 |
| 126 |
26a5 ce88 803f 6494 d383 3298 6436 7bf0 |
| 127 |
3100 c060 8e62 8e61 136a d747 7094 a2be |
| 128 |
394c 519b 4af7 9880 6030 84c8 0991 19b5 |
| 129 |
6ba3 002e 0000 0000 0001 6e61 6d65 203d |
| 130 |
2022 5072 6f74 6f63 6f6c 2d48 5454 5032 |
| 131 |
220a 2320 6261 6467 6573 203d 205b 2274 |
| 132 |
7261 7669 7322 5d0a 0000 0001 0000 0001 |
| 133 |
EOF |
| 134 |
|
| 135 |
$offset = 0; |
| 136 |
while ( my $size = $con->frame_decode( \$data, $offset ) ) { |
| 137 |
$offset += $size; |
| 138 |
} |
| 139 |
is $offset, length($data), "read all data"; |
| 140 |
ok( $con->error == 0 && $run_test_flag ); |
| 141 |
|
| 142 |
}; |
| 143 |
|
| 144 |
done_testing(); |