Skip to content

Instantly share code, notes, and snippets.

@quicoju
Created June 19, 2015 14:06
Show Gist options
  • Save quicoju/00d90218ab2086c94613 to your computer and use it in GitHub Desktop.
Save quicoju/00d90218ab2086c94613 to your computer and use it in GitHub Desktop.
Extract from 09_body.t
use strict;
use warnings;
use Furl::HTTP;
use Test::TCP;
use Test::Requires qw(Plack::Request HTTP::Body), 'Plack';
use Plack::Loader;
use Test::More;
use Fcntl qw(SEEK_SET);
use Plack::Request;
test_tcp(
client => sub {
my $port = shift;
my $furl = Furl::HTTP->new(bufsize => 80);
open my $req_content_fh, '<', $0 or die "oops"; # Any input file with CR,LF line terminators
note 'request $0: ', -s $req_content_fh;
my $req_content = do{ local $/; <$req_content_fh> };
seek $req_content_fh, 0, SEEK_SET;
my ( undef, $code, $msg, $headers, $content ) =
$furl->request(
method => 'POST',
port => $port,
path_query => '/foo',
host => '127.0.0.1',
headers => [ "X-Foo" => "ppp" ],
content => $req_content_fh,
);
is $code, 200, "request()";
is $msg, "OK";
is Furl::HTTP::_header_get($headers, 'Content-Length'),
length($req_content);
is $content, $req_content
or do{ require Devel::Peek; Devel::Peek::Dump($content) };
done_testing;
},
server => sub {
my $port = shift;
Plack::Loader->auto(port => $port)->run(sub {
my $env = shift;
my $req = Plack::Request->new($env);
is $req->header('X-Foo'), "ppp" if $env->{REQUEST_URI} eq '/foo';
like $req->header('User-Agent'), qr/\A Furl::HTTP /xms;
return [ 200,
[ 'Content-Length' => length($req->content) ],
[$req->content]
];
});
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment