OAuthUtilTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. require_once dirname(__FILE__) . '/common.php';
  3. /**
  4. * Tests of OAuthUtil
  5. */
  6. class OAuthUtilTest extends PHPUnit_Framework_TestCase {
  7. public function testUrlencode() {
  8. // Tests taken from
  9. // http://wiki.oauth.net/TestCases ("Parameter Encoding")
  10. $this->assertEquals('abcABC123', OAuthUtil::urlencode_rfc3986('abcABC123'));
  11. $this->assertEquals('-._~', OAuthUtil::urlencode_rfc3986('-._~'));
  12. $this->assertEquals('%25', OAuthUtil::urlencode_rfc3986('%'));
  13. $this->assertEquals('%2B', OAuthUtil::urlencode_rfc3986('+'));
  14. $this->assertEquals('%0A', OAuthUtil::urlencode_rfc3986("\n"));
  15. $this->assertEquals('%20', OAuthUtil::urlencode_rfc3986(' '));
  16. $this->assertEquals('%7F', OAuthUtil::urlencode_rfc3986("\x7F"));
  17. //$this->assertEquals('%C2%80', OAuthUtil::urlencode_rfc3986("\x00\x80"));
  18. //$this->assertEquals('%E3%80%81', OAuthUtil::urlencode_rfc3986("\x30\x01"));
  19. // Last two checks disabled because of lack of UTF-8 support, or lack
  20. // of knowledge from me (morten.fangel) on how to use it properly..
  21. // A few tests to ensure code-coverage
  22. $this->assertEquals( '', OAuthUtil::urlencode_rfc3986(NULL));
  23. $this->assertEquals( '', OAuthUtil::urlencode_rfc3986(new stdClass()));
  24. }
  25. public function testUrldecode() {
  26. // Tests taken from
  27. // http://wiki.oauth.net/TestCases ("Parameter Encoding")
  28. $this->assertEquals('abcABC123', OAuthUtil::urldecode_rfc3986('abcABC123'));
  29. $this->assertEquals('-._~', OAuthUtil::urldecode_rfc3986('-._~'));
  30. $this->assertEquals('%', OAuthUtil::urldecode_rfc3986('%25'));
  31. $this->assertEquals('+', OAuthUtil::urldecode_rfc3986('%2B'));
  32. $this->assertEquals("\n", OAuthUtil::urldecode_rfc3986('%0A'));
  33. $this->assertEquals(' ', OAuthUtil::urldecode_rfc3986('%20'));
  34. $this->assertEquals("\x7F", OAuthUtil::urldecode_rfc3986('%7F'));
  35. //$this->assertEquals("\x00\x80", OAuthUtil::urldecode_rfc3986('%C2%80'));
  36. //$this->assertEquals("\x30\x01", OAuthUtil::urldecode_rfc3986('%E3%80%81'));
  37. // Last two checks disabled because of lack of UTF-8 support, or lack
  38. // of knowledge from me (morten.fangel) on how to use it properly..
  39. }
  40. public function testParseParameter() {
  41. // Tests taken from
  42. // http://wiki.oauth.net/TestCases ("Normalize Request Parameters")
  43. $this->assertEquals(
  44. array('name'=>''),
  45. OAuthUtil::parse_parameters('name')
  46. );
  47. $this->assertEquals(
  48. array('a'=>'b'),
  49. OAuthUtil::parse_parameters('a=b')
  50. );
  51. $this->assertEquals(
  52. array('a'=>'b','c'=>'d'),
  53. OAuthUtil::parse_parameters('a=b&c=d')
  54. );
  55. $this->assertEquals(
  56. array('a'=>array('x!y','x y')),
  57. OAuthUtil::parse_parameters('a=x!y&a=x+y')
  58. );
  59. $this->assertEquals(
  60. array('x!y'=>'a', 'x' =>'a'),
  61. OAuthUtil::parse_parameters('x!y=a&x=a')
  62. );
  63. }
  64. public function testBuildHttpQuery() {
  65. // Tests taken from
  66. // http://wiki.oauth.net/TestCases ("Normalize Request Parameters")
  67. $this->assertEquals(
  68. 'name=',
  69. OAuthUtil::build_http_query(array('name'=>''))
  70. );
  71. $this->assertEquals(
  72. 'a=b',
  73. OAuthUtil::build_http_query(array('a'=>'b'))
  74. );
  75. $this->assertEquals(
  76. 'a=b&c=d',
  77. OAuthUtil::build_http_query(array('a'=>'b','c'=>'d'))
  78. );
  79. $this->assertEquals(
  80. 'a=x%20y&a=x%21y',
  81. OAuthUtil::build_http_query(array('a'=>array('x!y','x y')))
  82. );
  83. $this->assertEquals(
  84. 'x=a&x%21y=a',
  85. OAuthUtil::build_http_query(array('x!y'=>'a', 'x' =>'a'))
  86. );
  87. // Test taken from the Spec 9.1.1
  88. $this->assertEquals(
  89. 'a=1&c=hi%20there&f=25&f=50&f=a&z=p&z=t',
  90. OAuthUtil::build_http_query(array('a'=>'1', 'c' =>'hi there', 'f'=>array(25, 50, 'a'), 'z'=>array('p','t')))
  91. );
  92. // From issue 164, by hidetaka
  93. // Based on discussion at
  94. // http://groups.google.com/group/oauth/browse_thread/thread/7c698004be0d536/dced7b6c82b917b2?lnk=gst&q=sort#
  95. $this->assertEquals(
  96. 'x=200&x=25&y=B&y=a',
  97. OAuthUtil::build_http_query(array('x'=>array(25, 200), 'y'=>array('a', 'B')))
  98. );
  99. }
  100. public function testSplitHeader() {
  101. $this->assertEquals(
  102. array('oauth_foo'=>'bar','oauth_baz'=>'bla,rgh'),
  103. OAuthUtil::split_header('OAuth realm="",oauth_foo=bar,oauth_baz="bla,rgh"')
  104. );
  105. $this->assertEquals(
  106. array(),
  107. OAuthUtil::split_header('OAuth realm="",foo=bar,baz="bla,rgh"')
  108. );
  109. $this->assertEquals(
  110. array('foo'=>'bar', 'baz'=>'bla,rgh'),
  111. OAuthUtil::split_header('OAuth realm="",foo=bar,baz="bla,rgh"', false)
  112. );
  113. $this->assertEquals(
  114. array('oauth_foo' => 'hi there'),
  115. OAuthUtil::split_header('OAuth realm="",oauth_foo=hi+there,foo=bar,baz="bla,rgh"')
  116. );
  117. }
  118. public function testGetHeaders() {
  119. if (function_exists('apache_request_headers')) {
  120. $this->markTestSkipped('We assume the apache module is well tested. Since this module is present, no need testing our suplement');
  121. }
  122. $_SERVER['HTTP_HOST'] = 'foo';
  123. $_SERVER['HTTP_X_WHATEVER'] = 'bar';
  124. $this->assertEquals( array('Host'=>'foo', 'X-Whatever'=>'bar'), OAuthUtil::get_headers() );
  125. // Test picking up the Content-Type of POST requests running as an Apache module but not having the ARH method
  126. $_SERVER['CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
  127. $this->assertEquals( array('Host'=>'foo', 'X-Whatever'=>'bar', 'Content-Type'=>'application/x-www-form-urlencoded'), OAuthUtil::get_headers() );
  128. // Test picking up the Content-Type of POST requests when using CGI
  129. unset($_SERVER['CONTENT_TYPE']);
  130. $this->assertEquals( array('Host'=>'foo', 'X-Whatever'=>'bar'), OAuthUtil::get_headers() );
  131. $_ENV['CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
  132. $this->assertEquals( array('Host'=>'foo', 'X-Whatever'=>'bar', 'Content-Type'=>'application/x-www-form-urlencoded'), OAuthUtil::get_headers() );
  133. }
  134. }