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.

136 lines
4.2 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\HTTPTest(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 testMetaEquivDeleted() {
  91. $url = 'http://source.example.com/deleted';
  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 testMetaEquivDeletedTargetProvided() {
  103. // for example when verifying a webmention but the source was replaced with an html deleted page
  104. $url = 'http://source.example.com/deleted';
  105. $response = $this->parse([
  106. 'url' => $url,
  107. 'target' => 'http://example.com/'
  108. ]);
  109. $body = $response->getContent();
  110. $this->assertEquals(200, $response->getStatusCode());
  111. $data = json_decode($body);
  112. $this->assertObjectHasAttribute('error', $data);
  113. $this->assertEquals('no_link_found', $data->error);
  114. $this->assertEquals(410, $data->code);
  115. }
  116. }