/
UnitTests.php
143 lines (130 loc) · 4.73 KB
/
UnitTests.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
// This file is intended as a sanity test for each REST API method in the MySpaceAPI. All edge
// conditions etc. are not be tested, only simple calls are made.
//
// Usage: php UnitTests.php
//
require_once 'MySpaceAPI.php';
class UnitTests {
// These are for an app that was added for the sole purpose of testing the API. You are free
// to use the app for testing against, as long as you don't do anything that would cause these
// tests to fail for other users.
//
// If you change the key / secret, some tests will fail because the signatures will be different.
//
// The app page is http://profile.myspace.com/index.cfm?fuseaction=user.viewProfile&friendID=457168699
//
static $UTF8_CONSUMER_KEY = 'http://www.myspace.com/457168699';
static $UTF8_CONSUMER_SECRET = '3a7dc34fa49c457986d1e7befe440d34';
static function test_request_signing() {
$s = new MySpaceAPI(UnitTests::$UTF8_CONSUMER_KEY, UnitTests::$UTF8_CONSUMER_SECRET);
$oauth_nonce = 'd8502726b041db1758892edee8826314';
$oauth_timestamp = '1235879978';
$output = $s->get_signed_request(
'PUT',
'v1/users/85628343/appdata.XML',
array('blah' => 'blah'),
$oauth_nonce,
$oauth_timestamp
);
$correct_output = array(
'oauth_consumer_key' => 'http%3A%2F%2Fwww.myspace.com%2F457168699',
'oauth_token' => '',
'oauth_nonce' => 'd8502726b041db1758892edee8826314',
'oauth_timestamp' => '1235879978',
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_version' => '1.0',
'blah' => 'blah',
'oauth_signature' => 'WXkYJlgVUAe3tJ0CAIXoSIywGio%3D'
);
return $output === $correct_output;
}
static function test_get_all_friends() {
$s = new MySpaceAPI(UnitTests::$UTF8_CONSUMER_KEY, UnitTests::$UTF8_CONSUMER_SECRET);
$friends = $s->get_all_friends(85628343);
return count($friends) >= 1;
}
static function test_get_user() {
$s = new MySpaceAPI(UnitTests::$UTF8_CONSUMER_KEY, UnitTests::$UTF8_CONSUMER_SECRET);
$uid = 85628343;
$user = $s->get_user(85628343);
return strpos($user['image'], "jpg") && $user['userid'] == $uid && $user['usertype'] == 'RegularUser';
}
static function test_set_and_get_appdata() {
$s = new MySpaceAPI(UnitTests::$UTF8_CONSUMER_KEY, UnitTests::$UTF8_CONSUMER_SECRET);
$test1 = rand();
$test2 = rand();
$data_array = array('test1' => $test1, 'test2' => $test2);
try {
$s->set_appdata(85628343, $data_array);
} catch (Exception $e) {
return FALSE;
}
sleep(1); // may take a moment for appdata to be saved
$array = $s->get_appdata(85628343);
$success = $array['test1'] == $test1 && $array['test2'] == $test2;
if (!$success) {
if ($array['test1'] != $test1) {
echo " (set {$test1} but got {$array['test1']}) ";
}
if ($array['test2'] != $test2) {
echo " (set {$test2} but got {$array['test2']}) ";
}
}
return $success;
}
// Correct signatures for testing can be generated by:
// http://www.hueniverse.com/hueniverse/2008/10/beginners-gui-1.html
static function common_authorization_header_test($user_data, $correct_base64_signature) {
$s = new MySpaceAPI(UnitTests::$UTF8_CONSUMER_KEY, UnitTests::$UTF8_CONSUMER_SECRET);
$oauth_nonce = 'd8502726b041db1758892edee8826314';
$oauth_timestamp = '1235879978';
$output = $s->get_signed_request(
'PUT',
'v1/users/85628343/appdata.XML',
$user_data,
$oauth_nonce,
$oauth_timestamp
);
$auth = MySpaceAPI::get_request_authorization_header(
'v1/users/85628343/appdata.XML', $output
);
$correct_auth = <<<AUTH
Authorization: OAuth realm="http://api.myspace.com/v1/users/85628343/appdata.XML" oauth_consumer_key="http%3A%2F%2Fwww.myspace.com%2F457168699" oauth_token="" oauth_nonce="d8502726b041db1758892edee8826314" oauth_timestamp="1235879978" oauth_signature_method="HMAC-SHA1" oauth_version="1.0" oauth_signature="{$correct_base64_signature}"
AUTH;
return $auth == $correct_auth;
}
static function test_appdata_authorization_header_single_param() {
return UnitTests::common_authorization_header_test(
array('blah' => 'blah'),
'WXkYJlgVUAe3tJ0CAIXoSIywGio%3D'
);
}
static function test_appdata_authorization_header_multi_param() {
return UnitTests::common_authorization_header_test(
array('blah' => 'blah', 'kissa' => 'koira'),
'MQtaEwNV8gfz%2Fs%2F96wkwm5VknFA%3D'
);
}
static function run() {
$all_succeeded = TRUE;
foreach (get_class_methods('UnitTests') as $method) {
if (strpos($method, 'test_') === 0) {
echo $method.' -- ';
$success = call_user_func('UnitTests::'.$method);
$text = $success?'PASS':'FAIL';
$color = $success?32:31;
echo chr(27)."[01;$color m$text".chr(27).'[00m';
if (!$success) $all_succeeded = FALSE;
echo "\n";
}
}
if (!$all_succeeded) {
echo "\n**** SOME TESTS FAILED ****\n";
exit(1);
}
exit(0);
}
}
UnitTests::run();
?>