Skip to content

Commit

Permalink
fixup! Add ability to process prefetched content
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Decherf <kevin@kdecherf.com>
  • Loading branch information
Kdecherf committed Jan 15, 2022
1 parent 0208102 commit 7a9b7a2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Graby.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public function cleanupHtml($contentBlock, string $url): string
return trim($this->cleanupXss((string) $html));
}

private function fakeFetch($url, $prefetchedContent) {
private function getResponseForPrefetchedContent($url, $prefetchedContent) {
return [
'body' => $prefetchedContent,
'effective_url' => $url,
Expand All @@ -291,8 +291,8 @@ private function doFetchContent(string $url, string $prefetchedContent = null):
$this->logger->info('Fetching url: {url}', ['url' => $url]);
$response = $this->httpClient->fetch($url, false, $siteConfig->http_header);
} else {
$this->logger->info('Fake fetching url: {url}', ['url' => $url]);
$response = $this->fakeFetch($url, $prefetchedContent);
$this->logger->info('Content provided, does not fetch url: {url}', ['url' => $url]);
$response = $this->getResponseForPrefetchedContent($url, $prefetchedContent);
}

$effectiveUrl = $response['effective_url'];
Expand Down
16 changes: 16 additions & 0 deletions tests/GrabyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,22 @@ public function testWithTooLongHtmlJitFail(): void
$this->assertNotSame('No title found', $res['title']);
}

public function testPrefetchedContent()
{
$httpMockClient = new HttpMockClient();
$graby = new Graby([
'debug' => true
], $httpMockClient);

$input = '<html><body><h1>This is my awesome article</h1><article><p>' . str_repeat('This is an awesome text with some links, here there are the awesome', 7) . '</p></article></body></html>';

$res = $graby->fetchContent('https://example.com/prefetched-content', $input);

$this->assertSame('This is my awesome article', $res['title']);
$this->assertSame('https://example.com/prefetched-content', $res['url']);
$this->assertStringContainsString('here there are the awesome', $res['html']);
}

/**
* Return an instance of graby with a mocked Guzzle client returning data from a predefined file.
*/
Expand Down

0 comments on commit 7a9b7a2

Please sign in to comment.