From ab2ebeda104555928ef044c662b1e672c067e218 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Thu, 5 Mar 2020 21:26:57 +0100 Subject: Add atom and rss feed link url extraction --- tests/HubUrlExtractorTest.php | 225 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 225 insertions(+) create mode 100644 tests/HubUrlExtractorTest.php (limited to 'tests') diff --git a/tests/HubUrlExtractorTest.php b/tests/HubUrlExtractorTest.php new file mode 100644 index 0000000..4c0a44b --- /dev/null +++ b/tests/HubUrlExtractorTest.php @@ -0,0 +1,225 @@ +addResponse( + $mock, + "HTTP/1.0 200 OK\r\n" + . "Content-type: text/html\r\n" + . "Link: ; rel=\"hub\"\r\n" + . "Link: ; rel=\"self\"\r\n" + . "\r\n", + 'http://example.org/' + ); + + $extractor = new phinde\HubUrlExtractor(); + $extractor->setRequestTemplate( + new HTTP_Request2(null, null, ['adapter' => $mock]) + ); + + $this->assertEquals( + [ + 'hub' => 'https://hub.example.com/', + 'self' => 'http://example.com/feed', + ], + $extractor->getUrls('http://example.org/') + ); + } + + public function testGetUrlsHtml() + { + $mock = new HTTP_Request2_Adapter_Mock(); + //HEAD + $this->addResponse( + $mock, + "HTTP/1.0 200 OK\r\n" + . "Content-type: text/html\r\n" + . "\r\n", + 'http://example.org/' + ); + //HEAD + $this->addResponse( + $mock, + "HTTP/1.0 200 OK\r\n" + . "Content-type: text/html\r\n" + . "\r\n" + . << + + + + + +HTM, + 'http://example.org/' + ); + + $extractor = new phinde\HubUrlExtractor(); + $extractor->setRequestTemplate( + new HTTP_Request2(null, null, ['adapter' => $mock]) + ); + + $this->assertEquals( + [ + 'hub' => 'https://hub.example.com/', + 'self' => 'http://example.com/feed', + ], + $extractor->getUrls('http://example.org/') + ); + } + + public function testGetUrlsXHtml() + { + $mock = new HTTP_Request2_Adapter_Mock(); + //HEAD + $this->addResponse( + $mock, + "HTTP/1.0 200 OK\r\n" + . "Content-type: application/xhtml+xml\r\n" + . "\r\n", + 'http://example.org/' + ); + //HEAD + $this->addResponse( + $mock, + "HTTP/1.0 200 OK\r\n" + . "Content-type: application/xhtml+xml\r\n" + . "\r\n" + . << + + + + + +HTM, + 'http://example.org/' + ); + + $extractor = new phinde\HubUrlExtractor(); + $extractor->setRequestTemplate( + new HTTP_Request2(null, null, ['adapter' => $mock]) + ); + + $this->assertEquals( + [ + 'hub' => 'https://hub.example.com/', + 'self' => 'http://example.com/feed', + ], + $extractor->getUrls('http://example.org/') + ); + } + + public function testGetUrlsAtom() + { + $mock = new HTTP_Request2_Adapter_Mock(); + //HEAD + $this->addResponse( + $mock, + "HTTP/1.0 200 OK\r\n" + . "Content-type: application/atom+xml\r\n" + . "\r\n", + 'http://example.org/' + ); + //HEAD + $this->addResponse( + $mock, + "HTTP/1.0 200 OK\r\n" + . "Content-type: application/atom+xml\r\n" + . "\r\n" + . << + + + + + +HTM, + 'http://example.org/' + ); + + $extractor = new phinde\HubUrlExtractor(); + $extractor->setRequestTemplate( + new HTTP_Request2(null, null, ['adapter' => $mock]) + ); + + $this->assertEquals( + [ + 'hub' => 'https://hub.example.com/', + 'self' => 'http://example.com/feed', + ], + $extractor->getUrls('http://example.org/') + ); + } + + public function testGetUrlsRss2() + { + $mock = new HTTP_Request2_Adapter_Mock(); + //HEAD + $this->addResponse( + $mock, + "HTTP/1.0 200 OK\r\n" + . "Content-type: application/rss+xml\r\n" + . "\r\n", + 'http://example.org/' + ); + //HEAD + $this->addResponse( + $mock, + "HTTP/1.0 200 OK\r\n" + . "Content-type: application/rss+xml\r\n" + . "\r\n" + . << + + + http://www.example.com/main.html + + + + +HTM, + 'http://example.org/' + ); + + $extractor = new phinde\HubUrlExtractor(); + $extractor->setRequestTemplate( + new HTTP_Request2(null, null, ['adapter' => $mock]) + ); + + $this->assertEquals( + [ + 'hub' => 'https://hub.example.com/', + 'self' => 'http://example.com/feed', + ], + $extractor->getUrls('http://example.org/') + ); + } + + protected function addResponse($mock, $responseContent, $effectiveUrl) + { + $mock->addResponse( + static::createResponseFromString($responseContent, $effectiveUrl) + ); + } + + public static function createResponseFromString($str, $effectiveUrl) + { + $parts = preg_split('!(\r?\n){2}!m', $str, 2); + $headerLines = explode("\n", $parts[0]); + $response = new HTTP_Request2_Response( + array_shift($headerLines), true, $effectiveUrl + ); + foreach ($headerLines as $headerLine) { + $response->parseHeaderLine($headerLine); + } + $response->parseHeaderLine(''); + if (isset($parts[1])) { + $response->appendBody($parts[1]); + } + return $response; + } +} +?> -- cgit v1.2.3