Repository navigation
Expand file tree
/
Copy pathtest.js
More file actions
169 lines (154 loc) · 4.97 KB
/
Copy pathtest.js
File metadata and controls
169 lines (154 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* Tests for the v2 crawler (crawl_v2.js), using the built-in node:test runner.
* The end-to-end test serves a small interlinked site (including a robots.txt)
* from a local HTTP server — no external network needed.
*
* Run: npm test (or: node --test test.js)
*/
'use strict';
const assert = require('node:assert/strict');
const fsp = require('node:fs/promises');
const http = require('node:http');
const os = require('node:os');
const path = require('node:path');
const { test } = require('node:test');
const {
crawl,
extractLinks,
normalizeForDedup,
shouldCrawl,
} = require('./crawl_v2.js');
const PAGES = {
'/robots.txt': {
type: 'text/plain',
body: 'User-agent: *\nDisallow: /private\n',
},
'/': {
type: 'text/html',
body:
'<a href="/a">a</a> <a href="/b">b</a> <a href="/a">dup</a> ' +
'<a href="/assets/app.css">css</a> <a href="/private/secret">private</a>',
},
'/a': {
type: 'text/html',
body: '<a href="/b">b</a> <a href="/c">c</a> <a href="mailto:x@y.z">m</a>',
},
'/b': {
type: 'text/html',
body: '<a href="/">home</a> <a href="/missing">missing</a>',
},
'/c': { type: 'text/html', body: 'done' },
'/private/secret': {
type: 'text/html',
body: '<a href="/d">should never be fetched</a>',
},
};
function startServer() {
return new Promise((resolve) => {
const server = http.createServer((req, res) => {
const page = PAGES[req.url];
if (!page) {
res.writeHead(404).end();
return;
}
res.writeHead(200, { 'content-type': page.type }).end(page.body);
});
server.listen(0, '127.0.0.1', () => resolve(server));
});
}
test('extractLinks resolves relative and absolute hrefs and filters junk', () => {
const base = new URL('https://example.com/blog/');
const html = `
<a href="/about">About</a>
<a href="post-1">Post</a>
<a href="https://other.example/x">Other</a>
<a href="mailto:hi@example.com">Mail</a>
<a href="javascript:void(0)">JS</a>
<a href="styles.css">CSS</a>
<a name="no-href">Anchor</a>`;
const links = extractLinks(html, base).map(String);
assert.deepEqual(links, [
'https://example.com/about',
'https://example.com/blog/post-1',
'https://other.example/x',
]);
});
test('shouldCrawl filters schemes and asset extensions', () => {
const ok = (s) => shouldCrawl(new URL(s));
assert.equal(ok('https://example.com/page'), true);
assert.equal(ok('http://example.com/'), true);
assert.equal(ok('ftp://example.com/file'), false);
assert.equal(ok('https://example.com/image.PNG'), false);
assert.equal(ok('https://example.com/app.js'), false);
assert.equal(ok('https://example.com/movie.mp4'), false);
});
test('normalizeForDedup treats fragment/trailing-slash variants as one page', () => {
const a = normalizeForDedup(new URL('https://example.com/a/'));
const b = normalizeForDedup(new URL('https://example.com/a#section'));
const c = normalizeForDedup(new URL('https://example.com/a'));
assert.equal(a, b);
assert.equal(b, c);
});
test('crawls a local site end to end', async () => {
const server = await startServer();
const { port } = server.address();
const outputFilePath = path.join(
os.tmpdir(),
`crawl_v2_e2e_${process.pid}_${Date.now()}.txt`,
);
try {
const report = await crawl({
seedLink: `http://127.0.0.1:${port}/`,
outputFilePath,
maxPages: 10,
concurrency: 4,
requestTimeoutMs: 5000,
respectRobots: true,
quiet: true,
});
// "/", "/a", "/b", "/c" succeed; "/missing" 404s; "/private/secret" is
// blocked by robots.txt; the .css link is filtered before fetching.
assert.equal(report.pagesCrawled, 4);
assert.equal(report.pagesFailed, 1);
assert.equal(report.skippedByRobots, 1);
const written = await fsp.readFile(outputFilePath, 'utf8');
const lines = written.split('\n').filter(Boolean);
assert.equal(lines.length, 4);
assert.equal(
new Set(lines).size,
lines.length,
'output contains duplicate URLs',
);
assert.ok(!written.includes('.css'), 'stylesheets must not be crawled');
assert.ok(!written.includes('/private'), 'robots-disallowed pages must not be crawled');
} finally {
server.close();
await fsp.rm(outputFilePath, { force: true });
}
});
test('honors the maxPages budget', async () => {
const server = await startServer();
const { port } = server.address();
const outputFilePath = path.join(
os.tmpdir(),
`crawl_v2_budget_${process.pid}_${Date.now()}.txt`,
);
try {
const report = await crawl({
seedLink: `http://127.0.0.1:${port}/`,
outputFilePath,
maxPages: 2,
concurrency: 4,
requestTimeoutMs: 5000,
respectRobots: false,
quiet: true,
});
assert.ok(
report.pagesCrawled + report.pagesFailed <= 2,
`crawled ${report.pagesCrawled} + failed ${report.pagesFailed} exceeds budget`,
);
} finally {
server.close();
await fsp.rm(outputFilePath, { force: true });
}
});