Skip to content

Commit 40ecefa

Browse files
authored
Fix runAction result if no actions (#331)
1 parent ef56f61 commit 40ecefa

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

lib/scraper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ class Scraper {
219219
async runActions (actionName, params) {
220220
logger.debug(`run ${this.actions[actionName].length} actions ${actionName}`);
221221

222-
let result = {};
222+
let result = extend(params);
223223
for (let action of this.actions[actionName]) {
224224
if (typeof action === 'function') {
225225
result = await action(extend(params, result));

test/functional/request-function/request-function.test.js renamed to test/functional/request-customization/request.test.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,25 @@ describe('Functional: customize request options with plugin', function() {
1919
fs.removeSync(testDirname);
2020
});
2121

22-
it('should use function which returns request option for resource ', function() {
22+
it('should use options from request property if no beforeRequest actions', function() {
23+
nock('http://example.com/').get('/').query({myParam: 122}).reply(200, 'response for url with query');
24+
25+
const options = {
26+
urls: [ 'http://example.com/' ],
27+
directory: testDirname,
28+
request: {
29+
qs: {myParam: 122}
30+
}
31+
};
32+
33+
return scrape(options).then(function() {
34+
fs.existsSync(testDirname + '/index.html').should.be.eql(true);
35+
const indexHtml = fs.readFileSync(testDirname + '/index.html').toString();
36+
should(indexHtml).containEql('response for url with query');
37+
});
38+
});
39+
40+
it('should use options returned by beforeRequest action', function() {
2341
nock('http://example.com/').get('/').query({myParam: 122}).reply(200, 'response for url with query');
2442

2543
class CustomRequestOptions {

test/unit/scraper-test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,17 @@ describe('Scraper', function () {
405405
should(err.message).eql('Error from beforeStart 2');
406406
}
407407
});
408+
409+
it('should return passed params as result if no actions to run', async () => {
410+
const s = new Scraper({
411+
urls: ['http://example.com'],
412+
directory: testDirname
413+
});
414+
415+
const result = await s.runActions('beforeRequest', {requestOptions: {a: 1}});
416+
417+
should(result).eql({requestOptions: {a: 1}});
418+
});
408419
});
409420

410421
describe('export defaults', function() {

0 commit comments

Comments
 (0)