added tests for mod-proxy

git-svn-id: svn://svn.lighttpd.net/lighttpd/branches/lighttpd-1.3.x@351 152afb58-edef-0310-8abb-c4023f1b3aa9
svn/tags/lighttpd-1.3.14
Jan Kneschke 18 years ago
parent b5e848d5b4
commit cee3c0d880

@ -0,0 +1,228 @@
#! /usr/bin/perl -w
use strict;
use IO::Socket;
use Test::More tests => 5;
my $basedir = (defined $ENV{'top_builddir'} ? $ENV{'top_builddir'} : '..');
my $srcdir = (defined $ENV{'srcdir'} ? $ENV{'srcdir'} : '.');
my $testname;
my @request;
my @response;
my $configfile = 'lighttpd.conf';
my $lighttpd_path = $basedir.'/src/lighttpd';
my $pidfile = '/tmp/lighttpd/lighttpd.pid';
my $pidoffile = '/tmp/lighttpd/pidof.pid';
# proxy is on 2049, real server on 2048
my $port = 2049;
sub pidof {
my $prog = $_[0];
open F, "ps ax | grep $prog | grep -v grep | awk '{ print \$1 }'|" or
open F, "ps -ef | grep $prog | grep -v grep | awk '{ print \$2 }'|" or
return -1;
my $pid = <F>;
close F;
if (defined $pid) { return $pid; }
return -1;
}
sub stop_proc {
open F, $pidfile or return -1;
my $pid = <F>;
close F;
if (defined $pid) {
kill('TERM',$pid) or return -1;
select(undef, undef, undef, 0.01);
}
return 0;
}
sub start_proc {
# kill old proc if necessary
stop_proc;
# pre-process configfile if necessary
#
my $pwd = `pwd`;
chomp($pwd);
unlink("/tmp/cfg.file");
system("cat ".$srcdir."/".$configfile.' | perl -pe "s#\@SRCDIR\@#'.$pwd.'/'.$basedir.'/tests/#" > /tmp/cfg.file');
unlink($pidfile);
system($lighttpd_path." -f /tmp/cfg.file");
unlink("/tmp/cfg.file");
if (-e $pidfile) {
return 0;
} else {
return -1;
}
}
sub handle_http {
my $EOL = "\015\012";
my $BLANK = $EOL x 2;
my $host = "127.0.0.1";
my $remote =
IO::Socket::INET->new(Proto => "tcp",
PeerAddr => $host,
PeerPort => $port)
or return -1;
$remote->autoflush(1);
foreach(@request) {
# pipeline requests
s/\r//g;
s/\n/$EOL/g;
print $remote $_.$BLANK;
}
my $lines = "";
# read everything
while(<$remote>) {
$lines .= $_;
}
close $remote;
my $href;
foreach $href (@response) {
# first line is always response header
my %resp_hdr;
my $resp_body;
my $resp_line;
my $conditions = $_;
for (my $ln = 0; defined $lines; $ln++) {
(my $line, $lines) = split($EOL, $lines, 2);
# header finished
last if(length($line) == 0);
if ($ln == 0) {
# response header
$resp_line = $line;
} else {
# response vars
if ($line =~ /^([^:]+):\s*(.+)$/) {
(my $h = $1) =~ tr/[A-Z]/[a-z]/;
$resp_hdr{$h} = $2;
} else {
return -1;
}
}
}
# check length
if (defined $resp_hdr{"content-length"}) {
($resp_body, $lines) = split("^.".$resp_hdr{"content-length"}, $lines, 2);
} else {
$resp_body = $lines;
undef $lines;
}
# check conditions
if ($resp_line =~ /^(HTTP\/1\.[01]) ([0-9]{3}) .+$/) {
if ($href->{'HTTP-Protocol'} ne $1) {
diag(sprintf("proto failed: expected '%s', got '%s'\n", $href->{'HTTP-Protocol'}, $1));
return -1;
}
if ($href->{'HTTP-Status'} ne $2) {
diag(sprintf("status failed: expected '%s', got '%s'\n", $href->{'HTTP-Status'}, $2));
return -1;
}
} else {
return -1;
}
if (defined $href->{'HTTP-Content'}) {
if ($href->{'HTTP-Content'} ne $resp_body) {
diag(sprintf("body failed: expected '%s', got '%s'\n", $href->{'HTTP-Content'}, $resp_body));
return -1;
}
}
if (defined $href->{'-HTTP-Content'}) {
if (defined $resp_body && $resp_body ne '') {
diag(sprintf("body failed: expected empty body, got '%s'\n", $resp_body));
return -1;
}
}
foreach (keys %{ $href }) {
next if $_ eq 'HTTP-Protocol';
next if $_ eq 'HTTP-Status';
next if $_ eq 'HTTP-Content';
next if $_ eq '-HTTP-Content';
(my $k = $_) =~ tr/[A-Z]/[a-z]/;
my $no_val = 0;
if (substr($k, 0, 1) eq '+') {
$k = substr($k, 1);
$no_val = 1;
}
if (!defined $resp_hdr{$k}) {
diag(sprintf("required header '%s' is missing\n", $k));
return -1;
}
if ($no_val == 0 &&
$href->{$_} ne $resp_hdr{$k}) {
diag(sprintf("response-header failed: expected '%s', got '%s'\n", $href->{$_}, $resp_hdr{$k}));
return -1;
}
}
}
# we should have sucked up everything
return -1 if (defined $lines);
return 0;
}
## we need two procs
## 1. the real webserver
## 2. the proxy server
$configfile = 'lighttpd.conf';
$pidfile = '/tmp/lighttpd/lighttpd.pid';
ok(start_proc == 0, "Starting lighttpd") or die();
$configfile = 'proxy.conf';
$pidfile = '/tmp/lighttpd/lighttpd-proxy.pid';
ok(start_proc == 0, "Starting lighttpd as proxy") or die();
@request = ( <<EOF
GET /phpinfo.php HTTP/1.0
Host: www.example.org
EOF
);
@response = ( { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } );
ok(handle_http == 0, 'valid request');
$pidfile = '/tmp/lighttpd/lighttpd-proxy.pid';
ok(stop_proc == 0, "Stopping lighttpd proxy");
$pidfile = '/tmp/lighttpd/lighttpd.pid';
ok(stop_proc == 0, "Stopping lighttpd");

