-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathperl.t
330 lines (251 loc) · 7.81 KB
/
perl.t
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/perl
# (C) Maxim Dounin
# Tests for embedded perl module.
###############################################################################
use warnings;
use strict;
use Test::More;
use Socket qw/ CRLF /;
BEGIN { use FindBin; chdir($FindBin::Bin); }
use lib 'lib';
use Test::Nginx;
###############################################################################
select STDERR; $| = 1;
select STDOUT; $| = 1;
my $t = Test::Nginx->new()->has(qw/http perl rewrite/)->plan(27)
->write_file_expand('nginx.conf', <<'EOF');
%%TEST_GLOBALS%%
daemon off;
events {
}
http {
%%TEST_GLOBALS_HTTP%%
server {
listen 127.0.0.1:8080;
server_name localhost;
location / {
set $testvar "TEST";
perl 'sub {
use warnings;
use strict;
my $r = shift;
$r->status(204) if $r->args =~ /204/;
$r->send_http_header("text/plain");
return OK if $r->header_only;
my $v = $r->variable("testvar");
$r->print("testvar: $v\n");
$r->print("host: ", $r->header_in("Host"), "\n");
$r->print("xfoo: ", $r->header_in("X-Foo"), "\n");
$r->print("cookie: ", $r->header_in("Cookie"), "\n");
$r->print("xff: ", $r->header_in("X-Forwarded-For"), "\n");
$r->print("connection: ", $r->header_in("Connection"), "\n");
return OK;
}';
}
location /range {
perl 'sub {
use warnings;
use strict;
my $r = shift;
$r->header_out("Content-Length", "42");
$r->allow_ranges();
$r->send_http_header("text/plain");
return OK if $r->header_only;
$r->print("x" x 42);
return OK;
}';
}
location /body {
perl 'sub {
use warnings;
use strict;
my $r = shift;
if ($r->has_request_body(\&post)) {
return OK;
}
return HTTP_BAD_REQUEST;
sub post {
my $r = shift;
$r->send_http_header;
$r->print("body: ", $r->request_body, "\n");
$r->print("file: ", $r->request_body_file, "\n");
}
}';
}
location /discard {
perl 'sub {
use warnings;
use strict;
my $r = shift;
$r->discard_request_body;
$r->send_http_header("text/plain");
return OK if $r->header_only;
$r->print("host: ", $r->header_in("Host"), "\n");
return OK;
}';
}
}
}
EOF
$t->run();
###############################################################################
like(http_get('/'), qr/ 200 .*TEST/s, 'perl response');
like(http_head('/'), qr/ 200 (?!.*TEST)/s, 'perl header_only');
like(http_get('/?204'), qr/ 204 (?!.*TEST)/s, 'perl status, args');
# various $r->header_in() cases
like(http(
'GET / HTTP/1.0' . CRLF
. 'Host: localhost' . CRLF . CRLF
), qr/host: localhost/, 'perl header_in known');
like(http(
'GET / HTTP/1.0' . CRLF
. 'X-Foo: foo' . CRLF
. 'Host: localhost' . CRLF . CRLF
), qr/xfoo: foo/, 'perl header_in unknown');
TODO: {
local $TODO = 'not yet' unless $t->has_version('1.23.0');
like(http(
'GET / HTTP/1.0' . CRLF
. 'X-Foo: foo' . CRLF
. 'X-Foo: bar' . CRLF
. 'Host: localhost' . CRLF . CRLF
), qr/xfoo: foo, bar/, 'perl header_in unknown2');
}
like(http(
'GET / HTTP/1.0' . CRLF
. 'Cookie: foo' . CRLF
. 'Host: localhost' . CRLF . CRLF
), qr/cookie: foo/, 'perl header_in cookie');
like(http(
'GET / HTTP/1.0' . CRLF
. 'Cookie: foo1' . CRLF
. 'Cookie: foo2' . CRLF
. 'Host: localhost' . CRLF . CRLF
), qr/cookie: foo1; foo2/, 'perl header_in cookie2');
like(http(
'GET / HTTP/1.0' . CRLF
. 'X-Forwarded-For: foo' . CRLF
. 'Host: localhost' . CRLF . CRLF
), qr/xff: foo/, 'perl header_in xff');
like(http(
'GET / HTTP/1.0' . CRLF
. 'X-Forwarded-For: foo1' . CRLF
. 'X-Forwarded-For: foo2' . CRLF
. 'Host: localhost' . CRLF . CRLF
), qr/xff: foo1, foo2/, 'perl header_in xff2');
TODO: {
local $TODO = 'not yet' unless $t->has_version('1.23.0');
like(http(
'GET / HTTP/1.0' . CRLF
. 'Connection: close' . CRLF
. 'Host: localhost' . CRLF . CRLF
), qr/connection: close/, 'perl header_in connection');
like(http(
'GET / HTTP/1.0' . CRLF
. 'Connection: close' . CRLF
. 'Connection: foo' . CRLF
. 'Host: localhost' . CRLF . CRLF
), qr/connection: close, foo/, 'perl header_in connection2');
}
# headers_out content-length tests with range filter
like(http_get('/range'), qr/Content-Length: 42.*^x{42}$/ms,
'perl header_out content-length');
like(http(
'GET /range HTTP/1.0' . CRLF
. 'Host: localhost' . CRLF
. 'Range: bytes=0-1' . CRLF . CRLF
), qr/Content-Length: 2.*^xx$/ms, 'perl header_out content-length range');
like(http(
'GET /range HTTP/1.0' . CRLF
. 'Host: localhost' . CRLF
. 'Range: bytes=0-1,3-5' . CRLF . CRLF
), qr/Content-Length: (?!42).*^xx\x0d.*^xxx\x0d/ms,
'perl header_out content-length multipart');
like(http(
'GET /range HTTP/1.0' . CRLF
. 'Host: localhost' . CRLF
. 'Range: bytes=100000-' . CRLF . CRLF
), qr|^\QHTTP/1.1 416\E.*(?!xxx)|ms, 'perl range not satisfiable');
like(http(
'GET / HTTP/1.0' . CRLF
. 'Host: localhost' . CRLF
. 'If-Match: tt' . CRLF . CRLF
), qr|200 OK|ms, 'perl precondition failed');
# various request body tests
like(http_get('/body'), qr/400 Bad Request/, 'perl no body');
like(http(
'GET /body HTTP/1.0' . CRLF
. 'Host: localhost' . CRLF
. 'Content-Length: 10' . CRLF . CRLF
. '1234567890'
), qr/body: 1234567890/, 'perl body preread');
like(http(
'GET /body HTTP/1.0' . CRLF
. 'Host: localhost' . CRLF
. 'Content-Length: 10' . CRLF . CRLF,
sleep => 0.1,
body => '1234567890'
), qr/body: 1234567890/, 'perl body late');
like(http(
'GET /body HTTP/1.0' . CRLF
. 'Host: localhost' . CRLF
. 'Content-Length: 10' . CRLF . CRLF
. '12345',
sleep => 0.1,
body => '67890'
), qr/body: 1234567890/, 'perl body split');
like(http(
'GET /body HTTP/1.1' . CRLF
. 'Host: localhost' . CRLF
. 'Connection: close' . CRLF
. 'Transfer-Encoding: chunked' . CRLF . CRLF
. 'a' . CRLF
. '1234567890' . CRLF
. '0' . CRLF . CRLF
), qr/body: 1234567890/, 'perl body chunked');
like(http(
'GET /body HTTP/1.1' . CRLF
. 'Host: localhost' . CRLF
. 'Connection: close' . CRLF
. 'Transfer-Encoding: chunked' . CRLF . CRLF,
sleep => 0.1,
body => 'a' . CRLF . '1234567890' . CRLF . '0' . CRLF . CRLF
), qr/body: 1234567890/, 'perl body chunked late');
like(http(
'GET /body HTTP/1.1' . CRLF
. 'Host: localhost' . CRLF
. 'Connection: close' . CRLF
. 'Transfer-Encoding: chunked' . CRLF . CRLF
. 'a' . CRLF
. '12345',
sleep => 0.1,
body => '67890' . CRLF . '0' . CRLF . CRLF
), qr/body: 1234567890/, 'perl body chunked split');
like(http(
'GET /discard HTTP/1.1' . CRLF
. 'Host: localhost' . CRLF
. 'Connection: close' . CRLF
. 'Transfer-Encoding: chunked' . CRLF . CRLF
. 'a' . CRLF
. '1234567890' . CRLF
. '0' . CRLF . CRLF
), qr/host: localhost/, 'perl body discard');
like(http(
'GET /discard HTTP/1.1' . CRLF
. 'Host: localhost' . CRLF
. 'Connection: close' . CRLF
. 'Transfer-Encoding: chunked' . CRLF . CRLF
. 'ak' . CRLF
. '1234567890' . CRLF
. '0' . CRLF . CRLF
), qr/400 Bad Request/, 'perl body discard bad chunk');
like(http(
'GET /body HTTP/1.1' . CRLF
. 'Host: localhost' . CRLF
. 'Connection: close' . CRLF
. 'Transfer-Encoding: chunked' . CRLF . CRLF
. 'ak' . CRLF
. '1234567890' . CRLF
. '0' . CRLF . CRLF
), qr/400 Bad Request/, 'perl body bad chunk');
###############################################################################