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.

232 lines
7.0 KiB

  1. <?php
  2. use Symfony\Component\HttpFoundation\Request;
  3. use Symfony\Component\HttpFoundation\Response;
  4. class FetchTest extends PHPUnit\Framework\TestCase
  5. {
  6. private $http;
  7. public function setUp(): void
  8. {
  9. $this->client = new Parse();
  10. $this->client->http = new p3k\HTTP\Test(dirname(__FILE__).'/data/');
  11. $this->client->mc = null;
  12. }
  13. private function parse($params)
  14. {
  15. $request = new Request($params);
  16. $response = new Response();
  17. return $this->client->parse($request, $response);
  18. }
  19. public function testRedirectLimit()
  20. {
  21. $url = 'http://redirect.example.com/3';
  22. $response = $this->parse(
  23. [
  24. 'url' => $url,
  25. 'max_redirects' => 1
  26. ]
  27. );
  28. $body = $response->getContent();
  29. $this->assertEquals(200, $response->getStatusCode());
  30. $data = json_decode($body);
  31. $this->assertObjectHasAttribute('error', $data);
  32. $this->assertEquals('too_many_redirects', $data->error);
  33. $url = 'http://redirect.example.com/2';
  34. $response = $this->parse(
  35. [
  36. 'url' => $url,
  37. 'max_redirects' => 1
  38. ]
  39. );
  40. $body = $response->getContent();
  41. $this->assertEquals(200, $response->getStatusCode());
  42. $data = json_decode($body);
  43. $this->assertObjectHasAttribute('error', $data);
  44. $this->assertEquals('too_many_redirects', $data->error);
  45. }
  46. public function testRedirectUnderLimit()
  47. {
  48. $url = 'http://redirect.example.com/2';
  49. $response = $this->parse(
  50. [
  51. 'url' => $url,
  52. 'max_redirects' => 2
  53. ]
  54. );
  55. $body = $response->getContent();
  56. $this->assertEquals(200, $response->getStatusCode());
  57. $data = json_decode($body);
  58. $this->assertObjectNotHasAttribute('error', $data);
  59. $this->assertEquals(200, $data->code);
  60. $this->assertEquals('The Final Page', $data->data->name);
  61. $this->assertEquals('http://redirect.example.com/0', $data->url);
  62. }
  63. public function testReturnsHTTPStatusCode()
  64. {
  65. $url = 'http://redirect.example.com/code-418';
  66. $response = $this->parse(
  67. [
  68. 'url' => $url
  69. ]
  70. );
  71. $body = $response->getContent();
  72. $this->assertEquals(200, $response->getStatusCode());
  73. $data = json_decode($body);
  74. $this->assertObjectNotHasAttribute('error', $data);
  75. $this->assertEquals($url, $data->url);
  76. $this->assertEquals(418, $data->code);
  77. }
  78. public function testReturnsForbidden()
  79. {
  80. $url = 'http://redirect.example.com/code-403';
  81. $response = $this->parse(
  82. [
  83. 'url' => $url
  84. ]
  85. );
  86. $body = $response->getContent();
  87. $this->assertEquals(200, $response->getStatusCode());
  88. $data = json_decode($body);
  89. $this->assertObjectHasAttribute('error', $data);
  90. $this->assertEquals('forbidden', $data->error);
  91. $this->assertEquals($url, $data->url);
  92. $this->assertEquals(403, $data->code);
  93. }
  94. public function testReturnsUnauthorized()
  95. {
  96. $url = 'http://redirect.example.com/code-401';
  97. $response = $this->parse(
  98. [
  99. 'url' => $url
  100. ]
  101. );
  102. $body = $response->getContent();
  103. $this->assertEquals(200, $response->getStatusCode());
  104. $data = json_decode($body);
  105. $this->assertObjectHasAttribute('error', $data);
  106. $this->assertEquals('unauthorized', $data->error);
  107. $this->assertEquals($url, $data->url);
  108. $this->assertEquals(401, $data->code);
  109. }
  110. public function testDeleted()
  111. {
  112. $url = 'http://source.example.com/deleted-gone';
  113. $response = $this->parse(
  114. [
  115. 'url' => $url
  116. ]
  117. );
  118. $body = $response->getContent();
  119. $this->assertEquals(200, $response->getStatusCode());
  120. $data = json_decode($body);
  121. $this->assertObjectNotHasAttribute('error', $data);
  122. $this->assertEquals(410, $data->code);
  123. $this->assertEquals('This post has been deleted.', $data->data->content->text);
  124. }
  125. public function testDeletedEmptyBody()
  126. {
  127. $url = 'http://source.example.com/deleted-empty';
  128. $response = $this->parse(
  129. [
  130. 'url' => $url
  131. ]
  132. );
  133. $body = $response->getContent();
  134. $this->assertEquals(200, $response->getStatusCode());
  135. $data = json_decode($body);
  136. $this->assertObjectNotHasAttribute('error', $data);
  137. $this->assertEquals(410, $data->code);
  138. $this->assertEquals('unknown', $data->data->type);
  139. }
  140. public function testDeletedTargetProvided()
  141. {
  142. $url = 'http://source.example.com/deleted-gone';
  143. $response = $this->parse(
  144. [
  145. 'url' => $url,
  146. 'target' => 'http://example.com/'
  147. ]
  148. );
  149. $body = $response->getContent();
  150. $this->assertEquals(200, $response->getStatusCode());
  151. $data = json_decode($body);
  152. $this->assertObjectHasAttribute('error', $data);
  153. $this->assertEquals('no_link_found', $data->error);
  154. $this->assertEquals(410, $data->code);
  155. }
  156. public function testMetaEquivDeleted()
  157. {
  158. $url = 'http://source.example.com/deleted';
  159. $response = $this->parse(
  160. [
  161. 'url' => $url
  162. ]
  163. );
  164. $body = $response->getContent();
  165. $this->assertEquals(200, $response->getStatusCode());
  166. $data = json_decode($body);
  167. $this->assertObjectNotHasAttribute('error', $data);
  168. $this->assertEquals(410, $data->code);
  169. $this->assertEquals('This post has been deleted.', $data->data->content->text);
  170. }
  171. public function testMetaEquivDeletedCaps()
  172. {
  173. $url = 'http://source.example.com/deleted-2';
  174. $response = $this->parse(
  175. [
  176. 'url' => $url
  177. ]
  178. );
  179. $body = $response->getContent();
  180. $this->assertEquals(200, $response->getStatusCode());
  181. $data = json_decode($body);
  182. $this->assertObjectNotHasAttribute('error', $data);
  183. $this->assertEquals(410, $data->code);
  184. $this->assertEquals('This post has been deleted.', $data->data->content->text);
  185. }
  186. public function testMetaEquivDeletedTargetProvided()
  187. {
  188. // for example when verifying a webmention but the source was replaced with an html deleted page
  189. $url = 'http://source.example.com/deleted';
  190. $response = $this->parse(
  191. [
  192. 'url' => $url,
  193. 'target' => 'http://example.com/'
  194. ]
  195. );
  196. $body = $response->getContent();
  197. $this->assertEquals(200, $response->getStatusCode());
  198. $data = json_decode($body);
  199. $this->assertObjectHasAttribute('error', $data);
  200. $this->assertEquals('no_link_found', $data->error);
  201. $this->assertEquals(410, $data->code);
  202. }
  203. }