robots-parser 2.2.0 → 2.3.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.
Files changed (4) hide show
  1. package/Robots.js +33 -21
  2. package/package.json +31 -21
  3. package/readme.md +25 -2
  4. package/test/Robots.js +0 -734
package/Robots.js CHANGED
@@ -86,7 +86,7 @@ function formatUserAgent(userAgent) {
86
86
  function normaliseEncoding(path) {
87
87
  try {
88
88
  return urlEncodeToUpper(encodeURI(path).replace(/%25/g, '%'));
89
- } catch(e) {
89
+ } catch (e) {
90
90
  return path;
91
91
  }
92
92
  }
@@ -155,7 +155,10 @@ function matches(pattern, path) {
155
155
  // does increment it and add it as a valid length, ignore if not.
156
156
  var numMatches = 0;
157
157
  for (var i = 0; i < numMatchingLengths; i++) {
158
- if (matchingLengths[i] < path.length && path[matchingLengths[i]] === pattern[p]) {
158
+ if (
159
+ matchingLengths[i] < path.length &&
160
+ path[matchingLengths[i]] === pattern[p]
161
+ ) {
159
162
  matchingLengths[numMatches++] = matchingLengths[i] + 1;
160
163
  }
161
164
  }
@@ -182,7 +185,7 @@ function parseRobots(contents, robots) {
182
185
 
183
186
  var currentUserAgents = [];
184
187
  var isNoneUserAgentState = true;
185
- for (var i=0; i < lines.length; i++) {
188
+ for (var i = 0; i < lines.length; i++) {
186
189
  var line = lines[i];
187
190
 
188
191
  if (!line || !line[0]) {
@@ -233,25 +236,29 @@ function parseRobots(contents, robots) {
233
236
  * @private
234
237
  */
235
238
  function findRule(path, rules) {
236
- var matchingRule = null;
239
+ var matchedRule = null;
237
240
 
238
- for (var i=0; i < rules.length; i++) {
239
- var rule = rules[i];
241
+ for (var i = 0; i < rules.length; i++) {
242
+ var rule = rules[i];
240
243
 
241
244
  if (!matches(rule.pattern, path)) {
242
245
  continue;
243
246
  }
244
247
 
245
248
  // The longest matching rule takes precedence
246
- if (!matchingRule || rule.pattern.length > matchingRule.pattern.length) {
247
- matchingRule = rule;
248
- } else if (rule.pattern.length == matchingRule.pattern.length &&
249
- rule.allow && !matchingRule.allow) {
250
- matchingRule = rule;
249
+ // If rules are the same length then allow takes precedence
250
+ if (!matchedRule || rule.pattern.length > matchedRule.pattern.length) {
251
+ matchedRule = rule;
252
+ } else if (
253
+ rule.pattern.length == matchedRule.pattern.length &&
254
+ rule.allow &&
255
+ !matchedRule.allow
256
+ ) {
257
+ matchedRule = rule;
251
258
  }
252
- }
259
+ }
253
260
 
254
- return matchingRule;
261
+ return matchedRule;
255
262
  }
256
263
 
257
264
  /**
@@ -265,18 +272,21 @@ function findRule(path, rules) {
265
272
  */
266
273
  function parseUrl(url) {
267
274
  try {
268
- return new URL(url);
269
- } catch(e) {
275
+ // Specify a URL to be used with relative paths
276
+ // Using non-existent subdomain so can never cause conflict unless
277
+ // trying to crawl it but doesn't exist and even if tried worst that can
278
+ // happen is it allows relative URLs on it.
279
+ return new URL(url, 'http://robots-relative.samclarke.com/');
280
+ } catch (e) {
270
281
  return null;
271
282
  }
272
283
  }
273
284
 
274
-
275
285
  function Robots(url, contents) {
276
286
  this._url = parseUrl(url) || {};
277
287
  this._url.port = this._url.port || 80;
278
288
 
279
- this._rules = {};
289
+ this._rules = Object.create(null);
280
290
  this._sitemaps = [];
281
291
  this._preferredHost = null;
282
292
 
@@ -353,17 +363,19 @@ Robots.prototype._getRule = function (url, ua) {
353
363
  var parsedUrl = parseUrl(url) || {};
354
364
  var userAgent = formatUserAgent(ua || '*');
355
365
 
356
- parsedUrl.port = parsedUrl.port || '80';
366
+ parsedUrl.port = parsedUrl.port || 80;
357
367
 
358
368
  // The base URL must match otherwise this robots.txt is not valid for it.
359
- if (parsedUrl.protocol !== this._url.protocol ||
369
+ if (
370
+ parsedUrl.protocol !== this._url.protocol ||
360
371
  parsedUrl.hostname !== this._url.hostname ||
361
- parsedUrl.port !== this._url.port) {
372
+ parsedUrl.port !== this._url.port
373
+ ) {
362
374
  return;
363
375
  }
364
376
 
365
377
  var rules = this._rules[userAgent] || this._rules['*'] || [];
366
- var path = urlEncodeToUpper(parsedUrl.pathname + parsedUrl.search)
378
+ var path = urlEncodeToUpper(parsedUrl.pathname + parsedUrl.search);
367
379
  var rule = findRule(path, rules);
368
380
 
369
381
  return rule;
package/package.json CHANGED
@@ -1,23 +1,33 @@
1
1
  {
2
- "name": "robots-parser",
3
- "version": "2.2.0",
4
- "description": "Robots.txt parser.",
5
- "main": "index.js",
6
- "directories": {
7
- "test": "tests"
8
- },
9
- "scripts": {
10
- "test": "nyc --clean --reporter html --report-dir ./report/coverage ./node_modules/.bin/mocha"
11
- },
12
- "repository": {
13
- "type": "git",
14
- "url": "https://github.com/samclarke/robots-parser.git"
15
- },
16
- "author": "Sam Clarke <sam@samclarke.com>",
17
- "license": "MIT",
18
- "devDependencies": {
19
- "chai": "^4.2.0",
20
- "mocha": "^6.1.4",
21
- "nyc": "^14.1.1"
22
- }
2
+ "name": "robots-parser",
3
+ "version": "2.3.0",
4
+ "description": "Robots.txt parser.",
5
+ "main": "index.js",
6
+ "directories": {
7
+ "test": "tests"
8
+ },
9
+ "scripts": {
10
+ "test": "nyc --reporter=text-summary --reporter=html --reporter=lcovonly mocha"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/samclarke/robots-parser.git"
15
+ },
16
+ "author": "Sam Clarke <sam@samclarke.com>",
17
+ "license": "MIT",
18
+ "files": [
19
+ "/Robots.js",
20
+ "/index.js"
21
+ ],
22
+ "prettier": {
23
+ "tabWidth": 4,
24
+ "useTabs": true,
25
+ "singleQuote": true,
26
+ "trailingComma": "none"
27
+ },
28
+ "devDependencies": {
29
+ "chai": "^4.2.0",
30
+ "mocha": "^6.1.4",
31
+ "nyc": "^14.1.1"
32
+ }
23
33
  }
package/readme.md CHANGED
@@ -1,4 +1,4 @@
1
- # Robots Parser [![DeepScan Grade](https://deepscan.io/api/projects/1275/branches/3378/badge/grade.svg)](https://deepscan.io/dashboard/#view=project&pid=1275&bid=3378) [![GitHub license](https://img.shields.io/github/license/samclarke/robots-parser.svg)](https://github.com/samclarke/robots-parser/blob/master/license.md)
1
+ # 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
3
  NodeJS robots.txt parser.
4
4
 
@@ -95,9 +95,32 @@ Returns the preferred host name specified by the `host:` directive or null if th
95
95
 
96
96
  # Changes
97
97
 
98
+ ### Version 2.3.0:
99
+
100
+ * Fixed bug where if the user-agent passed to `isAllowed()` / `isDisallowed()` is called "constructor" it would throw an error.
101
+ * Added support for relative URLs. This does not affect the default behavior so can safely be upgraded.
102
+
103
+ Relative matching is only allowed if both the robots.txt URL and the URLs being checked are relative.
104
+
105
+ For example:
106
+ ```js
107
+ var robots = robotsParser('/robots.txt', [
108
+ 'User-agent: *',
109
+ 'Disallow: /dir/',
110
+ 'Disallow: /test.html',
111
+ 'Allow: /dir/test.html',
112
+ 'Allow: /test.html'
113
+ ].join('\n'));
114
+
115
+ robots.isAllowed('/test.html', 'Sams-Bot/1.0'); // false
116
+ robots.isAllowed('/dir/test.html', 'Sams-Bot/1.0'); // true
117
+ robots.isDisallowed('/dir/test2.html', 'Sams-Bot/1.0'); // true
118
+ ```
119
+
120
+
98
121
  ### Version 2.2.0:
99
122
 
100
- * Fix bug that with matching wildcard patterns with some URLs
123
+ * Fixed bug that with matching wildcard patterns with some URLs
101
124
  &ndash; Thanks to @ckylape for reporting and fixing
102
125
  * Changed matching algorithm to match Google's implementation in google/robotstxt
103
126
  * Changed order of precedence to match current spec
package/test/Robots.js DELETED
@@ -1,734 +0,0 @@
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://www.example.net/test/index.html',
315
- 'http://www.examsple.com/test/',
316
- 'example.com/test/',
317
- ':::::;;`\\|/.example.com/test/'
318
- ];
319
-
320
- var robots = robotsParser('http://www.example.com/robots.txt', contents);
321
-
322
- invalidUrls.forEach(function (url) {
323
- expect(robots.isAllowed(url)).to.equal(undefined);
324
- });
325
- });
326
-
327
- it('should handle Unicode, urlencoded and punycode URLs', function () {
328
- var contents = [
329
- 'User-agent: *',
330
- 'Disallow: /secret.html',
331
- 'Disallow: /test',
332
- ].join('\n');
333
-
334
- var allowed = [
335
- 'http://www.münich.com/index.html',
336
- 'http://www.xn--mnich-kva.com/index.html',
337
- 'http://www.m%C3%BCnich.com/index.html'
338
- ];
339
-
340
- var disallowed = [
341
- 'http://www.münich.com/secret.html',
342
- 'http://www.xn--mnich-kva.com/secret.html',
343
- 'http://www.m%C3%BCnich.com/secret.html'
344
- ];
345
-
346
- testRobots('http://www.münich.com/robots.txt', contents, allowed, disallowed);
347
- testRobots('http://www.xn--mnich-kva.com/robots.txt', contents, allowed, disallowed);
348
- testRobots('http://www.m%C3%BCnich.com/robots.txt', contents, allowed, disallowed);
349
- });
350
-
351
- it('should handle Unicode and urlencoded paths', function () {
352
- var contents = [
353
- 'User-agent: *',
354
- 'Disallow: /%CF%80',
355
- 'Disallow: /%e2%9d%83',
356
- 'Disallow: /%a%a',
357
- 'Disallow: /💩',
358
- 'Disallow: /✼*t$',
359
- 'Disallow: /%E2%9C%A4*t$',
360
- 'Disallow: /✿%a',
361
- 'Disallow: /http%3A%2F%2Fexample.org'
362
- ].join('\n');
363
-
364
- var allowed = [
365
- 'http://www.example.com/✼testing',
366
- 'http://www.example.com/%E2%9C%BCtesting',
367
- 'http://www.example.com/✤testing',
368
- 'http://www.example.com/%E2%9C%A4testing',
369
- 'http://www.example.com/http://example.org',
370
- 'http://www.example.com/http:%2F%2Fexample.org'
371
- ];
372
-
373
- var disallowed = [
374
- 'http://www.example.com/%CF%80',
375
- 'http://www.example.com/%CF%80/index.html',
376
- 'http://www.example.com/π',
377
- 'http://www.example.com/π/index.html',
378
- 'http://www.example.com/%e2%9d%83',
379
- 'http://www.example.com/%E2%9D%83/index.html',
380
- 'http://www.example.com/❃',
381
- 'http://www.example.com/❃/index.html',
382
- 'http://www.example.com/%F0%9F%92%A9',
383
- 'http://www.example.com/%F0%9F%92%A9/index.html',
384
- 'http://www.example.com/💩',
385
- 'http://www.example.com/💩/index.html',
386
- 'http://www.example.com/%a%a',
387
- 'http://www.example.com/%a%a/index.html',
388
- 'http://www.example.com/✼test',
389
- 'http://www.example.com/%E2%9C%BCtest',
390
- 'http://www.example.com/✤test',
391
- 'http://www.example.com/%E2%9C%A4testt',
392
- 'http://www.example.com/✿%a',
393
- 'http://www.example.com/%E2%9C%BF%atest',
394
- 'http://www.example.com/http%3A%2F%2Fexample.org'
395
- ];
396
-
397
- testRobots('http://www.example.com/robots.txt', contents, allowed, disallowed);
398
- });
399
-
400
- it('should handle lone high / low surrogates', function () {
401
- var contents = [
402
- 'User-agent: *',
403
- 'Disallow: /\uD800',
404
- 'Disallow: /\uDC00'
405
- ].join('\n');
406
-
407
- // These are invalid so can't be disallowed
408
- var allowed = [
409
- 'http://www.example.com/\uDC00',
410
- 'http://www.example.com/\uD800'
411
- ];
412
-
413
- var disallowed = [];
414
-
415
- testRobots('http://www.example.com/robots.txt', contents, allowed, disallowed);
416
- });
417
-
418
- it('should ignore host case', function () {
419
- var contents = [
420
- 'User-agent: *',
421
- 'Disallow: /secret.html',
422
- 'Disallow: /test',
423
- ].join('\n');
424
-
425
- var allowed = [
426
- 'http://www.example.com/index.html',
427
- 'http://www.ExAmPlE.com/index.html',
428
- 'http://www.EXAMPLE.com/index.html'
429
- ];
430
-
431
- var disallowed = [
432
- 'http://www.example.com/secret.html',
433
- 'http://www.ExAmPlE.com/secret.html',
434
- 'http://www.EXAMPLE.com/secret.html'
435
- ];
436
-
437
- testRobots('http://www.eXample.com/robots.txt', contents, allowed, disallowed);
438
- });
439
-
440
- it('should allow all if empty robots.txt', function () {
441
- var allowed = [
442
- 'http://www.example.com/secret.html',
443
- 'http://www.example.com/test/index.html',
444
- 'http://www.example.com/test/'
445
- ];
446
-
447
- var robots = robotsParser('http://www.example.com/robots.txt', '');
448
-
449
- allowed.forEach(function (url) {
450
- expect(robots.isAllowed(url)).to.equal(true);
451
- });
452
- });
453
-
454
- it('should treat null as allowing all', function () {
455
- var robots = robotsParser('http://www.example.com/robots.txt', null);
456
-
457
- expect(robots.isAllowed("http://www.example.com/", "userAgent")).to.equal(true);
458
- expect(robots.isAllowed("http://www.example.com/")).to.equal(true);
459
- });
460
-
461
- it('should handle invalid robots.txt urls', function () {
462
- var contents = [
463
- 'user-agent: *',
464
- 'disallow: /',
465
-
466
- 'host: www.example.com',
467
- 'sitemap: /sitemap.xml'
468
- ].join('\n');
469
-
470
- var sitemapUrls = [
471
- undefined,
472
- null,
473
- 'null',
474
- ':/wom/test/'
475
- ];
476
-
477
- sitemapUrls.forEach(function (url) {
478
- var robots = robotsParser(url, contents);
479
- expect(robots.isAllowed('http://www.example.com/index.html')).to.equal(undefined);
480
- expect(robots.getPreferredHost()).to.equal('www.example.com');
481
- expect(robots.getSitemaps()).to.eql(['/sitemap.xml']);
482
- });
483
- });
484
-
485
- it('should parse the crawl-delay directive', function () {
486
- var contents = [
487
- 'user-agent: a',
488
- 'crawl-delay: 1',
489
-
490
- 'user-agent: b',
491
- 'disallow: /d',
492
-
493
- 'user-agent: c',
494
- 'user-agent: d',
495
- 'crawl-delay: 10'
496
- ].join('\n');
497
-
498
- var robots = robotsParser('http://www.example.com/robots.txt', contents);
499
-
500
- expect(robots.getCrawlDelay('a')).to.equal(1);
501
- expect(robots.getCrawlDelay('b')).to.equal(undefined);
502
- expect(robots.getCrawlDelay('c')).to.equal(10);
503
- expect(robots.getCrawlDelay('d')).to.equal(10);
504
- expect(robots.getCrawlDelay()).to.equal(undefined);
505
- });
506
-
507
- it('should ignore invalid crawl-delay directives', function () {
508
- var contents = [
509
- 'user-agent: a',
510
- 'crawl-delay: 1.2.1',
511
-
512
- 'user-agent: b',
513
- 'crawl-delay: 1.a0',
514
-
515
- 'user-agent: c',
516
- 'user-agent: d',
517
- 'crawl-delay: 10a'
518
- ].join('\n');
519
-
520
- var robots = robotsParser('http://www.example.com/robots.txt', contents);
521
-
522
- expect(robots.getCrawlDelay('a')).to.equal(undefined);
523
- expect(robots.getCrawlDelay('b')).to.equal(undefined);
524
- expect(robots.getCrawlDelay('c')).to.equal(undefined);
525
- expect(robots.getCrawlDelay('d')).to.equal(undefined);
526
- });
527
-
528
- it('should parse the sitemap directive', function () {
529
- var contents = [
530
- 'user-agent: a',
531
- 'crawl-delay: 1',
532
- 'sitemap: http://example.com/test.xml',
533
-
534
- 'user-agent: b',
535
- 'disallow: /d',
536
-
537
- 'sitemap: /sitemap.xml',
538
- 'sitemap: http://example.com/test/sitemap.xml '
539
- ].join('\n');
540
-
541
- var robots = robotsParser('http://www.example.com/robots.txt', contents);
542
-
543
- expect(robots.getSitemaps()).to.eql([
544
- 'http://example.com/test.xml',
545
- '/sitemap.xml',
546
- 'http://example.com/test/sitemap.xml'
547
- ]);
548
- });
549
-
550
- it('should parse the host directive', function () {
551
- var contents = [
552
- 'user-agent: a',
553
- 'crawl-delay: 1',
554
- 'host: www.example.net',
555
-
556
- 'user-agent: b',
557
- 'disallow: /d',
558
-
559
- 'host: example.com'
560
- ].join('\n');
561
-
562
- var robots = robotsParser('http://www.example.com/robots.txt', contents);
563
-
564
- expect(robots.getPreferredHost()).to.equal('example.com');
565
- });
566
-
567
- it('should parse empty and invalid directives', function () {
568
- var contents = [
569
- 'user-agent:',
570
- 'user-agent:::: a::',
571
- 'crawl-delay:',
572
- 'crawl-delay:::: 0:',
573
- 'host:',
574
- 'host:: example.com',
575
- 'sitemap:',
576
- 'sitemap:: site:map.xml',
577
- 'disallow:',
578
- 'disallow::: /:',
579
- 'allow:',
580
- 'allow::: /:',
581
- ].join('\n');
582
-
583
- robotsParser('http://www.example.com/robots.txt', contents);
584
- });
585
-
586
- it('should treat only the last host directive as valid', function () {
587
- var contents = [
588
- 'user-agent: a',
589
- 'crawl-delay: 1',
590
- 'host: www.example.net',
591
-
592
- 'user-agent: b',
593
- 'disallow: /d',
594
-
595
- 'host: example.net',
596
- 'host: example.com'
597
- ].join('\n');
598
-
599
- var robots = robotsParser('http://www.example.com/robots.txt', contents);
600
-
601
- expect(robots.getPreferredHost()).to.equal('example.com');
602
- });
603
-
604
- it('should return null when there is no host directive', function () {
605
- var contents = [
606
- 'user-agent: a',
607
- 'crawl-delay: 1',
608
-
609
- 'user-agent: b',
610
- 'disallow: /d',
611
- ].join('\n');
612
-
613
- var robots = robotsParser('http://www.example.com/robots.txt', contents);
614
-
615
- expect(robots.getPreferredHost()).to.equal(null);
616
- });
617
-
618
- it('should fallback to * when a UA has no rules of its own', function () {
619
- var contents = [
620
- 'user-agent: *',
621
- 'crawl-delay: 1',
622
-
623
- 'user-agent: b',
624
- 'crawl-delay: 12',
625
-
626
- 'user-agent: c',
627
- 'user-agent: d',
628
- 'crawl-delay: 10'
629
- ].join('\n');
630
-
631
- var robots = robotsParser('http://www.example.com/robots.txt', contents);
632
-
633
- expect(robots.getCrawlDelay('should-fall-back')).to.equal(1);
634
- expect(robots.getCrawlDelay('d')).to.equal(10);
635
- expect(robots.getCrawlDelay('dd')).to.equal(1);
636
- });
637
-
638
- it('should not fallback to * when a UA has rules', function () {
639
- var contents = [
640
- 'user-agent: *',
641
- 'crawl-delay: 1',
642
-
643
- 'user-agent: b',
644
- 'disallow:'
645
- ].join('\n');
646
-
647
- var robots = robotsParser('http://www.example.com/robots.txt', contents);
648
-
649
- expect(robots.getCrawlDelay('b')).to.equal(undefined);
650
- });
651
-
652
- it('should ignore version numbers in the UA string', function () {
653
- var contents = [
654
- 'user-agent: *',
655
- 'crawl-delay: 1',
656
-
657
- 'user-agent: b',
658
- 'crawl-delay: 12',
659
-
660
- 'user-agent: c',
661
- 'user-agent: d',
662
- 'crawl-delay: 10'
663
- ].join('\n');
664
-
665
- var robots = robotsParser('http://www.example.com/robots.txt', contents);
666
-
667
- expect(robots.getCrawlDelay('should-fall-back/1.0.0')).to.equal(1);
668
- expect(robots.getCrawlDelay('d/12')).to.equal(10);
669
- expect(robots.getCrawlDelay('dd / 0-32-3')).to.equal(1);
670
- expect(robots.getCrawlDelay('b / 1.0')).to.equal(12);
671
- });
672
-
673
-
674
- it('should return the line number of the matching directive', function () {
675
- var contents = [
676
- '',
677
- 'User-agent: *',
678
- '',
679
- 'Disallow: /fish/',
680
- 'Disallow: /test.html',
681
- 'Allow: /fish/test.html',
682
- 'Allow: /test.html',
683
- '',
684
- 'User-agent: a',
685
- 'allow: /',
686
- '',
687
- 'User-agent: b',
688
- 'disallow: /test',
689
- 'disallow: /t*t',
690
- '',
691
- 'User-agent: c',
692
- 'Disallow: /fish*.php',
693
- 'Allow: /fish/index.php'
694
- ].join('\n');
695
-
696
- var robots = robotsParser('http://www.example.com/robots.txt', contents);
697
-
698
- expect(robots.getMatchingLineNumber('http://www.example.com/fish')).to.equal(-1);
699
- expect(robots.getMatchingLineNumber('http://www.example.com/fish/test.html')).to.equal(6);
700
- expect(robots.getMatchingLineNumber('http://www.example.com/Test.html')).to.equal(-1);
701
-
702
- expect(robots.getMatchingLineNumber('http://www.example.com/fish/index.php')).to.equal(4);
703
- expect(robots.getMatchingLineNumber('http://www.example.com/fish/')).to.equal(4);
704
- expect(robots.getMatchingLineNumber('http://www.example.com/test.html')).to.equal(7);
705
-
706
- expect(robots.getMatchingLineNumber('http://www.example.com/test.html', 'a')).to.equal(10);
707
-
708
- expect(robots.getMatchingLineNumber('http://www.example.com/fish.php', 'c')).to.equal(17);
709
- expect(robots.getMatchingLineNumber('http://www.example.com/fish/index.php', 'c')).to.equal(18);
710
- });
711
-
712
- it('should handle large wildcards efficiently', function () {
713
- var contents = [
714
- 'User-agent: *',
715
- 'Disallow: /' + '*'.repeat(2048) + '.html',
716
- ].join('\n');
717
-
718
- var allowed = [
719
- 'http://www.example.com/' + 'sub'.repeat(2048) + 'folder/index.php',
720
- ];
721
-
722
- var disallowed = [
723
- 'http://www.example.com/secret.html'
724
- ];
725
-
726
- const start = Date.now();
727
- testRobots('http://www.eXample.com/robots.txt', contents, allowed, disallowed);
728
- const end = Date.now();
729
-
730
- // Should take less than 500 ms (high to allow for variableness of
731
- // machines running the test, should normally be much less)
732
- expect(end - start).to.be.lessThan(500);
733
- });
734
- });