HTTP クライアントのテストを Plack でやる方法について
勝手に添削なかんじで。
http://d.hatena.ne.jp/Craftworks/20100819/1282237375
をみていておもったのですが、変にがんばるよりも、Test::TCP を直接つかってかいちゃった方が楽なのでは。
use strict;
use warnings;
use Test::More;
use Test::TCP;
use Plack::Loader;
use LWP::Simple qw/get/;
my $app = sub {
my $env = shift;
my $content = 'something'; # something
return [ 200, [ 'Content-Type' => 'text/plain' ], [ $content ] ];
};
my $client = sub {
my $base = shift;
is( get("$base/foo/bar"), 'something' );
};
test_tcp(
client => sub {
my $port = shift;
my $base = "http://127.0.0.1:$port";
$client->($base);
},
server => sub {
my $port = shift;
my $server = Plack::Loader->auto(
port => $port,
host => '127.0.0.1',
);
$server->run($app);
},
);
done_testing;
Published: 2010-08-18(Wed) 19:43