Commit: 279e53e8eb38cff1315356997ffec4a62faa726a

Author: gwoo | Date: 2010-05-25 14:41:21 -0700
refactoring to extend Service
diff --git a/extensions/service/Oauth.php b/extensions/service/Oauth.php index 1310d4e..56c89ea 100644 --- a/extensions/service/Oauth.php +++ b/extensions/service/Oauth.php @@ -13,7 +13,7 @@ namespace li3_oauth\extensions\service; * * */ -class Oauth extends \lithium\core\Object { +class Oauth extends \lithium\net\http\Service { protected $_autoConfig = array('classes' => 'merge'); @@ -23,8 +23,10 @@ class Oauth extends \lithium\core\Object { * @var array */ protected $_classes = array( - 'service' => '\lithium\net\http\Service', - 'storage' => '\li3_oauth\extensions\storage\File' + 'media' => '\lithium\net\http\Media', + 'request' => '\lithium\net\http\Request', + 'response' => '\lithium\net\http\Response', + 'socket' => '\lithium\net\socket\Context', ); /** @@ -41,7 +43,7 @@ class Oauth extends \lithium\core\Object { */ public function __construct($config = array()) { $defaults = array( - 'host' => null, + 'host' => 'localhost', 'authorize' => '/oauth/authorize', 'authenticate' => '/oauth/authenticate', 'request_token' => '/oauth/request_token', @@ -62,11 +64,10 @@ class Oauth extends \lithium\core\Object { public function _init() { parent::_init(); $config = $this->_config; + if (!empty($config['proxy'])) { $config['host'] = $config['proxy']; } - $this->service = new $this->_classes['service']($config); - $this->storage = new $this->_classes['storage']($config); } /** @@ -90,28 +91,27 @@ class Oauth extends \lithium\core\Object { * Send request * * @param string $method - * @param string $path + * @param string $url * @param string $data * @param string $options * @return void */ - public function send($path = null, $data = null, $options = array()) { - $defaults = array('via' => 'header'); + public function send($method, $path = null, $data = null, array $options = array()) { + $defaults = array('via' => 'headers'); $options += $defaults; $url = $this->config($path); $method = !empty($data['method']) ? $data['method'] : 'post'; $data = $this->sign($data + compact('url')); - if ($options['via'] == 'header') { - $auth = 'OAuth realm="",'; + if ($options['via'] == 'headers') { + $auth = 'OAuth realm="auth",'; foreach ($data as $key => $val) { $auth .= $key . '="'.rawurlencode($val).'",'; - $auth .= "\n"; } $options['headers'] = array('Authorization' => $auth); $data = array(); } - $response = $this->service->send($method, $url, $data, $options); + $response = parent::send($method, $url, $data, $options); if (strpos($response, 'oauth_token=') !== false) { return $this->_decode($response); @@ -150,7 +150,7 @@ class Oauth extends \lithium\core\Object { 'token' => array('oauth_token' => null, 'oauth_token_secret' => null), ); $options += $defaults; - $params = $this->_build($options['params'] + (array)$options['token']) + $options['data']; + $params = $this->_params($options['params'] + (array) $options['token']) + $options['data']; $base = $this->_base($options['method'], $options['url'], $params); $key = join("&", array( rawurlencode($options['oauth_consumer_secret']), @@ -182,11 +182,7 @@ class Oauth extends \lithium\core\Object { $query[] = $key . '=' . rawurlencode($value); }); $path = $this->url($url); - $base = join("&", array( - $method, rawurlencode($path), - rawurlencode(join('&', $query)) - )); - return $base; + return join("&", array($method, rawurlencode($path), rawurlencode(join('&', $query)))); } /** @@ -195,7 +191,7 @@ class Oauth extends \lithium\core\Object { * @param string $params * @return void */ - protected function _build($params = array()) { + protected function _params($params = array()) { $defaults = array( 'oauth_consumer_key' => 'key', 'oauth_nonce' => sha1(time() . mt_rand()), diff --git a/models/Consumer.php b/models/Consumer.php index a4762f7..06e7083 100644 --- a/models/Consumer.php +++ b/models/Consumer.php @@ -8,8 +8,6 @@ namespace li3_oauth\models; -use \li3_oauth\extensions\service\Oauth; - class Consumer extends \lithium\core\StaticObject { /** @@ -19,6 +17,10 @@ class Consumer extends \lithium\core\StaticObject { */ protected static $_service = null; + protected static $_classes = array( + 'oauth' => '\li3_oauth\extensions\service\Oauth' + ); + /** * Configure the Consumer to access the Oauth service layer * {{{ @@ -43,7 +45,11 @@ class Consumer extends \lithium\core\StaticObject { * @return void */ public static function config($config) { - static::$_service = new Oauth($config); + static::$_service = new static::$_classes['oauth']($config); + } + + public static function __callStatic($method, $params) { + return static::$_service->invokeMethod($method, $params); } /** @@ -52,7 +58,7 @@ class Consumer extends \lithium\core\StaticObject { * @param array $options optional params for the request * @return string */ - public static function request($params = array(), $options = array()) { + public static function token($type, $params = array(), $options = array()) { return static::$_service->send('request_token', $params + array( 'hash' => 'HMAC-SHA1', 'method' => 'POST' ), $options); diff --git a/tests/cases/extensions/service/OauthTest.php b/tests/cases/extensions/service/OauthTest.php index 1397c06..1fb2d77 100644 --- a/tests/cases/extensions/service/OauthTest.php +++ b/tests/cases/extensions/service/OauthTest.php @@ -14,7 +14,7 @@ class OauthTest extends \lithium\test\Unit { protected $_testConfig = array( 'classes' => array( - 'service' => '\li3_oauth\tests\mocks\extensions\service\MockService', + 'socket' => '\li3_oauth\tests\mocks\extensions\service\MockSocket', ), 'persistent' => false, 'protocol' => 'http', @@ -59,8 +59,8 @@ class OauthTest extends \lithium\test\Unit { 'oauth_token' => 'requestkey', 'oauth_token_secret' => 'requestsecret' ); - $result = $oauth->send('request_token', array( - 'hash' => 'HMAC-SHA1', 'method' => 'POST', 'params' => array() + $result = $oauth->post('request_token', array( + 'hash' => 'HMAC-SHA1', 'params' => array() )); $this->assertEqual($expected, $result); } @@ -72,8 +72,8 @@ class OauthTest extends \lithium\test\Unit { 'oauth_token' => 'accesskey', 'oauth_token_secret' => 'accesssecret' ); - $result = $oauth->send('access_token', array( - 'hash' => 'HMAC-SHA1', 'method' => 'POST', 'params' => array(), + $result = $oauth->post('access_token', array( + 'hash' => 'HMAC-SHA1', 'params' => array(), 'token' => array( 'oauth_token' => 'requestkey', 'oauth_token_secret' => 'requestsecret' diff --git a/tests/cases/models/ConsumerTest.php b/tests/cases/models/ConsumerTest.php index 62d91f6..7a58d1e 100644 --- a/tests/cases/models/ConsumerTest.php +++ b/tests/cases/models/ConsumerTest.php @@ -31,6 +31,42 @@ class ConsumerTest extends \lithium\test\Unit { )); $this->assertEqual($expected, $result); } + + public function testRequestToken() { + $expected = 'http://localhost/oauth/authorize?oauth_token=requestkey'; + $result = Consumer::authorize(array( + 'oauth_token' => 'requestkey', + 'oauth_token_secret' => 'requestsecret' + )); + $this->assertEqual($expected, $result); + } + + public function testAccessToken() { + $expected = 'http://localhost/oauth/authorize?oauth_token=requestkey'; + $result = Consumer::authorize(array( + 'oauth_token' => 'requestkey', + 'oauth_token_secret' => 'requestsecret' + )); + $this->assertEqual($expected, $result); + } + + public function testPost() { + $expected = 'http://localhost/oauth/authorize?oauth_token=requestkey'; + $result = Consumer::post(array( + 'oauth_token' => 'requestkey', + 'oauth_token_secret' => 'requestsecret' + )); + $this->assertEqual($expected, $result); + } + + public function testGet() { + $expected = 'http://localhost/oauth/authorize?oauth_token=requestkey'; + $result = Consumer::authorize(array( + 'oauth_token' => 'requestkey', + 'oauth_token_secret' => 'requestsecret' + )); + $this->assertEqual($expected, $result); + } } ?> \ No newline at end of file diff --git a/tests/mocks/extensions/service/MockService.php b/tests/mocks/extensions/service/MockService.php deleted file mode 100644 index f909215..0000000 --- a/tests/mocks/extensions/service/MockService.php +++ /dev/null @@ -1,22 +0,0 @@ -<?php -/** - * Lithium: the most rad php framework - * - * @copyright Copyright 2009, Union of RAD (http://union-of-rad.org) - * @license http://opensource.org/licenses/bsd-license.php The BSD License - */ - -namespace li3_oauth\tests\mocks\extensions\service; - -class MockService extends \lithium\net\http\Service { - - public function send($method, $path = null, $data = null, array $options = array()) { - if (strpos($path, 'request_token') !== false) { - return 'oauth_token=requestkey&oauth_token_secret=requestsecret'; - } - if (strpos($path, 'access_token') !== false) { - return 'oauth_token=accesskey&oauth_token_secret=accesssecret'; - } - } - -} \ No newline at end of file diff --git a/tests/mocks/extensions/service/MockSocket.php b/tests/mocks/extensions/service/MockSocket.php index fbe1b70..4025e92 100644 --- a/tests/mocks/extensions/service/MockSocket.php +++ b/tests/mocks/extensions/service/MockSocket.php @@ -6,9 +6,9 @@ * @license http://opensource.org/licenses/bsd-license.php The BSD License */ -namespace li3_oath\tests\mocks\extensions\service; +namespace li3_oauth\tests\mocks\extensions\service; -class MockSocket extends \lithium\util\Socket { +class MockSocket extends \lithium\net\Socket { public $data = null; @@ -24,14 +24,14 @@ class MockSocket extends \lithium\util\Socket { return true; } - public function read() { + public function read($body = 'Test!') { return join("\r\n", array( 'HTTP/1.1 200 OK', 'Header: Value', 'Connection: close', 'Content-Type: text/html;charset=UTF-8', '', - 'Test!' + $body )); } @@ -46,10 +46,17 @@ class MockSocket extends \lithium\util\Socket { public function encoding($charset) { return true; } - - public function send($message, $options = array()) { - $this->write($message); - return $this->read(); + + public function send($message, array $options = array()) { + $this->data = $this->write($message); + if (strpos($message->path, 'request_token') !== false) { + $body = 'oauth_token=requestkey&oauth_token_secret=requestsecret'; + } + if (strpos($message->path, 'access_token') !== false) { + $body = 'oauth_token=accesskey&oauth_token_secret=accesssecret'; + } + $message = $this->read($body); + return new $options['classes']['response'](compact('message')); } }