robots-parser 2.4.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ node_modules
2
+ coverage
3
+ .nyc_output
4
+ .vscode
package/CHANGELOG.md ADDED
@@ -0,0 +1,75 @@
1
+ # Changelog
2
+
3
+ ## Version 3.0.0
4
+
5
+ - Changed to using global URL object instead of importing. – Thanks to @brendankenny
6
+
7
+ ## Version 2.4.0:
8
+
9
+ - Added Typescript definitions. – Thanks to @danhab99 for creating
10
+ - Added SECURITY.md policy and CodeQL scanning
11
+
12
+ ## Version 2.3.0:
13
+
14
+ - Fixed bug where if the user-agent passed to `isAllowed()` / `isDisallowed()` is called "constructor" it would throw an error.
15
+ - Added support for relative URLs. This does not affect the default behavior so can safely be upgraded.
16
+
17
+ Relative matching is only allowed if both the robots.txt URL and the URLs being checked are relative.
18
+
19
+ For example:
20
+
21
+ ```js
22
+ var robots = robotsParser('/robots.txt', [
23
+ 'User-agent: *',
24
+ 'Disallow: /dir/',
25
+ 'Disallow: /test.html',
26
+ 'Allow: /dir/test.html',
27
+ 'Allow: /test.html'
28
+ ].join('\n'));
29
+
30
+ robots.isAllowed('/test.html', 'Sams-Bot/1.0'); // false
31
+ robots.isAllowed('/dir/test.html', 'Sams-Bot/1.0'); // true
32
+ robots.isDisallowed('/dir/test2.html', 'Sams-Bot/1.0'); // true
33
+ ```
34
+
35
+ ## Version 2.2.0:
36
+
37
+ - Fixed bug that with matching wildcard patterns with some URLs
38
+ – Thanks to @ckylape for reporting and fixing
39
+ - Changed matching algorithm to match Google's implementation in google/robotstxt
40
+ - Changed order of precedence to match current spec
41
+
42
+ ## Version 2.1.1:
43
+
44
+ - Fix bug that could be used to causing rule checking to take a long time
45
+ – Thanks to @andeanfog
46
+
47
+ ## Version 2.1.0:
48
+
49
+ - Removed use of punycode module API's as new URL API handles it
50
+ - Improved test coverage
51
+ - Added tests for percent encoded paths and improved support
52
+ - Added `getMatchingLineNumber()` method
53
+ - Fixed bug with comments on same line as directive
54
+
55
+ ## Version 2.0.0:
56
+
57
+ This release is not 100% backwards compatible as it now uses the new URL APIs which are not supported in Node < 7.
58
+
59
+ - Update code to not use deprecated URL module API's.
60
+ &ndash; Thanks to @kdzwinel
61
+
62
+ ## Version 1.0.2:
63
+
64
+ - Fixed error caused by invalid URLs missing the protocol.
65
+
66
+ ## Version 1.0.1:
67
+
68
+ - Fixed bug with the "user-agent" rule being treated as case sensitive.
69
+ &ndash; Thanks to @brendonboshell
70
+ - Improved test coverage.
71
+ &ndash; Thanks to @schornio
72
+
73
+ ## Version 1.0.0:
74
+
75
+ - Initial release.
File without changes
@@ -1,8 +1,8 @@
1
1
  # Robots Parser [![NPM downloads](https://img.shields.io/npm/dm/robots-parser)](https://www.npmjs.com/package/robots-parser) [![DeepScan grade](https://deepscan.io/api/teams/457/projects/16277/branches/344939/badge/grade.svg)](https://deepscan.io/dashboard#view=project&tid=457&pid=16277&bid=344939) [![GitHub license](https://img.shields.io/github/license/samclarke/robots-parser.svg)](https://github.com/samclarke/robots-parser/blob/master/license.md) [![Coverage Status](https://coveralls.io/repos/github/samclarke/robots-parser/badge.svg?branch=master)](https://coveralls.io/github/samclarke/robots-parser?branch=master)
2
2
 
