robots-parser 2.4.0 → 3.0.1
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 +4 -0
- package/CHANGELOG.md +86 -0
- package/{license.md → LICENSE.md} +0 -0
- package/{readme.md → README.md} +17 -2
- package/Robots.js +7 -7
- package/SECURITY.md +11 -0
- package/package.json +18 -5
- package/test/Robots.js +864 -0
package/.gitignore
ADDED
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## Version 3.0.1
|
|
4
|
+
|
|
5
|
+
- Fixed bug with `https:` URLs defaulting to port `80` instead of `443` if no port is specified.
|
|
6
|
+
Thanks to @dskvr for reporting
|
|
7
|
+
|
|
8
|
+
This affects comparing URLs with the default HTTPs port to URLs without it.
|
|
9
|
+
For example, comparing `https://example.com/` to `https://example.com:443/` or vice versa.
|
|
10
|
+
|
|
11
|
+
They should be treated as equivalent but weren't due to the incorrect port
|
|
12
|
+
being used for `https:`.
|
|
13
|
+
|
|
14
|
+
## Version 3.0.0
|
|
15
|
+
|
|
16
|
+
- Changed to using global URL object instead of importing. – Thanks to @brendankenny
|
|
17
|
+
|
|
18
|
+
## Version 2.4.0:
|
|
19
|
+
|
|
20
|
+
- Added Typescript definitions. – Thanks to @danhab99 for creating
|
|
21
|
+
- Added SECURITY.md policy and CodeQL scanning
|
|
22
|
+
|
|
23
|
+
## Version 2.3.0:
|
|
24
|
+
|
|
25
|
+
- Fixed bug where if the user-agent passed to `isAllowed()` / `isDisallowed()` is called "constructor" it would throw an error.
|
|
26
|
+
- Added support for relative URLs. This does not affect the default behavior so can safely be upgraded.
|
|
27
|
+
|
|
28
|
+
Relative matching is only allowed if both the robots.txt URL and the URLs being checked are relative.
|
|
29
|
+
|
|
30
|
+
For example:
|
|
31
|
+
|
|
32
|
+
```js
|
|
33
|
+
var robots = robotsParser('/robots.txt', [
|
|
34
|
+
'User-agent: *',
|
|
35
|
+
'Disallow: /dir/',
|
|
36
|
+
'Disallow: /test.html',
|
|
37
|
+
'Allow: /dir/test.html',
|
|
38
|
+
'Allow: /test.html'
|
|
39
|
+
].join('\n'));
|
|
40
|
+
|
|
41
|
+
robots.isAllowed('/test.html', 'Sams-Bot/1.0'); // false
|
|
42
|
+
robots.isAllowed('/dir/test.html', 'Sams-Bot/1.0'); // true
|
|
43
|
+
robots.isDisallowed('/dir/test2.html', 'Sams-Bot/1.0'); // true
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Version 2.2.0:
|
|
47
|
+
|
|
48
|
+
- Fixed bug that with matching wildcard patterns with some URLs
|
|
49
|
+
– Thanks to @ckylape for reporting and fixing
|
|
50
|
+
- Changed matching algorithm to match Google's implementation in google/robotstxt
|
|
51
|
+
- Changed order of precedence to match current spec
|
|
52
|
+
|
|
53
|
+
## Version 2.1.1:
|
|
54
|
+
|
|
55
|
+
- Fix bug that could be used to causing rule checking to take a long time
|
|
56
|
+
– Thanks to @andeanfog
|
|
57
|
+
|
|
58
|
+
## Version 2.1.0:
|
|
59
|
+
|
|
60
|
+
- Removed use of punycode module API's as new URL API handles it
|
|
61
|
+
- Improved test coverage
|
|
62
|
+
- Added tests for percent encoded paths and improved support
|
|
63
|
+
- Added `getMatchingLineNumber()` method
|
|
64
|
+
- Fixed bug with comments on same line as directive
|
|
65
|
+
|
|
66
|
+
## Version 2.0.0:
|
|
67
|
+
|
|
68
|
+
This release is not 100% backwards compatible as it now uses the new URL APIs which are not supported in Node < 7.
|
|
69
|
+
|
|
70
|
+
- Update code to not use deprecated URL module API's.
|
|
71
|
+
– Thanks to @kdzwinel
|
|
72
|
+
|
|
73
|
+
## Version 1.0.2:
|
|
74
|
+
|
|
75
|
+
- Fixed error caused by invalid URLs missing the protocol.
|
|
76
|
+
|
|
77
|
+
## Version 1.0.1:
|
|
78
|
+
|
|
79
|
+
- Fixed bug with the "user-agent" rule being treated as case sensitive.
|
|
80
|
+
– Thanks to @brendonboshell
|
|
81
|
+
- Improved test coverage.
|
|
82
|
+
– Thanks to @schornio
|
|
83
|
+
|
|
84
|
+
## Version 1.0.0:
|
|
85
|
+
|
|
86
|
+
- Initial release.
|
|
File without changes
|
package/{readme.md → README.md}
RENAMED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# Robots Parser [](https://www.npmjs.com/package/robots-parser) [](https://deepscan.io/dashboard#view=project&tid=457&pid=16277&bid=344939) [](https://github.com/samclarke/robots-parser/blob/master/license.md) [](https://coveralls.io/github/samclarke/robots-parser?branch=master)
|
|
2
2
|
|
|
3
|
-
|
|
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
|
-
|
|
5
|
+
The parser currently supports:
|
|
6
6
|
|
|
7
7
|
- User-agent:
|
|
8
8
|
- Allow:
|
|
@@ -94,6 +94,21 @@ 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.1
|
|
98
|
+
|
|
99
|
+
- Fixed bug with `https:` URLs defaulting to port `80` instead of `443` if no port is specified.
|
|
100
|
+
Thanks to @dskvr for reporting
|
|
101
|
+
|
|
102
|
+
This affects comparing URLs with the default HTTPs port to URLs without it.
|
|
103
|
+
For example, comparing `https://example.com/` to `https://example.com:443/` or vice versa.
|
|
104
|
+
|
|
105
|
+
They should be treated as equivalent but weren't due to the incorrect port
|
|
106
|
+
being used for `https:`.
|
|
107
|
+
|
|
108
|
+
### Version 3.0.0
|
|
109
|
+
|
|
110
|
+
- Changed to using global URL object instead of importing. – Thanks to @brendankenny
|
|
111
|
+
|
|
97
112
|
### Version 2.4.0:
|
|
98
113
|
|
|
99
114
|
- 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
|
*
|
|
@@ -276,7 +274,13 @@ function parseUrl(url) {
|
|
|
276
274
|
// Using non-existent subdomain so can never cause conflict unless
|
|
277
275
|
// trying to crawl it but doesn't exist and even if tried worst that can
|
|
278
276
|
// happen is it allows relative URLs on it.
|
|
279
|
-
|
|
277
|
+
var url = new URL(url, 'http://robots-relative.samclarke.com/');
|
|
278
|
+
|
|
279
|
+
if (!url.port) {
|
|
280
|
+
url.port = url.protocol === 'https:' ? 443 : 80;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
return url;
|
|
280
284
|
} catch (e) {
|
|
281
285
|
return null;
|
|
282
286
|
}
|
|
@@ -284,8 +288,6 @@ function parseUrl(url) {
|
|
|
284
288
|
|
|
285
289
|
function Robots(url, contents) {
|
|
286
290
|
this._url = parseUrl(url) || {};
|
|
287
|
-
this._url.port = this._url.port || 80;
|
|
288
|
-
|
|
289
291
|
this._rules = Object.create(null);
|
|
290
292
|
this._sitemaps = [];
|
|
291
293
|
this._preferredHost = null;
|
|
@@ -363,8 +365,6 @@ Robots.prototype._getRule = function (url, ua) {
|
|
|
363
365
|
var parsedUrl = parseUrl(url) || {};
|
|
364
366
|
var userAgent = formatUserAgent(ua || '*');
|
|
365
367
|
|
|
366
|
-
parsedUrl.port = parsedUrl.port || 80;
|
|
367
|
-
|
|
368
368
|
// The base URL must match otherwise this robots.txt is not valid for it.
|
|
369
369
|
if (
|
|
370
370
|
parsedUrl.protocol !== this._url.protocol ||
|
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": "
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "3.0.1",
|
|
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": ">=10.0.0"
|
|
44
|
+
},
|
|
32
45
|
"prettier": {
|
|
33
46
|
"tabWidth": 4,
|
|
34
47
|
"useTabs": true,
|
|
@@ -36,8 +49,8 @@
|
|
|
36
49
|
"trailingComma": "none"
|
|
37
50
|
},
|
|
38
51
|
"devDependencies": {
|
|
39
|
-
"chai": "^4.3.
|
|
40
|
-
"mocha": "^
|
|
52
|
+
"chai": "^4.3.6",
|
|
53
|
+
"mocha": "^10.0.0",
|
|
41
54
|
"nyc": "^15.1.0"
|
|
42
55
|
},
|
|
43
56
|
"types": "./index.d.ts"
|
package/test/Robots.js
ADDED
|
@@ -0,0 +1,864 @@
|
|
|
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
|
+
|
|
795
|
+
it('should honor given port number', function () {
|
|
796
|
+
var contents = [
|
|
797
|
+
'User-agent: *',
|
|
798
|
+
'Disallow: /fish/',
|
|
799
|
+
'Disallow: /test.html'
|
|
800
|
+
].join('\n');
|
|
801
|
+
|
|
802
|
+
var allowed = [
|
|
803
|
+
'http://www.example.com:8080/fish',
|
|
804
|
+
'http://www.example.com:8080/Test.html'
|
|
805
|
+
];
|
|
806
|
+
|
|
807
|
+
var disallowed = [
|
|
808
|
+
'http://www.example.com/fish',
|
|
809
|
+
'http://www.example.com/Test.html',
|
|
810
|
+
'http://www.example.com:80/fish',
|
|
811
|
+
'http://www.example.com:80/Test.html'
|
|
812
|
+
];
|
|
813
|
+
|
|
814
|
+
testRobots('http://www.example.com:8080/robots.txt', contents, allowed, disallowed);
|
|
815
|
+
});
|
|
816
|
+
|
|
817
|
+
it('should default to port 80 for http: if no port given', function () {
|
|
818
|
+
var contents = [
|
|
819
|
+
'User-agent: *',
|
|
820
|
+
'Disallow: /fish/',
|
|
821
|
+
'Disallow: /test.html'
|
|
822
|
+
].join('\n');
|
|
823
|
+
|
|
824
|
+
var allowed = [
|
|
825
|
+
'http://www.example.com:80/fish',
|
|
826
|
+
'http://www.example.com:80/Test.html'
|
|
827
|
+
];
|
|
828
|
+
|
|
829
|
+
var disallowed = [
|
|
830
|
+
'http://www.example.com:443/fish',
|
|
831
|
+
'http://www.example.com:443/Test.html',
|
|
832
|
+
'http://www.example.com:80/fish/index.php',
|
|
833
|
+
'http://www.example.com:80/fish/',
|
|
834
|
+
'http://www.example.com:80/test.html'
|
|
835
|
+
];
|
|
836
|
+
|
|
837
|
+
testRobots('http://www.example.com/robots.txt', contents, allowed, disallowed);
|
|
838
|
+
});
|
|
839
|
+
|
|
840
|
+
it('should default to port 443 for https: if no port given', function () {
|
|
841
|
+
var contents = [
|
|
842
|
+
'User-agent: *',
|
|
843
|
+
'Disallow: /fish/',
|
|
844
|
+
'Disallow: /test.html'
|
|
845
|
+
].join('\n');
|
|
846
|
+
|
|
847
|
+
var allowed = [
|
|
848
|
+
'https://www.example.com:443/fish',
|
|
849
|
+
'https://www.example.com:443/Test.html',
|
|
850
|
+
'https://www.example.com/fish',
|
|
851
|
+
'https://www.example.com/Test.html'
|
|
852
|
+
];
|
|
853
|
+
|
|
854
|
+
var disallowed = [
|
|
855
|
+
'http://www.example.com:80/fish',
|
|
856
|
+
'http://www.example.com:80/Test.html',
|
|
857
|
+
'http://www.example.com:443/fish/index.php',
|
|
858
|
+
'http://www.example.com:443/fish/',
|
|
859
|
+
'http://www.example.com:443/test.html'
|
|
860
|
+
];
|
|
861
|
+
|
|
862
|
+
testRobots('https://www.example.com/robots.txt', contents, allowed, disallowed);
|
|
863
|
+
});
|
|
864
|
+
});
|