[tests] give time for periodic jobs to detect exit
[lighttpd.git] / tests / mod-fastcgi.t
blob582e04fb7a7990eeb840a817aa15663a93926eb3
1 #!/usr/bin/env perl
2 BEGIN {
3 # add current source dir to the include-path
4 # we need this for make distcheck
5 (my $srcdir = $0) =~ s,/[^/]+$,/,;
6 unshift @INC, $srcdir;
9 use strict;
10 use Test::More tests => 59;
11 use LightyTest;
13 my $tf = LightyTest->new();
15 my $t;
16 my $php_child = -1;
18 SKIP: {
19 skip "PHP already running on port 1026", 1 if $tf->listening_on(1026);
20 skip "no php binary found", 1 unless $LightyTest::HAVE_PHP;
21 ok(-1 != ($php_child = $tf->spawnfcgi($ENV{'PHP'}, 1026)), "Spawning php");
24 SKIP: {
25 skip "no PHP running on port 1026", 35 unless $tf->listening_on(1026);
27 ok($tf->start_proc == 0, "Starting lighttpd") or goto cleanup;
29 $t->{REQUEST} = ( <<EOF
30 GET /phpinfo.php HTTP/1.0
31 Host: www.example.org
32 EOF
34 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
35 ok($tf->handle_http($t) == 0, 'valid request');
37 $t->{REQUEST} = ( <<EOF
38 GET /phpinfofoobar.php HTTP/1.0
39 Host: www.example.org
40 EOF
42 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } ];
43 ok($tf->handle_http($t) == 0, 'file not found');
45 $t->{REQUEST} = ( <<EOF
46 GET /go/ HTTP/1.0
47 Host: www.example.org
48 EOF
50 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
51 ok($tf->handle_http($t) == 0, 'index-file handling');
53 $t->{REQUEST} = ( <<EOF
54 GET /redirect.php HTTP/1.0
55 Host: www.example.org
56 EOF
58 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 302, 'Location' => 'http://www.example.org:2048/' } ];
59 ok($tf->handle_http($t) == 0, 'Status + Location via FastCGI');
61 $t->{REQUEST} = ( <<EOF
62 GET /redirect.php/ HTTP/1.0
63 Host: www.example.org
64 EOF
66 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 302, 'Location' => 'http://www.example.org:2048/' } ];
67 ok($tf->handle_http($t) == 0, 'Trailing slash as path-info (#1989: workaround broken operating systems)');
69 $t->{REQUEST} = ( <<EOF
70 GET /get-server-env.php?env=PHP_SELF HTTP/1.0
71 Host: www.example.org
72 EOF
74 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
75 ok($tf->handle_http($t) == 0, '$_SERVER["PHP_SELF"]');
77 $t->{REQUEST} = ( <<EOF
78 GET /get-server-env.php/foo?env=SCRIPT_NAME HTTP/1.0
79 Host: www.example.org
80 EOF
82 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/get-server-env.php' } ];
83 ok($tf->handle_http($t) == 0, '$_SERVER["SCRIPT_NAME"]');
85 $t->{REQUEST} = ( <<EOF
86 GET /get-server-env.php/foo?env=PATH_INFO HTTP/1.0
87 Host: www.example.org
88 EOF
90 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/foo' } ];
91 ok($tf->handle_http($t) == 0, '$_SERVER["PATH_INFO"]');
93 $t->{REQUEST} = ( <<EOF
94 GET /get-server-env.php?env=SERVER_NAME HTTP/1.0
95 Host: www.example.org
96 EOF
98 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'www.example.org' } ];
99 ok($tf->handle_http($t) == 0, 'SERVER_NAME');
101 $t->{REQUEST} = ( <<EOF
102 GET /get-server-env.php?env=SERVER_NAME HTTP/1.0
103 Host: foo.example.org
106 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'www.example.org' } ];
107 ok($tf->handle_http($t) == 0, 'SERVER_NAME');
109 $t->{REQUEST} = ( <<EOF
110 GET /get-server-env.php?env=SERVER_NAME HTTP/1.0
111 Host: vvv.example.org
114 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'www.example.org' } ];
115 ok($tf->handle_http($t) == 0, 'SERVER_NAME');
117 $t->{REQUEST} = ( <<EOF
118 GET /get-server-env.php?env=SERVER_NAME HTTP/1.0
119 Host: zzz.example.org
122 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'www.example.org' } ];
123 ok($tf->handle_http($t) == 0, 'SERVER_NAME');
125 $t->{REQUEST} = ( <<EOF
126 GET /cgi.php/abc HTTP/1.0
129 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
130 ok($tf->handle_http($t) == 0, 'PATHINFO');
132 if ($^O ne "cygwin") {
133 $t->{REQUEST} = ( <<EOF
134 GET /cgi.php%20%20%20 HTTP/1.0
137 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } ];
138 ok($tf->handle_http($t) == 0, 'No source retrieval');
139 } else {
140 ok(1, 'No source retrieval; skipped on cygwin; see response.c');
143 $t->{REQUEST} = ( <<EOF
144 GET /www/abc/def HTTP/1.0
147 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } ];
148 ok($tf->handle_http($t) == 0, 'PATHINFO on a directory');
150 $t->{REQUEST} = ( <<EOF
151 GET /indexfile/ HTTP/1.0
154 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/indexfile/index.php' } ];
155 ok($tf->handle_http($t) == 0, 'PHP_SELF + Indexfile, Bug #3');
157 $t->{REQUEST} = ( <<EOF
158 GET /prefix.fcgi?var=SCRIPT_NAME HTTP/1.0
161 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/prefix.fcgi' } ];
162 ok($tf->handle_http($t) == 0, 'PATH_INFO, check-local off');
164 $t->{REQUEST} = ( <<EOF
165 GET /prefix.fcgi/foo/bar?var=SCRIPT_NAME HTTP/1.0
168 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/prefix.fcgi' } ];
169 ok($tf->handle_http($t) == 0, 'PATH_INFO, check-local off');
171 $t->{REQUEST} = ( <<EOF
172 GET /prefix.fcgi/foo/bar?var=PATH_INFO HTTP/1.0
175 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/foo/bar' } ];
176 ok($tf->handle_http($t) == 0, 'PATH_INFO, check-local off');
178 $t->{REQUEST} = ( <<EOF
179 GET /sendfile.php?range=0- HTTP/1.0
182 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'Content-Length' => 4348 } ];
183 ok($tf->handle_http($t) == 0, 'X-Sendfile2');
185 $t->{REQUEST} = ( <<EOF
186 GET /sendfile.php?range=0-4&range2=5- HTTP/1.0
189 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'Content-Length' => 4348 } ];
190 ok($tf->handle_http($t) == 0, 'X-Sendfile2');
192 $t->{REQUEST} = ( <<EOF
193 GET /get-server-env.php?env=REMOTE_USER HTTP/1.0
194 Host: auth.example.org
195 Authorization: Basic ZGVzOmRlcw==
198 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'des' } ];
199 ok($tf->handle_http($t) == 0, '$_SERVER["REMOTE_USER"]');
201 $t->{REQUEST} = ( <<EOF
202 GET /get-server-env.php?env=AUTH_TYPE HTTP/1.0
203 Host: auth.example.org
204 Authorization: Basic ZGVzOmRlcw==
207 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'Basic' } ];
208 ok($tf->handle_http($t) == 0, '$_SERVER["AUTH_TYPE"]');
211 ok($tf->stop_proc == 0, "Stopping lighttpd");
214 $tf->{CONFIGFILE} = 'fastcgi-10.conf';
215 ok($tf->start_proc == 0, "Starting lighttpd with $tf->{CONFIGFILE}") or goto cleanup;
216 $t->{REQUEST} = ( <<EOF
217 GET /get-server-env.php?env=SERVER_NAME HTTP/1.0
218 Host: zzz.example.org
221 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'zzz.example.org' } ];
222 ok($tf->handle_http($t) == 0, 'FastCGI + Host');
224 $t->{REQUEST} = ( <<EOF
225 GET http://zzz.example.org/get-server-env.php?env=SERVER_NAME HTTP/1.0
226 Host: aaa.example.org
229 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'zzz.example.org' } ];
230 ok($tf->handle_http($t) == 0, 'SERVER_NAME (absolute url in request line)');
232 ok($tf->stop_proc == 0, "Stopping lighttpd");
234 $tf->{CONFIGFILE} = 'bug-06.conf';
235 ok($tf->start_proc == 0, "Starting lighttpd with $tf->{CONFIGFILE}") or goto cleanup;
236 $t->{REQUEST} = ( <<EOF
237 GET /indexfile/ HTTP/1.0
238 Host: www.example.org
241 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/indexfile/index.php' } ];
242 ok($tf->handle_http($t) == 0, 'Bug #6');
244 ok($tf->stop_proc == 0, "Stopping lighttpd");
246 $tf->{CONFIGFILE} = 'bug-12.conf';
247 ok($tf->start_proc == 0, "Starting lighttpd with bug-12.conf") or goto cleanup;
248 $t->{REQUEST} = ( <<EOF
249 POST /indexfile/abc HTTP/1.0
250 Host: www.example.org
251 Content-Length: 0
254 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404, 'HTTP-Content' => '/indexfile/return-404.php' } ];
255 ok($tf->handle_http($t) == 0, 'Bug #12');
257 ok($tf->stop_proc == 0, "Stopping lighttpd");
260 SKIP: {
261 skip "PHP not started, cannot stop it", 1 unless $php_child != -1;
262 ok(0 == $tf->endspawnfcgi($php_child), "Stopping php");
263 $php_child = -1;
266 SKIP: {
267 skip "no fcgi-auth found", 7 unless -x $tf->{BASEDIR}."/tests/fcgi-auth" || -x $tf->{BASEDIR}."/tests/fcgi-auth.exe";
269 $tf->{CONFIGFILE} = 'fastcgi-auth.conf';
270 ok($tf->start_proc == 0, "Starting lighttpd with $tf->{CONFIGFILE}") or die();
271 $t->{REQUEST} = ( <<EOF
272 GET /index.html?ok HTTP/1.0
273 Host: www.example.org
276 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
277 ok($tf->handle_http($t) == 0, 'FastCGI - Auth');
279 $t->{REQUEST} = ( <<EOF
280 GET /index.html?fail HTTP/1.0
281 Host: www.example.org
284 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 403 } ];
285 ok($tf->handle_http($t) == 0, 'FastCGI - Auth');
287 $t->{REQUEST} = ( <<EOF
288 GET /expire/access.txt?ok HTTP/1.0
289 Host: www.example.org
292 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
293 ok($tf->handle_http($t) == 0, 'FastCGI - Auth in subdirectory');
295 $t->{REQUEST} = ( <<EOF
296 GET /index.fcgi?varfail HTTP/1.0
297 Host: www.example.org
300 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 403 } ];
301 ok($tf->handle_http($t) == 0, 'FastCGI - Auth Fail with FastCGI responder afterwards');
303 $t->{REQUEST} = ( <<EOF
304 GET /index.fcgi?var HTTP/1.0
305 Host: www.example.org
308 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'LighttpdTestContent' } ];
309 ok($tf->handle_http($t) == 0, 'FastCGI - Auth Success with Variable- to Env expansion');
312 ok($tf->stop_proc == 0, "Stopping lighttpd");
315 SKIP: {
316 skip "no php found", 5 unless $LightyTest::HAVE_PHP;
317 $tf->{CONFIGFILE} = 'fastcgi-13.conf';
318 ok($tf->start_proc == 0, "Starting lighttpd with $tf->{CONFIGFILE}") or die();
319 $t->{REQUEST} = ( <<EOF
320 GET /indexfile/index.php HTTP/1.0
321 Host: www.example.org
324 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
325 ok($tf->handle_http($t) == 0, 'FastCGI + local spawning');
327 $t->{REQUEST} = ( <<EOF
328 HEAD /indexfile/index.php HTTP/1.0
329 Host: www.example.org
332 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, '-Content-Length' => '0' } ];
333 # Of course a valid content-length != 0 would be ok, but we assume for now that such one is not generated.
334 ok($tf->handle_http($t) == 0, 'Check for buggy content length with HEAD');
336 $t->{REQUEST} = ( <<EOF
337 GET /get-env.php?env=MAIL HTTP/1.0
338 Host: www.example.org
341 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 , 'HTTP-Content' => '' } ];
342 ok($tf->handle_http($t) == 0, 'FastCGI + bin-copy-environment');
346 ok($tf->stop_proc == 0, "Stopping lighttpd");
350 SKIP: {
351 skip "no fcgi-responder found", 10 unless -x $tf->{BASEDIR}."/tests/fcgi-responder" || -x $tf->{BASEDIR}."/tests/fcgi-responder.exe";
353 $tf->{CONFIGFILE} = 'fastcgi-responder.conf';
354 ok($tf->start_proc == 0, "Starting lighttpd with $tf->{CONFIGFILE}") or die();
355 $t->{REQUEST} = ( <<EOF
356 GET /index.fcgi?lf HTTP/1.0
357 Host: www.example.org
360 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
361 ok($tf->handle_http($t) == 0, 'line-ending \n\n');
363 $t->{REQUEST} = ( <<EOF
364 GET /index.fcgi?crlf HTTP/1.0
365 Host: www.example.org
368 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
369 ok($tf->handle_http($t) == 0, 'line-ending \r\n\r\n');
371 $t->{REQUEST} = ( <<EOF
372 GET /index.fcgi?slow-lf HTTP/1.0
373 Host: www.example.org
376 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
377 ok($tf->handle_http($t) == 0, 'line-ending \n + \n');
379 $t->{REQUEST} = ( <<EOF
380 GET /index.fcgi?slow-crlf HTTP/1.0
381 Host: www.example.org
384 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
385 ok($tf->handle_http($t) == 0, 'line-ending \r\n + \r\n');
387 $t->{REQUEST} = ( <<EOF
388 GET /abc/def/ghi?path_info HTTP/1.0
389 Host: wsgi.example.org
392 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/abc/def/ghi' } ];
393 ok($tf->handle_http($t) == 0, 'PATH_INFO (wsgi)');
395 $t->{REQUEST} = ( <<EOF
396 GET /abc/def/ghi?script_name HTTP/1.0
397 Host: wsgi.example.org
400 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '' } ];
401 ok($tf->handle_http($t) == 0, 'SCRIPT_NAME (wsgi)');
404 $t->{REQUEST} = ( <<EOF
405 GET /index.fcgi?die-at-end HTTP/1.0
406 Host: www.example.org
409 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
410 ok($tf->handle_http($t) == 0, 'killing fastcgi and wait for restart');
412 # (might take lighttpd 1 sec to detect backend exit)
413 select(undef, undef, undef, .9);
414 select(undef, undef, undef, .1) while (!$tf->listening_on(10000));
415 $t->{REQUEST} = ( <<EOF
416 GET /index.fcgi?crlf HTTP/1.0
417 Host: www.example.org
420 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
421 ok($tf->handle_http($t) == 0, 'regular response of after restart');
424 ok($tf->stop_proc == 0, "Stopping lighttpd");
427 exit 0;
429 cleanup: ;
431 $tf->endspawnfcgi($php_child) if $php_child != -1;
433 die();