| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
use lib 't/lib'; |
| 5 |
use PH2ClientServerTest; |
| 6 |
use Test::TCP; |
| 7 |
use Protocol::HTTP2; |
| 8 |
|
| 9 |
my $host = '127.0.0.1'; |
| 10 |
|
| 11 |
subtest 'client/server' => sub { |
| 12 |
for my $opts ( |
| 13 |
[ "without tls", [], [] ], |
| 14 |
[ "without tls, upgrade", [ upgrade => 1 ], [ upgrade => 1 ] ], |
| 15 |
[ |
| 16 |
"tls/npn", |
| 17 |
[ npn => 1 ], |
| 18 |
[ |
| 19 |
npn => 1, |
| 20 |
tls_crt => 'examples/test.crt', |
| 21 |
tls_key => 'examples/test.key' |
| 22 |
] |
| 23 |
], |
| 24 |
[ |
| 25 |
"tls/alpn", |
| 26 |
[ alpn => 1 ], |
| 27 |
[ |
| 28 |
alpn => 1, |
| 29 |
tls_crt => 'examples/test.crt', |
| 30 |
tls_key => 'examples/test.key' |
| 31 |
] |
| 32 |
], |
| 33 |
) |
| 34 |
{ |
| 35 |
my $test = shift @$opts; |
| 36 |
note "test: $test\n"; |
| 37 |
|
| 38 |
# Check for NPN/ALPN |
| 39 |
if ( !check_tls( @{ $opts->[0] } ) ) { |
| 40 |
note "skipped $test: feature not available\n"; |
| 41 |
next; |
| 42 |
} |
| 43 |
|
| 44 |
eval { |
| 45 |
local $SIG{ALRM} = sub { die "timeout\n" }; |
| 46 |
alarm 16; |
| 47 |
test_tcp( |
| 48 |
client => sub { |
| 49 |
my $port = shift; |
| 50 |
client( |
| 51 |
@{ $opts->[0] }, |
| 52 |
port => $port, |
| 53 |
host => $host, |
| 54 |
on_error => sub { |
| 55 |
fail "error occurred: " . shift; |
| 56 |
}, |
| 57 |
test_cb => sub { |
| 58 |
my $client = shift; |
| 59 |
$client->request( |
| 60 |
':scheme' => "http", |
| 61 |
':authority' => $host . ":" . $port, |
| 62 |
':path' => "/", |
| 63 |
':method' => "GET", |
| 64 |
headers => [ |
| 65 |
'accept' => '*/*', |
| 66 |
'user-agent' => 'perl-Protocol-HTTP2/' |
| 67 |
. $Protocol::HTTP2::VERSION, |
| 68 |
], |
| 69 |
on_done => sub { |
| 70 |
my ( $headers, $data ) = @_; |
| 71 |
is scalar(@$headers) / 2, 6, |
| 72 |
"get response headers"; |
| 73 |
is length($data), 13, "get body"; |
| 74 |
}, |
| 75 |
); |
| 76 |
} |
| 77 |
); |
| 78 |
}, |
| 79 |
server => sub { |
| 80 |
my $port = shift; |
| 81 |
my $server; |
| 82 |
server( |
| 83 |
@{ $opts->[1] }, |
| 84 |
port => $port, |
| 85 |
host => $host, |
| 86 |
on_error => sub { |
| 87 |
fail "error occurred: " . shift; |
| 88 |
}, |
| 89 |
test_cb => sub { |
| 90 |
$server = shift; |
| 91 |
}, |
| 92 |
on_request => sub { |
| 93 |
my ( $stream_id, $headers, $data ) = @_; |
| 94 |
my $message = "hello, world!"; |
| 95 |
$server->response( |
| 96 |
':status' => 200, |
| 97 |
stream_id => $stream_id, |
| 98 |
headers => [ |
| 99 |
'server' => 'perl-Protocol-HTTP2/' |
| 100 |
. $Protocol::HTTP2::VERSION, |
| 101 |
'content-length' => length($message), |
| 102 |
'cache-control' => 'max-age=3600', |
| 103 |
'date' => 'Fri, 18 Apr 2014 07:27:11 GMT', |
| 104 |
'last-modified' => |
| 105 |
'Thu, 27 Feb 2014 10:30:37 GMT', |
| 106 |
], |
| 107 |
data => $message, |
| 108 |
); |
| 109 |
}, |
| 110 |
); |
| 111 |
}, |
| 112 |
); |
| 113 |
alarm 0; |
| 114 |
}; |
| 115 |
is $@, '', "no errors"; |
| 116 |
} |
| 117 |
}; |
| 118 |
|
| 119 |
done_testing; |