1#!perl 2 3use strict; 4use warnings; 5 6use Test::More; 7use lib 't'; 8use Util qw[tmpfile monkey_patch set_socket_source clear_socket_source]; 9 10use HTTP::Tiny; 11 12BEGIN { monkey_patch() } 13 14my %usage = ( 15 'get' => q/Usage: $http->get(URL, [HASHREF])/, 16 'mirror' => q/Usage: $http->mirror(URL, FILE, [HASHREF])/, 17 'request' => q/Usage: $http->request(METHOD, URL, [HASHREF])/, 18); 19 20my @cases = ( 21 ['get'], 22 ['get','http://www.example.com/','extra'], 23 ['get','http://www.example.com/','extra', 'extra'], 24 ['mirror'], 25 ['mirror','http://www.example.com/',], 26 ['mirror','http://www.example.com/','extra', 'extra'], 27 ['mirror','http://www.example.com/','extra', 'extra', 'extra'], 28 ['request'], 29 ['request','GET'], 30 ['request','GET','http://www.example.com/','extra'], 31 ['request','GET','http://www.example.com/','extra', 'extra'], 32); 33 34my $res_fh = tmpfile(); 35my $req_fh = tmpfile(); 36 37my $http = HTTP::Tiny->new; 38clear_socket_source(); 39set_socket_source($req_fh, $res_fh); 40 41for my $c ( @cases ) { 42 my ($method, @args) = @$c; 43 eval {$http->$method(@args)}; 44 my $err = $@; 45 like ($err, qr/\Q$usage{$method}\E/, join("|",@$c) ); 46} 47 48my $res = eval{ $http->get("http://www.example.com/", { headers => { host => "www.example2.com" } } ) }; 49is( $res->{status}, 599, "Providing a Host header errors with 599" ); 50like( $res->{content}, qr/'Host' header/, "Providing a Host header gives right error message" ); 51 52$res = eval { $http->head("hxxp://www.example.com/") }; 53is( $res->{status}, 599, "Error on unsupported scheme" ); 54like( 55 $res->{content}, 56 qr/Unsupported URL scheme 'hxxp'/, 57 "Error for unsupported scheme" 58); 59 60$res = eval { $http->post_form("http://www.example.com/", [undef, "123"]) }; 61my $err = $@; 62like( 63 $err, 64 qr/form data keys must not be undef/, 65 "Error for undef key in form" 66); 67 68done_testing; 69 70