File: request_rec.pm

package info (click to toggle)
libapache2-mod-perl2 2.0.9~1624218-2%2Bdeb8u2
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 11,912 kB
  • ctags: 4,588
  • sloc: perl: 95,064; ansic: 14,527; makefile: 49; sh: 18
file content (277 lines) | stat: -rw-r--r-- 7,243 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# please insert nothing before this line: -*- mode: cperl; cperl-indent-level: 4; cperl-continued-statement-offset: 4; indent-tabs-mode: nil -*-
package TestAPI::request_rec;

use strict;
use warnings FATAL => 'all';

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

use Apache2::RequestRec ();
use Apache2::RequestUtil ();

use APR::Finfo ();
use APR::Pool ();

use Apache2::Const -compile => qw(OK M_GET M_PUT);
use APR::Const    -compile => qw(FINFO_NORM);

#this test module is only for testing fields in the request_rec
#listed in apache_structures.map
#XXX: GloabalRequest test should be moved elsewhere
#     as should $| test

sub handler {
    my $r = shift;

    plan $r, tests => 55;

    #Apache2::RequestUtil->request($r); #PerlOptions +GlobalRequest takes care
    my $gr = Apache2::RequestUtil->request;

    ok $$gr == $$r;

    my $newr = Apache2::RequestRec->new($r->connection, $r->pool);
    Apache2::RequestUtil->request($newr);
    $gr = Apache2::RequestUtil->request;

    ok $$gr == $$newr;

    Apache2::RequestUtil->request($r);

    ok $r->pool->isa('APR::Pool');

    ok $r->connection->isa('Apache2::Connection');

    ok $r->server->isa('Apache2::ServerRec');

    for (qw(next prev main)) {
        ok (! $r->$_()) || $r->$_()->isa('Apache2::RequestRec');
    }

    ok !$r->assbackwards;

    ok !$r->proxyreq; # see also TestModules::proxy

    ok !$r->header_only;

    ok $r->protocol =~ /http/i;

    # LWP >=6.00 uses HTTP/1.1, other HTTP/1.0
    ok t_cmp $r->proto_num, 1000+substr($r->the_request, -1),
	't->proto_num';

    ok t_cmp lc($r->hostname), lc($r->get_server_name), '$r->hostname';

    {
        my $old_hostname = $r->hostname("other.hostname");
        ok t_cmp $r->hostname, "other.hostname", '$r->hostname rw';
        $r->hostname($old_hostname);
    }

    ok $r->request_time;

    ok $r->status_line || 1;

    ok $r->status || 1;

    ok t_cmp $r->method, 'GET', '$r->method';

    ok t_cmp $r->method_number, Apache2::Const::M_GET, '$r->method_number';

    ok $r->headers_in;

    ok $r->headers_out;

    # tested in TestAPI::err_headers_out
    ok $r->err_headers_out;

    ok $r->subprocess_env;

    ok $r->notes;

    ok $r->content_type;

    ok $r->handler;

    ok $r->ap_auth_type || 1;

    ok $r->no_cache || 1;

    ok !$r->no_local_copy;

    {
        local $| = 0;
        ok t_cmp $r->print("# buffered\n"), 11, "buffered print";
        ok t_cmp $r->print(), "0E0", "buffered print";

        local $| = 1;
        my $string = "# not buffered\n";
        ok t_cmp $r->print(split //, $string), length($string),
            "unbuffered print";
    }

    # GET header components
    {
        my $args      = "my_args=3";
        my $path_info = "/my_path_info";
        my $base_uri  = "/TestAPI__request_rec";

        ok t_cmp $r->unparsed_uri, "$base_uri$path_info?$args";

        ok t_cmp $r->uri, "$base_uri$path_info", '$r->uri';

        ok t_cmp $r->path_info, $path_info, '$r->path_info';

        ok t_cmp $r->args, $args, '$r->args';

	# LWP uses HTTP/1.1 since 6.00
        ok t_cmp $r->the_request, qr!GET
				     \x20
				     \Q$base_uri$path_info\E\?\Q$args\E
				     \x20
				     HTTP/1\.\d!x,
            '$r->the_request';

        {
            my $new_request = "GET $base_uri$path_info?$args&foo=bar HTTP/1.0";
            my $old_request = $r->the_request($new_request);
            ok t_cmp $r->the_request, $new_request, '$r->the_request rw';
            $r->the_request($old_request);
        }

        ok $r->filename;

        my $location = '/' . Apache::TestRequest::module2path(__PACKAGE__);
        ok t_cmp $r->location, $location, '$r->location';
    }

    # bytes_sent
    {
        $r->rflush;
        my $sent = $r->bytes_sent;
        t_debug "sent so far: $sent bytes";
        # at least 100 chars were sent already
        ok $sent > 100;
    }

    # mtime
    {
        my $mtime = (stat __FILE__)[9];
        $r->mtime($mtime);
        ok t_cmp $r->mtime, $mtime, "mtime";
    }

    # finfo
    {
        my $finfo = APR::Finfo::stat(__FILE__, APR::Const::FINFO_NORM, $r->pool);
        $r->finfo($finfo);
        # just one field test, all accessors are fully tested in
        # TestAPR::finfo
        ok t_cmp($r->finfo->fname,
                 __FILE__,
                 '$r->finfo');
    }

    # allowed
    {
        $r->allowed(1 << Apache2::Const::M_GET);

        ok $r->allowed & (1 << Apache2::Const::M_GET);
        ok ! ($r->allowed & (1 << Apache2::Const::M_PUT));

        $r->allowed($r->allowed | (1 << Apache2::Const::M_PUT));
        ok $r->allowed & (1 << Apache2::Const::M_PUT);
    }

    # content_languages
    {
        my $def = [qw(fr)];       #default value
        my $l   = [qw(fr us cn)]; #new value

        if (have_module('mod_mime')) {
            ok t_cmp $r->content_languages, $def, '$r->content_languages';
        }
        else {
            skip "Need mod_mime", 0;
        }

        my $old = $r->content_languages($l);
        if (have_module('mod_mime')) {
            ok t_cmp $old, $def, '$r->content_languages';
        }
        else {
            skip "Need mod_mime", 0;
        }

        ok t_cmp $r->content_languages, $l, '$r->content_languages';

        eval { $r->content_languages({}) };
        ok t_cmp $@, qr/Not an array reference/,
                '$r->content_languages(invalid)';
    }

    ### invalid $r
    {
        my $r = bless {}, "Apache2::RequestRec";
        my $err = q[method `uri' invoked by a `Apache2::RequestRec' ] .
            q[object with no `r' key!];
        eval { $r->uri };
        ok t_cmp $@, qr/$err/, "invalid $r object";
    }
    {
        my $r = bless {}, "NonExisting";
        my $err = q[method `uri' invoked by a `NonExisting' ] .
            q[object with no `r' key!];
        eval { Apache2::RequestRec::uri($r) };
        ok t_cmp $@, qr/$err/, "invalid $r object";
    }
    {
        my $r = {};
        my $err = q[method `uri' invoked by a `unknown' ] .
            q[object with no `r' key!];
        eval { Apache2::RequestRec::uri($r) };
        ok t_cmp $@, qr/$err/, "invalid $r object";
    }

    # out-of-scope pools
    {
        my $newr = Apache2::RequestRec->new($r->connection, APR::Pool->new);
        {
            require APR::Table;
            # try to overwrite the pool
            my $table = APR::Table::make(APR::Pool->new, 50);
            $table->set($_ => $_) for 'aa'..'za';
        }
        # check if $newr is still OK
        ok $newr->connection->isa('Apache2::Connection');
    }

    # tested in other tests
    # - input_filters:    TestAPI::in_out_filters
    # - output_filters:   TestAPI::in_out_filters
    # - per_dir_config:   in several other tests
    # - content_encoding: TestAPI::content_encoding
    # - user:             TestHooks::authz / TestHooks::authen

    # XXX: untested
    # - request_config
    # - allowed_xmethods
    # - allowed_methods

    Apache2::Const::OK;
}

1;
__END__
<NoAutoConfig>
<Location /TestAPI__request_rec>
    PerlOptions +GlobalRequest
    <IfModule mod_mime.c>
        DefaultLanguage fr
    </IfModule>
    SetHandler modperl
    PerlResponseHandler TestAPI::request_rec
</Location>
</NoAutoConfig>