Commit 1e3aa5f4 authored by David Golden's avatar David Golden

add basic redirection checking

parent 3baf4825
#!perl
use strict;
use warnings;
use File::Basename;
use Test::More 0.88;
use t::Util qw[tmpfile rewind slurp monkey_patch dir_list parse_case
hashify connect_args set_socket_source sort_headers $CRLF $LF];
use HTTP::Tiny;
BEGIN { monkey_patch() }
for my $file ( dir_list("t/cases", qr/^redirect/ ) ) {
my $label = basename($file);
my $data = do { local (@ARGV,$/) = $file; <> };
my ($params, @case_pairs) = split /--+\n/, $data;
my $case = parse_case($params);
my $url = $case->{url}->[0];
my %headers = hashify( $case->{headers} );
my %new_args = hashify( $case->{new_args} );
my %options;
$options{headers} = \%headers if %headers;
my @call_args = %options ? ($url, \%options) : ($url);
my $version = HTTP::Tiny->VERSION || 0;
my $agent = $new_args{agent} || "HTTP-Tiny/$version";
my (@socket_pairs);
while ( @case_pairs ) {
my ($expect_req, $give_res) = splice( @case_pairs, 0, 2 );
# cleanup source data
$expect_req =~ s{HTTP-Tiny/VERSION}{$agent};
s{\n}{$CRLF}g for ($expect_req, $give_res);
# setup mocking and test
my $req_fh = tmpfile();
my $res_fh = tmpfile($give_res);
push @socket_pairs, [$req_fh, $res_fh, $expect_req];
}
set_socket_source(@{$_}[0,1]) for @socket_pairs;
my $http = HTTP::Tiny->new(%new_args);
my $response = $http->get(@call_args);
my $calls = 0
+ (defined($new_args{max_redirect}) ? $new_args{max_redirect} : 5);
for my $i ( 0 .. $calls ) {
last unless @socket_pairs;
my ($req_fh, $res_fh, $expect_req) = @{ shift @socket_pairs };
my $got_req = slurp($req_fh);
is( sort_headers($got_req), sort_headers($expect_req), "$label request $i data");
$i++;
}
my $exp_content = $case->{expected}
? join("$CRLF", @{$case->{expected}}) : '';
is ( $response->{content}, $exp_content, "$label content" );
}
done_testing;
url
http://example.com/index.html
expected
abcdefghijklmnopqrstuvwxyz1234567890abcdef
----------
GET /index.html HTTP/1.1
Host: example.com
Connection: close
User-Agent: HTTP-Tiny/VERSION
----------
HTTP/1.1 302 Found
Date: Thu, 03 Feb 1994 00:00:00 GMT
Content-Type: text/html
Content-Length: 53
Location: http://example.com/index2.html
<a href="http://example.com/index2.html">redirect</a>
----------
GET /index2.html HTTP/1.1
Host: example.com
Connection: close
User-Agent: HTTP-Tiny/VERSION
----------
HTTP/1.1 200 OK
Date: Thu, 03 Feb 1994 00:00:00 GMT
Content-Type: text/plain
Content-Length: 42
abcdefghijklmnopqrstuvwxyz1234567890abcdef
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment