<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Plack::Test::MockHTTP;
use strict;
use warnings;

use Carp;
use HTTP::Request;
use HTTP::Response;
use HTTP::Message::PSGI;
use Try::Tiny;

sub test_psgi {
    my %args = @_;

    my $client = delete $args{client} or croak "client test code needed";
    my $app    = delete $args{app}    or croak "app needed";

    my $cb = sub {
        my $req = shift;
        $req-&gt;uri-&gt;scheme('http')    unless defined $req-&gt;uri-&gt;scheme;
        $req-&gt;uri-&gt;host('localhost') unless defined $req-&gt;uri-&gt;host;
        my $env = $req-&gt;to_psgi;

        my $res = try {
            HTTP::Response-&gt;from_psgi($app-&gt;($env));
        } catch {
            HTTP::Response-&gt;from_psgi([ 500, [ 'Content-Type' =&gt; 'text/plain' ], [ $_ ] ]);
        };

        $res-&gt;request($req);
        return $res;
    };

    $client-&gt;($cb);
}

1;

__END__

=head1 NAME

Plack::Test::MockHTTP - Run mocked HTTP tests through PSGI applications

=head1 DESCRIPTION

Plack::Test::MockHTTP is a utility to run PSGI application given
HTTP::Request objects and return HTTP::Response object out of PSGI
application response. See L&lt;Plack::Test&gt; how to use this module.

=head1 AUTHOR

Tatsuhiko Miyagawa

=head1 SEE ALSO

L&lt;Plack::Test&gt;

=cut


</pre></body></html>