XRI.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * XRI resolution / handling tests.
  4. *
  5. * @package OpenID
  6. * @author JanRain, Inc. <openid@janrain.com>
  7. * @copyright 2005-2008 Janrain, Inc.
  8. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
  9. */
  10. require_once "PHPUnit.php";
  11. require_once "Auth/Yadis/XRIRes.php";
  12. require_once "Auth/Yadis/XRI.php";
  13. require_once "Auth/Yadis/Yadis.php";
  14. class Tests_Auth_Yadis_XriDiscoveryTestCase extends PHPUnit_TestCase {
  15. function runTest()
  16. {
  17. $this->assertEquals(
  18. Auth_Yadis_identifierScheme('=john.smith'), 'XRI');
  19. $this->assertEquals(
  20. Auth_Yadis_identifierScheme(''), 'URI');
  21. $this->assertEquals(
  22. Auth_Yadis_identifierScheme('@smiths/john'), 'XRI');
  23. $this->assertEquals(
  24. Auth_Yadis_identifierScheme('smoker.myopenid.com'), 'URI');
  25. $this->assertEquals(
  26. Auth_Yadis_identifierScheme('xri://=john'), 'XRI');
  27. }
  28. }
  29. class Tests_Auth_Yadis_XriEscapingTestCase extends PHPUnit_TestCase {
  30. function test_escaping_percents()
  31. {
  32. $this->assertEquals(Auth_Yadis_escapeForIRI('@example/abc%2Fd/ef'),
  33. '@example/abc%252Fd/ef');
  34. }
  35. function runTest()
  36. {
  37. // no escapes
  38. $this->assertEquals('@example/foo/(@bar)',
  39. Auth_Yadis_escapeForIRI('@example/foo/(@bar)'));
  40. // escape slashes
  41. $this->assertEquals('@example/foo/(@bar%2Fbaz)',
  42. Auth_Yadis_escapeForIRI('@example/foo/(@bar/baz)'));
  43. $this->assertEquals('@example/foo/(@bar%2Fbaz)/(+a%2Fb)',
  44. Auth_Yadis_escapeForIRI('@example/foo/(@bar/baz)/(+a/b)'));
  45. // escape query ? and fragment
  46. $this->assertEquals('@example/foo/(@baz%3Fp=q%23r)?i=j#k',
  47. Auth_Yadis_escapeForIRI('@example/foo/(@baz?p=q#r)?i=j#k'));
  48. }
  49. }
  50. class Tests_Auth_Yadis_ProxyQueryTestCase extends PHPUnit_TestCase {
  51. function setUp()
  52. {
  53. $this->proxy_url = 'http://xri.example.com/';
  54. $this->fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
  55. $this->proxy = new Auth_Yadis_ProxyResolver($fetcher,
  56. $this->proxy_url);
  57. $this->servicetype = 'xri://+i-service*(+forwarding)*($v*1.0)';
  58. $this->servicetype_enc = 'xri%3A%2F%2F%2Bi-service%2A%28%2Bforwarding%29%2A%28%24v%2A1.0%29';
  59. }
  60. function runTest()
  61. {
  62. $st = $this->servicetype;
  63. $ste = $this->servicetype_enc;
  64. $args_esc = "_xrd_r=application%2Fxrds%2Bxml&_xrd_t=" . $ste;
  65. $h = $this->proxy_url;
  66. $this->assertEquals($h . '=foo?' . $args_esc,
  67. $this->proxy->queryURL('=foo', $st));
  68. $this->assertEquals($h . '=foo/bar?baz&' . $args_esc,
  69. $this->proxy->queryURL('=foo/bar?baz', $st));
  70. $this->assertEquals($h . '=foo/bar?baz=quux&' . $args_esc,
  71. $this->proxy->queryURL('=foo/bar?baz=quux', $st));
  72. $this->assertEquals($h . '=foo/bar?mi=fa&so=la&' . $args_esc,
  73. $this->proxy->queryURL('=foo/bar?mi=fa&so=la', $st));
  74. $args_esc = "_xrd_r=application%2Fxrds%2Bxml&_xrd_t=" . $ste;
  75. $h = $this->proxy_url;
  76. $this->assertEquals($h . '=foo/bar??' . $args_esc,
  77. $this->proxy->queryURL('=foo/bar?', $st));
  78. $this->assertEquals($h . '=foo/bar????' . $args_esc,
  79. $this->proxy->queryURL('=foo/bar???', $st));
  80. }
  81. }
  82. class Tests_Auth_Yadis_TestGetRootAuthority extends PHPUnit_TestCase {
  83. function runTest()
  84. {
  85. $xris = array(
  86. array("@foo", "@"),
  87. array("@foo*bar", "@"),
  88. array("@*foo*bar", "@"),
  89. array("@foo/bar", "@"),
  90. array("!!990!991", "!"),
  91. array("!1001!02", "!"),
  92. array("=foo*bar", "="),
  93. array("(example.com)/foo", "(example.com)"),
  94. array("(example.com)*bar/foo", "(example.com)"),
  95. array("baz.example.com/foo", "baz.example.com"),
  96. array("baz.example.com:8080/foo", "baz.example.com:8080")
  97. // Looking at the ABNF in XRI Syntax 2.0, I don't think you can
  98. // have example.com*bar. You can do (example.com)*bar, but that
  99. // would mean something else.
  100. // ("example.com*bar/(=baz)", "example.com*bar"),
  101. // ("baz.example.com!01/foo", "baz.example.com!01"),
  102. );
  103. foreach ($xris as $tupl) {
  104. list($thexri, $expected_root) = $tupl;
  105. $this->assertEquals(Auth_Yadis_XRI($expected_root),
  106. Auth_Yadis_rootAuthority($thexri),
  107. 'rootAuthority test ('.$thexri.')');
  108. }
  109. }
  110. }
  111. class Tests_Auth_Yadis_XRI extends PHPUnit_TestSuite {
  112. function getName()
  113. {
  114. return "Tests_Auth_Yadis_XRI";
  115. }
  116. function Tests_Auth_Yadis_XRI()
  117. {
  118. $this->addTest(new Tests_Auth_Yadis_ProxyQueryTestCase());
  119. $this->addTest(new Tests_Auth_Yadis_XriEscapingTestCase());
  120. $this->addTest(new Tests_Auth_Yadis_XriDiscoveryTestCase());
  121. $this->addTest(new Tests_Auth_Yadis_TestGetRootAuthority());
  122. }
  123. }
  124. ?>