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.

193 lines
6.0 KiB

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