AE::HTTP で HTTP::Request をなげるには
HTTP::Request をつかって呼ぶのはそんなにむずかしい話ではない。という。たとえば、以下のようにすれば、普通によべる。
sub http_request_by_http_request { my $cb = pop @_; my ($req, @args) = @_; my %headers; $req->headers->scan(sub { $headers{$_[0]} = $_[1]; }); http_request($req->method, $req->uri, body => $req->content, headers => \%headers, @args, $cb); }
↓のようにしてもいいかも。$req->headers のあたりがちょっとアヤシイけど。
http_request($req->method, $req->uri, body => $req->content, headers => $req->headers, $cb);
↓検証コード
use common::sense; use AE; use AnyEvent::HTTP qw/http_request/; use HTTP::Request; use Test::More; use Test::TCP; use Plack::Runner; use Plack::Request; use Data::Dumper; test_tcp( client => sub { my $port = shift; my $cv = AE::cv(); my $req = HTTP::Request->new(POST => "http://127.0.0.1:$port/", ["X-Dan" => "Kogai"], "Hello"); http_request_by_http_request( $req => sub { my ($body, $headers) = @_; is $body, 'ok'; is $headers->{Status}, 200; $cv->send(); } ); $cv->recv(); done_testing; }, server => sub { my $port = shift; my $p = Plack::Runner->new(); $p->parse_options('-p' => $port, '-E' => 'production'); $p->run(sub { my $req = Plack::Request->new(shift); is $req->header('X-Dan'), 'Kogai'; is $req->content, 'Hello', 'body'; return [200, [], ['ok']]; }); } ); sub http_request_by_http_request { my $cb = pop @_; my ($req, @args) = @_; my %headers; $req->headers->scan(sub { $headers{$_[0]} = $_[1]; }); http_request($req->method, $req->uri, body => $req->content, headers => \%headers, @args, $cb); }