Created
December 2, 2009 05:44
-
-
Save erikeldridge/246976 to your computer and use it in GitHub Desktop.
oauthpanda = programmatic ui exploration + http request + oauth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(E_ALL); | |
require 'private.php'; | |
require 'gistfile1.php'; | |
$foo = new Foo(array( | |
'request_client' => new YahooCurlWrapper, | |
'oauth_client' => new StandardOauthClient, | |
'consumer_key' => YAHOO_OAUTH_CONSUMER_KEY, | |
'consumer_secret' => YAHOO_OAUTH_CONSUMER_SECRET | |
)); | |
if (is_file('access_token.txt')) { | |
//retrieve token | |
$access_token = unserialize(file_get_contents('access_token.txt')); | |
$url = "http://social.yahooapis.com/v1/users.guid($access_token->guid)/profile"; | |
$params = array( | |
'format' => 'json' | |
); | |
$response = $foo->set(array( | |
'oauth_params_location' => 'header', | |
'token' => $access_token | |
))->GET(array( | |
'url' => $url, | |
'params' => $params | |
)); | |
var_dump($response); | |
} elseif (isset($_GET['oauth_verifier'])) { | |
//fetch token | |
$request_token = unserialize(file_get_contents('request_token.txt')); | |
//exchange request token for access token | |
$response = $foo->set(array( | |
'oauth_params_location' => 'post', | |
'token' => $request_token | |
))->POST(array( | |
'url' => 'https://api.login.yahoo.com/oauth/v2/get_token', | |
'params' => array('oauth_verifier' => $_GET['oauth_verifier']) | |
)); | |
parse_str($response['response_body'], $access_token_data); | |
//sanity check | |
assert(isset($access_token_data['oauth_token'])); | |
//format access token obj as expected by std oauth lib | |
$now = time(); | |
$access_token = new stdclass(); | |
$access_token->key = $access_token_data["oauth_token"]; | |
$access_token->secret = $access_token_data["oauth_token_secret"]; | |
$access_token->guid = $access_token_data["xoauth_yahoo_guid"]; | |
$access_token->consumer = YAHOO_OAUTH_CONSUMER_KEY; | |
$access_token->sessionHandle = $access_token_data["oauth_session_handle"]; | |
// Check to see if the access token ever expires. | |
if(array_key_exists("oauth_expires_in", $access_token_data)) { | |
$access_token->tokenExpires = $now + $access_token_data["oauth_expires_in"]; | |
} else { | |
$access_token->tokenExpires = -1; | |
} | |
// Check to see if the access session handle ever expires. | |
if(array_key_exists("oauth_authorization_expires_in", $access_token_data)) { | |
$access_token->handleExpires = $now + $access_token_data["oauth_authorization_expires_in"]; | |
} else { | |
$access_token->handleExpires = -1; | |
} | |
//store token for future usage | |
file_put_contents('access_token.txt', serialize($access_token)); | |
//fetch data using token | |
$url = 'http://query.yahooapis.com/v1/public/yql'; | |
$params = array( | |
'q' => 'select%20*%20from%20social.profile%20where%20guid%3Dme', | |
'format' => 'json' | |
); | |
$response = $foo->set(array( | |
'oauth_params_location' => 'header', | |
'token' => $access_token | |
))->GET(array( | |
'url' => $url, | |
'params' => $params | |
)); | |
var_dump($response); | |
} else { | |
//get request token | |
$response = $foo->GET(array( | |
'url' => 'https://api.login.yahoo.com/oauth/v2/get_request_token', | |
'params' => array('oauth_callback' => OAUTH_CALLBACK_URL) | |
)); | |
//extract token | |
parse_str($response['response_body'], $request_token_response); | |
//sanity check | |
assert(isset($request_token_response['oauth_token'])); | |
//standard oauth lib expects request token stdclass obj | |
$request_token = (object) array( | |
'key' => $request_token_response['oauth_token'], | |
'secret' => $request_token_response['oauth_token_secret'] | |
); | |
//cache token for retreival after auth | |
file_put_contents('request_token.txt', serialize($request_token)); | |
//redirect user for auth | |
$redirect_url = sprintf( | |
'https://api.login.yahoo.com/oauth/v2/request_auth?oauth_token=%s&oauth_callback=%s', | |
$request_token_response['oauth_token'], | |
urlencode(OAUTH_CALLBACK_URL) | |
); | |
header('Location: '.$redirect_url); | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
I7$$$ZZ$Z8DNDDZZOZ$$$7$ZZ$$77$7$OZ$$$ZZZ$$$$$Z77ZZZZZ$$$$$$ZOOOOZZZZ$7$Z$$ZOOZZ$ | |
$$$$77$O8ND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M8+7OD?II8I8O | |
77:III?ZZDNMMMMMMMMMMMMMMZM$N8ZNMO888ZOOO8OZD8N$IDDD8O+NMZMMMMMDM$+MMI88$O$78?88 | |
++Z++I$I?IZMMMMMMMMMMMMM$M?ZMZMO$OZOZ$ZDNDNMMMNMM8?NI7MMINO7DMMM$ODO7Z=7$DOD?=IO | |
$7OONZI+=IDMMMMMMMMMMMMMM8NDNZMMMMNMMMN$ZZO87DMMMM+??MMMM87NNMMMZ8N8DDI8O7IIOD8O | |
?+7MZ$??$OMMMMMMMMMMMMMMMMDOM?MM8MMMOMZ8MMMMMMMMMDMDMO?OMMM8NM8$ZN78NN?8Z$DDDDDD | |
78N7OZI$8MMMMMMMMMMMMMNMMMMMM7DN$MZMNMM8MMMMMMMMMNMNDMN8Z$NNM8NN7N8NNND?8=8N$DZD | |
$Z8$8ZZDMMMMMMMMMMMMMMMZMDMMMDNMM878MDM8MMMMMMMMMNNMMOZ+DNNMMNDNDMODNNNO+D=?O8DO | |
7?OO8$NMMMMMMMZO$OMMMMMDMM8D8OZOO8O8MOM8NZMMMNNDZZ8ZOOZ$8NMMMM8?DDNDMNNN$778I7O$ | |
ZZ$Z8DMMMMMMZMM$8N8DDMMM$NIONZ87ZZZZZO87NMNNDN8Z$$$$$+88DNMMNM$8D8NMNDMMN$OZDDO? | |
$I7M8NNNMN7MZMN7IDMDMMMMNZD8$ND$Z7OD$:O8MMN88M8O$$$7$Z8IDNMMMM78NNNNMMMMN7ION8D7 | |
ODNNNNMMMM7ZMNMD7MD$NMMMM7O8$?D7NMMZN7?8Z8ODMZ$Z88ZN?DN8MMMMMDZMMMMMMMMZON7$88D8 | |
ZODDNMMMMM7O7MMMZMMMZDMMN$ZODOD=OMNO87$+O78NZ8MZ$I$$+O$D8OMMMO$MMMMMMMMON7$8DOD7 | |
www.chine-informations.com/images/upload2/panda%20geant.jpg --> GlassGiant.com | |
*/ | |
interface OauthClient | |
{ | |
//not static because we can't call static methods from objects assigned to arrays in php 5.2 | |
function sign($consumer_key, $consumer_secret, $url, Array $params, $request_method, $token, $oauth_signature_method); | |
} | |
class StandardOauthClient implements OauthClient | |
{ | |
//use hard enforcement because we're not interfacing w/ users | |
function sign($consumer_key, $consumer_secret, $url, Array $params, $request_method, $token, $oauth_signature_method) | |
{ | |
//http://oauth.googlecode.com/svn/code/php/OAuth.php | |
require_once 'OAuth.php'; | |
//enforce contract | |
assert(is_string($consumer_key)); | |
assert(is_string($consumer_secret)); | |
assert(is_string($url)); | |
assert(is_string($request_method)); | |
assert(is_null($token) || is_object($token)); | |
assert(is_string($oauth_signature_method)); | |
//validate input according to client particulars | |
$parsed_url = parse_url($url); | |
if (false === isset($parsed_url['scheme']) || false === isset($parsed_url['host'])) { | |
$message = 'A valid url of the form <i>{scheme}://{host}</i> is required, e.g., <i>http://example.com</i>. Original input: '.$url; | |
throw new Exception($message); | |
} | |
$consumer = new OAuthConsumer( | |
$consumer_key, | |
$consumer_secret | |
); | |
$request = OAuthRequest::from_consumer_and_token( | |
$consumer, | |
$token, | |
$request_method, | |
$url, | |
$params | |
); | |
switch($oauth_signature_method){ | |
case 'hmac': | |
$oauth_signature_method_obj = new OAuthSignatureMethod_HMAC_SHA1(); | |
break; | |
case 'text': | |
$oauth_signature_method_obj = new OAuthSignatureMethod_PLAINTEXT(); | |
break; | |
default: | |
throw(new Exception()); | |
} | |
$request->sign_request( | |
$oauth_signature_method_obj, | |
$consumer, | |
$token | |
); | |
// parse_str($request->to_postdata(), $signed_params); | |
return $request->to_postdata(); | |
} | |
} | |
interface HttpRequestWrapper | |
{ | |
function request($request_method, Array $headers, $url, $param_string); | |
} | |
class YahooCurlWrapper implements HttpRequestWrapper | |
{ | |
function request($request_method, Array $headers, $url, $param_string) | |
{ | |
assert(is_string($url)); | |
assert(is_string($param_string) || is_null($param_string)); | |
//for now, only support get and post | |
assert(is_string($request_method) && in_array($request_method, array('GET', 'POST'))); | |
switch ($request_method) { | |
case 'GET': | |
//append params to url, so they aren't encoded in a non-oauth-compliant way inside lib | |
$url .= '?'.$param_string; | |
$post_params = null; | |
break; | |
case 'POST': | |
$post_params = $param_string; | |
break; | |
default: | |
break; | |
} | |
//http://github.com/yahoo/yos-social-php5/blob/master/lib/Yahoo/YahooCurl.class.php | |
require_once 'YahooCurl.class.php'; | |
$http = YahooCurl::fetch( | |
$url, | |
null, | |
$headers, | |
$request_method, | |
$post_params | |
); | |
return $http; | |
} | |
} | |
//the standard exception class truncates the message string | |
class VerboseException extends Exception | |
{ | |
function __construct($message) | |
{ | |
$this->message = $message; | |
} | |
} | |
class Foo | |
{ | |
public $class_settings; | |
function __construct() | |
{ | |
$args = func_get_args(); | |
//class-level settings | |
$this->class_settings = array( | |
'request_client' => array( | |
'required' => 'true', | |
'validate' => create_function('$value', | |
'if(false === is_object($value)){ | |
$message = "<i>request_client</i> must be an object, not: ".gettype($value); | |
throw(new Exception($message)); | |
}' | |
) | |
), | |
'oauth_client' => array( | |
'required' => 'true', | |
'validate' => create_function('$value', | |
'if(false === is_object($value)){ | |
$message = "<i>oauth_client</i> must be an object, not: ".gettype($value); | |
throw(new Exception($message)); | |
}' | |
) | |
), | |
'consumer_key' => array( | |
'required' => 'true', | |
'validate' => create_function('$value', | |
'if (false === is_string($value)) { | |
$message = "<i>consumer_key</i> must be a string not: ".gettype($value); | |
} | |
if (isset($message)) { | |
throw (new Exception($message)); | |
}' | |
), | |
), | |
'consumer_secret' => array( | |
'required' => 'true', | |
'validate' => create_function('$value', | |
'if (false === is_string($value)) { | |
$message = "<i>consumer_secret</i> must be a string not: ".gettype($value); | |
} | |
if (isset($message)) { | |
throw (new Exception($message)); | |
}' | |
), | |
), | |
'token' => array( | |
'required' => 'false', | |
'value' => null, | |
'validate' => create_function('$value', | |
'if (false === is_object($value)) { | |
$message = "<i>token</i> must be an object not a(n): ".gettype($value); | |
throw (new Exception($message)); | |
}' | |
), | |
), | |
'oauth_signature_method' => array( | |
'required' => 'false', | |
'value' => 'hmac', | |
'validate' => create_function('$value', | |
'$valid_request_method_names = array("hmac", "plain"); | |
if (false === is_string($value)){ | |
$message = sprintf("<i>oauth_signature_method</i> must be a string not an %s.", gettype($value)); | |
} elseif (false === in_array($value, $valid_request_method_names)) { | |
$message = sprintf( | |
"<i>oauth_signature_method</i> must be \"hmac\" or \"plain\", not \"%s\".", | |
$value); | |
} | |
if (isset($message)) { | |
throw (new Exception($message)); | |
}' | |
), | |
), | |
'oauth_params_location' => array( | |
'required' => 'false', | |
'value' => 'url', | |
'validate' => create_function('$value', | |
'$valid_values = array("header", "url", "post"); | |
if (false === is_string($value)){ | |
$message = sprintf("<i>oauth_params_location</i> must be a string not an %s.", gettype($value)); | |
} elseif (false === in_array($value, $valid_values)) { | |
$message = sprintf( | |
"<i>oauth_params_location</i> must be \"header\", \"url\", or \"post\", not \"%s\".", | |
$value); | |
} | |
if (isset($message)) { | |
throw (new Exception($message)); | |
}' | |
), | |
), | |
'exception_handling' => array( | |
'validate' => create_function('$value', | |
'$valid_values = array("print", "throw"); | |
if (false === is_string($value) | |
|| false === in_array($value, $valid_values)) { | |
$message = "<i>exception_handling</i> must be \'print\' or \'throw\'"; | |
throw(new Exception($message)); | |
}' | |
), | |
'value' => 'print', | |
'required' => 'false' | |
) | |
); | |
$this->requireInput($args, $this->class_settings); | |
$this->set($args[0]); | |
} | |
function set() | |
{ | |
$args = func_get_args(); | |
$this->requireInput($args, $this->class_settings); | |
$validated = $this->validateInput($args[0], $this->class_settings); | |
//set validated values | |
foreach ($validated as $validated_name => $validated_value) { | |
$this->class_settings[$validated_name]['value'] = $validated_value; | |
} | |
$this->enforceRequiredInput($this->class_settings); | |
//make function chainable | |
return $this; | |
} | |
function __call($called_method_name, $args) | |
{ | |
$function_settings = array( | |
'url' => array( | |
'required' => 'true', | |
'validate' => create_function('$value', | |
'if (false === is_string($value)) { | |
$message = sprintf("<i>url</i> must be a string not an %s.", gettype($value)); | |
throw (new Exception($message)); | |
}' | |
), | |
), | |
'headers' => array( | |
'required' => false, | |
'value' => array(), | |
'validate' => create_function('$value', | |
'if (false === is_array($value)) { | |
$message = sprintf("<i>headers</i> must be an array not a(n) %s.", gettype($value)); | |
throw (new Exception($message)); | |
}' | |
), | |
), | |
'params' => array( | |
'required' => false, | |
'value' => array(), | |
'validate' => create_function('$value', | |
'if (false === is_array($value)) { | |
$message = sprintf("<i>params</i> must be an array not a(n) %s.", gettype($value)); | |
throw (new Exception($message)); | |
}' | |
), | |
), | |
'request_method' => array( | |
'validate' => create_function('$value', | |
'$valid_request_method_names = array("GET", "POST"); | |
if (false === is_string($value)){ | |
$message = sprintf("<i>request_method</i> must be a string not an %s.", gettype($value)); | |
} elseif (false === in_array($value, $valid_request_method_names)) { | |
$message = sprintf( | |
"<i>request_method</i> must be \"GET\" or \"POST\", not \"%s\"." | |
." Note: <i>request_method</i> is defined by name of method called, " | |
."e.g., calling GET() sets <i>request_method</i> to \"GET\"", | |
$value); | |
} | |
if (isset($message)) { | |
throw (new Exception($message)); | |
}' | |
), | |
'required' => 'true' | |
), | |
); | |
$this->requireInput($args, $function_settings); | |
//use deliberately set request method (though still validate it) | |
if (false === array_key_exists('request_method', $args[0])) { | |
$args[0]['request_method'] = strtoupper($called_method_name); | |
} | |
$validated = $this->validateInput($args[0], $function_settings); | |
//set validated values | |
foreach ($validated as $validated_name => $validated_value) { | |
$function_settings[$validated_name]['value'] = $validated_value; | |
} | |
$this->enforceRequiredInput($function_settings); | |
try { | |
$signed_param_string = $this->class_settings['oauth_client']['value']->sign( | |
$this->class_settings['consumer_key']['value'], | |
$this->class_settings['consumer_secret']['value'], | |
$function_settings['url']['value'], | |
$function_settings['params']['value'], | |
$function_settings['request_method']['value'], | |
$this->class_settings['token']['value'], | |
$this->class_settings['oauth_signature_method']['value'] | |
); | |
} catch(Exception $exception) { | |
$this->handleException($exception); | |
} | |
switch($this->class_settings['oauth_params_location']['value']){ | |
case 'url': | |
$param_string = $signed_param_string; | |
break; | |
case 'header': | |
$header_string = 'Authorization: OAuth realm=""'; | |
$params = array(); | |
$signed_params = explode('&', $signed_param_string); | |
//differentiate oauth params & build header param str | |
foreach ($signed_params as $signed_param_pair) { | |
if (substr($signed_param_pair, 0, 5) == "oauth") { | |
list($param_name, $param_value) = explode('=', $signed_param_pair); | |
$header_string .= sprintf(',%s="%s"', $param_name, $param_value); | |
} else { | |
$params[] = $signed_param_pair; | |
} | |
} | |
$param_string = implode('&', $params); | |
//add header string to header array | |
$function_settings['headers']['value'][] = $header_string; | |
break; | |
case 'post': | |
$param_string = $signed_param_string; | |
break; | |
default: | |
$exception = new Exception('invalid param location'); | |
$this->handleException($exception); | |
} | |
$response = $this->class_settings['request_client']['value']->request( | |
$function_settings['request_method']['value'], | |
$function_settings['headers']['value'], | |
$function_settings['url']['value'], | |
$param_string | |
); | |
return $response; | |
} | |
private function requireInput(Array $args, Array $settings) | |
{ | |
if (count($args) == 0) { | |
$message = 'input reqd: '; | |
foreach($settings as $setting_name => $setting_value){ | |
$message .= "<i>$setting_name"; | |
if ('true' == $setting_value['required']) { | |
$message .= ' (required)'; | |
} | |
$message .= '</i>, '; | |
} | |
$exception = new VerboseException(rtrim($message, ', ')); | |
} elseif (false === is_array($args[0])) { | |
$exception = new VerboseException(sprintf( | |
'input must be array, not: %s ', | |
gettype($args[0]) | |
)); | |
} elseif (count($args[0]) == 0) { | |
$exception = new VerboseException(sprintf( | |
'input reqd: %s ', | |
print_r($settings, true) | |
)); | |
} | |
if (isset($exception)) { | |
$this->handleException($exception); | |
} | |
} | |
private function validateInput(Array $input, Array $settings) | |
{ | |
$validated = array(); | |
foreach ($input as $arg_name => $arg_value) { | |
//validate name | |
if (false === array_key_exists($arg_name, $settings)) { | |
$exception = new Exception(sprintf( | |
'invalid setting name: <i>%s</i>. Valid setting names are: <i>%s</i>.', | |
$arg_name, | |
implode(', ', array_keys($settings)) | |
)); | |
$this->handleException($exception); | |
} | |
//validate value | |
try { | |
$settings[$arg_name]['validate']($arg_value); | |
} catch (Exception $exception) { | |
$this->handleException($exception); | |
} | |
//assign value | |
$validated[$arg_name] = $arg_value; | |
} | |
return $validated; | |
} | |
private function enforceRequiredInput(Array $settings) | |
{ | |
foreach ($settings as $name => $value) { | |
if ('true' == $value['required'] && false === isset($value['value'])) { | |
$exception = new Exception(sprintf('<i>%s</i> is required', $name)); | |
$this->handleException($exception); | |
} | |
} | |
} | |
private function handleException(Exception $exception) | |
{ | |
switch($this->class_settings['exception_handling']['value']){ | |
case 'throw': | |
throw($exception); | |
break; | |
default: | |
printf( | |
'<pre>%s</pre>', | |
print_r($exception, true) | |
); | |
exit; | |
} | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(E_ALL); | |
require 'private.php'; | |
require 'gistfile1.php'; | |
// //test no args | |
// new Foo; | |
//test invalid setting name | |
// $foo = new Foo(array( | |
// 'blah'=>'baz' | |
// )); | |
// assert(false); | |
//test invalid setting value | |
// new Foo(array( | |
// 'exception_handling'=>'bar' | |
// )); | |
// assert(false); | |
//test required setting | |
// new Foo(array( | |
// 'exception_handling'=> 'print' | |
// )); | |
// assert(false); | |
//test default setting value | |
$foo = new Foo(array( | |
'request_client' => new stdclass, | |
'oauth_client' => new StandardOauthClient, | |
'consumer_key' => YAHOO_OAUTH_CONSUMER_KEY, | |
'consumer_secret' => YAHOO_OAUTH_CONSUMER_SECRET | |
)); | |
assert('print' == $foo->class_settings['exception_handling']['value']); | |
//test custom setting value | |
$foo = new Foo(array( | |
'exception_handling' => 'throw', | |
'request_client' => new YahooCurlWrapper, | |
'oauth_client' => new StandardOauthClient, | |
'consumer_key' => YAHOO_OAUTH_CONSUMER_KEY, | |
'consumer_secret' => YAHOO_OAUTH_CONSUMER_SECRET | |
)); | |
assert('throw' == $foo->class_settings['exception_handling']['value']); | |
//test exception actually does get thrown when exception_handling is set to 'throw' | |
try { | |
$foo->set(array( | |
'boo'=> 'baz' | |
)); | |
} catch (Exception $e) { | |
assert(false !== strpos($e->getMessage(), 'invalid setting name: <i>boo</i>')); | |
} | |
//test setting invalid attribute name using set() | |
// $foo->set(array( | |
// 'request_client' => (object) array('boo'=>'baz') | |
// )); | |
// assert(false); | |
//test setting invalid attribute value using set() | |
// $foo->set(array( | |
// 'request_client' => array('boo'=>'baz') | |
// )); | |
// assert(false); | |
//test chaining set() | |
$foo->set(array( | |
'request_client' => (object) array('boo'=>'baz') | |
))->set(array( | |
'exception_handling'=> 'print' | |
)); | |
assert('print' == $foo->class_settings['exception_handling']['value']); | |
assert('baz' == $foo->class_settings['request_client']['value']->boo); | |
//test key validation | |
$foo = new Foo(array( | |
'exception_handling' => 'throw', | |
'request_client' => new YahooCurlWrapper, | |
'oauth_client' => new StandardOauthClient, | |
'consumer_key' => YAHOO_OAUTH_CONSUMER_KEY, | |
'consumer_secret' => YAHOO_OAUTH_CONSUMER_SECRET | |
)); | |
//test bad key type | |
try { | |
$foo->set(array('consumer_key'=>array()))->GET(array( | |
'url' => 'https://api.login.yahoo.com/oauth/v2/get_request_token' | |
)); | |
assert(false); | |
} catch(Exception $e) { | |
assert(false !== strpos($e->getMessage(), '<i>consumer_key</i> must be a string not: array')); | |
} | |
//test secret validation | |
$foo = new Foo(array( | |
'exception_handling' => 'throw', | |
'request_client' => new YahooCurlWrapper, | |
'oauth_client' => new StandardOauthClient, | |
'consumer_key' => YAHOO_OAUTH_CONSUMER_KEY, | |
'consumer_secret' => YAHOO_OAUTH_CONSUMER_SECRET | |
)); | |
//test oauth client w/ bad secret type --> fail | |
try { | |
$foo->set(array('consumer_secret'=>array()))->GET(array( | |
'url' => 'https://api.login.yahoo.com/oauth/v2/get_request_token' | |
)); | |
assert(false); | |
} catch(Exception $e) { | |
assert(false !== strpos($e->getMessage(), '<i>consumer_secret</i> must be a string not: array')); | |
} | |
//test called method name validation w/ incorrect method name --> fail | |
// $foo->BAR(array( | |
// 'url' => 'blah', | |
// 'key' => 'qwe123' | |
// )); | |
//test called method name validation w/ deliberate method name --> pass | |
$foo->set(array('request_client' => new YahooCurlWrapper))->BAR(array( | |
'request_method' => 'GET', | |
'url' => 'https://api.login.yahoo.com/oauth/v2/get_request_token', | |
)); | |
// test method input name validation w/ invalid value | |
// $foo->GET(array()); | |
//test correct method input name validation w/ valid value | |
$foo->GET(array( | |
// 'url' => 'https://api.login.yahoo.com/oauth/v2/get_request_token', | |
'url' => 'https://api.login.yahoo.com/oauth/v2/get_request_token', | |
'params' => array('oauth_callback' => OAUTH_CALLBACK_URL) | |
)); | |
//BEGIN: standard oauth client tests | |
//test oauth client w/ no scheme in url --> fail | |
// $foo->GET(array( | |
// // 'url' => 'https://api.login.yahoo.com/oauth/v2/get_request_token', | |
// 'url' => 'localhost/~eldridge/gist-246976/provider.php', | |
// 'params' => array('oauth_callback' => OAUTH_CALLBACK_URL) | |
// )); | |
//test oauth client w/ non-(http|https) scheme --> pass | |
$foo->GET(array( | |
'url' => 'zzz://localhost/~eldridge/gist-246976/provider.php', | |
'params' => array('oauth_callback' => OAUTH_CALLBACK_URL) | |
)); | |
//test oauth client w/ bad url --> fail | |
// $foo->GET(array( | |
// 'url' => 'http//ovider.php' | |
// )); | |
//test correct response from yahoo | |
$foo = new Foo(array( | |
'exception_handling' => 'throw', | |
'request_client' => new YahooCurlWrapper, | |
'oauth_client' => new StandardOauthClient, | |
'consumer_key' => YAHOO_OAUTH_CONSUMER_KEY, | |
'consumer_secret' => YAHOO_OAUTH_CONSUMER_SECRET | |
)); | |
$response = $foo->GET(array( | |
'url' => 'https://api.login.yahoo.com/oauth/v2/get_request_token', | |
'params' => array('oauth_callback' => OAUTH_CALLBACK_URL) | |
)); | |
assert(false !== strpos($response['response_body'], 'oauth_token=')); | |
printf('<pre>%s</pre>', print_r($response, true)); | |
//test correct method input value validation | |
// $foo->BAR(array( | |
// 'url' => array() | |
// )); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment