<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Plack::LWPish;
use strict;
use warnings;
use HTTP::Tiny;
use HTTP::Response;
use Hash::MultiValue;

sub new {
    my $class = shift;
    my $self  = bless {}, $class;
    $self-&gt;{http} = @_ == 1 ? $_[0] : HTTP::Tiny-&gt;new(@_);
    $self;
}

sub request {
    my($self, $req) = @_;

    my @headers;
    $req-&gt;headers-&gt;scan(sub { push @headers, @_ });

    my $options = {
        headers =&gt; Hash::MultiValue-&gt;new(@headers)-&gt;mixed,
    };
    $options-&gt;{content} = $req-&gt;content if defined $req-&gt;content &amp;&amp; length($req-&gt;content);

    my $response = $self-&gt;{http}-&gt;request($req-&gt;method, $req-&gt;url, $options);

    my $res = HTTP::Response-&gt;new(
        $response-&gt;{status},
        $response-&gt;{reason},
        [ Hash::MultiValue-&gt;from_mixed($response-&gt;{headers})-&gt;flatten ],
        $response-&gt;{content},
    );
    $res-&gt;request($req);

    return $res;
}

1;

__END__

=head1 NAME

Plack::LWPish - HTTP::Request/Response compatible interface with HTTP::Tiny backend

=head1 SYNOPSIS

  use Plack::LWPish;

  my $request = HTTP::Request-&gt;new(GET =&gt; 'http://perl.com/');

  my $ua = Plack::LWPish-&gt;new;
  my $res = $ua-&gt;request($request); # returns HTTP::Response

=head1 DESCRIPTION

This module is an adapter object that implements one method,
C&lt;request&gt; that acts like L&lt;LWP::UserAgent&gt;'s request method
i.e. takes HTTP::Request object and returns HTTP::Response object.

This module is used solely inside L&lt;Plack::Test::Suite&gt; and
L&lt;Plack::Test::Server&gt;, and you are recommended to take a look at
L&lt;HTTP::Thin&gt; if you would like to use this outside Plack.

=head1 AUTHOR

Tatsuhiko Miyagawa

=head1 SEE ALSO

L&lt;HTTP::Thin&gt; L&lt;HTTP::Tiny&gt; L&lt;LWP::UserAgent&gt;

=cut
</pre></body></html>