1#!perl 2use Carp; 3use strict; 4use Test::More; 5use Test::Fatal; 6use lib qw(t/lib); 7 8{ 9 # simple mock object for Net::Netrc 10 package # hide from PAUSE 11 Net::Netrc; 12 use Moose; 13 sub lookup { shift->new } 14 sub lpa { qw/fred bedrock/ } 15 16 $INC{'Net/Netrc.pm'} = __FILE__; 17} 18 19 20eval 'use TestUA'; 21plan skip_all => 'LWP::UserAgent 5.819 required for tests' if $@; 22 23use Net::Twitter; 24 25my $nt = Net::Twitter->new( 26 ssl => 0, 27 traits => [qw/API::REST/], 28 username => 'homer', 29 password => 'doh!', 30); 31 32my $t = TestUA->new(1, $nt->ua); 33 34ok $nt->friends_timeline, 'friends_timeline no args'; 35ok $nt->create_friend('flanders'), 'create_friend scalar arg'; 36ok $nt->create_friend({ id => 'flanders' }), 'create_friend hashref'; 37ok $nt->destroy_friend('flanders'), 'destroy_friend scalar arg'; 38 39$t->response->content('true'); 40my $r; 41 42# back compat: 1.23 accepts scalar args 43is exception { $r = $nt->relationship_exists('homer', 'marge') }, undef, 'relationship_exists scalar args'; 44 45ok $r = $nt->relationship_exists({ user_a => 'homer', user_b => 'marge' }), 46 'relationship_exists hashref'; 47 48# back compat: 1.23 returns bool 49ok $r, 'relationship_exists returns true'; 50$t->reset_response; 51 52# Net::Twitter calls used by POE::Component::Server::Twirc 53ok $nt->new_direct_message({ user => 'marge', text => 'hello, world' }), 'new_direct_message'; 54ok $nt->friends({page => 2}), 'friends'; 55cmp_ok $t->arg('page'), '==', 2, 'page argument passed'; 56ok $nt->followers({page => 2}), 'followers'; 57ok $nt->direct_messages, 'direct_messages'; 58ok $nt->direct_messages({ since_id => 1 }), 'direct_messages since_id'; 59ok $nt->friends_timeline({ since_id => 1 }), 'friends_timeline since_id'; 60ok $nt->replies({ since_id => 1 }), 'replies since_id'; 61ok $nt->user_timeline, 'user_timeline'; 62ok $nt->update('hello, world'), 'update'; 63ok $nt->create_friend('flanders'), 'create_friend'; 64ok $nt->relationship_exists('homer', 'flanders'), 'relationship exists scalar args'; 65ok $nt->relationship_exists({ user_a => 'homer', user_b => 'flanders' }), 'relationship exists hashref'; 66ok $nt->destroy_friend('flanders'), 'destroy_friend'; 67ok $nt->create_block('flanders'), 'create_block'; 68ok $nt->destroy_block('flanders'), 'destroy_block'; 69ok $nt->create_favorite({ id => 12345678 }), 'create_favorite hashref'; 70ok $nt->rate_limit_status, 'rate_limit_status'; 71 72### Regression: broken in 2.03 73ok $nt->show_status('flanders'), 'show_status string arg'; 74$t->add_id_arg('flanders'); 75is $t->arg('id'), 'flanders', 'show_status ID set'; 76 77ok $nt->show_user('marge'), 'show_user string arg'; 78$t->add_id_arg('marge'); 79is $t->arg('id'), 'marge', 'show_user ID set'; 80 81ok $nt->show_user({ id => 'homer' }), 'show_user hashref'; 82$t->add_id_arg('homer'); 83is $t->arg('id'), 'homer', 'show_user ID set 2'; 84 85ok $nt->public_timeline, 'public_timeline blankargs'; 86 87### v3.09000 ### Role BUILD methods not called need after BUILD => sub {...} 88$nt = Net::Twitter->new(ssl => 1, traits => [qw/API::REST API::Lists/]); 89$t = TestUA->new(1, $nt->ua); 90 91$r = $nt->home_timeline; 92is $t->request->uri->scheme, 'https', 'ssl used for REST'; 93$r = $nt->list_lists('perl_api'); 94is $t->request->uri->scheme, 'https', 'ssl used for Lists'; 95 96### v3.10001 ### netrc used $self->apiurl, which is only available via the API::REST trait 97is exception { Net::Twitter->new(ssl => 0, netrc => 1, traits => [qw/API::Lists/]) }, undef, 'netrc with API::Lists lives'; 98### v3.11004 ### single array ref arg to update_profile_image not proprerly handled 99$r = $nt->update_profile_image([ undef, 'my_mug.jpg', Content_Type => 'image/jpeg', Content => '' ]); 100is $t->request->content_type, 'multipart/form-data', 'multipart/form-data'; 101 102### v4.00007 ### unicode args in search result in 401 "Could not authenticate you" 103{ 104 use utf8; 105 require URI::Escape; 106 107 my $nt = Net::Twitter->new( 108 ssl => 0, 109 traits => [qw/API::RESTv1_1 OAuth/], 110 consumer_key => 'my-consumer-key', 111 consumer_secret => 'my-consumer-secret', 112 access_token => 'my-access-token', 113 access_token_secret => 'my-access-token-secret', 114 ); 115 116 my $search_term = '作家'; # writer 117 118 $nt->ua->add_handler(request_send => sub { 119 my $req = shift; 120 121 my $uri = $req->uri->clone; 122 $uri->query(undef); 123 124 my $oauth = Net::OAuth->request('protected_resource')->from_authorization_header( 125 $req->header('Authorization'), 126 request_url => $uri, 127 consumer_key => 'my-consumer-key', 128 consumer_secret => 'my-consumer-secret', 129 token => 'my-access-token', 130 token_secret => 'my-access-token-secret', 131 version => '1.0', 132 request_method => 'GET', 133 signature_method => 'HMAC-SHA1', 134 extra_params => { map Encode::decode_utf8($_), $req->uri->query_form }, 135 ); 136 137 ok $oauth->verify, 'valid oauth signature'; 138 139 my $res = HTTP::Response->new(200); 140 $res->header('Content-Type' => 'application/json'); 141 $res->content('{}'); 142 return $res; 143 }); 144 145 $nt->search($search_term); 146} 147 148done_testing 149