xref: /lighttpd1.4/tests/mod-fastcgi.t (revision 9a37ba54)
1#!/usr/bin/env perl
2BEGIN {
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;
7}
8
9use strict;
10use Test::More tests => 24;
11use LightyTest;
12
13my $tf = LightyTest->new();
14
15my $t;
16
17SKIP: {
18	skip "no fcgi-responder found", 24
19	  unless (   -x $tf->{BASEDIR}."/tests/fcgi-responder"
20		  || -x $tf->{BASEDIR}."/tests/fcgi-responder.exe");
21
22	my $ephemeral_port = LightyTest->get_ephemeral_tcp_port();
23	$ENV{EPHEMERAL_PORT} = $ephemeral_port;
24
25	$tf->{CONFIGFILE} = 'fastcgi-responder.conf';
26	ok($tf->start_proc == 0, "Starting lighttpd with $tf->{CONFIGFILE}") or die();
27
28	$t->{REQUEST}  = ( <<EOF
29GET /prefix.fcgi-nonexistent HTTP/1.0
30Host: www.example.org
31EOF
32 );
33	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 404 } ];
34	ok($tf->handle_http($t) == 0, 'file not found');
35
36	$t->{REQUEST}  = ( <<EOF
37GET /prefix.fcgi?env=SCRIPT_NAME HTTP/1.0
38EOF
39 );
40	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/prefix.fcgi' } ];
41	ok($tf->handle_http($t) == 0, 'SCRIPT_NAME');
42
43	$t->{REQUEST}  = ( <<EOF
44GET /prefix.fcgi/foo/bar?env=SCRIPT_NAME HTTP/1.0
45EOF
46 );
47	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/prefix.fcgi' } ];
48	ok($tf->handle_http($t) == 0, 'SCRIPT_NAME w/ PATH_INFO');
49
50	$t->{REQUEST}  = ( <<EOF
51GET /prefix.fcgi/foo/bar?env=PATH_INFO HTTP/1.0
52EOF
53 );
54	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/foo/bar' } ];
55	ok($tf->handle_http($t) == 0, 'PATH_INFO');
56
57	$t->{REQUEST} = ( <<EOF
58GET /phpinfo.php HTTP/1.0
59Host: www.example.org
60EOF
61 );
62	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
63	ok($tf->handle_http($t) == 0, 'valid request');
64
65	$t->{REQUEST}  = ( <<EOF
66GET /get-server-env.php?env=USER HTTP/1.0
67Host: bin-env.example.org
68EOF
69 );
70	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 , 'HTTP-Content' => $ENV{USER} } ];
71	ok($tf->handle_http($t) == 0, 'FastCGI + bin-copy-environment');
72
73	$t->{REQUEST}  = ( <<EOF
74GET /get-server-env.php?env=MAIL HTTP/1.0
75Host: bin-env.example.org
76EOF
77 );
78	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 , 'HTTP-Content' => '' } ];
79	ok($tf->handle_http($t) == 0, 'FastCGI + bin-copy-environment');
80
81SKIP: {
82	skip "no crypt-des under openbsd", 2 if $^O eq 'openbsd';
83
84	$t->{REQUEST}  = ( <<EOF
85GET /get-server-env.php?env=REMOTE_USER HTTP/1.0
86Host: auth.example.org
87Authorization: Basic ZGVzOmRlcw==
88EOF
89 );
90	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'des' } ];
91	ok($tf->handle_http($t) == 0, '$_SERVER["REMOTE_USER"]');
92
93	$t->{REQUEST}  = ( <<EOF
94GET /get-server-env.php?env=AUTH_TYPE HTTP/1.0
95Host: auth.example.org
96Authorization: Basic ZGVzOmRlcw==
97EOF
98 );
99	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'Basic' } ];
100	ok($tf->handle_http($t) == 0, '$_SERVER["AUTH_TYPE"]');
101}
102
103	$t->{REQUEST}  = ( <<EOF
104GET /index.html?auth-ok HTTP/1.0
105Host: auth.example.org
106EOF
107 );
108	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
109	ok($tf->handle_http($t) == 0, 'FastCGI - Auth');
110
111	$t->{REQUEST}  = ( <<EOF
112GET /index.html?auth-fail HTTP/1.0
113Host: auth.example.org
114EOF
115 );
116	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 403 } ];
117	ok($tf->handle_http($t) == 0, 'FastCGI - Auth');
118
119	$t->{REQUEST}  = ( <<EOF
120GET /expire/access.txt?auth-ok HTTP/1.0
121Host: auth.example.org
122EOF
123 );
124	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
125	ok($tf->handle_http($t) == 0, 'FastCGI - Auth in subdirectory');
126
127	$t->{REQUEST}  = ( <<EOF
128GET /index.fcgi?auth-varfail HTTP/1.0
129Host: auth.example.org
130EOF
131 );
132	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 403 } ];
133	ok($tf->handle_http($t) == 0, 'FastCGI - Auth Fail with FastCGI responder afterwards');
134
135	$t->{REQUEST}  = ( <<EOF
136GET /index.fcgi?auth-var HTTP/1.0
137Host: auth.example.org
138EOF
139 );
140	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'LighttpdTestContent' } ];
141	ok($tf->handle_http($t) == 0, 'FastCGI - Auth Success with Variable- to Env expansion');
142
143	$t->{REQUEST}  = ( <<EOF
144GET /index.fcgi?lf HTTP/1.0
145Host: www.example.org
146EOF
147 );
148	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
149	ok($tf->handle_http($t) == 0, 'line-ending \n\n');
150
151	$t->{REQUEST}  = ( <<EOF
152GET /index.fcgi?crlf HTTP/1.0
153Host: www.example.org
154EOF
155 );
156	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
157	ok($tf->handle_http($t) == 0, 'line-ending \r\n\r\n');
158
159	$t->{REQUEST}  = ( <<EOF
160GET /index.fcgi?slow-lf HTTP/1.0
161Host: www.example.org
162EOF
163 );
164	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
165	ok($tf->handle_http($t) == 0, 'line-ending \n + \n');
166
167	$t->{REQUEST}  = ( <<EOF
168GET /index.fcgi?slow-crlf HTTP/1.0
169Host: www.example.org
170EOF
171 );
172	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
173	ok($tf->handle_http($t) == 0, 'line-ending \r\n + \r\n');
174
175	$t->{REQUEST}  = ( <<EOF
176GET /abc/def/ghi?env=PATH_INFO HTTP/1.0
177Host: wsgi.example.org
178EOF
179 );
180	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/abc/def/ghi' } ];
181	ok($tf->handle_http($t) == 0, 'PATH_INFO (wsgi)');
182
183	$t->{REQUEST}  = ( <<EOF
184GET /abc/def/ghi?env=SCRIPT_NAME HTTP/1.0
185Host: wsgi.example.org
186EOF
187 );
188	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '' } ];
189	ok($tf->handle_http($t) == 0, 'SCRIPT_NAME (wsgi)');
190
191
192    # skip timing-sensitive test during CI testing, but run for user 'gps'
193    my $user = `id -un`;
194    chomp($user) if $user;
195    if (($user || "") eq "gps") {
196	$t->{REQUEST}  = ( <<EOF
197GET /index.fcgi?die-at-end HTTP/1.0
198Host: www.example.org
199EOF
200 );
201    }
202    else {
203	$t->{REQUEST}  = ( <<EOF
204GET /index.fcgi?crlf HTTP/1.0
205Host: www.example.org
206EOF
207 );
208    }
209	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
210	ok($tf->handle_http($t) == 0, 'killing fastcgi and wait for restart');
211
212	# (might take lighttpd 1 sec to detect backend exit)
213	for (my $c = 2*30; $c && 0 == $tf->listening_on($ephemeral_port); --$c) {
214		select(undef, undef, undef, 0.05);
215	}
216	$t->{REQUEST}  = ( <<EOF
217GET /index.fcgi?crlf HTTP/1.0
218Host: www.example.org
219EOF
220 );
221	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
222	ok($tf->handle_http($t) == 0, 'regular response of after restart');
223
224
225	ok($tf->stop_proc == 0, "Stopping lighttpd");
226}
227