|
| 1 | +require('should'); |
| 2 | +const nock = require('nock'); |
| 3 | +const fs = require('fs-extra'); |
| 4 | +const scrape = require('../../../index'); |
| 5 | + |
| 6 | +const testDirname = __dirname + '/.tmp'; |
| 7 | +const mockDirname = __dirname + '/mocks'; |
| 8 | + |
| 9 | +describe('Functional: maxDepth and maxRecursiveDepth ', () => { |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + nock.cleanAll(); |
| 13 | + nock.disableNetConnect(); |
| 14 | + }); |
| 15 | + |
| 16 | + afterEach(() => { |
| 17 | + nock.cleanAll(); |
| 18 | + nock.enableNetConnect(); |
| 19 | + fs.removeSync(testDirname); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should filter out all resources by depth > maxDepth', () => { |
| 23 | + const options = { |
| 24 | + urls: [ 'http://example.com/' ], |
| 25 | + directory: testDirname, |
| 26 | + subdirectories: null, |
| 27 | + sources: [ |
| 28 | + { selector: 'img', attr: 'src' }, |
| 29 | + { selector: 'script', attr: 'src' }, |
| 30 | + { selector: 'a', attr: 'href' } |
| 31 | + ], |
| 32 | + maxDepth: 2 |
| 33 | + }; |
| 34 | + |
| 35 | + nock('http://example.com/').get('/').replyWithFile(200, mockDirname + '/index.html'); |
| 36 | + |
| 37 | + nock('http://example.com/').get('/depth1.html').replyWithFile(200, mockDirname + '/depth1.html'); |
| 38 | + nock('http://example.com/').get('/img-depth1.jpg').reply(200, 'img-depth1.jpg'); |
| 39 | + nock('http://example.com/').get('/script-depth1.js').reply(200, 'script-depth1.js'); |
| 40 | + |
| 41 | + nock('http://example.com/').get('/depth2.html').replyWithFile(200, mockDirname + '/depth2.html'); |
| 42 | + nock('http://example.com/').get('/img-depth2.jpg').reply(200, 'img-depth2.jpg'); |
| 43 | + nock('http://example.com/').get('/script-depth2.js').reply(200, 'script-depth2.js'); |
| 44 | + |
| 45 | + nock('http://example.com/').get('/depth3.html').reply(200, 'OK'); |
| 46 | + nock('http://example.com/').get('/img-depth3.jpg').reply(200, 'img-depth3.jpg'); |
| 47 | + nock('http://example.com/').get('/script-depth3.js').reply(200, 'script-depth3.js'); |
| 48 | + |
| 49 | + return scrape(options).then(() => { |
| 50 | + fs.existsSync(testDirname + '/index.html').should.be.eql(true); |
| 51 | + |
| 52 | + fs.existsSync(testDirname + '/depth1.html').should.be.eql(true); |
| 53 | + fs.existsSync(testDirname + '/img-depth1.jpg').should.be.eql(true); |
| 54 | + fs.existsSync(testDirname + '/script-depth1.js').should.be.eql(true); |
| 55 | + |
| 56 | + fs.existsSync(testDirname + '/depth2.html').should.be.eql(true); |
| 57 | + fs.existsSync(testDirname + '/img-depth2.jpg').should.be.eql(true); |
| 58 | + fs.existsSync(testDirname + '/script-depth2.js').should.be.eql(true); |
| 59 | + |
| 60 | + fs.existsSync(testDirname + '/depth3.html').should.be.eql(false); |
| 61 | + fs.existsSync(testDirname + '/img-depth3.jpg').should.be.eql(false); |
| 62 | + fs.existsSync(testDirname + '/script-depth3.js').should.be.eql(false); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + |
| 67 | + it('should filter out only anchors by depth > maxRecursiveDepth', () => { |
| 68 | + const options = { |
| 69 | + urls: [ 'http://example.com/' ], |
| 70 | + directory: testDirname, |
| 71 | + subdirectories: null, |
| 72 | + sources: [ |
| 73 | + { selector: 'img', attr: 'src' }, |
| 74 | + { selector: 'script', attr: 'src' }, |
| 75 | + { selector: 'a', attr: 'href' } |
| 76 | + ], |
| 77 | + maxRecursiveDepth: 2 |
| 78 | + }; |
| 79 | + |
| 80 | + nock('http://example.com/').get('/').replyWithFile(200, mockDirname + '/index.html'); |
| 81 | + |
| 82 | + nock('http://example.com/').get('/depth1.html').replyWithFile(200, mockDirname + '/depth1.html'); |
| 83 | + nock('http://example.com/').get('/img-depth1.jpg').reply(200, 'img-depth1.jpg'); |
| 84 | + nock('http://example.com/').get('/script-depth1.js').reply(200, 'script-depth1.js'); |
| 85 | + |
| 86 | + nock('http://example.com/').get('/depth2.html').replyWithFile(200, mockDirname + '/depth2.html'); |
| 87 | + nock('http://example.com/').get('/img-depth2.jpg').reply(200, 'img-depth2.jpg'); |
| 88 | + nock('http://example.com/').get('/script-depth2.js').reply(200, 'script-depth2.js'); |
| 89 | + |
| 90 | + nock('http://example.com/').get('/depth3.html').reply(200, 'OK'); |
| 91 | + nock('http://example.com/').get('/img-depth3.jpg').reply(200, 'img-depth3.jpg'); |
| 92 | + nock('http://example.com/').get('/script-depth3.js').reply(200, 'script-depth3.js'); |
| 93 | + |
| 94 | + return scrape(options).then(() => { |
| 95 | + fs.existsSync(testDirname + '/index.html').should.be.eql(true); |
| 96 | + |
| 97 | + fs.existsSync(testDirname + '/depth1.html').should.be.eql(true); |
| 98 | + fs.existsSync(testDirname + '/img-depth1.jpg').should.be.eql(true); |
| 99 | + fs.existsSync(testDirname + '/script-depth1.js').should.be.eql(true); |
| 100 | + |
| 101 | + fs.existsSync(testDirname + '/depth2.html').should.be.eql(true); |
| 102 | + fs.existsSync(testDirname + '/img-depth2.jpg').should.be.eql(true); |
| 103 | + fs.existsSync(testDirname + '/script-depth2.js').should.be.eql(true); |
| 104 | + |
| 105 | + fs.existsSync(testDirname + '/depth3.html').should.be.eql(false); |
| 106 | + // img-depth3.jpg and script-depth3.js - dependencies of depth2.html |
| 107 | + // they should be loaded because maxRecursiveDepth applies only to <a href=''> |
| 108 | + fs.existsSync(testDirname + '/img-depth3.jpg').should.be.eql(true); |
| 109 | + fs.existsSync(testDirname + '/script-depth3.js').should.be.eql(true); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | +}); |
0 commit comments