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 => 9;
12use LightyTest;
13
14my $tf_real = LightyTest->new();
15my $tf_proxy = LightyTest->new();
16
17my $t;
18my $php_child = -1;
19
20my $phpbin = (defined $ENV{'PHP'} ? $ENV{'PHP'} : '/usr/bin/php-cgi');
21$ENV{'PHP'} = $phpbin;
22
23SKIP: {
24	skip "PHP already running on port 1026", 1 if $tf_real->listening_on(1026);
25	skip "no php binary found", 1 unless -x $phpbin;
26	ok(-1 != ($php_child = $tf_real->spawnfcgi($phpbin, 1026)), "Spawning php");
27}
28
29## we need two procs
30## 1. the real webserver
31## 2. the proxy server
32
33$tf_real->{PORT} = 2048;
34$tf_real->{CONFIGFILE} = 'lighttpd.conf';
35
36$tf_proxy->{PORT} = 2050;
37$tf_proxy->{CONFIGFILE} = 'proxy.conf';
38
39ok($tf_real->start_proc == 0, "Starting lighttpd") or goto cleanup;
40
41ok($tf_proxy->start_proc == 0, "Starting lighttpd as proxy") or goto cleanup;
42
43$t->{REQUEST}  = ( <<EOF
44GET /index.html HTTP/1.0
45Host: www.example.org
46EOF
47 );
48$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
49ok($tf_proxy->handle_http($t) == 0, 'valid request');
50
51$t->{REQUEST}  = ( <<EOF
52GET /index.html HTTP/1.0
53Host: www.example.org
54EOF
55 );
56$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'Server' => 'Apache 1.3.29' } ];
57ok($tf_proxy->handle_http($t) == 0, 'drop Server from real server');
58
59SKIP: {
60	skip "no PHP running on port 1026", 1 unless $tf_real->listening_on(1026);
61	$t->{REQUEST}  = ( <<EOF
62GET /rewrite/all/some+test%3axxx%20with%20space HTTP/1.0
63Host: www.example.org
64EOF
65 );
66	$t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200, 'HTTP-Content' => '/some+test%3axxx%20with%20space' } ];
67	ok($tf_proxy->handle_http($t) == 0, 'rewrited urls work with encoded path');
68}
69
70ok($tf_proxy->stop_proc == 0, "Stopping lighttpd proxy");
71
72ok($tf_real->stop_proc == 0, "Stopping lighttpd");
73
74SKIP: {
75	skip "PHP not started, cannot stop it", 1 unless $php_child != -1;
76	ok(0 == $tf_real->endspawnfcgi($php_child), "Stopping php");
77	$php_child = -1;
78}
79
80exit 0;
81
82cleanup:
83
84$tf_real->endspawnfcgi($php_child) if $php_child != -1;
85$tf_real->stop_proc;
86$tf_proxy->stop_proc;
87
88die();
89