diff options
Diffstat (limited to 'cpan/HTTP-Tiny/t')
-rw-r--r-- | cpan/HTTP-Tiny/t/010_url.t | 31 | ||||
-rw-r--r-- | cpan/HTTP-Tiny/t/161_basic_auth.t | 75 | ||||
-rw-r--r-- | cpan/HTTP-Tiny/t/cases/auth-01.txt | 18 | ||||
-rw-r--r-- | cpan/HTTP-Tiny/t/cases/auth-02.txt | 18 | ||||
-rw-r--r-- | cpan/HTTP-Tiny/t/cases/auth-03.txt | 36 |
5 files changed, 163 insertions, 15 deletions
diff --git a/cpan/HTTP-Tiny/t/010_url.t b/cpan/HTTP-Tiny/t/010_url.t index ed63396014..bbaf14ed8b 100644 --- a/cpan/HTTP-Tiny/t/010_url.t +++ b/cpan/HTTP-Tiny/t/010_url.t @@ -3,23 +3,24 @@ use strict; use warnings; -use Test::More; +use Test::More 0.86; use HTTP::Tiny; my @tests = ( - [ 'HtTp://Example.COM/', 'http', 'example.com', 80, '/' ], - [ 'HtTp://Example.com:1024/', 'http', 'example.com', 1024, '/' ], - [ 'http://example.com', 'http', 'example.com', 80, '/' ], - [ 'http://example.com:', 'http', 'example.com', 80, '/' ], - [ 'http://foo@example.com:', 'http', 'example.com', 80, '/' ], - [ 'http://@example.com:', 'http', 'example.com', 80, '/' ], - [ 'http://example.com?foo=bar', 'http', 'example.com', 80, '/?foo=bar' ], - [ 'http://example.com?foo=bar#fragment', 'http', 'example.com', 80, '/?foo=bar' ], - [ 'http://example.com/path?foo=bar', 'http', 'example.com', 80, '/path?foo=bar' ], - [ 'http:///path?foo=bar', 'http', 'localhost', 80, '/path?foo=bar' ], - [ 'HTTPS://example.com/', 'https', 'example.com', 443, '/' ], - [ 'http://[::]:1024', 'http', '[::]', 1024, '/' ], - [ 'xxx://foo/', 'xxx', 'foo', undef, '/' ], + [ 'HtTp://Example.COM/', 'http', 'example.com', 80, '/', '', ], + [ 'HtTp://Example.com:1024/', 'http', 'example.com', 1024, '/', '', ], + [ 'http://example.com', 'http', 'example.com', 80, '/', '', ], + [ 'http://example.com:', 'http', 'example.com', 80, '/', '', ], + [ 'http://foo@example.com:', 'http', 'example.com', 80, '/', 'foo', ], + [ 'http://foo:pass@example.com:', 'http', 'example.com', 80, '/', 'foo:pass', ], + [ 'http://@example.com:', 'http', 'example.com', 80, '/', '', ], + [ 'http://example.com?foo=bar', 'http', 'example.com', 80, '/?foo=bar', '', ], + [ 'http://example.com?foo=bar#fragment', 'http', 'example.com', 80, '/?foo=bar', '', ], + [ 'http://example.com/path?foo=bar', 'http', 'example.com', 80, '/path?foo=bar', '', ], + [ 'http:///path?foo=bar', 'http', 'localhost', 80, '/path?foo=bar', '', ], + [ 'HTTPS://example.com/', 'https', 'example.com', 443, '/', '', ], + [ 'http://[::]:1024', 'http', '[::]', 1024, '/', '', ], + [ 'xxx://foo/', 'xxx', 'foo', undef, '/', '', ], ); plan tests => scalar @tests; @@ -28,7 +29,7 @@ for my $test (@tests) { my $url = shift(@$test); my $got = [ HTTP::Tiny->_split_url($url) ]; my $exp = $test; - is_deeply($got, $exp, "->split_url('$url')"); + is_deeply($got, $exp, "->split_url('$url')") or diag explain $got; } diff --git a/cpan/HTTP-Tiny/t/161_basic_auth.t b/cpan/HTTP-Tiny/t/161_basic_auth.t new file mode 100644 index 0000000000..1d44934020 --- /dev/null +++ b/cpan/HTTP-Tiny/t/161_basic_auth.t @@ -0,0 +1,75 @@ +#!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 clear_socket_source set_socket_source sort_headers + $CRLF $LF]; + +use HTTP::Tiny; +BEGIN { monkey_patch() } + +for my $file ( dir_list("t/cases", qr/^auth/ ) ) { + 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 $method = $case->{method}[0] || 'GET'; + my %headers = hashify( $case->{headers} ); + my %new_args = hashify( $case->{new_args} ); + + my %options; + $options{headers} = \%headers if %headers; + my $call_args = %options ? [$method, $url, \%options] : [$method, $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]; + } + + clear_socket_source(); + set_socket_source(@$_) for @socket_pairs; + + my $http = HTTP::Tiny->new(%new_args); + my $response = $http->request(@$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)"); + $i++; + } + + my $exp_content = $case->{expected} + ? join("$CRLF", @{$case->{expected}}) : ''; + + is ( $response->{content}, $exp_content, "$label content" ); + + if ( $case->{expected_url} ) { + is ( $response->{url}, $case->{expected_url}[0], "$label response URL" ); + } + +} + +done_testing; diff --git a/cpan/HTTP-Tiny/t/cases/auth-01.txt b/cpan/HTTP-Tiny/t/cases/auth-01.txt new file mode 100644 index 0000000000..e4a97c8435 --- /dev/null +++ b/cpan/HTTP-Tiny/t/cases/auth-01.txt @@ -0,0 +1,18 @@ +url + http://foo:bar@example.com/index.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Authorization: Basic Zm9vOmJhcg== + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 42 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef diff --git a/cpan/HTTP-Tiny/t/cases/auth-02.txt b/cpan/HTTP-Tiny/t/cases/auth-02.txt new file mode 100644 index 0000000000..9b9a0fa03a --- /dev/null +++ b/cpan/HTTP-Tiny/t/cases/auth-02.txt @@ -0,0 +1,18 @@ +url + http://Aladdin:open sesame@example.com/index.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== + +---------- +HTTP/1.1 200 OK +Date: Thu, 03 Feb 1994 00:00:00 GMT +Content-Type: text/plain +Content-Length: 42 + +abcdefghijklmnopqrstuvwxyz1234567890abcdef diff --git a/cpan/HTTP-Tiny/t/cases/auth-03.txt b/cpan/HTTP-Tiny/t/cases/auth-03.txt new file mode 100644 index 0000000000..8852e9ae04 --- /dev/null +++ b/cpan/HTTP-Tiny/t/cases/auth-03.txt @@ -0,0 +1,36 @@ +url + http://foo:bar@example.com/index.html +expected + abcdefghijklmnopqrstuvwxyz1234567890abcdef +expected_url + http://example.com/index2.html +---------- +GET /index.html HTTP/1.1 +Host: example.com +Connection: close +User-Agent: HTTP-Tiny/VERSION +Authorization: Basic Zm9vOmJhcg== + +---------- +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 + |