#!/usr/bin/perl
use strict;
use warnings;
use Test::More;
use IO::Async::Test;
use IO::Async::Loop;
use Net::Async::HTTP;
my $CRLF = "\x0d\x0a"; # because \r\n isn't portable
my $loop = IO::Async::Loop->new();
testing_loop( $loop );
my $http = Net::Async::HTTP->new(
user_agent => "", # Don't put one in request headers
);
$loop->add( $http );
# path-only redirects respect SSL
{
my $redir_response;
my $location;
my $response;
my $peersock;
no warnings 'redefine';
local *IO::Async::Handle::connect = sub {
my $self = shift;
my %args = @_;
$args{host} eq "host0" or die "Expected $args{host} eq host0";
$args{service} eq "443" or die "Expected $args{service} eq 443";
$args{extensions} and $args{extensions}[0] eq "SSL" or
die "Expected 'SSL' extension";
( my $selfsock, $peersock ) = IO::Async::OS->socketpair() or die "Cannot create socket pair - $!";
$self->set_handle( $selfsock );
return Future->new->done( $self );
};
my $future = $http->do_request(
uri => URI->new( "
https://host0/doc" ),
timeout => 10,
on_response => sub { $response = $_[0] },
on_redirect => sub { ( $redir_response, $location ) = @_ },
on_error => sub { die "Test died early - $_[0]" },
);
my $request_stream = "";
wait_for_stream { $request_stream =~ m/$CRLF$CRLF/ } $peersock => $request_stream;
$peersock->syswrite( "HTTP/1.1 301 Moved Permanently$CRLF" .
"Content-Length: 0$CRLF" .
"Location: /some/other/path$CRLF" .
"Connection: Keep-Alive$CRLF" .
"$CRLF" );
$request_stream = "";
wait_for_stream { $request_stream =~ m/$CRLF$CRLF/ } $peersock => $request_stream;
$request_stream =~ s/^(.*)$CRLF//;
my $req_firstline = $1;
is( $req_firstline, "GET /some/other/path HTTP/1.1", 'First line for redirected request' );
$peersock->syswrite( "HTTP/1.1 200 OK$CRLF" .
"Content-Length: 8$CRLF".
"Content-Type: text/plain$CRLF" .
"Connection: Keep-Alive$CRLF" .
"$CRLF" .
"Document" );
wait_for { defined $response };
is( $response->content_type, "text/plain", 'Content type of final response' );
is( $response->content, "Document", 'Content of final response' );
}
done_testing;