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 IO::Socket;
11use Test::More tests => 8;
12use LightyTest;
13
14my $tf = LightyTest->new();
15my $t;
16my $php_child = -1;
17
18my $phpbin = (defined $ENV{'PHP'} ? $ENV{'PHP'} : '/usr/bin/php-cgi');
19
20SKIP: {
21	skip "PHP already running on port 1026", 1 if $tf->listening_on(1026);
22	skip "no php binary found", 1 unless -x $phpbin;
23	ok(-1 != ($php_child = $tf->spawnfcgi($phpbin, 1026)), "Spawning php");
24}
25
26SKIP: {
27	skip "no PHP running on port 1026", 6 unless $tf->listening_on(1026);
28
29	ok($tf->start_proc == 0, "Starting lighttpd") or goto cleanup;
30
31	$t->{REQUEST}  = ( <<EOF
32GET /rewrite/foo HTTP/1.0
33Host: www.example.org
34EOF
35 );
36	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '' } ];
37	ok($tf->handle_http($t) == 0, 'valid request');
38
39	$t->{REQUEST}  = ( <<EOF
40GET /rewrite/foo?a=b HTTP/1.0
41Host: www.example.org
42EOF
43 );
44	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'a=b' } ];
45	ok($tf->handle_http($t) == 0, 'valid request');
46
47	$t->{REQUEST}  = ( <<EOF
48GET /rewrite/bar?a=b HTTP/1.0
49Host: www.example.org
50EOF
51 );
52	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'bar&a=b' } ];
53	ok($tf->handle_http($t) == 0, 'valid request');
54
55	$t->{REQUEST}  = ( <<EOF
56GET /rewrite/nofile?a=b HTTP/1.0
57Host: www.example.org
58EOF
59 );
60	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => 'file=/rewrite/nofile&a=b' } ];
61	ok($tf->handle_http($t) == 0, 'not existing file rewrite');
62
63	ok($tf->stop_proc == 0, "Stopping lighttpd");
64}
65
66SKIP: {
67	skip "PHP not started, cannot stop it", 1 unless $php_child != -1;
68	ok(0 == $tf->endspawnfcgi($php_child), "Stopping php");
69}
70
71
72exit 0;
73
74cleanup: ;
75
76$tf->endspawnfcgi($php_child) if $php_child != -1;
77
78die();
79