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 => 10;
11use LightyTest;
12
13my $tf = LightyTest->new();
14my $t;
15
16SKIP: {
17	skip "no scgi-responder found", 10 unless -x $tf->{BASEDIR}."/tests/scgi-responder" || -x $tf->{BASEDIR}."/tests/scgi-responder.exe";
18
19	my $ephemeral_port = LightyTest->get_ephemeral_tcp_port();
20	$ENV{EPHEMERAL_PORT} = $ephemeral_port;
21
22	$tf->{CONFIGFILE} = 'scgi-responder.conf';
23	ok($tf->start_proc == 0, "Starting lighttpd with $tf->{CONFIGFILE}") or die();
24
25	$t->{REQUEST}  = ( <<EOF
26GET /index.scgi?lf HTTP/1.0
27Host: www.example.org
28EOF
29 );
30	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
31	ok($tf->handle_http($t) == 0, 'line-ending \n\n');
32
33	$t->{REQUEST}  = ( <<EOF
34GET /index.scgi?crlf HTTP/1.0
35Host: www.example.org
36EOF
37 );
38	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
39	ok($tf->handle_http($t) == 0, 'line-ending \r\n\r\n');
40
41	$t->{REQUEST}  = ( <<EOF
42GET /index.scgi?slow-lf HTTP/1.0
43Host: www.example.org
44EOF
45 );
46	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
47	ok($tf->handle_http($t) == 0, 'line-ending \n + \n');
48
49	$t->{REQUEST}  = ( <<EOF
50GET /index.scgi?slow-crlf HTTP/1.0
51Host: www.example.org
52EOF
53 );
54	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
55	ok($tf->handle_http($t) == 0, 'line-ending \r\n + \r\n');
56
57	$t->{REQUEST}  = ( <<EOF
58GET /abc/def/ghi?env=PATH_INFO HTTP/1.0
59Host: wsgi.example.org
60EOF
61 );
62	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/abc/def/ghi' } ];
63	ok($tf->handle_http($t) == 0, 'PATH_INFO (wsgi)');
64
65	$t->{REQUEST}  = ( <<EOF
66GET /abc/def/ghi?env=SCRIPT_NAME HTTP/1.0
67Host: wsgi.example.org
68EOF
69 );
70	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '' } ];
71	ok($tf->handle_http($t) == 0, 'SCRIPT_NAME (wsgi)');
72
73
74    # skip timing-sensitive test during CI testing, but run for user 'gps'
75    my $user = `id -un`;
76    chomp($user) if $user;
77    if (($user || "") eq "gps") {
78	$t->{REQUEST}  = ( <<EOF
79GET /index.scgi?die-at-end HTTP/1.0
80Host: www.example.org
81EOF
82 );
83    }
84    else {
85	$t->{REQUEST}  = ( <<EOF
86GET /index.scgi?crlf HTTP/1.0
87Host: www.example.org
88EOF
89 );
90    }
91	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
92	ok($tf->handle_http($t) == 0, 'killing scgi and wait for restart');
93
94	# (might take lighttpd 1 sec to detect backend exit)
95	for (my $c = 2*30; $c && 0 == $tf->listening_on($ephemeral_port); --$c) {
96		select(undef, undef, undef, 0.05);
97	}
98	$t->{REQUEST}  = ( <<EOF
99GET /index.scgi?crlf HTTP/1.0
100Host: www.example.org
101EOF
102 );
103	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'test123' } ];
104	ok($tf->handle_http($t) == 0, 'regular response of after restart');
105
106
107	ok($tf->stop_proc == 0, "Stopping lighttpd");
108}
109