1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
use strict;
use warnings;
use URI::Escape;
use Test::More;
use Try::Tiny;
use YAML::Syck;
use Net::HTTP::Spore;
use JSON;
my $api = {
base_url => "http://localhost",
name => "term.ie",
methods => {
get_request_token => {
path => "/request_token",
method => "GET",
expected_status => [200],
authentication => 1,
},
authorize_token => {
path => "/authorize_token",
method => "GET",
expected_status => [200],
required_params => ["oauth_token"],
authentication => 1,
},
get_access_token => {
path => "/access_token",
method => "GET",
expected_status => [200],
authentication => 1,
}
},
};
my $mock_server = {
'/request_token' => sub {
my $req = shift;
my $auth = $req->header('Authorization');
ok $auth;
like $auth, qr/oauth_consumer_key="key"/;
$req->new_response(
200,
[ 'Content-Type' => 'text/plain' ],
'oauth_token=requestkey&oauth_token_secret=requestsecret'
);
},
'/access_token' => sub {
my $req = shift;
my $auth = $req->header('Authorization');
like $auth, qr/oauth_verifier="foo"/;
$req->new_response( 200, [ 'Content-Type' => 'text/plain' ], 'oauth_token=new_token' );
},
'/authorize_token' => sub {
my $req = shift;
my $auth = $req->header('Authorization');
like $auth, qr/OAuth oauth_consumer_key="key",/;
$req->new_response( 200, [ 'Content-Type' => 'text/plain' ], 'ok' );
},
};
my $options = {
oauth_consumer_key => 'key',
oauth_consumer_secret => 'secret',
};
my $client =
Net::HTTP::Spore->new_from_string( JSON::encode_json($api), trace => 0 );
$client->enable( 'Auth::OAuth', %$options );
$client->enable( 'Mock', tests => $mock_server );
ok my $r = $client->get_request_token();
my $body = $r->body;
while ( $body =~ /([^&=]+)=([^&=]*)&?/g ) {
my ( $k, $v ) = ( $1, $2 );
$options->{$k} = uri_unescape($v);
}
is $options->{oauth_token}, 'requestkey';
my $r2 = $client->authorize_token( oauth_token => $options->{oauth_token} );
$options->{oauth_verifier} = "foo";
$client =
Net::HTTP::Spore->new_from_string( JSON::encode_json($api), trace => 0 );
$client->enable( 'Auth::OAuth', %$options );
$client->enable( 'Mock', tests => $mock_server );
my $r3 = $client->get_access_token();
$body = $r3->body;
while ( $body =~ /([^&=]+)=([^&=]*)&?/g ) {
my ( $k, $v ) = ( $1, $2 );
$options->{$k} = uri_unescape($v);
}
is $options->{oauth_token}, 'new_token';
done_testing;
|