summary refs log tree commit diff
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2011-07-22 16:19:12 +0200
committerfranck cuny <franck@lumberjaph.net>2011-07-26 13:21:02 +0200
commit64ef624c05bd7e9a5c4ac6d92af3d32efaa2b602 (patch)
tree081c0ee68696bf9a5190405f6290af45108ffa5e
parentfix test for port and schene (diff)
downloadnet-http-spore-64ef624c05bd7e9a5c4ac6d92af3d32efaa2b602.tar.gz
add tests for oauth
Signed-off-by: franck cuny <franck@lumberjaph.net>
-rw-r--r--t/spore-middleware/auth-oauth.t95
1 files changed, 72 insertions, 23 deletions
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;