DiscoverData.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. require_once "Auth/Yadis/Yadis.php";
  3. require_once "Tests/Auth/Yadis/TestUtil.php";
  4. global $testlist;
  5. $testlist = array(
  6. // success, input_name, id_name, result_name
  7. array(true, "equiv", "equiv", "xrds"),
  8. array(true, "header", "header", "xrds"),
  9. array(true, "lowercase_header", "lowercase_header", "xrds"),
  10. array(true, "xrds", "xrds", "xrds"),
  11. array(true, "xrds_ctparam", "xrds_ctparam", "xrds_ctparam"),
  12. array(true, "xrds_ctcase", "xrds_ctcase", "xrds_ctcase"),
  13. array(false, "xrds_html", "xrds_html", "xrds_html"),
  14. array(true, "redir_equiv", "equiv", "xrds"),
  15. array(true, "redir_header", "header", "xrds"),
  16. array(true, "redir_xrds", "xrds", "xrds"),
  17. array(false, "redir_xrds_html", "xrds_html", "xrds_html"),
  18. array(true, "redir_redir_equiv", "equiv", "xrds"),
  19. array(false, "404_server_response", null, null),
  20. array(false, "404_with_header", null, null),
  21. array(false, "404_with_meta", null, null),
  22. array(false, "201_server_response", null, null),
  23. array(false, "500_server_response", null, null)
  24. );
  25. function getExampleXRDS()
  26. {
  27. return Tests_Auth_Yadis_readdata('example-xrds.xml');
  28. }
  29. global $example_xrds;
  30. $example_xrds = getExampleXRDS();
  31. global $default_test_file;
  32. $default_test_file = 'test1-discover.txt';
  33. global $discover_tests;
  34. $discover_tests = array();
  35. function readTests($filename)
  36. {
  37. $data = Tests_Auth_Yadis_readdata($filename);
  38. if ($data === null) {
  39. return null;
  40. }
  41. $tests = array();
  42. foreach (preg_split("/\f\n/", $data) as $case) {
  43. list($name, $content) = explode("\n", $case, 2);
  44. $tests[$name] = $content;
  45. }
  46. return $tests;
  47. }
  48. function getData($filename, $name)
  49. {
  50. global $discover_tests;
  51. if (!array_key_exists($filename, $discover_tests)) {
  52. $data = readTests($filename);
  53. if ($data === null) {
  54. return null;
  55. }
  56. $discover_tests[$filename] = $data;
  57. }
  58. $file_tests = $discover_tests[$filename];
  59. if (array_key_exists($name, $file_tests)) {
  60. return $file_tests[$name];
  61. } else {
  62. return null;
  63. }
  64. }
  65. function fillTemplate($test_name, $template, $base_url, $example_xrds)
  66. {
  67. $mapping = array(
  68. array('URL_BASE/', $base_url),
  69. array('<XRDS Content>', $example_xrds),
  70. array('YADIS_HEADER', 'X-XRDS-Location'),
  71. array('NAME', $test_name));
  72. foreach ($mapping as $pair) {
  73. list($k, $v) = $pair;
  74. $template = str_replace($k, $v, $template);
  75. }
  76. return $template;
  77. }
  78. function generateSample($test_name, $base_url,
  79. $_example_xrds = null, $filename = null)
  80. {
  81. global $example_xrds, $default_test_file;
  82. if ($_example_xrds === null) {
  83. $_example_xrds = $example_xrds;
  84. }
  85. if ($filename === null) {
  86. $filename = $default_test_file;
  87. }
  88. $template = getData($filename, $test_name);
  89. if ($template === null) {
  90. return null;
  91. }
  92. return fillTemplate($test_name, $template, $base_url, $_example_xrds);
  93. }
  94. function generateResult($base_url, $input_name, $id_name, $result_name, $success)
  95. {
  96. $input_url = $base_url . $input_name; // urlparse.urljoin(base_url, input_name)
  97. // If the name is null then we expect the protocol to fail, which
  98. // we represent by null
  99. if ($id_name === null) {
  100. // assert result_name is null
  101. return array($input_url, null); // DiscoveryFailure
  102. }
  103. $result = generateSample($result_name, $base_url);
  104. list($headers, $content) = explode("\n\n", $result, 2);
  105. $header_lines = explode("\n", $headers);
  106. $ctype = null;
  107. foreach ($header_lines as $header_line) {
  108. if (strpos($header_line, 'Content-Type:') === 0) {
  109. list($temp, $ctype) = explode(":", $header_line, 2);
  110. $ctype = trim($ctype);
  111. break;
  112. }
  113. }
  114. $id_url = $base_url . $id_name;
  115. $result = new Auth_Yadis_Yadis();
  116. $result->uri = $id_url;
  117. if ($success) {
  118. $result->xrds_uri = $base_url . $result_name;
  119. } else {
  120. $result->xrds_uri = null;
  121. }
  122. $result->content_type = $ctype;
  123. $result->body = $content;
  124. return array($input_url, $result);
  125. }