Skip to content

Commit

Permalink
Refactored tests
Browse files Browse the repository at this point in the history
  • Loading branch information
spiritloose committed Oct 10, 2009
1 parent d92d8c7 commit b8e8623
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 153 deletions.
5 changes: 2 additions & 3 deletions Makefile.in
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ install: install-modules-yes


# cleanup # cleanup
clean: clean:
-rm -f mod_psgi.o mod_psgi.lo mod_psgi.slo mod_psgi.la rm -f mod_psgi.o mod_psgi.lo mod_psgi.slo mod_psgi.la
rm -f ppport.h rm -f ppport.h
$(MAKE) -C t clean


realclean: clean realclean: clean
rm -f Makefile rm -f Makefile
Expand All @@ -52,7 +51,7 @@ ppport.h:
testconf: testconf:
$(MAKE) -C t conf $(MAKE) -C t conf


test: reload test:
$(MAKE) -C t test $(MAKE) -C t test


# install and activate shared object by reloading Apache to # install and activate shared object by reloading Apache to
Expand Down
2 changes: 1 addition & 1 deletion t/00_output.t
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ BEGIN {


return eval_response_app if running_in_mod_psgi; return eval_response_app if running_in_mod_psgi;


run_eval_request; run_server_tests;


__END__ __END__
Expand Down
2 changes: 1 addition & 1 deletion t/01_lint.t
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use t::TestModPSGI;


return eval_body_app if running_in_mod_psgi; return eval_body_app if running_in_mod_psgi;


run_eval_request; run_server_tests;


__END__ __END__
Expand Down
2 changes: 1 addition & 1 deletion t/02_input.t
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use t::TestModPSGI;


return eval_body_app if running_in_mod_psgi; return eval_body_app if running_in_mod_psgi;


run_eval_request; run_server_tests;


__END__ __END__
Expand Down
2 changes: 1 addition & 1 deletion t/03_errors.t
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use t::TestModPSGI;


return eval_body_app if running_in_mod_psgi; return eval_body_app if running_in_mod_psgi;


run_eval_request; run_server_tests;


__END__ __END__
Expand Down
2 changes: 1 addition & 1 deletion t/04_die.t
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use t::TestModPSGI;


return eval_body_app if running_in_mod_psgi; return eval_body_app if running_in_mod_psgi;


run_eval_request; run_server_tests;


__END__ __END__
Expand Down
17 changes: 5 additions & 12 deletions t/Makefile.in
Original file line number Original file line Diff line number Diff line change
@@ -1,13 +1,6 @@
APXS=@APXS@ APXS=@APXS@
AP_SBINDIR=$(shell $(APXS) -q sbindir) TESTS=.
AP_PROGNAME=$(shell $(APXS) -q progname) all: test
HTTPD=$(AP_SBINDIR)/$(AP_PROGNAME) test:
all: conf APXS=$(APXS) prove -I.. $(TESTS)
test: test.conf .PHONY: test
prove -I.. .
conf: test.conf
test.conf: test.conf.tt *.t mkconf.pl
./mkconf.pl --input $< --output $@
clean:
rm -f test.conf
.PHONY: conf clean test
74 changes: 54 additions & 20 deletions t/TestModPSGI.pm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ use Test::Base -Base;
use File::Basename; use File::Basename;
use URI::Escape; use URI::Escape;
use List::Util qw(sum); use List::Util qw(sum);
use Test::TCP;
use File::Temp;


our @EXPORT = qw( our @EXPORT = qw(
running_in_mod_psgi eval_body_app eval_response_app running_in_mod_psgi eval_body_app eval_response_app
run_eval_request run_server_tests
); );


our $Host = '127.0.0.1';
our $Path = '/psgi/t';

BEGIN { BEGIN {
no warnings 'redefine'; no warnings 'redefine';
*Test::Base::run_compare = sub {}; # XXX *Test::Base::run_compare = sub {}; # XXX
Expand Down Expand Up @@ -49,13 +48,12 @@ sub ua() {
} }


sub eval_request($$$;@) { sub eval_request($$$;@) {
my ($file, $method, $code, @args) = @_; my ($port, $method, $code, @args) = @_;
if (ref $code eq 'CODE') { if (ref $code eq 'CODE') {
no warnings 'prototype'; no warnings 'prototype';
return eval_request($file, $method, $code->(), @args); return eval_request($port, $method, $code->(), @args);
} }
my $uri = sprintf 'http://%s%s/%s?%s', $Host, $Path, my $uri = sprintf 'http://localhost:%d/?%s', $port, uri_escape($code);
basename($file), uri_escape($code);
$method = lc $method; $method = lc $method;
ua->$method($uri, @args); ua->$method($uri, @args);
} }
Expand Down Expand Up @@ -100,19 +98,55 @@ sub compare($$$;@) {
} }
} }


