File: 11_server_stream.t

package info (click to toggle)
libprotocol-http2-perl 1.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 448 kB
  • sloc: perl: 3,370; makefile: 7
file content (119 lines) | stat: -rw-r--r-- 3,117 bytes parent folder | download | duplicates (4)
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
use strict;
use warnings;
use Test::More;
use Protocol::HTTP2::Client;
use Protocol::HTTP2::Server;
use lib 't/lib';
use PH2Test qw(fake_connect);

subtest 'server stream' => sub {

    my $server;
    $server = Protocol::HTTP2::Server->new(
        on_request => sub {
            my ( $stream_id, $headers, $data ) = @_;

            my $s_stream = $server->response_stream(
                ':status' => 200,
                stream_id => $stream_id,

                # HTTP/1.1 Headers
                headers => [
                    'server'        => 'perl-Protocol-HTTP2/0.16',
                    'cache-control' => 'max-age=3600',
                    'date'          => 'Fri, 18 Apr 2014 07:27:11 GMT',
                    'last-modified' => 'Thu, 27 Feb 2014 10:30:37 GMT',
                ],
            );

            isa_ok( $s_stream, 'Protocol::HTTP2::Server::Stream' );
            $s_stream->send('a');
            $s_stream->send('b');
            $s_stream->close;
        },
    );

    my $client = Protocol::HTTP2::Client->new;
    $client->request(

        # HTTP/2 headers
        ':scheme'    => 'http',
        ':authority' => 'localhost:8000',
        ':path'      => '/',
        ':method'    => 'GET',

        # HTTP/1.1 headers
        headers => [
            'accept'     => '*/*',
            'user-agent' => 'perl-Protocol-HTTP2/0.16',
        ],

        # Callback when receive server's response
        on_done => sub {
            my ( $headers, $data ) = @_;
            is $data, "ab", "stream data ok";
        },
    );

    fake_connect( $server, $client );
};

subtest 'client cancel' => sub {

    my $cancel = 0;
    my $s_stream;
    my $server;
    $server = Protocol::HTTP2::Server->new(
        on_request => sub {
            my ( $stream_id, $headers, $data ) = @_;

            $s_stream = $server->response_stream(
                ':status' => 200,
                stream_id => $stream_id,

                # HTTP/1.1 Headers
                headers   => [ 'server' => 'perl-Protocol-HTTP2/0.16', ],
                on_cancel => sub {
                    $cancel = 1;
                }
            );

            isa_ok( $s_stream, 'Protocol::HTTP2::Server::Stream' );
            $s_stream->send('a');
        },
    );

    my $client = Protocol::HTTP2::Client->new;
    $client->request(

        # HTTP/2 headers
        ':scheme'    => 'http',
        ':authority' => 'localhost:8000',
        ':path'      => '/',
        ':method'    => 'GET',

        # HTTP/1.1 headers
        headers => [ 'user-agent' => 'perl-Protocol-HTTP2/0.16', ],

        on_headers => sub {
            is_deeply $_[0],
              [ ':status' => 200, 'server' => 'perl-Protocol-HTTP2/0.16' ],
              "correct headers";
            1;
        },

        # Callback when receive server's response
        on_data => sub {
            my ( $chunk, $headers ) = @_;
            is $chunk, "a", "stream data ok";

            # cancel
            0;
        },
    );

    fake_connect( $server, $client );
    is $cancel, 1, "successfully canceled";
};

done_testing;