From 8f0e644af95be9dcf66003a91a9ad08a6b559d5f Mon Sep 17 00:00:00 2001 From: David Racovan Date: Mon, 29 Jul 2024 17:14:03 -0400 Subject: [PATCH] Add tests --- tests/CrawlerTest.php | 19 +++++++++++++++++++ tests/server/server.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/tests/CrawlerTest.php b/tests/CrawlerTest.php index ccfd953..40c566d 100644 --- a/tests/CrawlerTest.php +++ b/tests/CrawlerTest.php @@ -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(); @@ -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 { diff --git a/tests/server/server.js b/tests/server/server.js index bfc8e3e..74489b8 100644 --- a/tests/server/server.js +++ b/tests/server/server.js @@ -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(` + + + + + + Simulated Network Activity + + +

This page simulates a never-ending network request

+ + + + `); +}); + let server = app.listen(8080, function () { const host = 'localhost';