Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const url = require('url');
const path = require('path');
const Promise = require('bluebird');
const normalizeUrl = require('normalize-url');
const normalize = require('normalize-url');
const htmlEntities = require('he');
const _ = require('lodash');
const typeByMime = require('../config/resource-type-by-mime');
Expand Down Expand Up @@ -66,7 +66,7 @@ function getFilenameFromUrl (u) {
* @returns {string} path
*/
function getFilepathFromUrl (u) {
var nu = normalizeUrl(u);
const nu = normalizeUrl(u, {removeTrailingSlash: true});
return getPathnameFromUrl(nu).substring(1);
}

Expand Down Expand Up @@ -109,6 +109,10 @@ function waitAllFulfilled (promises) {
}));
}

function normalizeUrl (u, opts) {
return normalize(u, extend({removeTrailingSlash: false}, opts));
}

function urlsEqual (url1, url2) {
return normalizeUrl(url1) === normalizeUrl(url2);
}
Expand Down
12 changes: 12 additions & 0 deletions test/functional/redirect/mocks/relative-resources-about.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>About</title>
<link rel="stylesheet" type="text/css" href="/style.css"/> <!-- /style.css -->
<link rel="stylesheet" type="text/css" href="style.css"/> <!-- /about/style.css -->
</head>
<body>

</body>
</html>
11 changes: 11 additions & 0 deletions test/functional/redirect/mocks/relative-resources-index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Index</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<a href="/about">About</a>
</body>
</html>
58 changes: 52 additions & 6 deletions test/functional/redirect/redirect.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
'use strict';

require('should');
var nock = require('nock');
var fs = require('fs-extra');
var sinon = require('sinon');
var Scraper = require('../../../lib/scraper');
const nock = require('nock');
const fs = require('fs-extra');
const sinon = require('sinon');
const Scraper = require('../../../lib/scraper');
const scrape = require('../../../index');

var testDirname = __dirname + '/.tmp';
var mockDirname = __dirname + '/mocks';
const testDirname = __dirname + '/.tmp';
const mockDirname = __dirname + '/mocks';

describe('Functional redirects', function() {

Expand Down Expand Up @@ -60,4 +63,47 @@ describe('Functional redirects', function() {
fs.readFileSync(testDirname + '/true-page.html').toString().should.be.eql('true page 1');
});
});

it('should correctly handle relative source in redirected page', () => {
const options = {
urls: [
{ url: 'http://example.com', filename: 'index.html'}
],
directory: testDirname,
subdirectories: [
{ directory: 'css', extensions: ['.css'] }
],
maxRecursiveDepth: 1,
sources: [
{selector: 'link', attr: 'href'},
{selector: 'a', attr: 'href'}
]
};

nock('http://example.com/').get('/').replyWithFile(200, mockDirname + '/relative-resources-index.html');
nock('http://example.com/').get('/about').reply(301, '', {'Location': 'http://example.com/about/'});
nock('http://example.com/').get('/about/').replyWithFile(200, mockDirname + '/relative-resources-about.html', {'content-type': 'text/html'});
nock('http://example.com/').get('/style.css').reply(200, 'style.css');
nock('http://example.com/').get('/about/style.css').reply(200, 'about/style.css');

return scrape(options).then(function() {
fs.existsSync(testDirname + '/index.html').should.be.eql(true);
fs.existsSync(testDirname + '/about.html').should.be.eql(true);
fs.existsSync(testDirname + '/css/style.css').should.be.eql(true);
fs.existsSync(testDirname + '/css/style_1.css').should.be.eql(true);

const style = fs.readFileSync(testDirname + '/css/style.css').toString();
style.should.be.eql('style.css');

const style_1 = fs.readFileSync(testDirname + '/css/style_1.css').toString();
style_1.should.be.eql('about/style.css');

const index = fs.readFileSync(testDirname + '/index.html').toString();
index.should.containEql('<link rel="stylesheet" type="text/css" href="css/style.css">');

const about = fs.readFileSync(testDirname + '/about.html').toString();
about.should.containEql('<link rel="stylesheet" type="text/css" href="css/style.css">');
about.should.containEql('<link rel="stylesheet" type="text/css" href="css/style_1.css">');
});
});
});
6 changes: 6 additions & 0 deletions test/unit/utils/utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,10 @@ describe('Utils', function () {
should(utils.decodeHtmlEntities('?a=1&amp;v=2')).be.eql('?a=1&v=2');
});
});

describe('#urlsEqual', () => {
it('should return false for /path and /path/', function() {
should(utils.urlsEqual('http://example.com/path', 'http://example.com/path/')).be.eql(false);
});
})
});