untangle overly complex control flow logic
[lighttpd.git] / tests / mod-fastcgi.t
blobb3680d0dbb61581b56e3e1c836879cba2de0d8d5
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 => 58;
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 $t->{REQUEST} = ( <<EOF
133 GET /cgi.php%20%20%20 HTTP/1.0
136 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } ];
137 ok($tf->handle_http($t) == 0, 'No source retrieval');
139 $t->{REQUEST} = ( <<EOF
140 GET /www/abc/def HTTP/1.0
143 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } ];
144 ok($tf->handle_http($t) == 0, 'PATHINFO on a directory');
146 $t->{REQUEST} = ( <<EOF
147 GET /indexfile/ HTTP/1.0
150 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/indexfile/index.php' } ];
151 ok($tf->handle_http($t) == 0, 'PHP_SELF + Indexfile, Bug #3');
153 $t->{REQUEST} = ( <<EOF
154 GET /prefix.fcgi?var=SCRIPT_NAME HTTP/1.0
157 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/prefix.fcgi' } ];
158 ok($tf->handle_http($t) == 0, 'PATH_INFO, check-local off');
160 $t->{REQUEST} = ( <<EOF
161 GET /prefix.fcgi/foo/bar?var=SCRIPT_NAME HTTP/1.0
164 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/prefix.fcgi' } ];
165 ok($tf->handle_http($t) == 0, 'PATH_INFO, check-local off');
167 $t->{REQUEST} = ( <<EOF
168 GET /prefix.fcgi/foo/bar?var=PATH_INFO HTTP/1.0
171 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/foo/bar' } ];
172 ok($tf->handle_http($t) == 0, 'PATH_INFO, check-local off');
174 $t->{REQUEST} = ( <<EOF
175 GET /sendfile.php?range=0- HTTP/1.0
178 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'Content-Length' => 4348 } ];
179 ok($tf->handle_http($t) == 0, 'X-Sendfile2');
181 $t->{REQUEST} = ( <<EOF
182 GET /sendfile.php?range=0-4&range2=5- HTTP/1.0
185 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'Content-Length' => 4348 } ];
186 ok($tf->handle_http($t) == 0, 'X-Sendfile2');
188 $t->{REQUEST} = ( <<EOF
189 GET /get-server-env.php?env=REMOTE_USER HTTP/1.0
190 Host: auth.example.org
191 Authorization: Basic ZGVzOmRlcw==
194 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'des' } ];
195 ok($tf->handle_http($t) == 0, '$_SERVER["REMOTE_USER"]');
197 $t->{REQUEST} = ( <<EOF
198 GET /get-server-env.php?env=AUTH_TYPE HTTP/1.0
199 Host: auth.example.org
200 Authorization: Basic ZGVzOmRlcw==
203 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'Basic' } ];
204 ok($tf->handle_http($t) == 0, '$_SERVER["AUTH_TYPE"]');
207 ok($tf->stop_proc == 0, "Stopping lighttpd");
210 $tf->{CONFIGFILE} = 'fastcgi-10.conf';
211 ok($tf->start_proc == 0, "Starting lighttpd with $tf->{CONFIGFILE}") or goto cleanup;
212 $t->{REQUEST} = ( <<EOF
213 GET /get-server-env.php?env=SERVER_NAME HTTP/1.0
214 Host: zzz.example.org
217 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'zzz.example.org' } ];
218 ok($tf->handle_http($t) == 0, 'FastCGI + Host');
220 $t->{REQUEST} = ( <<EOF
221 GET http://zzz.example.org/get-server-env.php?env=SERVER_NAME HTTP/1.0
222 Host: aaa.example.org
225 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'zzz.example.org' } ];
226 ok($tf->handle_http($t) == 0, 'SERVER_NAME (absolute url in request line)');
228 ok($tf->stop_proc == 0, "Stopping lighttpd");
230 $tf->{CONFIGFILE} = 'bug-06.conf';
231 ok($tf->start_proc == 0, "Starting lighttpd with $tf->{CONFIGFILE}") or goto cleanup;
232 $t->{REQUEST} = ( <<EOF
233 GET /indexfile/ HTTP/1.0
234 Host: www.example.org
237 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/indexfile/index.php' } ];
238 ok($tf->handle_http($t) == 0, 'Bug #6');
240 ok($tf->stop_proc == 0, "Stopping lighttpd");
242 $tf->{CONFIGFILE} = 'bug-12.conf';
243 ok($tf->start_proc == 0, "Starting lighttpd with bug-12.conf") or goto cleanup;
244 $t->{REQUEST} = ( <<EOF
245 POST /indexfile/abc HTTP/1.0
246 Host: www.example.org
247 Content-Length: 0
250 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404, 'HTTP-Content' => '/indexfile/return-404.php' } ];
251 ok($tf->handle_http($t) == 0, 'Bug #12');
253 ok($tf->stop_proc == 0, "Stopping lighttpd");
256 SKIP: {
257 skip "PHP not started, cannot stop it", 1 unless $php_child != -1;
258 ok(0 == $tf->endspawnfcgi($php_child), "Stopping php");
259 $php_child = -1;
262 SKIP: {
263 skip "no fcgi-auth found", 5 unless -x $tf->{BASEDIR}."/tests/fcgi-auth" || -x $tf->{BASEDIR}."/tests/fcgi-auth.exe";
265 $tf->{CONFIGFILE} = 'fastcgi-auth.conf';
266 ok($tf->start_proc == 0, "Starting lighttpd with $tf->{CONFIGFILE}") or die();
267 $t->{REQUEST} = ( <<EOF
268 GET /index.html?ok HTTP/1.0
269 Host: www.example.org
272 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
273 ok($tf->handle_http($t) == 0, 'FastCGI - Auth');
275 $t->{REQUEST} = ( <<EOF
276 GET /index.html?fail HTTP/1.0
277 Host: www.example.org
280 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 403 } ];
281 ok($tf->handle_http($t) == 0, 'FastCGI - Auth');
283 $t->{REQUEST} = ( <<EOF
284 GET /expire/access.txt?ok HTTP/1.0
285 Host: www.example.org
288 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
289 ok($tf->handle_http($t) == 0, 'FastCGI - Auth in subdirectory');
291 ok($tf->stop_proc == 0, "Stopping lighttpd");
294 SKIP: {
295 skip "no php found", 5 unless $LightyTest::HAVE_PHP;
296 $tf->{CONFIGFILE} = 'fastcgi-13.conf';
297 ok($tf->start_proc == 0, "Starting lighttpd with $tf->{CONFIGFILE}") or die();
298 $t->{REQUEST} = ( <<EOF
299 GET /indexfile/index.php HTTP/1.0
300 Host: www.example.org
303 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
304 ok($tf->handle_http($t) == 0, 'FastCGI + local spawning');
306 $t->{REQUEST} = ( <<EOF
307 HEAD /indexfile/index.php HTTP/1.0
308 Host: www.example.org
311 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, '-Content-Length' => '0' } ];
312 # Of course a valid content-length != 0 would be ok, but we assume for now that such one is not generated.
313 ok($tf->handle_http($t) == 0, 'Check for buggy content length with HEAD');
315 $t->{REQUEST} = ( <<EOF
316 GET /get-env.php?env=MAIL HTTP/1.0
317 Host: www.example.org
320 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 , 'HTTP-Content' => '' } ];
321 ok($tf->handle_http($t) == 0, 'FastCGI + bin-copy-environment');
325 ok($tf->stop_proc == 0, "Stopping lighttpd");
329 SKIP: {
330 skip "no fcgi-responder found", 11 unless -x $tf->{BASEDIR}."/tests/fcgi-responder" || -x $tf->{BASEDIR}."/tests/fcgi-responder.exe";
332 $tf->{CONFIGFILE} = 'fastcgi-responder.conf';
333 ok($tf->start_proc == 0, "Starting lighttpd with $tf->{CONFIGFILE}") or die();
334 $t->{REQUEST} = ( <<EOF
335 GET /index.fcgi?lf HTTP/1.0
336 Host: www.example.org
339 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
340 ok($tf->handle_http($t) == 0, 'line-ending \n\n');
342 $t->{REQUEST} = ( <<EOF
343 GET /index.fcgi?crlf HTTP/1.0
344 Host: www.example.org
347 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
348 ok($tf->handle_http($t) == 0, 'line-ending \r\n\r\n');
350 $t->{REQUEST} = ( <<EOF
351 GET /index.fcgi?slow-lf HTTP/1.0
352 Host: www.example.org
355 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
356 ok($tf->handle_http($t) == 0, 'line-ending \n + \n');
358 $t->{REQUEST} = ( <<EOF
359 GET /index.fcgi?slow-crlf HTTP/1.0
360 Host: www.example.org
363 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
364 ok($tf->handle_http($t) == 0, 'line-ending \r\n + \r\n');
366 $t->{REQUEST} = ( <<EOF
367 GET /abc/def/ghi?path_info HTTP/1.0
368 Host: wsgi.example.org
371 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/abc/def/ghi' } ];
372 ok($tf->handle_http($t) == 0, 'PATH_INFO (wsgi)');
374 $t->{REQUEST} = ( <<EOF
375 GET /abc/def/ghi?script_name HTTP/1.0
376 Host: wsgi.example.org
379 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '' } ];
380 ok($tf->handle_http($t) == 0, 'SCRIPT_NAME (wsgi)');
383 $t->{REQUEST} = ( <<EOF
384 GET /index.fcgi?die-at-end HTTP/1.0
385 Host: www.example.org
388 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
389 ok($tf->handle_http($t) == 0, 'killing fastcgi and wait for restart');
391 select(undef, undef, undef, .2);
392 $t->{REQUEST} = ( <<EOF
393 GET /index.fcgi?die-at-end HTTP/1.0
394 Host: www.example.org
397 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
398 ok($tf->handle_http($t) == 0, 'killing fastcgi and wait for restart');
401 select(undef, undef, undef, .2);
402 $t->{REQUEST} = ( <<EOF
403 GET /index.fcgi?crlf HTTP/1.0
404 Host: www.example.org
407 $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
408 ok($tf->handle_http($t) == 0, 'regular response of after restart');
411 ok($tf->stop_proc == 0, "Stopping lighttpd");
414 exit 0;
416 cleanup: ;
418 $tf->endspawnfcgi($php_child) if $php_child != -1;
420 die();