|
1 | | -var _ = require('lodash'); |
2 | | -var should = require('should'); |
| 1 | +const _ = require('lodash'); |
| 2 | +const should = require('should'); |
3 | 3 | require('../../utils/assertions'); |
4 | | -var sinon = require('sinon'); |
5 | | -var Resource = require('../../../lib/resource'); |
6 | | -var bySiteStructureFilenameGenerator = require('../../../lib/filename-generator/by-site-structure'); |
| 4 | +const sinon = require('sinon'); |
| 5 | +const Resource = require('../../../lib/resource'); |
| 6 | +const bySiteStructureFilenameGenerator = require('../../../lib/filename-generator/by-site-structure'); |
7 | 7 |
|
8 | | -var options = { defaultFilename: 'index.html' }; |
| 8 | +const options = { defaultFilename: 'index.html' }; |
9 | 9 |
|
10 | | -describe('FilenameGenerator: bySiteStructure', function() { |
11 | | - it('should return the normalized relative path of the resource url', function(){ |
12 | | - var r1 = new Resource('http://example.com/some/path/a.png'); |
| 10 | +describe('FilenameGenerator: bySiteStructure', () => { |
| 11 | + it('should return the normalized relative path of the resource url', () =>{ |
| 12 | + const r1 = new Resource('http://example.com/some/path/a.png'); |
13 | 13 | bySiteStructureFilenameGenerator(r1, options).should.equalFileSystemPath('example.com/some/path/a.png'); |
14 | 14 |
|
15 | | - var r2 = new Resource('http://example.com/a.png'); |
| 15 | + const r2 = new Resource('http://example.com/a.png'); |
16 | 16 | bySiteStructureFilenameGenerator(r2, options).should.equalFileSystemPath('example.com/a.png'); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should remove . and .. from path', () => { |
| 20 | + const r1 = new Resource('http://example.com/some/path/../images/a.png'); |
| 21 | + bySiteStructureFilenameGenerator(r1, options).should.equalFileSystemPath('example.com/some/images/a.png'); |
| 22 | + |
| 23 | + const r2 = new Resource('http://example.com/some/path/../../../images/b.png'); |
| 24 | + bySiteStructureFilenameGenerator(r2, options).should.equalFileSystemPath('example.com/images/b.png'); |
17 | 25 |
|
18 | | - var r3 = new Resource('http://example.com/some/path/../images/a.png'); |
19 | | - bySiteStructureFilenameGenerator(r3, options).should.equalFileSystemPath('example.com/some/images/a.png'); |
| 26 | + const r3 = new Resource('http://example.com/some/path/./images/c.png'); |
| 27 | + bySiteStructureFilenameGenerator(r3, options).should.equalFileSystemPath('example.com/some/path/images/c.png'); |
20 | 28 | }); |
21 | 29 |
|
22 | | - it('should replace the colon, for url with port number', function(){ |
23 | | - var r1 = new Resource('http://example.com:8080/some/path/a.png'); |
| 30 | + it('should replace the colon, for url with port number', () =>{ |
| 31 | + const r1 = new Resource('http://example.com:8080/some/path/a.png'); |
24 | 32 | bySiteStructureFilenameGenerator(r1, options).should.equalFileSystemPath('example.com_8080/some/path/a.png'); |
25 | 33 | }); |
26 | 34 |
|
27 | | - it('should add the defaultFilename to the path, for html resources without extension', function(){ |
28 | | - var isHtmlMock = sinon.stub().returns(true); |
| 35 | + it('should replace not allowed characters from filename', () => { |
| 36 | + const r1 = new Resource('http://example.com/some/path/<*a*>.png'); |
| 37 | + bySiteStructureFilenameGenerator(r1, options).should.equalFileSystemPath('example.com/some/path/__a__.png'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should replace not allowed characters from path', () => { |
| 41 | + const r1 = new Resource('http://example.com/some:path/a.png'); |
| 42 | + bySiteStructureFilenameGenerator(r1, options).should.equalFileSystemPath('example.com/some_path/a.png'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should add the defaultFilename to the path, for html resources without extension', () =>{ |
| 46 | + const isHtmlMock = sinon.stub().returns(true); |
29 | 47 |
|
30 | | - var r1 = new Resource('http://example.com/some/path/'); |
| 48 | + const r1 = new Resource('http://example.com/some/path/'); |
31 | 49 | r1.isHtml = isHtmlMock; |
32 | 50 | bySiteStructureFilenameGenerator(r1, options).should.equalFileSystemPath('example.com/some/path/index.html'); |
33 | 51 |
|
34 | | - var r2 = new Resource('http://example.com/some/path'); |
| 52 | + const r2 = new Resource('http://example.com/some/path'); |
35 | 53 | r2.isHtml = isHtmlMock; |
36 | 54 | bySiteStructureFilenameGenerator(r2, options).should.equalFileSystemPath('example.com/some/path/index.html'); |
37 | 55 |
|
38 | | - var r3 = new Resource('http://example.com'); |
| 56 | + const r3 = new Resource('http://example.com'); |
39 | 57 | r3.isHtml = isHtmlMock; |
40 | 58 | bySiteStructureFilenameGenerator(r3, options).should.equalFileSystemPath('example.com/index.html'); |
41 | 59 | }); |
42 | 60 |
|
43 | | - it('should add the defaultFilename to the path, for html resources with wrong extension', function(){ |
44 | | - var isHtmlMock = sinon.stub().returns(true); |
| 61 | + it('should add the defaultFilename to the path, for html resources with wrong extension', () =>{ |
| 62 | + const isHtmlMock = sinon.stub().returns(true); |
45 | 63 |
|
46 | | - var r1 = new Resource('http://example.com/some/path/test.com'); |
| 64 | + const r1 = new Resource('http://example.com/some/path/test.com'); |
47 | 65 | r1.isHtml = isHtmlMock; |
48 | 66 | bySiteStructureFilenameGenerator(r1, options).should.equalFileSystemPath('example.com/some/path/test.com/index.html'); |
49 | 67 | }); |
50 | 68 |
|
51 | | - it('should normalize to safe relative paths, without ..', function(){ |
52 | | - var r = new Resource('http://example.com/some/path/../../../../images/a.png'); |
| 69 | + it('should normalize to safe relative paths, without ..', () =>{ |
| 70 | + const r = new Resource('http://example.com/some/path/../../../../images/a.png'); |
53 | 71 | bySiteStructureFilenameGenerator(r, options).should.equalFileSystemPath('example.com/images/a.png'); |
54 | 72 | }); |
55 | 73 |
|
56 | | - it('should not replace thrice dot in filenames', function() { |
57 | | - // if it replaces them we receive 'some/path/../../../../etc/passwd' |
58 | | - // path.resolve('some/path/../../../../etc/passwd'); = '/etc/passwd' => which is not safe |
59 | | - var r = new Resource('http://example.com/some/path/.../.../.../.../etc/passwd'); |
60 | | - bySiteStructureFilenameGenerator(r, options).should.equalFileSystemPath('example.com/some/path/.../.../.../.../etc/passwd'); |
61 | | - }); |
62 | | - |
63 | | - it('should shorten filename', function() { |
64 | | - var resourceFilename = _.repeat('1', 1000) + '.png'; |
65 | | - var r = new Resource('http://example.com/' + resourceFilename); |
66 | | - var filename = bySiteStructureFilenameGenerator(r, options); |
| 74 | + it('should shorten filename', () => { |
| 75 | + const resourceFilename = _.repeat('1', 1000) + '.png'; |
| 76 | + const r = new Resource('http://example.com/' + resourceFilename); |
| 77 | + const filename = bySiteStructureFilenameGenerator(r, options); |
67 | 78 | should(filename.length).be.lessThan(255); |
68 | 79 | }); |
69 | 80 |
|
70 | | - it('should shorten filename if resource is html without ext and default name is too long', function() { |
71 | | - var defaultFilename = _.repeat('1', 1000) + '.html'; |
72 | | - var r = new Resource('http://example.com/path'); |
| 81 | + it('should shorten filename if resource is html without ext and default name is too long', () => { |
| 82 | + const defaultFilename = _.repeat('1', 1000) + '.html'; |
| 83 | + const r = new Resource('http://example.com/path'); |
73 | 84 | r.isHtml = sinon.stub().returns(true); |
74 | | - var filepath = bySiteStructureFilenameGenerator(r, { defaultFilename: defaultFilename }); |
75 | | - var filename = _.last(filepath.split('/')); |
| 85 | + const filepath = bySiteStructureFilenameGenerator(r, { defaultFilename: defaultFilename }); |
| 86 | + const filename = _.last(filepath.split('/')); |
76 | 87 | should(filename.length).be.lessThan(255); |
77 | 88 | }); |
78 | 89 |
|
79 | | - it('should return decoded filepath', function() { |
80 | | - var r = new Resource('https://developer.mozilla.org/ru/docs/JavaScript_%D1%88%D0%B5%D0%BB%D0%BB%D1%8B'); |
81 | | - var filename = bySiteStructureFilenameGenerator(r, options); |
| 90 | + it('should return decoded filepath', () => { |
| 91 | + const r = new Resource('https://developer.mozilla.org/ru/docs/JavaScript_%D1%88%D0%B5%D0%BB%D0%BB%D1%8B'); |
| 92 | + const filename = bySiteStructureFilenameGenerator(r, options); |
82 | 93 | filename.should.equalFileSystemPath('developer.mozilla.org/ru/docs/JavaScript_шеллы'); |
83 | 94 |
|
84 | | - var r2 = new Resource('https://developer.mozilla.org/Hello%20G%C3%BCnter.png'); |
85 | | - var filename2 = bySiteStructureFilenameGenerator(r2, options); |
| 95 | + const r2 = new Resource('https://developer.mozilla.org/Hello%20G%C3%BCnter.png'); |
| 96 | + const filename2 = bySiteStructureFilenameGenerator(r2, options); |
86 | 97 | filename2.should.equalFileSystemPath('developer.mozilla.org/Hello Günter.png'); |
87 | 98 | }); |
88 | 99 |
|
89 | | - it('should keep query strings', function () { |
90 | | - var isHtmlMock = sinon.stub().returns(true); |
| 100 | + it('should keep query strings', () => { |
| 101 | + const isHtmlMock = sinon.stub().returns(true); |
91 | 102 |
|
92 | | - var r1 = new Resource('http://example.com/path?q=test'); |
| 103 | + const r1 = new Resource('http://example.com/path?q=test'); |
93 | 104 | r1.isHtml = isHtmlMock; |
94 | 105 | bySiteStructureFilenameGenerator(r1, options).should.equalFileSystemPath('example.com/path/q=test.html'); |
95 | 106 |
|
96 | | - var r2 = new Resource('http://example.com/path?q1=test1&q2=test2'); |
| 107 | + const r2 = new Resource('http://example.com/path?q1=test1&q2=test2'); |
97 | 108 | r2.isHtml = isHtmlMock; |
98 | 109 | bySiteStructureFilenameGenerator(r2, options).should.equalFileSystemPath('example.com/path/q1=test1&q2=test2.html'); |
99 | 110 |
|
100 | | - var r3 = new Resource('http://example.com/path/picture.png?q1=test1&q2=test2'); |
| 111 | + const r3 = new Resource('http://example.com/path/picture.png?q1=test1&q2=test2'); |
101 | 112 | bySiteStructureFilenameGenerator(r3, options).should.equalFileSystemPath('example.com/path/picture_q1=test1&q2=test2.png'); |
102 | | - }) |
| 113 | + }); |
103 | 114 | }); |
0 commit comments