POE-Component-Client-HTTP
view release on metacpan or search on metacpan
t/14_gzipped_content.t view on Meta::CPAN
use constant MAGIC2 => 0x8b ;
use constant OSCODE => 3 ;
use constant MIN_HDR_SIZE => 10 ; # minimum gzip header size
use bytes;
# Create the first outgoing portion of the content:
my $gzipHeader = pack(
"C" . MIN_HDR_SIZE, MAGIC1, MAGIC2, Z_DEFLATED(), 0,0,0,0,0,0, OSCODE
);
$gzipped_content = $gzipHeader;
my $gzip_handler = deflateInit(
-Level => Z_BEST_COMPRESSION(),
-WindowBits => - MAX_WBITS(),
);
$_ = $original_content;
my ($out, $status) = $gzip_handler->deflate(\$_);
unless (length($out)) {
($out, $status) = $gzip_handler->flush();
}
$gzipped_content .= $out;
# almost the same thing, but I wanted to go thru all the hoops:
if (0) {
$_ = $original_content;
$gzipped_content = Compress::Zlib::memGzip($_);
}
}
my @tests = (
# Gzipped content decoded correctly.
[
(
"HTTP/1.1 200 OK$CRLF" .
"Connection: close$CRLF" .
"Content-Encoding: gzip$CRLF" .
"Content-type: text/plain$CRLF" .
$CRLF .
"$gzipped_content$CRLF"
),
sub {
my $response = shift;
ok(
$response->code() == 200 &&
$response->decoded_content eq $original_content,
"gzip encoded transfers decode correctly"
);
},
],
);
# We are testing against a localhost server.
# Don't proxy, because localhost takes on new meaning.
BEGIN {
delete $ENV{HTTP_PROXY};
}
# Spawn one server per test response.
{
foreach (@tests) {
POE::Component::Server::TCP->new(
Alias => "server_$_",
Address => "127.0.0.1",
Port => 0,
Started => \®ister_port,
ClientInputFilter => "POE::Filter::Line",
ClientOutputFilter => "POE::Filter::Stream",
ClientInput => \&parse_next_request,
);
}
sub register_port {
push(
@server_ports,
(sockaddr_in($_[HEAP]->{listener}->getsockname()))[0]
);
}
sub parse_next_request {
my $input = $_[ARG0];
DEBUG and diag "got line: [$input]";
return if $input ne "";
my $response = $tests[$test_number][0];
$_[HEAP]->{client}->put($response);
$response =~ s/$CRLF/{CRLF}/g;
DEBUG and diag "sending: [$response]";
$_[KERNEL]->yield("shutdown");
}
}
# Spawn the HTTP user-agent component.
POE::Component::Client::HTTP->spawn();
# Create a client session to drive the HTTP component.
POE::Session->create(
inline_states => {
_start => sub {
$_[KERNEL]->yield("run_next_test");
},
run_next_test => sub {
my $port = $server_ports[$test_number];
$_[KERNEL]->post(
weeble => request => response =>
GET "http://127.0.0.1:${port}/"
);
},
response => sub {
my $response = $_[ARG1][0];
my $test = $tests[$test_number][1];
$test->($response);
( run in 0.608 second using v1.01-cache-2.11-cpan-71847e10f99 )