sub run_eval_request() { our $TestFile;

sub run_httpd($) {
my $port = shift;
my $tmpdir = $ENV{APACHE2_TMP_DIR} || File::Temp::tempdir(CLEANUP => 1);
chomp(my $libexecdir = `$ENV{APXS} -q libexecdir`);
chomp(my $sbindir = `$ENV{APXS} -q sbindir`);
chomp(my $progname = `$ENV{APXS} -q progname`);
my $httpd = "$sbindir/$progname";
my $conf = <<"END_CONF";
LoadModule psgi_module $libexecdir/mod_psgi.so
PidFile $tmpdir/httpd.pid
LockFile $tmpdir/httpd.lock
ErrorLog $tmpdir/error_log
Listen $port
<Location />
SetHandler psgi
PSGIApp $TestFile
</Location>
END_CONF
open my $fh, '>', "$tmpdir/httpd.conf" or die $!;
print $fh $conf;
close $fh;
exec "$httpd -X -D FOREGROUND -f $tmpdir/httpd.conf";
}

sub run_server_tests() {
my ($pkg, $file) = caller; my ($pkg, $file) = caller;
setup_tests; $TestFile = $file;
run { test_tcp(
my $block = shift; client => sub {
my $req = $block->request; my $port = shift;
my $res = eval_request($file, $req->{method}, $req->{code}, setup_tests;
@{$req->{args}}); run {
my $response = $block->response; my $block = shift;
while (my ($input, $expected) = each %$response) { my $req = $block->request;
compare($res, $input, $expected); my $res = eval_request($port, $req->{method}, $req->{code}, @{$req->{args}});
} my $response = $block->response;
}; #local $Test::Builder::Level = $Test::Builder::Level + 3;
while (my ($input, $expected) = each %$response) {
compare($res, $input, $expected);
}
};
},
server => sub {
my $port = shift;
run_httpd($port);
},
);
} }


1; 1;
60 changes: 0 additions & 60 deletions t/mkconf.pl

This file was deleted.

52 changes: 7 additions & 45 deletions t/suite.t
Original file line number Original file line Diff line number Diff line change
@@ -1,52 +1,14 @@
use strict; use strict;
use warnings; use warnings;
use Test::More; use Test::More;
use File::Temp;
use Plack::Test::Suite; use Plack::Test::Suite;
require t::TestModPSGI;


warn $ENV{HTTPD}; BEGIN {
Plack::Test::Suite->run_server_tests(\&run_httpd); no warnings 'once';
done_testing(); $t::TestModPSGI::TestFile = __FILE__;

sub run_httpd {
my $port = shift;

my $tmpdir = $ENV{APACHE2_TMP_DIR} || File::Temp::tempdir( CLEANUP => 1 );

write_file("$tmpdir/app.psgi", _render_psgi());
write_file("$tmpdir/httpd.conf", _render_conf($tmpdir, $port, "$tmpdir/app.psgi"));

exec "httpd -X -D FOREGROUND -f $tmpdir/httpd.conf";
}

sub write_file {
my($path, $content) = @_;

open my $out, ">", $path or die "$path: $!";
print $out $content;
} }


sub _render_psgi { return Plack::Test::Suite->test_app_handler if t::TestModPSGI::running_in_mod_psgi();
return <<'EOF'; Plack::Test::Suite->run_server_tests(\&t::TestModPSGI::run_httpd);
use lib "lib"; done_testing();
use Plack::Test::Suite;
Plack::Test::Suite->test_app_handler;
EOF
}

sub _render_conf {
my ($tmpdir, $port, $psgi_path) = @_;
<<"END";
LoadModule psgi_module modules/mod_psgi.so
PidFile $tmpdir/httpd.pid
LockFile $tmpdir/httpd.lock
ErrorLog $tmpdir/error_log
Listen $port
<Location />
SetHandler psgi
PSGIApp $tmpdir/app.psgi
</Location>
END
}
8 changes: 0 additions & 8 deletions t/test.conf.tt

This file was deleted.

0 comments on commit b8e8623

Please sign in to comment.