Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
David Racovan committed Jul 29, 2024
1 parent 0210d60 commit 8f0e644
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
19 changes: 19 additions & 0 deletions tests/CrawlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Spatie\Crawler\Test\TestClasses\CrawlLogger;
use Spatie\Crawler\Test\TestClasses\Log;
use stdClass;
use Symfony\Component\Process\Exception\ProcessFailedException;

beforeEach(function () {
skipIfTestServerIsNotRunning();
Expand Down Expand Up @@ -117,6 +118,24 @@
expect(javascriptInjectedUrls())->each->notToBeCrawled();
});

it('fails gracefully when browsershot fails', function () {
expect(function () {
$browsershot = (new Browsershot)->waitUntilNetworkIdle();

Crawler::create([
RequestOptions::CONNECT_TIMEOUT => 60,
RequestOptions::TIMEOUT => 60,
RequestOptions::READ_TIMEOUT => 60,
])
->setBrowsershot($browsershot)
->executeJavaScript()
->setCrawlObserver(new CrawlLogger())
->startCrawling('http://localhost:8080/simulate-activity');
})->not->toThrow(ProcessFailedException::class);

expect(['url' => 'http://localhost:8080/simulate-activity'])->toBeCrawledOnce();
})->only();

it('uses a crawl profile to determine what should be crawled', function () {
$crawlProfile = new class() extends CrawlProfile
{
Expand Down
32 changes: 32 additions & 0 deletions tests/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,38 @@ app.get('/sitemap2.xml', function (req, res) {
res.end(sitemap2);
});

// Route that initiates but never completes the response
app.get('/never-complete', (req, res) => {
req.socket.setTimeout(0); // Disable automatic socket timeout
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('Starting but never completing...\n');
// Intentionally do not call res.end() or send more data, leaving the response hanging
});

app.get('/simulate-activity', (req, res) => {
res.send(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simulated Network Activity</title>
</head>
<body>
<h1>This page simulates a never-ending network request</h1>
<script>
function keepBusy() {
fetch('/never-complete')
}
keepBusy();
setInterval(keepBusy, 1000);
</script>
</body>
</html>
`);
});


let server = app.listen(8080, function () {
const host = 'localhost';
Expand Down

0 comments on commit 8f0e644

Please sign in to comment.