summaryrefslogtreecommitdiffstats
path: root/debian/perl-framework/t/http11/chunked.t
blob: 2331239c8457528d184e85b888a40a8de924b789 (plain)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use strict;
use warnings FATAL => 'all';

use Apache::Test;
use Apache::TestRequest;
use Apache::TestUtil;

Apache::TestRequest::user_agent(keep_alive => 1);

Apache::TestRequest::scheme('http')
  unless have_module 'LWP::Protocol::https10'; #lwp 5.60

#In httpd-2.0, chunked encoding is optional and will only be used
#if response is > 4*AP_MIN_BYTES_TO_WRITE (see server/protocol.c)

my @small_sizes = (100, 5000);
my @chunk_sizes = (25432, 75962, 100_000, 300_000);

my $tests = (@chunk_sizes + @small_sizes) * 5;

if (! have_module 'random_chunk') {
    print "# Skipping; missing prerequisite module 'random_chunk'\n";
}
plan tests => $tests, need_module 'random_chunk';

my $location = '/random_chunk';
my $requests = 0;

sub expect_chunked {
    my $size = shift;
    sok sub {
        my $res = GET "/random_chunk?0,$size";
        my $body = $res->content;
        my $length = 0;

        if ($body =~ s/__END__:(\d+)$//) {
            $length = $1;
        }

        ok t_cmp($res->protocol,
                 "HTTP/1.1",
                 "response protocol"
                );

        my $enc = $res->header('Transfer-Encoding') || 
                  $res->header('Client-Transfer-Encoding') || #lwp 5.61+
                  '';
        my $ct  = $res->header('Content-Length') || 0;

        ok t_cmp($enc,
                 "chunked",
                 "response Transfer-Encoding"
                );

        ok t_cmp($ct,
                 0,
                 "no Content-Length"
                );

        ok t_cmp(length($body),
                 $length,
                 "body length"
                );

        $requests++;
        my $request_num =
          Apache::TestRequest::user_agent_request_num($res);

        return t_cmp($request_num,
                     $requests,
                     "number of requests"
                    );
    }, 5;
}

sub expect_not_chunked {
    my $size = shift;
    sok sub {
        my $res = GET "/random_chunk?0,$size";
        my $body = $res->content;
        my $content_length = length $res->content;
        my $length = 0;

        if ($body =~ s/__END__:(\d+)$//) {
            $length = $1;
        }

        ok t_cmp($res->protocol,
                 "HTTP/1.1",
                 "response protocol"
                );

        my $enc = $res->header('Transfer-Encoding') || '';
        my $ct  = $res->header('Content-Length') || '';

        ok !t_cmp($enc,
                  "chunked",
                  "no Transfer-Encoding (test result inverted)"
                 );

        ok t_cmp($ct,
                 (($ct eq '') ? $ct : $content_length),
                 "content length"
                );

        ok t_cmp(length($body),
                 $length,
                 "body length"
                );

        $requests++;
        my $request_num =
          Apache::TestRequest::user_agent_request_num($res);

        return t_cmp($request_num,
                     $requests,
                     "number of requests"
                    );
    }, 5;
}

for my $size (@chunk_sizes) {
    expect_chunked $size;
}

for my $size (@small_sizes) {
    if (have_apache 1) {
        expect_chunked $size;
    }
    else {
        expect_not_chunked $size;
    }
}