From: Eric Wong Date: Mon, 30 Sep 2024 21:30:04 +0000 (+0000) Subject: t/www_static: test with our -httpd server, too X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bbd5f0d335cd04fe654a3a45c0cefb79530cf4bb;p=thirdparty%2Fpublic-inbox.git t/www_static: test with our -httpd server, too While our current HTTP implementation doesn't make special allowances for static files, it only hurts a little to fork off -httpd instances and ensure any future changes work as expected. --- diff --git a/lib/PublicInbox/TestCommon.pm b/lib/PublicInbox/TestCommon.pm index 913aa7e9b..6c3677d2d 100644 --- a/lib/PublicInbox/TestCommon.pm +++ b/lib/PublicInbox/TestCommon.pm @@ -949,17 +949,21 @@ sub create_inbox ($;@) { sub test_httpd ($$;$$) { my ($env, $client, $skip, $cb) = @_; my ($tmpdir, $for_destroy); + my $psgi = delete $env->{psgi_file}; + if (!defined $psgi) { + for (qw(PI_CONFIG)) { $env->{$_} or BAIL_OUT "$_ unset" } + } $env->{TMPDIR} //= do { ($tmpdir, $for_destroy) = tmpdir(); $tmpdir; }; - for (qw(PI_CONFIG)) { $env->{$_} or BAIL_OUT "$_ unset" } SKIP: { require_mods qw(Plack::Test::ExternalServer LWP::UserAgent -httpd), $skip // 1; my $sock = tcp_server() or die; my ($out, $err) = map { "$env->{TMPDIR}/std$_.log" } qw(out err); my $cmd = [ qw(-httpd -W0), "--stdout=$out", "--stderr=$err" ]; + push @$cmd, $psgi if defined $psgi; my $td = start_script($cmd, $env, { 3 => $sock }); my ($h, $p) = tcp_host_port($sock); local $ENV{PLACK_TEST_EXTERNALSERVER_URI} = "http://$h:$p"; diff --git a/t/www_static.t b/t/www_static.t index 48d120d68..b1181cab8 100644 --- a/t/www_static.t +++ b/t/www_static.t @@ -8,15 +8,23 @@ use PublicInbox::IO qw(write_file); my $tmpdir = tmpdir(); require_mods qw(psgi); require IO::Uncompress::Gunzip; +use File::Path qw(remove_tree); use_ok 'PublicInbox::WwwStatic'; -my $app = sub { - my $ws = PublicInbox::WwwStatic->new(docroot => "$tmpdir", @_); - sub { $ws->call(shift) }; +my $psgi_env = sub { # @_ is passed to WwwStatic->new + my $ret = "$tmpdir/www_static.psgi"; + write_file '>', $ret, <new(docroot => "$tmpdir" @_); +builder { sub { \$ws->call(shift) } } +EOM + { psgi_file => $ret, TMPDIR => "$tmpdir" }; }; -test_psgi($app->(), sub { +my $client = sub { my $cb = shift; + unlink "$tmpdir/index.html" if -f "$tmpdir/index.html"; my $res = $cb->(GET('/')); is($res->code, 404, '404 on "/" by default'); write_file '>', "$tmpdir/index.html", 'hi'; @@ -29,10 +37,15 @@ test_psgi($app->(), sub { is($res->header('Content-Length'), '2', 'content-length set'); like($res->header('Content-Type'), qr!^text/html\b!, 'content-type is html'); -}); +}; + +my $env = $psgi_env->(); +test_psgi(Plack::Util::load_psgi($env->{psgi_file}), $client); +test_httpd $env, $client; -test_psgi($app->(autoindex => 1, index => []), sub { +$client = sub { my $cb = shift; + write_file '>', "$tmpdir/index.html", 'hi'; my $res = $cb->(GET('/')); my $updir = 'href="../">../'; is($res->code, 200, '200 with autoindex default'); @@ -96,6 +109,11 @@ test_psgi($app->(autoindex => 1, index => []), sub { IO::Uncompress::Gunzip::gunzip(\$in => \$out); like($out, qr/\A/, 'got HTML start after gunzip'); like($out, qr{$}, 'got HTML end after gunzip'); -}); + remove_tree "$tmpdir/dir"; +}; + +$env = $psgi_env->(', autoindex => 1, index => []'); +test_psgi(Plack::Util::load_psgi($env->{psgi_file}), $client); +test_httpd $env, $client; done_testing();