More work on parsing endpoints in the body

This commit is contained in:
Lewis Dale 2023-03-08 16:07:43 +00:00
parent 4eba80387d
commit e5f7595321
2 changed files with 41 additions and 0 deletions

View File

@ -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;

View File

@ -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" => ['<https://webmention.rocks/test/2/webmention>; 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 = <<<XML
<html>
<head>
<link rel="webmention" href="/test/4/webmention" />
</head>
<body>
<h1>Some content</h1>
</html>
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));
}
}