OAuthRequestTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. /*
  3. * Tests of OAuthRequest
  4. *
  5. * The tests works by using OAuthTestUtils::build_request
  6. * to populare $_SERVER, $_GET & $_POST.
  7. *
  8. * Most of the base string and signature tests
  9. * are either very simple or based upon
  10. * http://wiki.oauth.net/TestCases
  11. */
  12. require_once dirname(__FILE__) . '/common.php';
  13. class OAuthRequestTest extends PHPUnit_Framework_TestCase {
  14. public function testCanGetSingleParameter() {
  15. // Yes, a awesomely boring test.. But if this doesn't work, the other tests is unreliable
  16. $request = new OAuthRequest('', '', array('test'=>'foo'));
  17. $this->assertEquals( 'foo', $request->get_parameter('test'), 'Failed to read back parameter');
  18. $request = new OAuthRequest('', '', array('test'=>array('foo', 'bar')));
  19. $this->assertEquals( array('foo', 'bar'), $request->get_parameter('test'), 'Failed to read back parameter');
  20. $request = new OAuthRequest('', '', array('test'=>'foo', 'bar'=>'baz'));
  21. $this->assertEquals( 'foo', $request->get_parameter('test'), 'Failed to read back parameter');
  22. $this->assertEquals( 'baz', $request->get_parameter('bar'), 'Failed to read back parameter');
  23. }
  24. public function testGetAllParameters() {
  25. // Yes, a awesomely boring test.. But if this doesn't work, the other tests is unreliable
  26. $request = new OAuthRequest('', '', array('test'=>'foo'));
  27. $this->assertEquals( array('test'=>'foo'), $request->get_parameters(), 'Failed to read back parameters');
  28. $request = new OAuthRequest('', '', array('test'=>'foo', 'bar'=>'baz'));
  29. $this->assertEquals( array('test'=>'foo', 'bar'=>'baz'), $request->get_parameters(), 'Failed to read back parameters');
  30. $request = new OAuthRequest('', '', array('test'=>array('foo', 'bar')));
  31. $this->assertEquals( array('test'=>array('foo', 'bar')), $request->get_parameters(), 'Failed to read back parameters');
  32. }
  33. public function testSetParameters() {
  34. $request = new OAuthRequest('', '');
  35. $this->assertEquals( NULL, $request->get_parameter('test'), 'Failed to assert that non-existing parameter is NULL');
  36. $request->set_parameter('test', 'foo');
  37. $this->assertEquals( 'foo', $request->get_parameter('test'), 'Failed to set single-entry parameter');
  38. $request->set_parameter('test', 'bar');
  39. $this->assertEquals( array('foo', 'bar'), $request->get_parameter('test'), 'Failed to set single-entry parameter');
  40. $request->set_parameter('test', 'bar', false);
  41. $this->assertEquals( 'bar', $request->get_parameter('test'), 'Failed to set single-entry parameter');
  42. }
  43. public function testUnsetParameter() {
  44. $request = new OAuthRequest('', '');
  45. $this->assertEquals( NULL, $request->get_parameter('test'));
  46. $request->set_parameter('test', 'foo');
  47. $this->assertEquals( 'foo', $request->get_parameter('test'));
  48. $request->unset_parameter('test');
  49. $this->assertEquals( NULL, $request->get_parameter('test'), 'Failed to unset parameter');
  50. }
  51. public function testCreateRequestFromConsumerAndToken() {
  52. $cons = new OAuthConsumer('key', 'kd94hf93k423kf44');
  53. $token = new OAuthToken('token', 'pfkkdhi9sl3r4s00');
  54. $request = OAuthRequest::from_consumer_and_token($cons, $token, 'POST', 'http://example.com');
  55. $this->assertEquals('POST', $request->get_normalized_http_method());
  56. $this->assertEquals('http://example.com', $request->get_normalized_http_url());
  57. $this->assertEquals('1.0', $request->get_parameter('oauth_version'));
  58. $this->assertEquals($cons->key, $request->get_parameter('oauth_consumer_key'));
  59. $this->assertEquals($token->key, $request->get_parameter('oauth_token'));
  60. $this->assertEquals(time(), $request->get_parameter('oauth_timestamp'));
  61. $this->assertRegExp('/[0-9a-f]{32}/', $request->get_parameter('oauth_nonce'));
  62. // We don't know what the nonce will be, except it'll be md5 and hence 32 hexa digits
  63. $request = OAuthRequest::from_consumer_and_token($cons, $token, 'POST', 'http://example.com', array('oauth_nonce'=>'foo'));
  64. $this->assertEquals('foo', $request->get_parameter('oauth_nonce'));
  65. $request = OAuthRequest::from_consumer_and_token($cons, NULL, 'POST', 'http://example.com', array('oauth_nonce'=>'foo'));
  66. $this->assertNull($request->get_parameter('oauth_token'));
  67. // Test that parameters given in the $http_url instead of in the $parameters-parameter
  68. // will still be picked up
  69. $request = OAuthRequest::from_consumer_and_token($cons, $token, 'POST', 'http://example.com/?foo=bar');
  70. $this->assertEquals('http://example.com/', $request->get_normalized_http_url());
  71. $this->assertEquals('bar', $request->get_parameter('foo'));
  72. }
  73. public function testBuildRequestFromPost() {
  74. OAuthTestUtils::build_request('POST', 'http://testbed/test', 'foo=bar&baz=blargh');
  75. $this->assertEquals(array('foo'=>'bar','baz'=>'blargh'), OAuthRequest::from_request()->get_parameters(), 'Failed to parse POST parameters');
  76. }
  77. public function testBuildRequestFromGet() {
  78. OAuthTestUtils::build_request('GET', 'http://testbed/test?foo=bar&baz=blargh');
  79. $this->assertEquals(array('foo'=>'bar','baz'=>'blargh'), OAuthRequest::from_request()->get_parameters(), 'Failed to parse GET parameters');
  80. }
  81. public function testBuildRequestFromHeader() {
  82. $test_header = 'OAuth realm="",oauth_foo=bar,oauth_baz="bla,rgh"';
  83. OAuthTestUtils::build_request('POST', 'http://testbed/test', '', $test_header);
  84. $this->assertEquals(array('oauth_foo'=>'bar','oauth_baz'=>'bla,rgh'), OAuthRequest::from_request()->get_parameters(), 'Failed to split auth-header correctly');
  85. }
  86. public function testHasProperParameterPriority() {
  87. $test_header = 'OAuth realm="",oauth_foo=header';
  88. OAuthTestUtils::build_request('POST', 'http://testbed/test?oauth_foo=get', 'oauth_foo=post', $test_header);
  89. $this->assertEquals('header', OAuthRequest::from_request()->get_parameter('oauth_foo'), 'Loaded parameters in with the wrong priorities');
  90. OAuthTestUtils::build_request('POST', 'http://testbed/test?oauth_foo=get', 'oauth_foo=post');
  91. $this->assertEquals('post', OAuthRequest::from_request()->get_parameter('oauth_foo'), 'Loaded parameters in with the wrong priorities');
  92. OAuthTestUtils::build_request('POST', 'http://testbed/test?oauth_foo=get');
  93. $this->assertEquals('get', OAuthRequest::from_request()->get_parameter('oauth_foo'), 'Loaded parameters in with the wrong priorities');
  94. }
  95. public function testNormalizeHttpMethod() {
  96. OAuthTestUtils::build_request('POST', 'http://testbed/test');
  97. $this->assertEquals('POST', OAuthRequest::from_request()->get_normalized_http_method(), 'Failed to normalize HTTP method: POST');
  98. OAuthTestUtils::build_request('post', 'http://testbed/test');
  99. $this->assertEquals('POST', OAuthRequest::from_request()->get_normalized_http_method(), 'Failed to normalize HTTP method: post');
  100. OAuthTestUtils::build_request('GET', 'http://testbed/test');
  101. $this->assertEquals('GET', OAuthRequest::from_request()->get_normalized_http_method(), 'Failed to normalize HTTP method: GET');
  102. OAuthTestUtils::build_request('PUT', 'http://testbed/test');
  103. $this->assertEquals('PUT', OAuthRequest::from_request()->get_normalized_http_method(), 'Failed to normalize HTTP method: PUT');
  104. }
  105. public function testNormalizeParameters() {
  106. // This is mostly repeats of OAuthUtilTest::testParseParameters & OAuthUtilTest::TestBuildHttpQuery
  107. // Tests taken from
  108. // http://wiki.oauth.net/TestCases ("Normalize Request Parameters")
  109. OAuthTestUtils::build_request('POST', 'http://testbed/test', 'name');
  110. $this->assertEquals( 'name=', OAuthRequest::from_request()->get_signable_parameters());
  111. OAuthTestUtils::build_request('POST', 'http://testbed/test', 'a=b');
  112. $this->assertEquals( 'a=b', OAuthRequest::from_request()->get_signable_parameters());
  113. OAuthTestUtils::build_request('POST', 'http://testbed/test', 'a=b&c=d');
  114. $this->assertEquals( 'a=b&c=d', OAuthRequest::from_request()->get_signable_parameters());
  115. OAuthTestUtils::build_request('POST', 'http://testbed/test', 'a=x%21y&a=x+y');
  116. $this->assertEquals( 'a=x%20y&a=x%21y', OAuthRequest::from_request()->get_signable_parameters());
  117. OAuthTestUtils::build_request('POST', 'http://testbed/test', 'x%21y=a&x=a');
  118. $this->assertEquals( 'x=a&x%21y=a', OAuthRequest::from_request()->get_signable_parameters());
  119. OAuthTestUtils::build_request('POST', 'http://testbed/test', 'a=1&c=hi there&f=25&f=50&f=a&z=p&z=t');
  120. $this->assertEquals( 'a=1&c=hi%20there&f=25&f=50&f=a&z=p&z=t', OAuthRequest::from_request()->get_signable_parameters());
  121. }
  122. public function testNormalizeHttpUrl() {
  123. OAuthTestUtils::build_request('POST', 'http://example.com');
  124. $this->assertEquals('http://example.com', OAuthRequest::from_request()->get_normalized_http_url());
  125. OAuthTestUtils::build_request('POST', 'https://example.com');
  126. $this->assertEquals('https://example.com', OAuthRequest::from_request()->get_normalized_http_url());
  127. // Tests that http on !80 and https on !443 keeps the port
  128. OAuthTestUtils::build_request('POST', 'http://example.com:8080');
  129. $this->assertEquals('http://example.com:8080', OAuthRequest::from_request()->get_normalized_http_url());
  130. OAuthTestUtils::build_request('POST', 'https://example.com:80');
  131. $this->assertEquals('https://example.com:80', OAuthRequest::from_request()->get_normalized_http_url());
  132. OAuthTestUtils::build_request('POST', 'http://example.com:443');
  133. $this->assertEquals('http://example.com:443', OAuthRequest::from_request()->get_normalized_http_url());
  134. OAuthTestUtils::build_request('POST', 'http://Example.COM');
  135. $this->assertEquals('http://example.com', OAuthRequest::from_request()->get_normalized_http_url());
  136. // Emulate silly behavior by some clients, where there Host header includes the port
  137. OAuthTestUtils::build_request('POST', 'http://example.com');
  138. $_SERVER['HTTP_HOST'] = $_SERVER['HTTP_HOST'] . ':' . $_SERVER['SERVER_PORT'];
  139. $this->assertEquals('http://example.com', OAuthRequest::from_request()->get_normalized_http_url());
  140. }
  141. public function testBuildPostData() {
  142. OAuthTestUtils::build_request('POST', 'http://example.com');
  143. $this->assertEquals('', OAuthRequest::from_request()->to_postdata());
  144. OAuthTestUtils::build_request('POST', 'http://example.com', 'foo=bar');
  145. $this->assertEquals('foo=bar', OAuthRequest::from_request()->to_postdata());
  146. OAuthTestUtils::build_request('GET', 'http://example.com?foo=bar');
  147. $this->assertEquals('foo=bar', OAuthRequest::from_request()->to_postdata());
  148. }
  149. public function testBuildUrl() {
  150. OAuthTestUtils::build_request('POST', 'http://example.com');
  151. $this->assertEquals('http://example.com', OAuthRequest::from_request()->to_url());
  152. OAuthTestUtils::build_request('POST', 'http://example.com', 'foo=bar');
  153. $this->assertEquals('http://example.com?foo=bar', OAuthRequest::from_request()->to_url());
  154. OAuthTestUtils::build_request('GET', 'http://example.com?foo=bar');
  155. $this->assertEquals('http://example.com?foo=bar', OAuthRequest::from_request()->to_url());
  156. }
  157. public function testConvertToString() {
  158. OAuthTestUtils::build_request('POST', 'http://example.com');
  159. $this->assertEquals('http://example.com', (string) OAuthRequest::from_request());
  160. OAuthTestUtils::build_request('POST', 'http://example.com', 'foo=bar');
  161. $this->assertEquals('http://example.com?foo=bar', (string) OAuthRequest::from_request());
  162. OAuthTestUtils::build_request('GET', 'http://example.com?foo=bar');
  163. $this->assertEquals('http://example.com?foo=bar', (string) OAuthRequest::from_request());
  164. }
  165. public function testBuildHeader() {
  166. OAuthTestUtils::build_request('POST', 'http://example.com');
  167. $this->assertEquals('Authorization: OAuth', OAuthRequest::from_request()->to_header());
  168. $this->assertEquals('Authorization: OAuth realm="test"', OAuthRequest::from_request()->to_header('test'));
  169. OAuthTestUtils::build_request('POST', 'http://example.com', 'foo=bar');
  170. $this->assertEquals('Authorization: OAuth', OAuthRequest::from_request()->to_header());
  171. $this->assertEquals('Authorization: OAuth realm="test"', OAuthRequest::from_request()->to_header('test'));
  172. OAuthTestUtils::build_request('POST', 'http://example.com', 'oauth_test=foo');
  173. $this->assertEquals('Authorization: OAuth oauth_test="foo"', OAuthRequest::from_request()->to_header());
  174. $this->assertEquals('Authorization: OAuth realm="test",oauth_test="foo"', OAuthRequest::from_request()->to_header('test'));
  175. // Is headers supposted to be Urlencoded. More to the point:
  176. // Should it be baz = bla,rgh or baz = bla%2Crgh ??
  177. // - morten.fangel
  178. OAuthTestUtils::build_request('POST', 'http://example.com', '', 'OAuth realm="",oauth_foo=bar,oauth_baz="bla,rgh"');
  179. $this->assertEquals('Authorization: OAuth oauth_foo="bar",oauth_baz="bla%2Crgh"', OAuthRequest::from_request()->to_header());
  180. $this->assertEquals('Authorization: OAuth realm="test",oauth_foo="bar",oauth_baz="bla%2Crgh"', OAuthRequest::from_request()->to_header('test'));
  181. }
  182. public function testWontBuildHeaderWithArrayInput() {
  183. $this->setExpectedException('OAuthException');
  184. OAuthTestUtils::build_request('POST', 'http://example.com', 'oauth_foo=bar&oauth_foo=baz');
  185. OAuthRequest::from_request()->to_header();
  186. }
  187. public function testBuildBaseString() {
  188. OAuthTestUtils::build_request('POST', 'http://testbed/test', 'n=v');
  189. $this->assertEquals('POST&http%3A%2F%2Ftestbed%2Ftest&n%3Dv', OAuthRequest::from_request()->get_signature_base_string());
  190. OAuthTestUtils::build_request('POST', 'http://testbed/test', 'n=v&n=v2');
  191. $this->assertEquals('POST&http%3A%2F%2Ftestbed%2Ftest&n%3Dv%26n%3Dv2', OAuthRequest::from_request()->get_signature_base_string());
  192. OAuthTestUtils::build_request('GET', 'http://example.com?n=v');
  193. $this->assertEquals('GET&http%3A%2F%2Fexample.com&n%3Dv', OAuthRequest::from_request()->get_signature_base_string());
  194. $params = 'oauth_version=1.0&oauth_consumer_key=dpf43f3p2l4k3l03&oauth_timestamp=1191242090';
  195. $params .= '&oauth_nonce=hsu94j3884jdopsl&oauth_signature_method=PLAINTEXT&oauth_signature=ignored';
  196. OAuthTestUtils::build_request('POST', 'https://photos.example.net/request_token', $params);
  197. $this->assertEquals('POST&https%3A%2F%2Fphotos.example.net%2Frequest_token&oauth_'
  198. .'consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dhsu94j3884j'
  199. .'dopsl%26oauth_signature_method%3DPLAINTEXT%26oauth_timestam'
  200. .'p%3D1191242090%26oauth_version%3D1.0',
  201. OAuthRequest::from_request()->get_signature_base_string());
  202. $params = 'file=vacation.jpg&size=original&oauth_version=1.0&oauth_consumer_key=dpf43f3p2l4k3l03';
  203. $params .= '&oauth_token=nnch734d00sl2jdk&oauth_timestamp=1191242096&oauth_nonce=kllo9940pd9333jh';
  204. $params .= '&oauth_signature=ignored&oauth_signature_method=HMAC-SHA1';
  205. OAuthTestUtils::build_request('GET', 'http://photos.example.net/photos?'.$params);
  206. $this->assertEquals('GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation'
  207. .'.jpg%26oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%'
  208. .'3Dkllo9940pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26o'
  209. .'auth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jd'
  210. .'k%26oauth_version%3D1.0%26size%3Doriginal',
  211. OAuthRequest::from_request()->get_signature_base_string());
  212. }
  213. public function testBuildSignature() {
  214. $params = 'file=vacation.jpg&size=original&oauth_version=1.0&oauth_consumer_key=dpf43f3p2l4k3l03';
  215. $params .= '&oauth_token=nnch734d00sl2jdk&oauth_timestamp=1191242096&oauth_nonce=kllo9940pd9333jh';
  216. $params .= '&oauth_signature=ignored&oauth_signature_method=HMAC-SHA1';
  217. OAuthTestUtils::build_request('GET', 'http://photos.example.net/photos?'.$params);
  218. $r = OAuthRequest::from_request();
  219. $cons = new OAuthConsumer('key', 'kd94hf93k423kf44');
  220. $token = new OAuthToken('token', 'pfkkdhi9sl3r4s00');
  221. $hmac = new OAuthSignatureMethod_HMAC_SHA1();
  222. $plaintext = new OAuthSignatureMethod_PLAINTEXT();
  223. $this->assertEquals('tR3+Ty81lMeYAr/Fid0kMTYa/WM=', $r->build_signature($hmac, $cons, $token));
  224. $this->assertEquals('kd94hf93k423kf44&pfkkdhi9sl3r4s00', $r->build_signature($plaintext, $cons, $token));
  225. }
  226. public function testSign() {
  227. $params = 'file=vacation.jpg&size=original&oauth_version=1.0&oauth_consumer_key=dpf43f3p2l4k3l03';
  228. $params .= '&oauth_token=nnch734d00sl2jdk&oauth_timestamp=1191242096&oauth_nonce=kllo9940pd9333jh';
  229. $params .= '&oauth_signature=__ignored__&oauth_signature_method=HMAC-SHA1';
  230. OAuthTestUtils::build_request('GET', 'http://photos.example.net/photos?'.$params);
  231. $r = OAuthRequest::from_request();
  232. $cons = new OAuthConsumer('key', 'kd94hf93k423kf44');
  233. $token = new OAuthToken('token', 'pfkkdhi9sl3r4s00');
  234. $hmac = new OAuthSignatureMethod_HMAC_SHA1();
  235. $plaintext = new OAuthSignatureMethod_PLAINTEXT();
  236. // We need to test both what the parameter is, and how the serialized request is..
  237. $r->sign_request($hmac, $cons, $token);
  238. $this->assertEquals('HMAC-SHA1', $r->get_parameter('oauth_signature_method'));
  239. $this->assertEquals('tR3+Ty81lMeYAr/Fid0kMTYa/WM=', $r->get_parameter('oauth_signature'));
  240. $expectedPostdata = 'file=vacation.jpg&oauth_consumer_key=dpf43f3p2l4k3l03&oauth_nonce=kllo9940pd9333jh&'
  241. . 'oauth_signature=tR3%2BTy81lMeYAr%2FFid0kMTYa%2FWM%3D&oauth_signature_method=HMAC-SHA1&'
  242. . 'oauth_timestamp=1191242096&oauth_token=nnch734d00sl2jdk&oauth_version=1.0&size=original';
  243. $this->assertEquals( $expectedPostdata, $r->to_postdata());
  244. $r->sign_request($plaintext, $cons, $token);
  245. $this->assertEquals('PLAINTEXT', $r->get_parameter('oauth_signature_method'));
  246. $this->assertEquals('kd94hf93k423kf44&pfkkdhi9sl3r4s00', $r->get_parameter('oauth_signature'));
  247. $expectedPostdata = 'file=vacation.jpg&oauth_consumer_key=dpf43f3p2l4k3l03&oauth_nonce=kllo9940pd9333jh&'
  248. . 'oauth_signature=kd94hf93k423kf44%26pfkkdhi9sl3r4s00&oauth_signature_method=PLAINTEXT&'
  249. . 'oauth_timestamp=1191242096&oauth_token=nnch734d00sl2jdk&oauth_version=1.0&size=original';
  250. $this->assertEquals( $expectedPostdata, $r->to_postdata());
  251. }
  252. }
  253. ?>