Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

Fix request mock

Ondra Urband469f1ed7352
diff --git a/src/cheerio_crawler.js b/src/cheerio_crawler.js
index f50bbbfed..231b1bd52 100644
--- a/src/cheerio_crawler.js
+++ b/src/cheerio_crawler.js
@@ -244,10 +244,6 @@ class CheerioCrawler {
maxConcurrency,
autoscaledPoolOptions,
});
-
- // Set Request as an internal property to enable mocking in tests,
- // since Sinon can't mock it due to it being a Function.
- this.rqst = rqst;
}
/**
@@ -322,7 +318,9 @@ class CheerioCrawler {
return new Promise((resolve, reject) => {
// Using the streaming API of Request to be able to
// handle the response based on headers receieved.
- this.rqst(this._getRequestOptions(request))
+ const opts = this._getRequestOptions(request);
+ const method = opts.method.toLowerCase();
+ rqst[method](opts)
.on('error', err => reject(err))
.on('response', async (res) => {
// First check what kind of response we received.
@@ -371,7 +369,7 @@ class CheerioCrawler {
if (type !== 'text/html') {
request.doNotRetry();
res.destroy();
- reject(new Error(
+ return reject(new Error(
`CheerioCrawler: Resource ${request.url} served Content-Type ${type} instead of text/html. Skipping resource.`,
));
}
diff --git a/test/cheerio_crawler.js b/test/cheerio_crawler.js
index 90b34f44d..53c1f6533 100644
--- a/test/cheerio_crawler.js
+++ b/test/cheerio_crawler.js
@@ -1,5 +1,6 @@
import { Readable } from 'stream';
import EventEmitter from 'events';
+import rqst from 'request';
import chai, { expect } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import log from 'apify-shared/log';
@@ -300,6 +301,7 @@ describe('CheerioCrawler', () => {
describe('by throwing', () => {
let crawler;
+ let originalGet;
let handlePageInvocationCount = 0;
let errorMessages = [];
let chunkReadCount = 0;
@@ -307,6 +309,15 @@ describe('CheerioCrawler', () => {
chunkReadCount++;
return chunk;
};
+
+ before(() => {
+ originalGet = rqst.get;
+ });
+
+ after(() => {
+ rqst.get = originalGet;
+ });
+
beforeEach(() => {
log.setLevel(log.LEVELS.OFF);
crawler = new Apify.CheerioCrawler({
@@ -331,7 +342,7 @@ describe('CheerioCrawler', () => {
it('when invalid Content-Type header is received', async () => {
// Mock Request to inject invalid response headers.
- crawler.rqst = () => {
+ rqst.get = () => {
const response = new Readable({
read() {
this.push(getChunk());
@@ -361,7 +372,7 @@ describe('CheerioCrawler', () => {
it('when response stream emits an error event', async () => {
// Mock Request to emit an error after a while.
- crawler.rqst = () => {
+ rqst.get = () => {
const start = Date.now();
const response = new Readable({
read() {
@@ -394,7 +405,7 @@ describe('CheerioCrawler', () => {
it('when request stream emits an error event', async () => {
// Mock Request to emit an error after a while.
- crawler.rqst = () => {
+ rqst.get = () => {
const response = new Readable({
// Just do nothing
read() {},
@@ -423,7 +434,7 @@ describe('CheerioCrawler', () => {
});
it('when statusCode >= 500 and text/html is received', async () => {
- crawler.rqst = () => {
+ rqst.get = () => {
const response = new Readable({
read() {
this.push(getChunk());
@@ -453,7 +464,7 @@ describe('CheerioCrawler', () => {
});
it('when statusCode >= 500 and application/json is received', async () => {
- crawler.rqst = () => {
+ rqst.get = () => {
const response = new Readable({
// Just do nothing
read() {
@@ -485,7 +496,7 @@ describe('CheerioCrawler', () => {
it('when 406 is received', async () => {
// Mock Request to respond with a 406.
- crawler.rqst = () => {
+ rqst.get = () => {
const response = new Readable({
read() {
this.push(getChunk());
@@ -516,7 +527,7 @@ describe('CheerioCrawler', () => {
it('when status is ok, but a wrong content type is received', async () => {
// Mock Request to respond with a 406.
- crawler.rqst = () => {
+ rqst.get = () => {
const response = new Readable({
read() {
this.push(getChunk());