Skip to content
Browse files

improved unit test

  • Loading branch information...
1 parent c40f3af commit d76b2c725d23ce3da39a45596d148b79840fe0ed @lyokato lyokato committed Jun 9, 2010
View
16 lib/OAuth/Lite2/Client/Token.pm
@@ -0,0 +1,16 @@
+package OAuth::Lite2::Client::Token;
+
+use strict;
+use warnings;
+
+use base 'Class::Accessor::Fast';
+
+__PACKAGE__->mk_accessors(qw(
+ access_token
+ expires_in
+ refresh_token
+ access_token_secret
+ scope
+));
+
+1;
View
55 lib/OAuth/Lite2/Client/TokenResponseParser.pm
@@ -0,0 +1,55 @@
+package OAuth::Lite2::Client::TokenResponseParser;
+
+use strict;
+use warnings;
+
+use Try::Tiny;
+use OAuth::Lite2::Formatters;
+use OAuth::Lite2::Error;
+use OAuth::Lite2::Client::Token;
+
+sub new {
+ bless {}, $_[0];
+}
+
+sub parse {
+ my ($self, $http_res) = @_;
+
+ my $formatter =
+ OAuth::Lite2::Formatters->get_formatter_by_type(
+ $http_res->content_type);
+
+ my $token;
+
+ if ($http_res->is_success) {
+
+ OAuth::Lite2::Error::InvalidFormat->throw(
+ message => sprintf(q{Invalid content type "%s"},
+ $http_res->content_type||'')
+ ) unless $formatter;
+
+ my $http_result = $formatter->parse($http_res->content);
+
+ OAuth::Lite2::Error::InvalidResponse->throw(
+ message => sprintf("Response doesn't include 'access_token'")
+ ) unless exists $http_result->{access_token};
+
+ $token = OAuth::Lite2::Client::Token->new($http_result);
+
+ } else {
+
+ my $errmsg = $http_res->content || $http_res->status_line;
+ if ($formatter && $http_res->content) {
+ try {
+ my $http_result = $formatter->parse($http_res->content);
+ $errmsg = $http_result->{error}
+ if exists $http_result->{error};
+ } catch { return };
+ }
+ OAuth::Lite2::Error::InvalidResponse->throw( message => $errmsg );
+ }
+ return $token;
+}
+
+
+1;
View
30 lib/OAuth/Lite2/Client/WebServer.pm
@@ -3,18 +3,22 @@ package OAuth::Lite2::Client::WebServer;
use strict;
use warnings;
+use base 'Class::ErrorHandler';
+
use Params::Validate qw(HASHREF);
use Carp ();
use bytes ();
use URI;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Headers;
+use Try::Tiny;
use OAuth::Lite2;
use OAuth::Lite2::Util qw(build_content);
use OAuth::Lite2::Error;
use OAuth::Lite2::Formatters;
+use OAuth::Lite2::Client::TokenResponseParser;
sub new {
@@ -46,6 +50,7 @@ sub new {
}
$self->{format} ||= 'json';
+ $self->{response_parser} = OAuth::Lite2::Client::TokenResponseParser->new;
return $self;
}
@@ -124,10 +129,15 @@ sub get_access_token {
my $res = $self->{agent}->request($req);
- my $formatter =
- OAuth::Lite2::Formatters->get_formatter_by_type($res->content_type);
- my $result = $formatter->parse($res->content);
-
+ my ($token, $errmsg);
+ try {
+ $token = $self->{response_parser}->parse($res);
+ } catch {
+ $errmsg = $_->isa("OAuth::Lite2::Error")
+ ? $_->message
+ : $_;
+ };
+ return $token || $self->error($errmsg);
}
sub refresh_access_token {
@@ -166,9 +176,15 @@ sub refresh_access_token {
my $res = $self->{agent}->request($req);
- my $formatter =
- OAuth::Lite2::Formatters->get_formatter_by_type($res->content_type);
- my $result = $formatter->parse($res->content);
+ my ($token, $errmsg);
+ try {
+ $token = $self->{response_parser}->parse($res);
+ } catch {
+ $errmsg = $_->isa("OAuth::Lite2::Error")
+ ? $_->message
+ : $_;
+ };
+ return $token || $self->error($errmsg);
}
View
4 lib/OAuth/Lite2/Error.pm
@@ -42,6 +42,10 @@ package OAuth::Lite2::Error::InvalidFormat;
our @ISA = qw(OAuth::Lite2::Error);
sub default_message { "invalid format" }
+package OAuth::Lite2::Error::InvalidResponse;
+our @ISA = qw(OAuth::Lite2::Error);
+sub default_message { "invalid response" }
+
package OAuth::Lite2::Error::InvalidParamMethod;
our @ISA = qw(OAuth::Lite2::Error);
sub default_message { "invalid param method" }
View
7 t/00_compile.t
@@ -1,5 +1,5 @@
use strict;
-use Test::More tests => 44;
+use Test::More tests => 47;
BEGIN {
# core
use_ok('OAuth::Lite2');
@@ -28,9 +28,13 @@ BEGIN {
use_ok('OAuth::Lite2::Agent');
use_ok('OAuth::Lite2::Agent::Dump');
use_ok('OAuth::Lite2::Agent::Strict');
+ use_ok('OAuth::Lite2::Agent::PSGIMock');
# client
use_ok('OAuth::Lite2::Client');
+ use_ok('OAuth::Lite2::Client::Token');
+ use_ok('OAuth::Lite2::Client::TokenResponseParser');
+
use_ok('OAuth::Lite2::Client::ClientCredentials');
use_ok('OAuth::Lite2::Client::UserAgent');
use_ok('OAuth::Lite2::Client::WebServer');
@@ -50,6 +54,7 @@ BEGIN {
use_ok('OAuth::Lite2::Server::Endpoint::Token');
use_ok('OAuth::Lite2::Server::Endpoint::EndUser');
+ #use_ok('OAuth::Lite2::Server::Endpoint::ProtectedResource');
use_ok('OAuth::Lite2::Server::Flows');
use_ok('OAuth::Lite2::Server::Flow');
View
78 t/040_unit/web_server.t
@@ -2,7 +2,7 @@ use strict;
use warnings;
use lib 't/lib';
-use Test::More tests => 1;
+use Test::More tests => 22;
use TestDataHandler;
use OAuth::Lite2::Server::Endpoint::Token;
@@ -15,7 +15,7 @@ my $dh = TestDataHandler->new;
# set authorization-fixture-data instead of user interaction
my $auth_info = $dh->create_or_update_auth_info(
- clinet_id => q{foo},
+ client_id => q{foo},
user_id => q{buz},
scope => q{email},
redirect_uri => q{http://example.org/callback},
@@ -33,20 +33,76 @@ my $agent = OAuth::Lite2::Agent::PSGIMock->new(app => $app);
my $client = OAuth::Lite2::Client::WebServer->new(
id => q{foo},
secret => q{bar},
- format => q{json},
authorize_url => q{http://localhost/authorize},
access_token_url => q{http://localhost/access_token},
agent => $agent,
);
+# format "json"
my $res = $client->get_access_token(
- code => q{foba},
- redirect_uri => q{fuba},
- #secret_type => q{},
- #format => q{xml},
- #url => q{},
+ code => q{invalid_code},
+ redirect_uri => q{http://example.org/callback},
+);
+
+ok(!$res, q{response should be undef});
+is($client->errstr, q{bad_verification_code}, q{verification code should be invalid});
+
+# format "xml"
+$res = $client->get_access_token(
+ code => q{invalid_code},
+ redirect_uri => q{http://example.org/callback},
+ format => q{xml},
+);
+
+ok(!$res, q{response should be undef});
+is($client->errstr, q{bad_verification_code}, q{verification code should be invalid});
+
+# format "form"
+$res = $client->get_access_token(
+ code => q{invalid_code},
+ redirect_uri => q{http://example.org/callback},
+ format => q{form},
+);
+
+ok(!$res, q{response should be undef});
+is($client->errstr, q{bad_verification_code}, q{verification code should be invalid});
+
+$res = $client->get_access_token(
+ code => q{valid_code},
+ redirect_uri => q{http://invalid.example.org/callback},
);
+ok(!$res, q{response should be undef});
+is($client->errstr, q{redirect_uri_mismatch}, q{redirect_uri should be invalid});
-# is($res->access_token, "");
-# is($res->refresh_token, "");
-# is($res->expires_in, "3600");
+
+$res = $client->get_access_token(
+ code => q{valid_code},
+ redirect_uri => q{http://example.org/callback},
+);
+
+ok($res, q{response should be not undef});
+is($res->access_token, q{access_token_0});
+is($res->refresh_token, q{refresh_token_0});
+is($res->expires_in, q{3600});
+ok(!$res->access_token_secret);
+is($res->scope, q{email});
+
+$res = $client->get_access_token(
+ code => q{valid_code},
+ redirect_uri => q{http://example.org/callback},
+ secret_type => q{hmac-sha1},
+);
+ok(!$res, q{response should be undef});
+is($client->errstr, q{unsupported_secret_type}, q{secret_type should be invalid});
+
+$res = $client->get_access_token(
+ code => q{valid_code},
+ redirect_uri => q{http://example.org/callback},
+ secret_type => q{hmac-sha256},
+);
+ok($res, q{response should be not undef});
+is($res->access_token, q{access_token_2});
+is($res->refresh_token, q{refresh_token_0});
+is($res->expires_in, q{3600});
+is($res->access_token_secret, q{access_token_secret_2});
+is($res->scope, q{email});
View
9 t/lib/TestDataHandler.pm
@@ -24,6 +24,15 @@ my %DEVICE_CODE;
my %CLIENTS;
my %USERS;
+sub clear {
+ my $class = shift;
+ %AUTH_INFO = ();
+ %ACCESS_TOKEN = ();
+ %DEVICE_CODE = ();
+ %CLIENTS = ();
+ %USERS = ();
+}
+
sub gen_next_auth_info_id {
my $class = shift;
$ID_POD{auth_info}++;

0 comments on commit d76b2c7

Please sign in to comment.
Something went wrong with that request. Please try again.