More work on parsing endpoints in the body
This commit is contained in:
		
							parent
							
								
									4eba80387d
								
							
						
					
					
						commit
						e5f7595321
					
				@ -3,6 +3,7 @@
 | 
				
			|||||||
namespace Lewisdale\Webmentions;
 | 
					namespace Lewisdale\Webmentions;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use Symfony\Contracts\HttpClient\ResponseInterface;
 | 
					use Symfony\Contracts\HttpClient\ResponseInterface;
 | 
				
			||||||
 | 
					use Symfony\Component\DomCrawler\Crawler;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class EndpointParser {
 | 
					class EndpointParser {
 | 
				
			||||||
    public static function parse(ResponseInterface $response) : string | null
 | 
					    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://")) {
 | 
					        if ($endpoint && !str_contains($endpoint, "https://")) {
 | 
				
			||||||
            $res = parse_url($response->getInfo('url'));
 | 
					            $res = parse_url($response->getInfo('url'));
 | 
				
			||||||
            $endpoint = $res["scheme"] . "://" . $res["host"] . $endpoint;
 | 
					            $endpoint = $res["scheme"] . "://" . $res["host"] . $endpoint;
 | 
				
			||||||
 | 
				
			|||||||
@ -35,4 +35,39 @@ class EndpointParserTest extends TestCase {
 | 
				
			|||||||
        $this->assertSame("https://webmention.rocks/test/5/webmention", EndpointParser::parse($response));   
 | 
					        $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));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user