Discover_Yadis.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. require_once "Tests/Auth/Yadis/DiscoverData.php";
  3. require_once "Auth/Yadis/Yadis.php";
  4. require_once "Auth/Yadis/HTTPFetcher.php";
  5. global $__status_header_re;
  6. $__status_header_re = '/Status: (\d+) .*?$/m';
  7. function mkResponse($data)
  8. {
  9. global $__status_header_re;
  10. $matches = array();
  11. $status_mo = preg_match($__status_header_re, $data, $matches);
  12. list($headers_str, $body) = explode("\n\n", $data, 2);
  13. $headers = array();
  14. foreach (explode("\n", $headers_str) as $line) {
  15. list($k, $v) = explode(":", $line, 2);
  16. $k = strtolower(trim($k));
  17. $v = trim($v);
  18. $headers[$k] = $v;
  19. }
  20. $status = intval($matches[1]);
  21. $r = new Auth_Yadis_HTTPResponse(null, $status, $headers, $body);
  22. return $r;
  23. }
  24. class TestFetcher {
  25. function TestFetcher($base_url)
  26. {
  27. $this->base_url = $base_url;
  28. }
  29. function get($url, $headers = null)
  30. {
  31. $current_url = $url;
  32. while (true) {
  33. $parsed = parse_url($current_url);
  34. $path = substr($parsed['path'], 1);
  35. $data = generateSample($path, $this->base_url);
  36. if ($data === null) {
  37. return new Auth_Yadis_HTTPResponse($current_url,
  38. 404,
  39. array(),
  40. '');
  41. }
  42. $response = mkResponse($data);
  43. if (in_array($response->status, array(301, 302, 303, 307))) {
  44. $current_url = $response->headers['location'];
  45. } else {
  46. $response->final_url = $current_url;
  47. return $response;
  48. }
  49. }
  50. }
  51. }
  52. class BlankContentTypeFetcher {
  53. function get($url, $headers=null)
  54. {
  55. return new Auth_Yadis_HTTPResponse(
  56. $url, 200, array("Content-Type" => ""), '');
  57. }
  58. }
  59. class NoContentTypeFetcher {
  60. function get($url, $headers=null)
  61. {
  62. return new Auth_Yadis_HTTPResponse($url, 200, array(), '');
  63. }
  64. }
  65. class MockFetcher {
  66. function MockFetcher() {
  67. $this->count = 0;
  68. }
  69. function get($uri, $headers = null, $body = null)
  70. {
  71. $this->count++;
  72. if ($this->count == 1) {
  73. $headers = array(strtolower('X-XRDS-Location') . ': http://unittest/404');
  74. return new Auth_Yadis_HTTPResponse($uri, 200, $headers, '');
  75. } else {
  76. return new Auth_Yadis_HTTPResponse($uri, 404);
  77. }
  78. }
  79. }
  80. class TestSecondGet extends PHPUnit_Framework_TestCase {
  81. function test_404()
  82. {
  83. $uri = "http://something.unittest/";
  84. $response = null;
  85. $fetcher = new MockFetcher();
  86. $this->assertTrue(
  87. Auth_Yadis_Yadis::discover($uri, $response, $fetcher) === null);
  88. }
  89. }
  90. class _TestCase extends PHPUnit_Framework_TestCase {
  91. var $base_url = 'http://invalid.unittest/';
  92. function _TestCase($input_name, $id_name, $result_name, $success)
  93. {
  94. parent::__construct();
  95. $this->input_name = $input_name;
  96. $this->id_name = $id_name;
  97. $this->result_name = $result_name;
  98. $this->success = $success;
  99. $this->fetcher = new TestFetcher($this->base_url);
  100. }
  101. function setUp()
  102. {
  103. list($this->input_url, $this->expected) = generateResult($this->base_url,
  104. $this->input_name,
  105. $this->id_name,
  106. $this->result_name,
  107. $this->success);
  108. }
  109. function runTest()
  110. {
  111. if ($this->expected === null) {
  112. $result = Auth_Yadis_Yadis::discover($this->input_url,
  113. $this->fetcher);
  114. $this->assertTrue($result->isFailure());
  115. } else {
  116. $result = Auth_Yadis_Yadis::discover($this->input_url,
  117. $this->fetcher);
  118. if ($result === null) {
  119. $this->fail("Discovery result was null");
  120. return;
  121. }
  122. $this->assertEquals($this->input_url, $result->request_uri);
  123. $msg = 'Identity URL mismatch: actual = %s, expected = %s';
  124. $msg = sprintf($msg, $result->normalized_uri, $this->expected->uri);
  125. $this->assertEquals($this->expected->uri, $result->normalized_uri, $msg);
  126. $msg = 'Content mismatch: actual = %s, expected = %s';
  127. $msg = sprintf($msg, $result->response_text, $this->expected->body);
  128. $this->assertEquals($this->expected->body, $result->response_text, $msg);
  129. $this->assertEquals($this->expected->xrds_uri, $result->xrds_uri);
  130. $this->assertEquals($this->expected->content_type, $result->content_type);
  131. }
  132. }
  133. function getName()
  134. {
  135. if ($this->input_url) {
  136. return $this->input_url;
  137. } else {
  138. return $this->input_name;
  139. }
  140. }
  141. }
  142. class Tests_Auth_Yadis_Discover_Yadis extends PHPUnit_Framework_TestSuite {
  143. function getName()
  144. {
  145. return "Tests_Auth_Yadis_Discover_Yadis";
  146. }
  147. function Tests_Auth_Yadis_Discover_Yadis()
  148. {
  149. global $testlist;
  150. foreach ($testlist as $test) {
  151. list($success, $input_name, $id_name, $result_name) = $test;
  152. $this->addTest(new _TestCase($input_name, $id_name, $result_name, $success));
  153. }
  154. }
  155. }
  156. class Tests_Auth_Yadis_Discover_Yadis_ContentTypes extends PHPUnit_Framework_TestCase {
  157. function test_is_xrds_yadis_location()
  158. {
  159. $result = new Auth_Yadis_DiscoveryResult('http://request.uri/');
  160. $result->normalized_uri = "http://normalized/";
  161. $result->xrds_uri = "http://normalized/xrds";
  162. $this->assertTrue($result->isXRDS());
  163. }
  164. function test_is_xrds_content_type()
  165. {
  166. $result = new Auth_Yadis_DiscoveryResult('http://request.uri/');
  167. $result->normalized_uri = $result->xrds_uri = "http://normalized/";
  168. $result->content_type = Auth_Yadis_CONTENT_TYPE;
  169. $this->assertTrue($result->isXRDS());
  170. }
  171. function test_is_xrds_neither()
  172. {
  173. $result = new Auth_Yadis_DiscoveryResult('http://request.uri/');
  174. $result->normalized_uri = $result->xrds_uri = "http://normalized/";
  175. $result->content_type = "another/content-type";
  176. $this->assertTrue(!$result->isXRDS());
  177. }
  178. function test_no_content_type()
  179. {
  180. $fetcher = new NoContentTypeFetcher();
  181. $result = Auth_Yadis_Yadis::discover("http://bogus", $fetcher);
  182. $this->assertEquals(null, $result->content_type);
  183. }
  184. function test_blank_content_type()
  185. {
  186. $fetcher = new BlankContentTypeFetcher();
  187. $result = Auth_Yadis_Yadis::discover("http://bogus", $fetcher);
  188. $this->assertEquals("", $result->content_type);
  189. }
  190. }
  191. global $Tests_Auth_Yadis_Discover_Yadis_other;
  192. $Tests_Auth_Yadis_Discover_Yadis_other = array(
  193. new Tests_Auth_Yadis_Discover_Yadis_ContentTypes()
  194. );