| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
use Protocol::HTTP2::Client; |
| 5 |
use Protocol::HTTP2::Server; |
| 6 |
use lib 't/lib'; |
| 7 |
use PH2Test qw(fake_connect); |
| 8 |
|
| 9 |
subtest 'server stream' => sub { |
| 10 |
|
| 11 |
my $server; |
| 12 |
$server = Protocol::HTTP2::Server->new( |
| 13 |
on_request => sub { |
| 14 |
my ( $stream_id, $headers, $data ) = @_; |
| 15 |
|
| 16 |
my $s_stream = $server->response_stream( |
| 17 |
':status' => 200, |
| 18 |
stream_id => $stream_id, |
| 19 |
|
| 20 |
# HTTP/1.1 Headers |
| 21 |
headers => [ |
| 22 |
'server' => 'perl-Protocol-HTTP2/0.16', |
| 23 |
'cache-control' => 'max-age=3600', |
| 24 |
'date' => 'Fri, 18 Apr 2014 07:27:11 GMT', |
| 25 |
'last-modified' => 'Thu, 27 Feb 2014 10:30:37 GMT', |
| 26 |
], |
| 27 |
); |
| 28 |
|
| 29 |
isa_ok( $s_stream, 'Protocol::HTTP2::Server::Stream' ); |
| 30 |
$s_stream->send('a'); |
| 31 |
$s_stream->send('b'); |
| 32 |
$s_stream->close; |
| 33 |
}, |
| 34 |
); |
| 35 |
|
| 36 |
my $client = Protocol::HTTP2::Client->new; |
| 37 |
$client->request( |
| 38 |
|
| 39 |
# HTTP/2 headers |
| 40 |
':scheme' => 'http', |
| 41 |
':authority' => 'localhost:8000', |
| 42 |
':path' => '/', |
| 43 |
':method' => 'GET', |
| 44 |
|
| 45 |
# HTTP/1.1 headers |
| 46 |
headers => [ |
| 47 |
'accept' => '*/*', |
| 48 |
'user-agent' => 'perl-Protocol-HTTP2/0.16', |
| 49 |
], |
| 50 |
|
| 51 |
# Callback when receive server's response |
| 52 |
on_done => sub { |
| 53 |
my ( $headers, $data ) = @_; |
| 54 |
is $data, "ab", "stream data ok"; |
| 55 |
}, |
| 56 |
); |
| 57 |
|
| 58 |
fake_connect( $server, $client ); |
| 59 |
}; |
| 60 |
|
| 61 |
subtest 'client cancel' => sub { |
| 62 |
|
| 63 |
my $cancel = 0; |
| 64 |
my $s_stream; |
| 65 |
my $server; |
| 66 |
$server = Protocol::HTTP2::Server->new( |
| 67 |
on_request => sub { |
| 68 |
my ( $stream_id, $headers, $data ) = @_; |
| 69 |
|
| 70 |
$s_stream = $server->response_stream( |
| 71 |
':status' => 200, |
| 72 |
stream_id => $stream_id, |
| 73 |
|
| 74 |
# HTTP/1.1 Headers |
| 75 |
headers => [ 'server' => 'perl-Protocol-HTTP2/0.16', ], |
| 76 |
on_cancel => sub { |
| 77 |
$cancel = 1; |
| 78 |
} |
| 79 |
); |
| 80 |
|
| 81 |
isa_ok( $s_stream, 'Protocol::HTTP2::Server::Stream' ); |
| 82 |
$s_stream->send('a'); |
| 83 |
}, |
| 84 |
); |
| 85 |
|
| 86 |
my $client = Protocol::HTTP2::Client->new; |
| 87 |
$client->request( |
| 88 |
|
| 89 |
# HTTP/2 headers |
| 90 |
':scheme' => 'http', |
| 91 |
':authority' => 'localhost:8000', |
| 92 |
':path' => '/', |
| 93 |
':method' => 'GET', |
| 94 |
|
| 95 |
# HTTP/1.1 headers |
| 96 |
headers => [ 'user-agent' => 'perl-Protocol-HTTP2/0.16', ], |
| 97 |
|
| 98 |
on_headers => sub { |
| 99 |
is_deeply $_[0], |
| 100 |
[ ':status' => 200, 'server' => 'perl-Protocol-HTTP2/0.16' ], |
| 101 |
"correct headers"; |
| 102 |
1; |
| 103 |
}, |
| 104 |
|
| 105 |
# Callback when receive server's response |
| 106 |
on_data => sub { |
| 107 |
my ( $chunk, $headers ) = @_; |
| 108 |
is $chunk, "a", "stream data ok"; |
| 109 |
|
| 110 |
# cancel |
| 111 |
0; |
| 112 |
}, |
| 113 |
); |
| 114 |
|
| 115 |
fake_connect( $server, $client ); |
| 116 |
is $cancel, 1, "successfully canceled"; |
| 117 |
}; |
| 118 |
|
| 119 |
done_testing; |