3
- NodeJS robots.txt parser.
3
+ A robots.txt parser which aims to be complaint with the [draft specification](https://datatracker.ietf.org/doc/html/draft-koster-rep).
4
4
 
5
- It currently supports:
5
+ The parser currently supports:
6
6
 
7
7
  - User-agent:
8
8
  - Allow:
@@ -94,6 +94,10 @@ Returns the preferred host name specified by the `host:` directive or null if th
94
94
 
95
95
  # Changes
96
96
 
97
+ ### Version 3.0.0
98
+
99
+ - Changed to using global URL object instead of importing. &ndash; Thanks to @brendankenny
100
+
97
101
  ### Version 2.4.0:
98
102
 
99
103
  - Added Typescript definitions
package/Robots.js CHANGED
@@ -1,5 +1,3 @@
1
- var URL = require('url').URL;
2
-
3
1
  /**
4
2
  * Trims the white space from the start and end of the line.
5
3
  *
package/SECURITY.md ADDED
@@ -0,0 +1,11 @@
1
+ # Security Policy
2
+
3
+ ## Supported Versions
4
+
5
+ The latest version is supported.
6
+
7
+ ## Reporting a Vulnerability
8
+
9
+ If you find or suspect a security vulnerability, please report it to sam@samclarke.com, and I will try to respond as quickly as possible.
10
+
11
+ If possible, please include "Security vulnerability report:" in the subject line.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "robots-parser",
3
- "version": "2.4.0",
4
- "description": "NodeJS robots.txt parser with support for wildcard (*) matching.",
3
+ "version": "3.0.0",
4
+ "description": "A specification compliant robots.txt parser with wildcard (*) matching support.",
5
5
  "keywords": [
6
6
  "robots.txt",
7
7
  "parser",
@@ -15,6 +15,10 @@
15
15
  "directories": {
16
16
  "test": "tests"
17
17
  },
18
+ "homepage": "https://github.com/samclarke/robots-parser",
19
+ "bugs": {
20
+ "url": "https://github.com/samclarke/robots-parser/issues"
21
+ },
18
22
  "scripts": {
19
23
  "test": "nyc --reporter=text-summary --reporter=html --reporter=lcovonly mocha"
20
24
  },
@@ -27,8 +31,17 @@
27
31
  "files": [
28
32
  "/Robots.js",
29
33
  "/index.js",
30
- "/index.d.ts"
34
+ "/index.d.ts",
35
+ "/README.md",
36
+ "/LICENSE.md",
37
+ "/CHANGELOG.md",
38
+ "/SECURITY.md",
39
+ "test/*.js",
40
+ ".gitignore"
31
41
  ],
42
+ "engines": {
43
+ "node": ">=0.10"
44
+ },
32
45
  "prettier": {
33
46
  "tabWidth": 4,
34
47
  "useTabs": true,
package/test/Robots.js ADDED
@@ -0,0 +1,794 @@
1
+ var robotsParser = require('../index');
2
+ var expect = require('chai').expect;
3
+
4
+
5
+ function testRobots(url, contents, allowed, disallowed) {
6
+ var robots = robotsParser(url, contents);
7
+
8
+ allowed.forEach(function (url) {
9
+ expect(robots.isAllowed(url)).to.equal(true);
10
+ });
11
+
12
+ disallowed.forEach(function (url) {
13
+ expect(robots.isDisallowed(url)).to.equal(true);
14
+ });
15
+ }
16
+
17
+ describe('Robots', function () {
18
+ it('should parse the disallow directive', function () {
19
+ var contents = [
20
+ 'User-agent: *',
21
+ 'Disallow: /fish/',
22
+ 'Disallow: /test.html'
23
+ ].join('\n');
24
+
25
+ var allowed = [
26
+ 'http://www.example.com/fish',
27
+ 'http://www.example.com/Test.html'
28
+ ];
29
+
30
+ var disallowed = [
31
+ 'http://www.example.com/fish/index.php',
32
+ 'http://www.example.com/fish/',
33
+ 'http://www.example.com/test.html'
34
+ ];
35
+
36
+ testRobots('http://www.example.com/robots.txt', contents, allowed, disallowed);
37
+ });
38
+
39
+ it('should parse the allow directive', function () {
40
+ var contents = [
41
+ 'User-agent: *',
42
+ 'Disallow: /fish/',
43
+ 'Disallow: /test.html',
44
+ 'Allow: /fish/test.html',
45
+ 'Allow: /test.html'
46
+ ].join('\n');
47
+
48
+ var allowed = [
49
+ 'http://www.example.com/fish',
50
+ 'http://www.example.com/fish/test.html',
51
+ 'http://www.example.com/Test.html',
52
+ 'http://www.example.com/test.html'
53
+ ];
54
+
55
+ var disallowed = [
56
+ 'http://www.example.com/fish/index.php',
57
+ 'http://www.example.com/fish/',
58
+ ];
59
+
60
+ testRobots('http://www.example.com/robots.txt', contents, allowed, disallowed);
61
+ });
62
+
63
+ it('should parse patterns', function () {
64
+ var contents = [
65
+ 'User-agent: *',
66
+ 'Disallow: /fish*.php',
67
+ 'Disallow: /*.dext$',
68
+ 'Disallow: /dir*'
69
+ ].join('\n');
70
+
71
+ var allowed = [
72
+ 'http://www.example.com/Fish.PHP',
73
+ 'http://www.example.com/Fish.dext1',
74
+ 'http://www.example.com/folder/dir.html',
75
+ 'http://www.example.com/folder/dir/test.html'
76
+ ];
77
+
78
+ var disallowed = [
79
+ 'http://www.example.com/fish.php',
80
+ 'http://www.example.com/fishheads/catfish.php?parameters',
81
+ 'http://www.example.com/AnYthInG.dext',
82
+ 'http://www.example.com/Fish.dext.dext',
83
+ 'http://www.example.com/dir/test.html',
84
+ 'http://www.example.com/directory.html'
85
+ ];
86
+
87
+ testRobots('http://www.example.com/robots.txt', contents, allowed, disallowed);
88
+ });
89
+
90
+ it('should have the correct order precedence for allow and disallow', function () {
91
+ var contents = [
92
+ 'User-agent: *',
93
+ 'Disallow: /fish*.php',
94
+ 'Allow: /fish/index.php',
95
+ 'Disallow: /test',
96
+ 'Allow: /test/',
97
+ 'Disallow: /aa/',
98
+ 'Allow: /aa/',
99
+ 'Allow: /bb/',
100
+ 'Disallow: /bb/',
101
+ ].join('\n');
102
+
103
+ var allowed = [
104
+ 'http://www.example.com/test/index.html',
105
+ 'http://www.example.com/fish/index.php',
106
+ 'http://www.example.com/test/',
107
+ 'http://www.example.com/aa/',
108
+ 'http://www.example.com/bb/',
109
+ 'http://www.example.com/x/'
110
+ ];
111
+
112
+ var disallowed = [
113
+ 'http://www.example.com/fish.php',
114
+ 'http://www.example.com/fishheads/catfish.php?parameters',
115
+ 'http://www.example.com/test'
116
+ ];
117
+
118
+ testRobots('http://www.example.com/robots.txt', contents, allowed, disallowed);
119
+ });
120
+
121
+ it('should have the correct order precedence for wildcards', function () {
122
+ var contents = [
123
+ 'User-agent: *',
124
+ 'Disallow: /*/',
125
+ 'Allow: /x/',
126
+ ].join('\n');
127
+
128
+ var allowed = [
129
+ 'http://www.example.com/x/',
130
+ 'http://www.example.com/fish.php',
131
+ 'http://www.example.com/test'
132
+ ];
133
+
134
+ var disallowed = [
135
+ 'http://www.example.com/a/',
136
+ 'http://www.example.com/xx/',
137
+ 'http://www.example.com/test/index.html'
138
+ ];
139
+
140
+ testRobots('http://www.example.com/robots.txt', contents, allowed, disallowed);
141
+ });
142
+
143
+ it('should parse lines delimitated by \\r', function () {
144
+ var contents = [
145
+ 'User-agent: *',
146
+ 'Disallow: /fish/',
147
+ 'Disallow: /test.html'
148
+ ].join('\r');
149
+
150
+ var allowed = [
151
+ 'http://www.example.com/fish',
152
+ 'http://www.example.com/Test.html'
153
+ ];
154
+
155
+ var disallowed = [
156
+ 'http://www.example.com/fish/index.php',
157
+ 'http://www.example.com/fish/',
158
+ 'http://www.example.com/test.html'
159
+ ];
160
+
161
+ testRobots('http://www.example.com/robots.txt', contents, allowed, disallowed);
162
+ });
163
+
164
+ it('should parse lines delimitated by \\r\\n', function () {
165
+ var contents = [
166
+ 'User-agent: *',
167
+ 'Disallow: /fish/',
168
+ 'Disallow: /test.html'
169
+ ].join('\r\n');
170
+
171
+ var allowed = [
172
+ 'http://www.example.com/fish',
173
+ 'http://www.example.com/Test.html'
174
+ ];
175
+
176
+ var disallowed = [
177
+ 'http://www.example.com/fish/index.php',
178
+ 'http://www.example.com/fish/',
179
+ 'http://www.example.com/test.html'
180
+ ];
181
+
182
+ testRobots('http://www.example.com/robots.txt', contents, allowed, disallowed);
183
+ });
184
+
185
+
186
+ it('should parse lines delimitated by mixed line endings', function () {
187
+ var contents = [
188
+ 'User-agent: *\r',
189
+ 'Disallow: /fish/\r\n',
190
+ 'Disallow: /test.html\n\n'
191
+ ].join('');
192
+
193
+ var allowed = [
194
+ 'http://www.example.com/fish',
195
+ 'http://www.example.com/Test.html'
196
+ ];
197
+
198
+ var disallowed = [
199
+ 'http://www.example.com/fish/index.php',
200
+ 'http://www.example.com/fish/',
201
+ 'http://www.example.com/test.html'
202
+ ];
203
+
204
+ testRobots('http://www.example.com/robots.txt', contents, allowed, disallowed);
205
+ });
206
+
207
+ it('should ignore rules that are not in a group', function () {
208
+ var contents = [
209
+ 'Disallow: /secret.html',
210
+ 'Disallow: /test',
211
+ ].join('\n');
212
+
213
+ var allowed = [
214
+ 'http://www.example.com/secret.html',
215
+ 'http://www.example.com/test/index.html',
216
+ 'http://www.example.com/test/'
217
+ ];
218
+
219
+ testRobots('http://www.example.com/robots.txt', contents, allowed, []);
220
+ });
221
+
222
+
223
+ it('should ignore comments', function () {
224
+ var contents = [
225
+ '#',
226
+ '# This is a comment',
227
+ '#',
228
+ 'User-agent: *',
229
+ '# This is a comment',
230
+ 'Disallow: /fish/ # ignore',
231
+ '# Disallow: fish',
232
+ 'Disallow: /test.html'
233
+ ].join('\n');
234
+
235
+ var allowed = [
236
+ 'http://www.example.com/fish',
237
+ 'http://www.example.com/Test.html'
238
+ ];
239
+
240
+ var disallowed = [
241
+ 'http://www.example.com/fish/index.php',
242
+ 'http://www.example.com/fish/',
243
+ 'http://www.example.com/test.html'
244
+ ];
245
+
246
+ testRobots('http://www.example.com/robots.txt', contents, allowed, disallowed);
247
+ });
248
+
249
+ it('should ignore invalid lines', function () {
250
+ var contents = [
251
+ 'invalid line',
252
+ 'User-agent: *',
253
+ 'Disallow: /fish/',
254
+ ':::::another invalid line:::::',
255
+ 'Disallow: /test.html',
256
+ 'Unknown: tule'
257
+ ].join('\n');
258
+
259
+ var allowed = [
260
+ 'http://www.example.com/fish',
261
+ 'http://www.example.com/Test.html'
262
+ ];
263
+
264
+ var disallowed = [
265
+ 'http://www.example.com/fish/index.php',
266
+ 'http://www.example.com/fish/',
267
+ 'http://www.example.com/test.html'
268
+ ];
269
+
270
+ testRobots('http://www.example.com/robots.txt', contents, allowed, disallowed);
271
+ });
272
+
273
+ it('should ignore empty user-agent lines', function () {
274
+ var contents = [
275
+ 'User-agent:',
276
+ 'Disallow: /fish/',
277
+ 'Disallow: /test.html'
278
+ ].join('\n');
279
+
280
+ var allowed = [
281
+ 'http://www.example.com/fish',
282
+ 'http://www.example.com/Test.html',
283
+ 'http://www.example.com/fish/index.php',
284
+ 'http://www.example.com/fish/',
285
+ 'http://www.example.com/test.html'
286
+ ];
287
+
288
+ var disallowed = [];
289
+
290
+ testRobots('http://www.example.com/robots.txt', contents, allowed, disallowed);
291
+ });
292
+
293
+ it('should support groups with multiple user agents (case insensitive)', function () {
294
+ var contents = [
295
+ 'User-agent: agenta',
296
+ 'User-agent: agentb',
297
+ 'Disallow: /fish',
298
+ ].join('\n');
299
+
300
+ var robots = robotsParser('http://www.example.com/robots.txt', contents);
301
+
302
+ expect(robots.isAllowed("http://www.example.com/fish", "agenta")).to.equal(false);
303
+ });
304
+
305
+ it('should return undefined for invalid urls', function () {
306
+ var contents = [
307
+ 'User-agent: *',
308
+ 'Disallow: /secret.html',
309
+ 'Disallow: /test',
310
+ ].join('\n');
311
+
312
+ var invalidUrls = [
313
+ 'http://example.com/secret.html',
314
+ 'http://ex ample.com/secret.html',
315
+ 'http://www.example.net/test/index.html',
316
+ 'http://www.examsple.com/test/',
317
+ 'example.com/test/',
318
+ ':::::;;`\\|/.example.com/test/'
319
+ ];
320
+
321
+ var robots = robotsParser('http://www.example.com/robots.txt', contents);
322
+
323
+ invalidUrls.forEach(function (url) {
324
+ expect(robots.isAllowed(url)).to.equal(undefined);
325
+ });
326
+ });
327
+
328
+ it('should handle Unicode, urlencoded and punycode URLs', function () {
329
+ var contents = [
330
+ 'User-agent: *',
331
+ 'Disallow: /secret.html',
332
+ 'Disallow: /test',
333
+ ].join('\n');
334
+
335
+ var allowed = [
336
+ 'http://www.münich.com/index.html',
337
+ 'http://www.xn--mnich-kva.com/index.html',
338
+ 'http://www.m%C3%BCnich.com/index.html'
339
+ ];
340
+
341
+ var disallowed = [
342
+ 'http://www.münich.com/secret.html',
343
+ 'http://www.xn--mnich-kva.com/secret.html',
344
+ 'http://www.m%C3%BCnich.com/secret.html'
345
+ ];
346
+
347
+ testRobots('http://www.münich.com/robots.txt', contents, allowed, disallowed);
348
+ testRobots('http://www.xn--mnich-kva.com/robots.txt', contents, allowed, disallowed);
349
+ testRobots('http://www.m%C3%BCnich.com/robots.txt', contents, allowed, disallowed);
350
+ });
351
+
352
+ it('should handle Unicode and urlencoded paths', function () {
353
+ var contents = [
354
+ 'User-agent: *',
355
+ 'Disallow: /%CF%80',
356
+ 'Disallow: /%e2%9d%83',
357
+ 'Disallow: /%a%a',
358
+ 'Disallow: /💩',
359
+ 'Disallow: /✼*t$',
360
+ 'Disallow: /%E2%9C%A4*t$',
361
+ 'Disallow: /✿%a',
362
+ 'Disallow: /http%3A%2F%2Fexample.org'
363
+ ].join('\n');
364
+
365
+ var allowed = [
366
+ 'http://www.example.com/✼testing',
367
+ 'http://www.example.com/%E2%9C%BCtesting',
368
+ 'http://www.example.com/✤testing',
369
+ 'http://www.example.com/%E2%9C%A4testing',
370
+ 'http://www.example.com/http://example.org',
371
+ 'http://www.example.com/http:%2F%2Fexample.org'
372
+ ];
373
+
374
+ var disallowed = [
375
+ 'http://www.example.com/%CF%80',
376
+ 'http://www.example.com/%CF%80/index.html',
377
+ 'http://www.example.com/π',
378
+ 'http://www.example.com/π/index.html',
379
+ 'http://www.example.com/%e2%9d%83',
380
+ 'http://www.example.com/%E2%9D%83/index.html',
381
+ 'http://www.example.com/❃',
382
+ 'http://www.example.com/❃/index.html',
383
+ 'http://www.example.com/%F0%9F%92%A9',
384
+ 'http://www.example.com/%F0%9F%92%A9/index.html',
385
+ 'http://www.example.com/💩',
386
+ 'http://www.example.com/💩/index.html',
387
+ 'http://www.example.com/%a%a',
388
+ 'http://www.example.com/%a%a/index.html',
389
+ 'http://www.example.com/✼test',
390
+ 'http://www.example.com/%E2%9C%BCtest',
391
+ 'http://www.example.com/✤test',
392
+ 'http://www.example.com/%E2%9C%A4testt',
393
+ 'http://www.example.com/✿%a',
394
+ 'http://www.example.com/%E2%9C%BF%atest',
395
+ 'http://www.example.com/http%3A%2F%2Fexample.org'
396
+ ];
397
+
398
+ testRobots('http://www.example.com/robots.txt', contents, allowed, disallowed);
399
+ });
400
+
401
+ it('should handle lone high / low surrogates', function () {
402
+ var contents = [
403
+ 'User-agent: *',
404
+ 'Disallow: /\uD800',
405
+ 'Disallow: /\uDC00'
406
+ ].join('\n');
407
+
408
+ // These are invalid so can't be disallowed
409
+ var allowed = [
410
+ 'http://www.example.com/\uDC00',
411
+ 'http://www.example.com/\uD800'
412
+ ];
413
+
414
+ var disallowed = [];
415
+
416
+ testRobots('http://www.example.com/robots.txt', contents, allowed, disallowed);
417
+ });
418
+
419
+ it('should ignore host case', function () {
420
+ var contents = [
421
+ 'User-agent: *',
422
+ 'Disallow: /secret.html',
423
+ 'Disallow: /test',
424
+ ].join('\n');
425
+
426
+ var allowed = [
427
+ 'http://www.example.com/index.html',
428
+ 'http://www.ExAmPlE.com/index.html',
429
+ 'http://www.EXAMPLE.com/index.html'
430
+ ];
431
+
432
+ var disallowed = [
433
+ 'http://www.example.com/secret.html',
434
+ 'http://www.ExAmPlE.com/secret.html',
435
+ 'http://www.EXAMPLE.com/secret.html'
436
+ ];
437
+
438
+ testRobots('http://www.eXample.com/robots.txt', contents, allowed, disallowed);
439
+ });
440
+
441
+ it('should handle relative paths', function () {
442
+ var contents = [
443
+ 'User-agent: *',
444
+ 'Disallow: /fish',
445
+ 'Allow: /fish/test',
446
+ ].join('\n');
447
+
448
+ var robots = robotsParser('/robots.txt', contents);
449
+ expect(robots.isAllowed('/fish/test')).to.equal(true);
450
+ expect(robots.isAllowed('/fish')).to.equal(false);
451
+ });
452
+
453
+ it('should not allow relative paths if domain specified', function () {
454
+ var contents = [
455
+ 'User-agent: *',
456
+ 'Disallow: /fish',
457
+ 'Allow: /fish/test',
458
+ ].join('\n');
459
+
460
+ var robots = robotsParser('http://www.example.com/robots.txt', contents);
461
+ expect(robots.isAllowed('/fish/test')).to.equal(undefined);
462
+ expect(robots.isAllowed('/fish')).to.equal(undefined);
463
+ });
464
+
465
+ it('should not treat invalid robots.txt URLs as relative', function () {
466
+ var contents = [
467
+ 'User-agent: *',
468
+ 'Disallow: /fish',
469
+ 'Allow: /fish/test',
470
+ ].join('\n');
471
+
472
+ var robots = robotsParser('https://ex ample.com/robots.txt', contents);
473
+ expect(robots.isAllowed('/fish/test')).to.equal(undefined);
474
+ expect(robots.isAllowed('/fish')).to.equal(undefined);
475
+ });
476
+
477
+ it('should not allow URls if domain specified and robots.txt is relative', function () {
478
+ var contents = [
479
+ 'User-agent: *',
480
+ 'Disallow: /fish',
481
+ 'Allow: /fish/test',
482
+ ].join('\n');
483
+
484
+ var robots = robotsParser('/robots.txt', contents);
485
+ expect(robots.isAllowed('http://www.example.com/fish/test')).to.equal(undefined);
486
+ expect(robots.isAllowed('http://www.example.com/fish')).to.equal(undefined);
487
+ });
488
+
489
+ it('should allow all if empty robots.txt', function () {
490
+ var allowed = [
491
+ 'http://www.example.com/secret.html',
492
+ 'http://www.example.com/test/index.html',
493
+ 'http://www.example.com/test/'
494
+ ];
495
+
496
+ var robots = robotsParser('http://www.example.com/robots.txt', '');
497
+
498
+ allowed.forEach(function (url) {
499
+ expect(robots.isAllowed(url)).to.equal(true);
500
+ });
501
+ });
502
+
503
+ it('should treat null as allowing all', function () {
504
+ var robots = robotsParser('http://www.example.com/robots.txt', null);
505
+
506
+ expect(robots.isAllowed("http://www.example.com/", "userAgent")).to.equal(true);
507
+ expect(robots.isAllowed("http://www.example.com/")).to.equal(true);
508
+ });
509
+
510
+ it('should handle invalid robots.txt urls', function () {
511
+ var contents = [
512
+ 'user-agent: *',
513
+ 'disallow: /',
514
+
515
+ 'host: www.example.com',
516
+ 'sitemap: /sitemap.xml'
517
+ ].join('\n');
518
+
519
+ var sitemapUrls = [
520
+ undefined,
521
+ null,
522
+ 'null',
523
+ ':/wom/test/'
524
+ ];
525
+
526
+ sitemapUrls.forEach(function (url) {
527
+ var robots = robotsParser(url, contents);
528
+ expect(robots.isAllowed('http://www.example.com/index.html')).to.equal(undefined);
529
+ expect(robots.getPreferredHost()).to.equal('www.example.com');
530
+ expect(robots.getSitemaps()).to.eql(['/sitemap.xml']);
531
+ });
532
+ });
533
+
534
+ it('should parse the crawl-delay directive', function () {
535
+ var contents = [
536
+ 'user-agent: a',
537
+ 'crawl-delay: 1',
538
+
539
+ 'user-agent: b',
540
+ 'disallow: /d',
541
+
542
+ 'user-agent: c',
543
+ 'user-agent: d',
544
+ 'crawl-delay: 10'
545
+ ].join('\n');
546
+
547
+ var robots = robotsParser('http://www.example.com/robots.txt', contents);
548
+
549
+ expect(robots.getCrawlDelay('a')).to.equal(1);
550
+ expect(robots.getCrawlDelay('b')).to.equal(undefined);
551
+ expect(robots.getCrawlDelay('c')).to.equal(10);
552
+ expect(robots.getCrawlDelay('d')).to.equal(10);
553
+ expect(robots.getCrawlDelay()).to.equal(undefined);
554
+ });
555
+
556
+ it('should ignore invalid crawl-delay directives', function () {
557
+ var contents = [
558
+ 'user-agent: a',
559
+ 'crawl-delay: 1.2.1',
560
+
561
+ 'user-agent: b',
562
+ 'crawl-delay: 1.a0',
563
+
564
+ 'user-agent: c',
565
+ 'user-agent: d',
566
+ 'crawl-delay: 10a'
567
+ ].join('\n');
568
+
569
+ var robots = robotsParser('http://www.example.com/robots.txt', contents);
570
+
571
+ expect(robots.getCrawlDelay('a')).to.equal(undefined);
572
+ expect(robots.getCrawlDelay('b')).to.equal(undefined);
573
+ expect(robots.getCrawlDelay('c')).to.equal(undefined);
574
+ expect(robots.getCrawlDelay('d')).to.equal(undefined);
575
+ });
576
+
577
+ it('should parse the sitemap directive', function () {
578
+ var contents = [
579
+ 'user-agent: a',
580
+ 'crawl-delay: 1',
581
+ 'sitemap: http://example.com/test.xml',
582
+
583
+ 'user-agent: b',
584
+ 'disallow: /d',
585
+
586
+ 'sitemap: /sitemap.xml',
587
+ 'sitemap: http://example.com/test/sitemap.xml '
588
+ ].join('\n');
589
+
590
+ var robots = robotsParser('http://www.example.com/robots.txt', contents);
591
+
592
+ expect(robots.getSitemaps()).to.eql([
593
+ 'http://example.com/test.xml',
594
+ '/sitemap.xml',
595
+ 'http://example.com/test/sitemap.xml'
596
+ ]);
597
+ });
598
+
599
+ it('should parse the host directive', function () {
600
+ var contents = [
601
+ 'user-agent: a',
602
+ 'crawl-delay: 1',
603
+ 'host: www.example.net',
604
+
605
+ 'user-agent: b',
606
+ 'disallow: /d',
607
+
608
+ 'host: example.com'
609
+ ].join('\n');
610
+
611
+ var robots = robotsParser('http://www.example.com/robots.txt', contents);
612
+
613
+ expect(robots.getPreferredHost()).to.equal('example.com');
614
+ });
615
+
616
+ it('should parse empty and invalid directives', function () {
617
+ var contents = [
618
+ 'user-agent:',
619
+ 'user-agent:::: a::',
620
+ 'crawl-delay:',
621
+ 'crawl-delay:::: 0:',
622
+ 'host:',
623
+ 'host:: example.com',
624
+ 'sitemap:',
625
+ 'sitemap:: site:map.xml',
626
+ 'disallow:',
627
+ 'disallow::: /:',
628
+ 'allow:',
629
+ 'allow::: /:',
630
+ ].join('\n');
631
+
632
+ robotsParser('http://www.example.com/robots.txt', contents);
633
+ });
634
+
635
+ it('should treat only the last host directive as valid', function () {
636
+ var contents = [
637
+ 'user-agent: a',
638
+ 'crawl-delay: 1',
639
+ 'host: www.example.net',
640
+
641
+ 'user-agent: b',
642
+ 'disallow: /d',
643
+
644
+ 'host: example.net',
645
+ 'host: example.com'
646
+ ].join('\n');
647
+
648
+ var robots = robotsParser('http://www.example.com/robots.txt', contents);
649
+
650
+ expect(robots.getPreferredHost()).to.equal('example.com');
651
+ });
652
+
653
+ it('should return null when there is no host directive', function () {
654
+ var contents = [
655
+ 'user-agent: a',
656
+ 'crawl-delay: 1',
657
+
658
+ 'user-agent: b',
659
+ 'disallow: /d',
660
+ ].join('\n');
661
+
662
+ var robots = robotsParser('http://www.example.com/robots.txt', contents);
663
+
664
+ expect(robots.getPreferredHost()).to.equal(null);
665
+ });
666
+
667
+ it('should fallback to * when a UA has no rules of its own', function () {
668
+ var contents = [
669
+ 'user-agent: *',
670
+ 'crawl-delay: 1',
671
+
672
+ 'user-agent: b',
673
+ 'crawl-delay: 12',
674
+
675
+ 'user-agent: c',
676
+ 'user-agent: d',
677
+ 'crawl-delay: 10'
678
+ ].join('\n');
679
+
680
+ var robots = robotsParser('http://www.example.com/robots.txt', contents);
681
+
682
+ expect(robots.getCrawlDelay('should-fall-back')).to.equal(1);
683
+ expect(robots.getCrawlDelay('d')).to.equal(10);
684
+ expect(robots.getCrawlDelay('dd')).to.equal(1);
685
+ });
686
+
687
+ it('should not fallback to * when a UA has rules', function () {
688
+ var contents = [
689
+ 'user-agent: *',
690
+ 'crawl-delay: 1',
691
+
692
+ 'user-agent: b',
693
+ 'disallow:'
694
+ ].join('\n');
695
+
696
+ var robots = robotsParser('http://www.example.com/robots.txt', contents);
697
+
698
+ expect(robots.getCrawlDelay('b')).to.equal(undefined);
699
+ });
700
+
701
+ it('should handle UAs with object property names', function () {
702
+ var contents = [
703
+ 'User-agent: *',
704
+ 'Disallow: /fish',
705
+ ].join('\n');
706
+
707
+ var robots = robotsParser('http://www.example.com/robots.txt', contents);
708
+ expect(robots.isAllowed('http://www.example.com/fish', 'constructor')).to.equal(false);
709
+ expect(robots.isAllowed('http://www.example.com/fish', '__proto__')).to.equal(false);
710
+ });
711
+
712
+ it('should ignore version numbers in the UA string', function () {
713
+ var contents = [
714
+ 'user-agent: *',
715
+ 'crawl-delay: 1',
716
+
717
+ 'user-agent: b',
718
+ 'crawl-delay: 12',
719
+
720
+ 'user-agent: c',
721
+ 'user-agent: d',
722
+ 'crawl-delay: 10'
723
+ ].join('\n');
724
+
725
+ var robots = robotsParser('http://www.example.com/robots.txt', contents);
726
+
727
+ expect(robots.getCrawlDelay('should-fall-back/1.0.0')).to.equal(1);
728
+ expect(robots.getCrawlDelay('d/12')).to.equal(10);
729
+ expect(robots.getCrawlDelay('dd / 0-32-3')).to.equal(1);
730
+ expect(robots.getCrawlDelay('b / 1.0')).to.equal(12);
731
+ });
732
+
733
+
734
+ it('should return the line number of the matching directive', function () {
735
+ var contents = [
736
+ '',
737
+ 'User-agent: *',
738
+ '',
739
+ 'Disallow: /fish/',
740
+ 'Disallow: /test.html',
741
+ 'Allow: /fish/test.html',
742
+ 'Allow: /test.html',
743
+ '',
744
+ 'User-agent: a',
745
+ 'allow: /',
746
+ '',
747
+ 'User-agent: b',
748
+ 'disallow: /test',
749
+ 'disallow: /t*t',
750
+ '',
751
+ 'User-agent: c',
752
+ 'Disallow: /fish*.php',
753
+ 'Allow: /fish/index.php'
754
+ ].join('\n');
755
+
756
+ var robots = robotsParser('http://www.example.com/robots.txt', contents);
757
+
758
+ expect(robots.getMatchingLineNumber('http://www.example.com/fish')).to.equal(-1);
759
+ expect(robots.getMatchingLineNumber('http://www.example.com/fish/test.html')).to.equal(6);
760
+ expect(robots.getMatchingLineNumber('http://www.example.com/Test.html')).to.equal(-1);
761
+
762
+ expect(robots.getMatchingLineNumber('http://www.example.com/fish/index.php')).to.equal(4);
763
+ expect(robots.getMatchingLineNumber('http://www.example.com/fish/')).to.equal(4);
764
+ expect(robots.getMatchingLineNumber('http://www.example.com/test.html')).to.equal(7);
765
+
766
+ expect(robots.getMatchingLineNumber('http://www.example.com/test.html', 'a')).to.equal(10);
767
+
768
+ expect(robots.getMatchingLineNumber('http://www.example.com/fish.php', 'c')).to.equal(17);
769
+ expect(robots.getMatchingLineNumber('http://www.example.com/fish/index.php', 'c')).to.equal(18);
770
+ });
771
+
772
+ it('should handle large wildcards efficiently', function () {
773
+ var contents = [
774
+ 'User-agent: *',
775
+ 'Disallow: /' + '*'.repeat(2048) + '.html',
776
+ ].join('\n');
777
+
778
+ var allowed = [
779
+ 'http://www.example.com/' + 'sub'.repeat(2048) + 'folder/index.php',
780
+ ];
781
+
782
+ var disallowed = [
783
+ 'http://www.example.com/secret.html'
784
+ ];
785
+
786
+ const start = Date.now();
787
+ testRobots('http://www.eXample.com/robots.txt', contents, allowed, disallowed);
788
+ const end = Date.now();
789
+
790
+ // Should take less than 500 ms (high to allow for variableness of
791
+ // machines running the test, should normally be much less)
792
+ expect(end - start).to.be.lessThan(500);
793
+ });
794
+ });