|
| 1 | +'use strict'; |
| 2 | + |
1 | 3 | require('should'); |
2 | | -var nock = require('nock'); |
3 | | -var fs = require('fs-extra'); |
4 | | -var sinon = require('sinon'); |
5 | | -var Scraper = require('../../../lib/scraper'); |
| 4 | +const nock = require('nock'); |
| 5 | +const fs = require('fs-extra'); |
| 6 | +const sinon = require('sinon'); |
| 7 | +const Scraper = require('../../../lib/scraper'); |
| 8 | +const scrape = require('../../../index'); |
6 | 9 |
|
7 | | -var testDirname = __dirname + '/.tmp'; |
8 | | -var mockDirname = __dirname + '/mocks'; |
| 10 | +const testDirname = __dirname + '/.tmp'; |
| 11 | +const mockDirname = __dirname + '/mocks'; |
9 | 12 |
|
10 | 13 | describe('Functional redirects', function() { |
11 | 14 |
|
@@ -60,4 +63,47 @@ describe('Functional redirects', function() { |
60 | 63 | fs.readFileSync(testDirname + '/true-page.html').toString().should.be.eql('true page 1'); |
61 | 64 | }); |
62 | 65 | }); |
| 66 | + |
| 67 | + it('should correctly handle relative source in redirected page', () => { |
| 68 | + const options = { |
| 69 | + urls: [ |
| 70 | + { url: 'http://example.com', filename: 'index.html'} |
| 71 | + ], |
| 72 | + directory: testDirname, |
| 73 | + subdirectories: [ |
| 74 | + { directory: 'css', extensions: ['.css'] } |
| 75 | + ], |
| 76 | + maxRecursiveDepth: 1, |
| 77 | + sources: [ |
| 78 | + {selector: 'link', attr: 'href'}, |
| 79 | + {selector: 'a', attr: 'href'} |
| 80 | + ] |
| 81 | + }; |
| 82 | + |
| 83 | + nock('http://example.com/').get('/').replyWithFile(200, mockDirname + '/relative-resources-index.html'); |
| 84 | + nock('http://example.com/').get('/about').reply(301, '', {'Location': 'http://example.com/about/'}); |
| 85 | + nock('http://example.com/').get('/about/').replyWithFile(200, mockDirname + '/relative-resources-about.html', {'content-type': 'text/html'}); |
| 86 | + nock('http://example.com/').get('/style.css').reply(200, 'style.css'); |
| 87 | + nock('http://example.com/').get('/about/style.css').reply(200, 'about/style.css'); |
| 88 | + |
| 89 | + return scrape(options).then(function() { |
| 90 | + fs.existsSync(testDirname + '/index.html').should.be.eql(true); |
| 91 | + fs.existsSync(testDirname + '/about.html').should.be.eql(true); |
| 92 | + fs.existsSync(testDirname + '/css/style.css').should.be.eql(true); |
| 93 | + fs.existsSync(testDirname + '/css/style_1.css').should.be.eql(true); |
| 94 | + |
| 95 | + const style = fs.readFileSync(testDirname + '/css/style.css').toString(); |
| 96 | + style.should.be.eql('style.css'); |
| 97 | + |
| 98 | + const style_1 = fs.readFileSync(testDirname + '/css/style_1.css').toString(); |
| 99 | + style_1.should.be.eql('about/style.css'); |
| 100 | + |
| 101 | + const index = fs.readFileSync(testDirname + '/index.html').toString(); |
| 102 | + index.should.containEql('<link rel="stylesheet" type="text/css" href="css/style.css">'); |
| 103 | + |
| 104 | + const about = fs.readFileSync(testDirname + '/about.html').toString(); |
| 105 | + about.should.containEql('<link rel="stylesheet" type="text/css" href="css/style.css">'); |
| 106 | + about.should.containEql('<link rel="stylesheet" type="text/css" href="css/style_1.css">'); |
| 107 | + }); |
| 108 | + }); |
63 | 109 | }); |
0 commit comments