]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/TestCommon.pm
tests: use test_httpd consistently
[public-inbox.git] / lib / PublicInbox / TestCommon.pm
index 04adede054e30a9005286495a7c941c54c78fc5f..abf4f3642b00c0fa57ef5163c8d2676cc6d35d53 100644 (file)
@@ -117,6 +117,12 @@ sub require_git ($;$) {
        1;
 }
 
+my %IPv6_VERSION = (
+       'Net::NNTP' => 3.00,
+       'Mail::IMAPClient' => 3.40,
+       'HTTP::Tiny' => 0.042,
+);
+
 sub require_mods {
        my @mods = @_;
        my $maybe = pop @mods if $mods[-1] =~ /\A[0-9]+\z/;
@@ -167,6 +173,9 @@ sub require_mods {
                                !eval{ IO::Socket::SSL->VERSION(2.007); 1 }) {
                        push @need, $@;
                }
+               if (defined(my $v = $IPv6_VERSION{$mod})) {
+                       $ENV{TEST_IPV4_ONLY} = 1 if !eval { $mod->VERSION($v) };
+               }
        }
        return unless @need;
        my $m = join(', ', @need)." missing for $0";
@@ -734,20 +743,28 @@ sub create_inbox ($$;@) {
        $ibx;
 }
 
-sub test_httpd ($$;$) {
-       my ($env, $client, $skip) = @_;
-       for (qw(PI_CONFIG TMPDIR)) {
-               $env->{$_} or BAIL_OUT "$_ unset";
-       }
+sub test_httpd ($$;$$) {
+       my ($env, $client, $skip, $cb) = @_;
+       my ($tmpdir, $for_destroy);
+       $env->{TMPDIR} //= do {
+               ($tmpdir, $for_destroy) = tmpdir();
+               $tmpdir;
+       };
+       for (qw(PI_CONFIG)) { $env->{$_} or BAIL_OUT "$_ unset" }
        SKIP: {
-               require_mods(qw(Plack::Test::ExternalServer), $skip // 1);
+               require_mods(qw(Plack::Test::ExternalServer LWP::UserAgent),
+                               $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" ];
                my $td = start_script($cmd, $env, { 3 => $sock });
                my ($h, $p) = tcp_host_port($sock);
                local $ENV{PLACK_TEST_EXTERNALSERVER_URI} = "http://$h:$p";
-               Plack::Test::ExternalServer::test_psgi(client => $client);
+               my $ua = LWP::UserAgent->new;
+               $ua->max_redirect(0);
+               Plack::Test::ExternalServer::test_psgi(client => $client,
+                                                       ua => $ua);
+               $cb->() if $cb;
                $td->join('TERM');
                open my $fh, '<', $err or BAIL_OUT $!;
                my $e = do { local $/; <$fh> };