From e5f7595321918db81290e2946668b71ba9192e13 Mon Sep 17 00:00:00 2001 From: Lewis Dale Date: Wed, 8 Mar 2023 16:07:43 +0000 Subject: [PATCH] More work on parsing endpoints in the body --- src/EndpointParser.php | 6 ++++++ tests/EndpointParserTest.php | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/EndpointParser.php b/src/EndpointParser.php index a909a6c..8960548 100644 --- a/src/EndpointParser.php +++ b/src/EndpointParser.php @@ -3,6 +3,7 @@ namespace Lewisdale\Webmentions; use Symfony\Contracts\HttpClient\ResponseInterface; +use Symfony\Component\DomCrawler\Crawler; class EndpointParser { public static function parse(ResponseInterface $response) : string | null @@ -22,6 +23,11 @@ class EndpointParser { } } + if (!$endpoint) { + $doc = new Crawler($response->getContent()); + $webmention = $doc->filter('rel="webmention"')->first(); + } + if ($endpoint && !str_contains($endpoint, "https://")) { $res = parse_url($response->getInfo('url')); $endpoint = $res["scheme"] . "://" . $res["host"] . $endpoint; diff --git a/tests/EndpointParserTest.php b/tests/EndpointParserTest.php index a0b4b3e..75255d4 100644 --- a/tests/EndpointParserTest.php +++ b/tests/EndpointParserTest.php @@ -35,4 +35,39 @@ class EndpointParserTest extends TestCase { $this->assertSame("https://webmention.rocks/test/5/webmention", EndpointParser::parse($response)); } + public function testParsesAbsoluteEndpointFromHeader() { + $response = $this->createStub(ResponseInterface::class); + + $response->method('getHeaders') + ->willReturn([ + "link" => ['; rel="webmention"'], + ]); + + + $response->method('getInfo') + ->willReturn('https://webmention.rocks/test/2'); + + $this->assertSame("https://webmention.rocks/test/2/webmention", EndpointParser::parse($response)); + } + + public function testParsesRelativeEndpointFromLink() { + $content = << + + + + +

Some content

+ + XML; + + $response = $this->createStub(ResponseInterface::class); + $response->method('getHeaders')->willReturn([]); + $response->method('getContent')->willReturn($content); + $response->method('getInfo') + ->willReturn('https://webmention.rocks/test/4'); + + $this->assertSame("https://webmention.rocks/test/4/webmention", EndpointParser::parse($response)); + + } } \ No newline at end of file