]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
t/www_static: test with our -httpd server, too
authorEric Wong <e@80x24.org>
Mon, 30 Sep 2024 21:30:04 +0000 (21:30 +0000)
committerEric Wong <e@80x24.org>
Wed, 2 Oct 2024 17:40:33 +0000 (17:40 +0000)
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.

lib/PublicInbox/TestCommon.pm
t/www_static.t

index 913aa7e9b2f77b9d24c3b30d2b13025167638fb7..6c3677d2d99ecf494dfa0ec2670915a1dbe96dc4 100644 (file)
@@ -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";
index 48d120d68cabad16b195528ef608a6f319072fd1..b1181cab8ed138b17a0df994921c92adef55f0f5 100644 (file)
@@ -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, <<EOM;
+use v5.12;
+use Plack::Builder;
+my \$ws = PublicInbox::WwwStatic->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="../">../</a>';
        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<html>/, 'got HTML start after gunzip');
        like($out, qr{</html>$}, '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();