From 64ef624c05bd7e9a5c4ac6d92af3d32efaa2b602 Mon Sep 17 00:00:00 2001 From: franck cuny Date: Fri, 22 Jul 2011 16:19:12 +0200 Subject: add tests for oauth Signed-off-by: franck cuny --- t/spore-middleware/auth-oauth.t | 95 +++++++++++++++++++++++++++++++---------- 1 file changed, 72 insertions(+), 23 deletions(-) (limited to 't/spore-middleware') diff --git a/t/spore-middleware/auth-oauth.t b/t/spore-middleware/auth-oauth.t index 39f6719..4564eba 100644 --- a/t/spore-middleware/auth-oauth.t +++ b/t/spore-middleware/auth-oauth.t @@ -1,52 +1,101 @@ use strict; use warnings; - +use URI::Escape; use Test::More; -plan tests => 3; +use Try::Tiny; +use YAML::Syck; use Net::HTTP::Spore; use JSON; my $api = { - base_url => "http://term.ie/oauth/example", + base_url => "http://localhost", name => "term.ie", methods => { - echo => { - path => "/echo_api.php", + get_request_token => { + path => "/request_token", method => "GET", expected_status => [200], authentication => 1, }, - get_request_token => { - path => "/request_token.php", + authorize_token => { + path => "/authorize_token", method => "GET", expected_status => [200], + required_params => ["oauth_token"], authentication => 1, }, get_access_token => { - path => "/access_token.php", - method => "GET", + path => "/access_token", + method => "GET", expected_status => [200], - authentication => 1, + authentication => 1, } }, }; -SKIP: { - skip "require RUN_HTTP_TEST", 3 unless $ENV{RUN_HTTP_TEST}; +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 ); - my $client = Net::HTTP::Spore->new_from_string( JSON::encode_json($api), trace => 1 ); +$client->enable( 'Auth::OAuth', %$options ); +$client->enable( 'Mock', tests => $mock_server ); - $client->enable( - 'Auth::OAuth', - oauth_consumer_key => 'key', - oauth_consumer_secret => 'secret', - ); +ok my $r = $client->get_request_token(); - my $body = $client->get_request_token->body; - use YAML::Syck; warn $body; ok 1; - # ok my $r = $client->echo(method => 'foo', bar => 'baz'); - # is $r->status, 200; - # like $r->body, qr/bar=baz&method=foo/; +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; -- cgit 1.4.1