You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
6.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <?php
  2. namespace p3k\WebSub\Tests;
  3. use PHPUnit\Framework\TestCase;
  4. use p3k\WebSub\Client;
  5. class DiscoverTest extends TestCase {
  6. public function testFindsHubAndSelfInHeadRequestLinkHeaders() {
  7. $http = new FakeHTTP([
  8. 'HEAD' => [
  9. 'headers' => ['Content-Type' => 'text/html; charset=utf-8'],
  10. 'rels' => [
  11. 'hub' => ['https://hub.example/'],
  12. 'self' => ['https://example.com/'],
  13. ],
  14. ],
  15. ]);
  16. $client = new Client($http);
  17. $result = $client->discover('https://example.com/');
  18. $this->assertSame([
  19. 'hub' => 'https://hub.example/',
  20. 'hub_source' => 'http',
  21. 'self' => 'https://example.com/',
  22. 'self_source' => 'http',
  23. 'type' => 'html',
  24. ], $result);
  25. // Everything was in the HEAD response, so no GET is needed
  26. $this->assertCount(1, $http->requests);
  27. $this->assertSame('HEAD', $http->requests[0]['method']);
  28. }
  29. public function testFallsBackToGetWhenHeadHasNoLinks() {
  30. $http = new FakeHTTP([
  31. 'GET' => [
  32. 'headers' => ['Content-Type' => 'text/html'],
  33. 'rels' => [
  34. 'hub' => ['https://hub.example/'],
  35. 'self' => ['https://example.com/'],
  36. ],
  37. ],
  38. ]);
  39. $client = new Client($http);
  40. $result = $client->discover('https://example.com/');
  41. $this->assertSame('https://hub.example/', $result['hub']);
  42. $this->assertSame('http', $result['hub_source']);
  43. $this->assertSame(['HEAD', 'GET'], array_column($http->requests, 'method'));
  44. }
  45. public function testSkipsHeadRequestWhenHeadfirstIsFalse() {
  46. $http = new FakeHTTP([
  47. 'GET' => [
  48. 'headers' => ['Content-Type' => 'text/html'],
  49. 'rels' => [
  50. 'hub' => ['https://hub.example/'],
  51. 'self' => ['https://example.com/'],
  52. ],
  53. ],
  54. ]);
  55. $client = new Client($http);
  56. $client->discover('https://example.com/', false);
  57. $this->assertSame(['GET'], array_column($http->requests, 'method'));
  58. }
  59. public function testDetectsRssFromHeadContentType() {
  60. $http = new FakeHTTP([
  61. 'HEAD' => [
  62. 'headers' => ['Content-Type' => 'application/rss+xml; charset=utf-8'],
  63. 'rels' => [
  64. 'hub' => ['https://hub.example/'],
  65. 'self' => ['https://example.com/feed.rss'],
  66. ],
  67. ],
  68. ]);
  69. $client = new Client($http);
  70. $result = $client->discover('https://example.com/feed.rss');
  71. $this->assertSame('rss', $result['type']);
  72. $this->assertCount(1, $http->requests);
  73. }
  74. public function testFindsLinksInAtomFeedBody() {
  75. $http = new FakeHTTP([
  76. 'GET' => [
  77. 'headers' => ['Content-Type' => 'application/atom+xml'],
  78. 'body' => '<?xml version="1.0" encoding="utf-8"?>
  79. <feed xmlns="http://www.w3.org/2005/Atom">
  80. <title>Example</title>
  81. <link rel="hub" href="https://hub.example/"/>
  82. <link rel="self" href="https://example.com/feed.atom"/>
  83. </feed>',
  84. ],
  85. ]);
  86. $client = new Client($http);
  87. $result = $client->discover('https://example.com/feed.atom');
  88. $this->assertSame([
  89. 'hub' => 'https://hub.example/',
  90. 'hub_source' => 'body',
  91. 'self' => 'https://example.com/feed.atom',
  92. 'self_source' => 'body',
  93. 'type' => 'atom',
  94. ], $result);
  95. }
  96. public function testFindsAtomLinksInRssFeedBody() {
  97. $http = new FakeHTTP([
  98. 'GET' => [
  99. 'headers' => ['Content-Type' => 'application/rss+xml'],
  100. 'body' => '<?xml version="1.0" encoding="utf-8"?>
  101. <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  102. <channel>
  103. <title>Example</title>
  104. <atom:link rel="hub" href="https://hub.example/"/>
  105. <atom:link rel="self" href="https://example.com/feed.rss"/>
  106. </channel>
  107. </rss>',
  108. ],
  109. ]);
  110. $client = new Client($http);
  111. $result = $client->discover('https://example.com/feed.rss');
  112. $this->assertSame('https://hub.example/', $result['hub']);
  113. $this->assertSame('https://example.com/feed.rss', $result['self']);
  114. $this->assertSame('body', $result['self_source']);
  115. $this->assertSame('rss', $result['type']);
  116. }
  117. public function testFindsLinkElementsInHtmlBody() {
  118. $http = new FakeHTTP([
  119. 'GET' => [
  120. 'headers' => ['Content-Type' => 'text/html'],
  121. 'body' => '<!doctype html>
  122. <html>
  123. <head>
  124. <title>Example</title>
  125. <link rel="hub" href="https://hub.example/">
  126. <link rel="self" href="https://example.com/">
  127. </head>
  128. <body></body>
  129. </html>',
  130. ],
  131. ]);
  132. $client = new Client($http);
  133. $result = $client->discover('https://example.com/');
  134. $this->assertSame('https://hub.example/', $result['hub']);
  135. $this->assertSame('body', $result['hub_source']);
  136. $this->assertSame('https://example.com/', $result['self']);
  137. $this->assertSame('html', $result['type']);
  138. }
  139. public function testLinkHeadersTakePriorityOverBody() {
  140. $http = new FakeHTTP([
  141. 'GET' => [
  142. 'headers' => ['Content-Type' => 'application/atom+xml'],
  143. 'rels' => ['hub' => ['https://header-hub.example/']],
  144. 'body' => '<?xml version="1.0" encoding="utf-8"?>
  145. <feed xmlns="http://www.w3.org/2005/Atom">
  146. <link rel="hub" href="https://body-hub.example/"/>
  147. <link rel="self" href="https://example.com/feed.atom"/>
  148. </feed>',
  149. ],
  150. ]);
  151. $client = new Client($http);
  152. $result = $client->discover('https://example.com/feed.atom');
  153. $this->assertSame('https://header-hub.example/', $result['hub']);
  154. $this->assertSame('http', $result['hub_source']);
  155. $this->assertSame('https://example.com/feed.atom', $result['self']);
  156. $this->assertSame('body', $result['self_source']);
  157. }
  158. public function testReturnsFalseWithoutHub() {
  159. $http = new FakeHTTP([
  160. 'GET' => [
  161. 'headers' => ['Content-Type' => 'text/html'],
  162. 'rels' => ['self' => ['https://example.com/']],
  163. ],
  164. ]);
  165. $client = new Client($http);
  166. $this->assertFalse($client->discover('https://example.com/'));
  167. }
  168. public function testVerboseIncludesDetails() {
  169. $http = new FakeHTTP([
  170. 'HEAD' => [
  171. 'headers' => ['Content-Type' => 'text/html'],
  172. 'rels' => [
  173. 'hub' => ['https://hub.example/'],
  174. 'self' => ['https://example.com/'],
  175. ],
  176. ],
  177. ]);
  178. $client = new Client($http);
  179. $result = $client->discover('https://example.com/', true, true);
  180. $this->assertSame(['https://hub.example/'], $result['details']['http']['hub']);
  181. $this->assertSame([], $result['details']['body']['hub']);
  182. }
  183. }