@ -0,0 +1,155 @@
server.document-root = "/tmp/lighttpd/servers/www.example.org/pages/"
server.pid-file = "/tmp/lighttpd/lighttpd-proxy.pid"
## bind to port (default: 80)
server.port = 2050
## bind to localhost (default: all interfaces)
server.bind = "localhost"
server.errorlog = "/tmp/lighttpd/logs/lighttpd.error.log"
server.name = "www.example.org"
server.tag = "Apache 1.3.29"
server.dir-listing = "enable"
#server.event-handler = "linux-sysepoll"
#server.event-handler = "linux-rtsig"
#server.modules.path = ""
server.modules = (
"mod_rewrite",
"mod_setenv",
"mod_access",
"mod_auth",
# "mod_httptls",
"mod_status",
"mod_expire",
"mod_simple_vhost",
"mod_redirect",
# "mod_evhost",
# "mod_localizer",
"mod_fastcgi",
"mod_proxy",
"mod_cgi",
"mod_compress",
"mod_userdir",
"mod_accesslog" )
server.indexfiles = ( "index.php", "index.html",
"index.htm", "default.htm" )
######################## MODULE CONFIG ############################
accesslog.filename = "/tmp/lighttpd/logs/lighttpd.access.log"
mimetype.assign = ( ".png" => "image/png",
".jpg" => "image/jpeg",
".jpeg" => "image/jpeg",
".gif" => "image/gif",
".html" => "text/html",
".htm" => "text/html",
".pdf" => "application/pdf",
".swf" => "application/x-shockwave-flash",
".spl" => "application/futuresplash",
".txt" => "text/plain",
".tar.gz" => "application/x-tgz",
".tgz" => "application/x-tgz",
".gz" => "application/x-gzip",
".c" => "text/plain",
".conf" => "text/plain" )
compress.cache-dir = "/tmp/lighttpd/cache/compress/"
compress.filetype = ("text/plain", "text/html")
setenv.add-environment = ( "TRAC_ENV" => "foo")
setenv.add-request-header = ( "FOO" => "foo")
setenv.add-response-header = ( "BAR" => "foo")
proxy.server = ( "" => (
"grisu" => (
"host" => "127.0.0.1",
"port" => 2048,
)
)
)
cgi.assign = ( ".pl" => "/usr/bin/perl",
".cgi" => "/usr/bin/perl",
".py" => "/usr/bin/python" )
userdir.include-user = ( "jan" )
userdir.path = "/"
ssl.engine = "disable"
ssl.pemfile = "server.pem"
auth.backend = "plain"
auth.backend.plain.userfile = "/tmp/lighttpd/lighttpd.user"
auth.backend.plain.groupfile = "lighttpd.group"
auth.backend.ldap.hostname = "localhost"
auth.backend.ldap.base-dn = "dc=my-domain,dc=com"
auth.backend.ldap.filter = "(uid=$)"
auth.require = ( "/server-status" =>
(
"method" => "digest",
"realm" => "download archiv",
# "require" => ("group=www", "user=jan", "host=192.168.2.10")
"require" => "group=www|user=jan|host=192.168.2.10"
),
"/auth.php" =>
(
"method" => "basic",
"realm" => "download archiv",
# "require" => ("group=www", "user=jan", "host=192.168.2.10")
"require" => "user=jan"
),
"/server-config" =>
(
"method" => "basic",
"realm" => "download archiv",
# "require" => ("group=www", "user=jan", "user=weigon", "host=192.168.2.10")
"require" => "group=www|user=jan|host=192.168.2.10"
)
)
url.access-deny = ( "~", ".inc")
url.redirect = ( "^/redirect/$" => "http://localhost:2048/" )
url.rewrite = ( "^/rewrite/foo($|\?.+)" => "/indexfile/rewrite.php$1",
"^/rewrite/bar(?:$|\?(.+))" => "/indexfile/rewrite.php?bar&$1" )
expire.url = ( "/expire/access" => "access 2 hours",
"/expire/modification" => "access plus 1 seconds 2 minutes")
#cache.cache-dir = "/home/weigon/wwwroot/cache/"
#### status module
status.status-url = "/server-status"
status.config-url = "/server-config"
$HTTP["host"] == "vvv.example.org" {
server.document-root = "/tmp/lighttpd/servers/www.example.org/pages/"
}
$HTTP["host"] == "zzz.example.org" {
server.document-root = "/tmp/lighttpd/servers/www.example.org/pages/"
server.name = "zzz.example.org"
}
$HTTP["host"] == "no-simple.example.org" {
server.document-root = "/tmp/lighttpd/servers/123.example.org/pages/"
server.name = "zzz.example.org"
}
$HTTP["host"] !~ "(no-simple\.example\.org)" {
simple-vhost.document-root = "pages"
simple-vhost.server-root = "/tmp/lighttpd/servers/"
simple-vhost.default-host = "www.example.org"
}
Loading…
Cancel
Save