Skip to content

Commit

Permalink
Merge 6b2dec5 into 4629793
Browse files Browse the repository at this point in the history
  • Loading branch information
miyagawa committed Jun 13, 2013
2 parents 4629793 + 6b2dec5 commit 5f6f9fe
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 11 deletions.
3 changes: 2 additions & 1 deletion META.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"Filesys::Notify::Simple" : "0",
"HTTP::Body" : "1.06",
"HTTP::Message" : "5.814",
"HTTP::Tiny" : "0.024",
"Hash::MultiValue" : "0.05",
"Pod::Usage" : "1.36",
"Stream::Buffered" : "0.02",
Expand Down Expand Up @@ -81,7 +82,7 @@
}
}
},
"release_status" : "testing",
"release_status" : "stable",
"resources" : {
"bugtracker" : {
"web" : "https://github.com/plack/Plack/issues"
Expand Down
1 change: 1 addition & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ my %WriteMakefileArgs = (
"Filesys::Notify::Simple" => 0,
"HTTP::Body" => "1.06",
"HTTP::Message" => "5.814",
"HTTP::Tiny" => "0.024",
"Hash::MultiValue" => "0.05",
"Pod::Usage" => "1.36",
"Stream::Buffered" => "0.02",
Expand Down
1 change: 1 addition & 0 deletions cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ requires 'Try::Tiny';
requires 'URI', '1.59';
requires 'parent';
requires 'Apache::LogFormat::Compiler', '0.12';
requires 'HTTP::Tiny', 0.024;

on test => sub {
requires 'Test::More', '0.88';
Expand Down
76 changes: 76 additions & 0 deletions lib/Plack/LWPish.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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->{http} = @_ == 1 ? $_[0] : HTTP::Tiny->new(@_);
$self;
}

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

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

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

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

my $res = HTTP::Response->new(
$response->{status},
$response->{reason},
[ Hash::MultiValue->from_mixed($response->{headers})->flatten ],
$response->{content},
);
$res->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->new(GET => 'http://perl.com/');
my $ua = Plack::LWPish->new;
my $res = $ua->request($request); # returns HTTP::Response
=head1 DESCRIPTION
This module is an adapter object that implements one method,
C<request> that acts like L<LWP::UserAgent>'s request method
i.e. takes HTTP::Request object and returns HTTP::Response object.
This module is used solely inside L<PLack::Test::Suite> and
L<Plack::Test::Server>, and you are recommended to take a look at
L<HTTP::Thin> if you would like to use this outside Plack.
=back
=head1 AUTHOR
Tatsuhiko Miyagawa
=head1 SEE ALSO
L<HTTP::Thin> L<HTTP::Tiny> L<LWP::UserAgent>
=cut
8 changes: 4 additions & 4 deletions lib/Plack/Test/Server.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package Plack::Test::Server;
use strict;
use warnings;
use Carp;
use HTTP::Request;
use HTTP::Response;
use Test::TCP;
use Plack::Loader;
use Test::Requires ();
use Plack::LWPish;

sub test_psgi {
my %args = @_;

Test::Requires::test_requires('LWP::UserAgent');

my $client = delete $args{client} or croak "client test code needed";
my $app = delete $args{app} or croak "app needed";
my $ua = delete $args{ua} || LWP::UserAgent->new;
my $ua = delete $args{ua} || Plack::LWPish->new;

test_tcp(
client => sub {
Expand Down
6 changes: 2 additions & 4 deletions lib/Plack/Test/Suite.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use Plack::Middleware::Lint;
use Plack::Util;
use Plack::Request;
use Try::Tiny;
use Test::Requires ();
use Plack::LWPish;

my $share_dir = try { File::ShareDir::dist_dir('Plack') } || 'share';

Expand Down Expand Up @@ -741,8 +741,6 @@ sub runtests {
sub run_server_tests {
my($class, $server, $server_port, $http_port, %args) = @_;

Test::Requires::test_requires('LWP::UserAgent');

if (ref $server ne 'CODE') {
my $server_class = $server;
$server = sub {
Expand All @@ -757,7 +755,7 @@ sub run_server_tests {
client => sub {
my $port = shift;

my $ua = LWP::UserAgent->new;
my $ua = Plack::LWPish->new;
for my $i (0..$#TEST) {
my $test = $TEST[$i];
note $test->[0];
Expand Down
7 changes: 5 additions & 2 deletions t/Plack-Middleware/chunked.t
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use strict;
use Test::More;
use Test::Requires qw(IO::Handle::Util LWP::Protocol::http10);
use Test::Requires qw(IO::Handle::Util LWP::UserAgent LWP::Protocol::http10);
use IO::Handle::Util qw(:io_from);
use HTTP::Request::Common;
use Plack::Test;
use Plack::Middleware::Chunked;

$Plack::Test::Impl = "Server";

local $ENV{PLACK_SERVER} = "HTTP::Server::PSGI";
Expand All @@ -22,7 +23,9 @@ my @app = (

my $app = sub { (shift @app)->(@_) };

test_psgi app => Plack::Middleware::Chunked->wrap($app), client => sub {
test_psgi
ua => LWP::UserAgent->new, # force LWP
app => Plack::Middleware::Chunked->wrap($app), client => sub {
my $cb = shift;

for my $proto (qw( HTTP/1.1 HTTP/1.0 )) {
Expand Down

0 comments on commit 5f6f9fe

Please sign in to comment.