@whotracksme/reporting 5.1.17
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/.editorconfig +12 -0
- package/.eslintignore +3 -0
- package/.eslintrc +16 -0
- package/.github/workflows/release.yml +34 -0
- package/.github/workflows/tests.yml +20 -0
- package/.prettierrc +9 -0
- package/.tool-versions +1 -0
- package/CHANGELOG.md +795 -0
- package/LICENSE +373 -0
- package/README.md +1 -0
- package/communication/README.md +11 -0
- package/communication/example/index.js +43 -0
- package/communication/example/manifest.json +16 -0
- package/communication/karma.conf.cjs +19 -0
- package/communication/package.json +52 -0
- package/communication/rollup.common-config.cjs +17 -0
- package/communication/rollup.config.cjs +10 -0
- package/communication/src/encoding.js +54 -0
- package/communication/src/errors.js +84 -0
- package/communication/src/index.js +76 -0
- package/communication/src/logger.js +41 -0
- package/communication/src/padding.js +60 -0
- package/communication/src/proxied-http.js +231 -0
- package/communication/src/server-public-key-accessor.js +123 -0
- package/communication/src/timestamps.js +37 -0
- package/communication/src/trusted-clock.js +58 -0
- package/communication/src/utils.js +28 -0
- package/communication/src/zlib.js +17 -0
- package/communication/test/helpers/in-memory-database.js +43 -0
- package/communication/test/server-public-key-accessor.spec.js +135 -0
- package/communication/test/utils.spec.js +43 -0
- package/package.json +50 -0
- package/reporting/TODO.md +2 -0
- package/reporting/assets/request/bloom_filter.dat +0 -0
- package/reporting/assets/request/update.json +1 -0
- package/reporting/example/content.js +11 -0
- package/reporting/example/index.js +132 -0
- package/reporting/example/inspector/index.html +17 -0
- package/reporting/example/inspector/index.js +27 -0
- package/reporting/example/manifests/chromium.json +37 -0
- package/reporting/example/manifests/firefox.json +35 -0
- package/reporting/example/rules.json +62 -0
- package/reporting/example/setup.js +1 -0
- package/reporting/example/tests/test1.html +10 -0
- package/reporting/karma.conf.cjs +22 -0
- package/reporting/package.json +63 -0
- package/reporting/rollup.common-config.cjs +19 -0
- package/reporting/rollup.config.cjs +20 -0
- package/reporting/src/LRU.js +149 -0
- package/reporting/src/activity-estimator.js +298 -0
- package/reporting/src/alive-check.js +139 -0
- package/reporting/src/alive-message-generator.js +142 -0
- package/reporting/src/bloom-filters.js +345 -0
- package/reporting/src/country-provider.js +295 -0
- package/reporting/src/cyrb53.js +32 -0
- package/reporting/src/digest.js +33 -0
- package/reporting/src/doublefetch-page-handler.js +548 -0
- package/reporting/src/duplicate-detector.js +233 -0
- package/reporting/src/errors.js +84 -0
- package/reporting/src/event-listener-queue.js +121 -0
- package/reporting/src/fixed-size-cache.js +63 -0
- package/reporting/src/hash-detector.js +1526 -0
- package/reporting/src/html-parser.js +17 -0
- package/reporting/src/http.js +265 -0
- package/reporting/src/index.js +14 -0
- package/reporting/src/job-scheduler.js +912 -0
- package/reporting/src/logger.js +121 -0
- package/reporting/src/md5.js +334 -0
- package/reporting/src/message-sender.js +51 -0
- package/reporting/src/network.js +140 -0
- package/reporting/src/new-page-approver.js +119 -0
- package/reporting/src/page-aggregator.js +185 -0
- package/reporting/src/page-quorum-check-handler.js +200 -0
- package/reporting/src/page-structure.js +136 -0
- package/reporting/src/pagedb.js +671 -0
- package/reporting/src/pages.js +1798 -0
- package/reporting/src/patterns-updater.js +310 -0
- package/reporting/src/patterns.js +416 -0
- package/reporting/src/persisted-bitarray.js +265 -0
- package/reporting/src/persisted-hashes.js +189 -0
- package/reporting/src/quorum-checker.js +327 -0
- package/reporting/src/random.js +39 -0
- package/reporting/src/reporting.js +290 -0
- package/reporting/src/request/config.js +116 -0
- package/reporting/src/request/content-script.js +77 -0
- package/reporting/src/request/database.js +147 -0
- package/reporting/src/request/dnt.js +24 -0
- package/reporting/src/request/hash.js +101 -0
- package/reporting/src/request/index.js +874 -0
- package/reporting/src/request/page-telemetry.js +42 -0
- package/reporting/src/request/qs-whitelist2.js +247 -0
- package/reporting/src/request/resources/prob.js +601 -0
- package/reporting/src/request/steps/block-rules.js +40 -0
- package/reporting/src/request/steps/check-context.js +37 -0
- package/reporting/src/request/steps/cookie-context.js +209 -0
- package/reporting/src/request/steps/oauth-detector.js +101 -0
- package/reporting/src/request/steps/page-logger.js +177 -0
- package/reporting/src/request/steps/redirect-tagger.js +75 -0
- package/reporting/src/request/steps/subdomain-check.js +34 -0
- package/reporting/src/request/steps/token-checker/index.js +189 -0
- package/reporting/src/request/steps/token-checker/token-domain.js +129 -0
- package/reporting/src/request/steps/token-examiner.js +182 -0
- package/reporting/src/request/steps/token-telemetry/cached-entry-pipeline.js +210 -0
- package/reporting/src/request/steps/token-telemetry/index.js +234 -0
- package/reporting/src/request/steps/token-telemetry/key-pipeline.js +109 -0
- package/reporting/src/request/steps/token-telemetry/token-pipeline.js +105 -0
- package/reporting/src/request/time.js +73 -0
- package/reporting/src/request/utils/bloom-filter-packed.js +107 -0
- package/reporting/src/request/utils/chrome-storage-map.js +282 -0
- package/reporting/src/request/utils/subject.js +37 -0
- package/reporting/src/request/utils/temp-set.js +56 -0
- package/reporting/src/request/utils/tlds.js +55 -0
- package/reporting/src/request/utils.js +41 -0
- package/reporting/src/request-reporter.js +111 -0
- package/reporting/src/sanitizer.js +519 -0
- package/reporting/src/search-extractor.js +338 -0
- package/reporting/src/self-check.js +105 -0
- package/reporting/src/seq-executor.js +43 -0
- package/reporting/src/session-storage.js +399 -0
- package/reporting/src/url-analyzer.js +161 -0
- package/reporting/src/url-cleaner.js +60 -0
- package/reporting/src/utils/pacemaker.js +279 -0
- package/reporting/src/utils/url.js +114 -0
- package/reporting/src/utils.js +345 -0
- package/reporting/src/webrequest-pipeline/cname-uncloak.js +110 -0
- package/reporting/src/webrequest-pipeline/index.js +287 -0
- package/reporting/src/webrequest-pipeline/logger.js +14 -0
- package/reporting/src/webrequest-pipeline/page-store.js +282 -0
- package/reporting/src/webrequest-pipeline/page.js +140 -0
- package/reporting/src/webrequest-pipeline/pipeline.js +249 -0
- package/reporting/src/webrequest-pipeline/utils/webrequest.js +113 -0
- package/reporting/src/webrequest-pipeline/webrequest-context.js +153 -0
- package/reporting/test/README.md +2 -0
- package/reporting/test/activity-estimator.spec.js +356 -0
- package/reporting/test/alive-check.spec.js +156 -0
- package/reporting/test/alive-message-generator.spec.js +186 -0
- package/reporting/test/country-provider.spec.js +263 -0
- package/reporting/test/doublefetch-page-handler.spec.js +818 -0
- package/reporting/test/duplicate-detector.spec.js +689 -0
- package/reporting/test/helpers/dom-parsers.js +41 -0
- package/reporting/test/helpers/fake-session-storage.js +69 -0
- package/reporting/test/helpers/fast-check-utils.js +28 -0
- package/reporting/test/helpers/fetch-mock.js +57 -0
- package/reporting/test/helpers/in-memory-database.js +14 -0
- package/reporting/test/helpers/memory-map.js +72 -0
- package/reporting/test/helpers/pagedb-mocks.js +41 -0
- package/reporting/test/index.js +47 -0
- package/reporting/test/job-scheduler.spec.js +691 -0
- package/reporting/test/network.spec.js +72 -0
- package/reporting/test/page-aggregator.spec.js +104 -0
- package/reporting/test/page-quorum-check-handler.spec.js +86 -0
- package/reporting/test/pagedb.spec.js +250 -0
- package/reporting/test/pages.spec.js +693 -0
- package/reporting/test/patterns-updater.spec.js +446 -0
- package/reporting/test/patterns.spec.js +751 -0
- package/reporting/test/persisted-hashes.spec.js +169 -0
- package/reporting/test/reporting.spec.js +205 -0
- package/reporting/test/request/database.spec.js +59 -0
- package/reporting/test/request/hash.spec.js +108 -0
- package/reporting/test/request/index.spec.js +378 -0
- package/reporting/test/request/qs-whitelist2.spec.js +189 -0
- package/reporting/test/request/steps/check-context.spec.js +51 -0
- package/reporting/test/request/steps/oauth-detector.spec.js +339 -0
- package/reporting/test/request/test-pages.js +1246 -0
- package/reporting/test/request/utils/bloom-filter-packed.spec.js +37 -0
- package/reporting/test/request/utils/chrome-storage-map.spec.js +28 -0
- package/reporting/test/request/utils/tldts.spec.js +62 -0
- package/reporting/test/request/utils.spec.js +168 -0
- package/reporting/test/sanitizer.spec.js +1087 -0
- package/reporting/test/search-extractor.spec.js +590 -0
- package/reporting/test/seq-executor.spec.js +158 -0
- package/reporting/test/session-storage.spec.js +400 -0
- package/reporting/test/setup.js +24 -0
- package/reporting/test/url-analyzer.spec.js +451 -0
- package/reporting/test/url-cleaner.spec.js +102 -0
- package/reporting/test/utils/pacemaker.spec.js +222 -0
- package/reporting/test/utils/url.spec.js +660 -0
- package/reporting/test/utils.spec.js +299 -0
- package/reporting/test/webrequest-pipeline/pipeline.spec.js +251 -0
|
@@ -0,0 +1,590 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WhoTracks.Me
|
|
3
|
+
* https://whotracks.me/
|
|
4
|
+
*
|
|
5
|
+
* Copyright 2017-present Ghostery GmbH. All rights reserved.
|
|
6
|
+
*
|
|
7
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
8
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
9
|
+
* file, You can obtain one at http://mozilla.org/MPL/2.0
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { expect } from 'chai';
|
|
13
|
+
import SearchExtractor from '../src/search-extractor.js';
|
|
14
|
+
import {
|
|
15
|
+
allSupportedParsers,
|
|
16
|
+
mockDocumentWith,
|
|
17
|
+
} from './helpers/dom-parsers.js';
|
|
18
|
+
import { lookupBuiltinTransform } from '../src/patterns.js';
|
|
19
|
+
|
|
20
|
+
const EMPTY_HTML_PAGE = `
|
|
21
|
+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
|
22
|
+
<html lang="en">
|
|
23
|
+
<head>
|
|
24
|
+
<meta http-equiv="content-type" content="text/html; charset=utf-8">
|
|
25
|
+
<title>Test page</title>
|
|
26
|
+
</head>
|
|
27
|
+
<body>
|
|
28
|
+
</body>
|
|
29
|
+
</html>`;
|
|
30
|
+
|
|
31
|
+
async function fetchJson(url) {
|
|
32
|
+
const response = await fetch(url);
|
|
33
|
+
if (!response.ok) {
|
|
34
|
+
throw new Error(`Unable to reach endpoint: ${url}: ${response.statusText}`);
|
|
35
|
+
}
|
|
36
|
+
return response.json();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function fetchText(url) {
|
|
40
|
+
const response = await fetch(url);
|
|
41
|
+
if (!response.ok) {
|
|
42
|
+
throw new Error(`Unable to reach endpoint: ${url}: ${response.statusText}`);
|
|
43
|
+
}
|
|
44
|
+
return response.text();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async function loadTestFixtures(url) {
|
|
48
|
+
const { links } = await fetchJson(url);
|
|
49
|
+
expect(links).to.be.an('object');
|
|
50
|
+
|
|
51
|
+
const expectedOrigin = new URL(url).origin;
|
|
52
|
+
return Promise.all(
|
|
53
|
+
Object.entries(links).map(async ([name, fixtureUrl]) => {
|
|
54
|
+
expect(new URL(fixtureUrl).origin).to.eql(expectedOrigin);
|
|
55
|
+
const { scenario, html: htmlUrl } = await fetchJson(fixtureUrl);
|
|
56
|
+
expect(new URL(htmlUrl).origin).to.eql(expectedOrigin);
|
|
57
|
+
return {
|
|
58
|
+
name,
|
|
59
|
+
scenario,
|
|
60
|
+
htmlUrl,
|
|
61
|
+
};
|
|
62
|
+
}),
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
describe('#SearchExtractor', function () {
|
|
67
|
+
allSupportedParsers.forEach((htmlParser) => {
|
|
68
|
+
describe(`using ${htmlParser} as HTML parser`, function () {
|
|
69
|
+
function runScenario({
|
|
70
|
+
url,
|
|
71
|
+
query,
|
|
72
|
+
category,
|
|
73
|
+
ctry = '--',
|
|
74
|
+
html,
|
|
75
|
+
patterns,
|
|
76
|
+
mustContain = [],
|
|
77
|
+
mustNotContain = [],
|
|
78
|
+
}) {
|
|
79
|
+
try {
|
|
80
|
+
expect(url, '<url>').to.be.a('string');
|
|
81
|
+
expect(query, '<query>').to.be.a('string');
|
|
82
|
+
expect(category, '<category>').to.be.a('string');
|
|
83
|
+
expect(ctry, '<ctry>').to.be.a('string');
|
|
84
|
+
expect(html, '<html>').to.be.a('string');
|
|
85
|
+
expect(patterns, '<patterns>').to.be.an('object');
|
|
86
|
+
expect(mustContain, '<mustContain>').to.be.an('array');
|
|
87
|
+
expect(mustNotContain, '<mustNotContain>').to.be.an('array');
|
|
88
|
+
} catch (e) {
|
|
89
|
+
e.message = `Broken scenario detected (${e.message})`;
|
|
90
|
+
throw e;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const extractor = new SearchExtractor({
|
|
94
|
+
patterns: {
|
|
95
|
+
getRulesSnapshot() {
|
|
96
|
+
return patterns;
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
sanitizer: {
|
|
100
|
+
getSafeCountryCode() {
|
|
101
|
+
return ctry;
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
persistedHashes: {},
|
|
105
|
+
jobScheduler: {
|
|
106
|
+
registerHandler() {},
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
const { window: mockWindow, document: doc } =
|
|
110
|
+
mockDocumentWith[htmlParser](html);
|
|
111
|
+
let results;
|
|
112
|
+
try {
|
|
113
|
+
results = extractor.extractMessages({
|
|
114
|
+
doc,
|
|
115
|
+
query,
|
|
116
|
+
category,
|
|
117
|
+
doublefetchRequest: {
|
|
118
|
+
url,
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
} finally {
|
|
122
|
+
try {
|
|
123
|
+
mockWindow?.close();
|
|
124
|
+
} catch (e) {
|
|
125
|
+
// ignore
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const messages = {};
|
|
130
|
+
results.forEach((msg) => {
|
|
131
|
+
messages[msg.body.action] = [];
|
|
132
|
+
});
|
|
133
|
+
results.forEach((msg) => {
|
|
134
|
+
messages[msg.body.action].push(msg.body);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
try {
|
|
138
|
+
for (const check of mustContain) {
|
|
139
|
+
if (!messages[check.action]) {
|
|
140
|
+
throw new Error(`Missing message with action=${check.action}`);
|
|
141
|
+
}
|
|
142
|
+
expect(messages[check.action].length === 1);
|
|
143
|
+
const realPayload = messages[check.action][0].payload;
|
|
144
|
+
expect(
|
|
145
|
+
realPayload,
|
|
146
|
+
`found mismatch in ${check.action}`,
|
|
147
|
+
).to.deep.equal(check.payload);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
for (const check of mustNotContain) {
|
|
151
|
+
const unexpectedMatch = new RegExp(
|
|
152
|
+
`^${check.action.replaceAll('.', '[.]').replaceAll('*', '.*')}$`,
|
|
153
|
+
);
|
|
154
|
+
const matches = Object.keys(messages).filter((x) =>
|
|
155
|
+
unexpectedMatch.test(x),
|
|
156
|
+
);
|
|
157
|
+
if (matches.length > 0) {
|
|
158
|
+
throw new Error(
|
|
159
|
+
`Expected no messages with action '${check.action}' ` +
|
|
160
|
+
`but got messages for the following actions: [${matches}]`,
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
} catch (e) {
|
|
165
|
+
e.results = results;
|
|
166
|
+
throw e;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
it('should not throw on an empty page with empty patterns', function () {
|
|
171
|
+
runScenario({
|
|
172
|
+
url: 'http://example.test/x?q=foo',
|
|
173
|
+
query: 'foo',
|
|
174
|
+
category: 'test-action',
|
|
175
|
+
html: EMPTY_HTML_PAGE,
|
|
176
|
+
patterns: {},
|
|
177
|
+
mustNotContain: [{ action: '*' }],
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it('should extract some dummy test', function () {
|
|
182
|
+
runScenario({
|
|
183
|
+
url: 'http://example.test/x?q=some-query',
|
|
184
|
+
query: 'some-query',
|
|
185
|
+
category: 'example-test',
|
|
186
|
+
ctry: 'de',
|
|
187
|
+
html: `
|
|
188
|
+
<!DOCTYPE html>
|
|
189
|
+
<html>
|
|
190
|
+
<head>
|
|
191
|
+
<meta http-equiv="content-type" content="text/html; charset=utf-8">
|
|
192
|
+
<title>Test page</title>
|
|
193
|
+
</head>
|
|
194
|
+
<body>
|
|
195
|
+
<div id="foo" bar="Some text to extract"></div>
|
|
196
|
+
</body>
|
|
197
|
+
</html>`,
|
|
198
|
+
patterns: {
|
|
199
|
+
'example-test': {
|
|
200
|
+
input: {
|
|
201
|
+
'html body': {
|
|
202
|
+
first: {
|
|
203
|
+
textFromDiv: {
|
|
204
|
+
select: 'div#foo',
|
|
205
|
+
attr: 'bar',
|
|
206
|
+
},
|
|
207
|
+
},
|
|
208
|
+
},
|
|
209
|
+
},
|
|
210
|
+
output: {
|
|
211
|
+
'test-action': {
|
|
212
|
+
fields: [
|
|
213
|
+
{
|
|
214
|
+
key: 'textFromDiv',
|
|
215
|
+
source: 'html body',
|
|
216
|
+
},
|
|
217
|
+
{ key: 'q' },
|
|
218
|
+
{ key: 'qurl' },
|
|
219
|
+
{ key: 'ctry' },
|
|
220
|
+
],
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
},
|
|
224
|
+
},
|
|
225
|
+
mustContain: [
|
|
226
|
+
{
|
|
227
|
+
action: 'test-action',
|
|
228
|
+
payload: {
|
|
229
|
+
textFromDiv: 'Some text to extract',
|
|
230
|
+
q: 'some-query',
|
|
231
|
+
qurl: 'http://example.test/x?q=some-query',
|
|
232
|
+
ctry: 'de',
|
|
233
|
+
},
|
|
234
|
+
},
|
|
235
|
+
],
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it('should not double-encode links', function () {
|
|
240
|
+
// Note: this URL has an encoded Umlaut. Depending on the DOMParser and
|
|
241
|
+
// the mechanism to extract the URL (elem.href vs elem.getAttribute('href'),
|
|
242
|
+
// it could lead to unintended double-encoding.
|
|
243
|
+
const abslink =
|
|
244
|
+
'https://www.mediamarkt.at/de/product/_krups-espresso-siebtr%C3%A4germaschine-xp442c-silber-schwarz-1824085.html';
|
|
245
|
+
|
|
246
|
+
runScenario({
|
|
247
|
+
url: 'http://example.test/x?q=some-query',
|
|
248
|
+
query: 'some-query',
|
|
249
|
+
category: 'example-test',
|
|
250
|
+
ctry: 'de',
|
|
251
|
+
html: `
|
|
252
|
+
<!DOCTYPE html>
|
|
253
|
+
<html>
|
|
254
|
+
<head>
|
|
255
|
+
<meta http-equiv="content-type" content="text/html; charset=utf-8">
|
|
256
|
+
<title>Test page</title>
|
|
257
|
+
</head>
|
|
258
|
+
<body>
|
|
259
|
+
<a id="abslink" href="${abslink}"></a>
|
|
260
|
+
</body>
|
|
261
|
+
</html>`,
|
|
262
|
+
patterns: {
|
|
263
|
+
'example-test': {
|
|
264
|
+
input: {
|
|
265
|
+
'html body': {
|
|
266
|
+
first: {
|
|
267
|
+
abslink: {
|
|
268
|
+
select: '#abslink',
|
|
269
|
+
attr: 'href',
|
|
270
|
+
},
|
|
271
|
+
},
|
|
272
|
+
},
|
|
273
|
+
},
|
|
274
|
+
output: {
|
|
275
|
+
'test-action': {
|
|
276
|
+
fields: [
|
|
277
|
+
{
|
|
278
|
+
key: 'abslink',
|
|
279
|
+
source: 'html body',
|
|
280
|
+
},
|
|
281
|
+
],
|
|
282
|
+
},
|
|
283
|
+
},
|
|
284
|
+
},
|
|
285
|
+
},
|
|
286
|
+
mustContain: [
|
|
287
|
+
{
|
|
288
|
+
action: 'test-action',
|
|
289
|
+
payload: {
|
|
290
|
+
abslink,
|
|
291
|
+
},
|
|
292
|
+
},
|
|
293
|
+
],
|
|
294
|
+
});
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it('should resolve relative links based to the real URL, not extension ID', function () {
|
|
298
|
+
runScenario({
|
|
299
|
+
url: 'http://example.test/x?q=some-query',
|
|
300
|
+
query: 'some-query',
|
|
301
|
+
category: 'example-test',
|
|
302
|
+
ctry: 'de',
|
|
303
|
+
html: `
|
|
304
|
+
<!DOCTYPE html>
|
|
305
|
+
<html>
|
|
306
|
+
<head>
|
|
307
|
+
<meta http-equiv="content-type" content="text/html; charset=utf-8">
|
|
308
|
+
<title>Test page</title>
|
|
309
|
+
</head>
|
|
310
|
+
<body>
|
|
311
|
+
<a id="rellink" href="/foo?bar=42"></a>
|
|
312
|
+
</body>
|
|
313
|
+
</html>`,
|
|
314
|
+
patterns: {
|
|
315
|
+
'example-test': {
|
|
316
|
+
input: {
|
|
317
|
+
'html body': {
|
|
318
|
+
first: {
|
|
319
|
+
rellink: {
|
|
320
|
+
select: '#rellink',
|
|
321
|
+
attr: 'href',
|
|
322
|
+
},
|
|
323
|
+
},
|
|
324
|
+
},
|
|
325
|
+
},
|
|
326
|
+
output: {
|
|
327
|
+
'test-action': {
|
|
328
|
+
fields: [
|
|
329
|
+
{
|
|
330
|
+
key: 'rellink',
|
|
331
|
+
source: 'html body',
|
|
332
|
+
},
|
|
333
|
+
],
|
|
334
|
+
},
|
|
335
|
+
},
|
|
336
|
+
},
|
|
337
|
+
},
|
|
338
|
+
mustContain: [
|
|
339
|
+
{
|
|
340
|
+
action: 'test-action',
|
|
341
|
+
payload: {
|
|
342
|
+
rellink: 'http://example.test/foo?bar=42',
|
|
343
|
+
},
|
|
344
|
+
},
|
|
345
|
+
],
|
|
346
|
+
});
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
it('should fail with a permanent error if a primitive is not supported', function () {
|
|
350
|
+
const nonExistingPrimitive = 'thisBuiltinDoesNotExist';
|
|
351
|
+
expect(() => lookupBuiltinTransform(nonExistingPrimitive)).to.throw();
|
|
352
|
+
|
|
353
|
+
expect(() => {
|
|
354
|
+
runScenario({
|
|
355
|
+
url: 'http://example.test/x?q=some-query',
|
|
356
|
+
query: 'some-query',
|
|
357
|
+
category: 'example-test',
|
|
358
|
+
ctry: 'de',
|
|
359
|
+
html: `
|
|
360
|
+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
|
361
|
+
<html lang="en">
|
|
362
|
+
<head>
|
|
363
|
+
<meta http-equiv="content-type" content="text/html; charset=utf-8">
|
|
364
|
+
<title>Test page</title>
|
|
365
|
+
</head>
|
|
366
|
+
<body>
|
|
367
|
+
<div>Some text</div>
|
|
368
|
+
</body>
|
|
369
|
+
</html>`,
|
|
370
|
+
patterns: {
|
|
371
|
+
'example-test': {
|
|
372
|
+
input: {
|
|
373
|
+
'html > body > div': {
|
|
374
|
+
first: {
|
|
375
|
+
title: {
|
|
376
|
+
attr: 'textContent',
|
|
377
|
+
transform: [[nonExistingPrimitive]],
|
|
378
|
+
},
|
|
379
|
+
},
|
|
380
|
+
},
|
|
381
|
+
},
|
|
382
|
+
output: {
|
|
383
|
+
'test-action': {
|
|
384
|
+
fields: [
|
|
385
|
+
{
|
|
386
|
+
key: 'extractedText',
|
|
387
|
+
source: 'html > body > div',
|
|
388
|
+
},
|
|
389
|
+
],
|
|
390
|
+
},
|
|
391
|
+
},
|
|
392
|
+
},
|
|
393
|
+
},
|
|
394
|
+
mustNotContain: [{ action: '*' }],
|
|
395
|
+
});
|
|
396
|
+
})
|
|
397
|
+
.to.throw()
|
|
398
|
+
.with.property('isPermanentError');
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
describe('during preprocessing', function () {
|
|
402
|
+
it('should support removing an inner div', function () {
|
|
403
|
+
runScenario({
|
|
404
|
+
url: 'http://example.test/x?q=some-query',
|
|
405
|
+
query: 'some-query',
|
|
406
|
+
category: 'example-test',
|
|
407
|
+
ctry: 'de',
|
|
408
|
+
html: `
|
|
409
|
+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
|
410
|
+
<html lang="en">
|
|
411
|
+
<head>
|
|
412
|
+
<meta http-equiv="content-type" content="text/html; charset=utf-8">
|
|
413
|
+
<title>Test page</title>
|
|
414
|
+
</head>
|
|
415
|
+
<body>
|
|
416
|
+
<div>It does <div>NOT</div>work</div>
|
|
417
|
+
</body>
|
|
418
|
+
</html>`,
|
|
419
|
+
patterns: {
|
|
420
|
+
'example-test': {
|
|
421
|
+
preprocess: {
|
|
422
|
+
prune: [{ first: 'div > div' }],
|
|
423
|
+
},
|
|
424
|
+
input: {
|
|
425
|
+
'html > body > div': {
|
|
426
|
+
first: {
|
|
427
|
+
extractedText: {
|
|
428
|
+
attr: 'textContent',
|
|
429
|
+
},
|
|
430
|
+
},
|
|
431
|
+
},
|
|
432
|
+
},
|
|
433
|
+
output: {
|
|
434
|
+
'test-action': {
|
|
435
|
+
fields: [
|
|
436
|
+
{
|
|
437
|
+
key: 'extractedText',
|
|
438
|
+
source: 'html > body > div',
|
|
439
|
+
},
|
|
440
|
+
],
|
|
441
|
+
},
|
|
442
|
+
},
|
|
443
|
+
},
|
|
444
|
+
},
|
|
445
|
+
mustContain: [
|
|
446
|
+
{
|
|
447
|
+
action: 'test-action',
|
|
448
|
+
payload: {
|
|
449
|
+
extractedText: 'It does work',
|
|
450
|
+
},
|
|
451
|
+
},
|
|
452
|
+
],
|
|
453
|
+
});
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
it('should support support removing one or many elements (complex scenario)', function () {
|
|
457
|
+
runScenario({
|
|
458
|
+
url: 'http://example.test/x?q=some-query',
|
|
459
|
+
query: 'some-query',
|
|
460
|
+
category: 'example-test',
|
|
461
|
+
ctry: 'de',
|
|
462
|
+
html: `
|
|
463
|
+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
|
464
|
+
<html lang="en">
|
|
465
|
+
<head>
|
|
466
|
+
<meta http-equiv="content-type" content="text/html; charset=utf-8">
|
|
467
|
+
<title>Test page</title>
|
|
468
|
+
</head>
|
|
469
|
+
<body>
|
|
470
|
+
<p>Remove the X's: only the first p, but all divs</p>
|
|
471
|
+
<div>1<p id="remove-me">X</p>2<p id="but-keep-me">3</p>4<div>X</div><div>X</div>5<div>X</div>6</div>
|
|
472
|
+
</body>
|
|
473
|
+
</html>`,
|
|
474
|
+
patterns: {
|
|
475
|
+
'example-test': {
|
|
476
|
+
preprocess: {
|
|
477
|
+
prune: [{ first: 'div > p' }, { all: 'div > div' }],
|
|
478
|
+
},
|
|
479
|
+
input: {
|
|
480
|
+
'html > body > div': {
|
|
481
|
+
first: {
|
|
482
|
+
extractedText: {
|
|
483
|
+
attr: 'textContent',
|
|
484
|
+
},
|
|
485
|
+
},
|
|
486
|
+
},
|
|
487
|
+
},
|
|
488
|
+
output: {
|
|
489
|
+
'test-action': {
|
|
490
|
+
fields: [
|
|
491
|
+
{
|
|
492
|
+
key: 'extractedText',
|
|
493
|
+
source: 'html > body > div',
|
|
494
|
+
},
|
|
495
|
+
],
|
|
496
|
+
},
|
|
497
|
+
},
|
|
498
|
+
},
|
|
499
|
+
},
|
|
500
|
+
mustContain: [
|
|
501
|
+
{
|
|
502
|
+
action: 'test-action',
|
|
503
|
+
payload: {
|
|
504
|
+
extractedText: '123456',
|
|
505
|
+
},
|
|
506
|
+
},
|
|
507
|
+
],
|
|
508
|
+
});
|
|
509
|
+
});
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
// This are optional tests that can be enabled if the TEST_FIXTURES_URL
|
|
513
|
+
// environment variable is defined. Karma lacks access to the filesystem,
|
|
514
|
+
// but we can fetch fixtures over the network.
|
|
515
|
+
//
|
|
516
|
+
// eslint-disable-next-line no-undef
|
|
517
|
+
const testFixturesUrl = __karma__.config.TEST_FIXTURES_URL;
|
|
518
|
+
if (testFixturesUrl) {
|
|
519
|
+
it('passes remote tests', async function () {
|
|
520
|
+
this.timeout(10000000);
|
|
521
|
+
const maxRequests = 3;
|
|
522
|
+
const fixtures = await loadTestFixtures(testFixturesUrl);
|
|
523
|
+
const fixtures2 = [...fixtures];
|
|
524
|
+
const prefetchHtml = () => {
|
|
525
|
+
const next = fixtures2.shift();
|
|
526
|
+
if (next) {
|
|
527
|
+
next.pendingHtml = fetchText(next.htmlUrl);
|
|
528
|
+
}
|
|
529
|
+
};
|
|
530
|
+
for (let i = 0; i < maxRequests - 1; i += 1) {
|
|
531
|
+
prefetchHtml();
|
|
532
|
+
}
|
|
533
|
+
let testsPassed = [];
|
|
534
|
+
let testsFailed = [];
|
|
535
|
+
try {
|
|
536
|
+
console.log(
|
|
537
|
+
`Running test fixtures: ${fixtures.length} tests found`,
|
|
538
|
+
);
|
|
539
|
+
while (fixtures.length > 0) {
|
|
540
|
+
prefetchHtml();
|
|
541
|
+
const { name, scenario, pendingHtml } = fixtures.shift();
|
|
542
|
+
scenario.html = await pendingHtml;
|
|
543
|
+
try {
|
|
544
|
+
scenario.category = scenario.category || scenario.type; // TODO: remove after migrating tests
|
|
545
|
+
runScenario(scenario);
|
|
546
|
+
testsPassed.push(name);
|
|
547
|
+
console.log(`test ${name}: PASSED`);
|
|
548
|
+
} catch (e) {
|
|
549
|
+
testsFailed.push({ name, error: e });
|
|
550
|
+
e.message = `Test fixture <${name}> failed: ${e}`;
|
|
551
|
+
console.error(e);
|
|
552
|
+
if (e.results) {
|
|
553
|
+
console.log(`These were the extracted messages:\n
|
|
554
|
+
----------------------------------------------------------------------
|
|
555
|
+
${JSON.stringify(e.results, null, 2)}
|
|
556
|
+
----------------------------------------------------------------------
|
|
557
|
+
`);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
if (testsFailed.length > 0) {
|
|
562
|
+
throw testsFailed[0].error;
|
|
563
|
+
}
|
|
564
|
+
} finally {
|
|
565
|
+
const passed = testsPassed.length;
|
|
566
|
+
const failed = testsFailed.length;
|
|
567
|
+
const total = testsPassed.length + testsFailed.length;
|
|
568
|
+
if (total > 0) {
|
|
569
|
+
const ratio = ((100 * passed) / total).toFixed(2);
|
|
570
|
+
console.log(
|
|
571
|
+
`
|
|
572
|
+
****************** remote test results *************
|
|
573
|
+
${total} tests run: ${passed} passed, ${failed} failed (${ratio}% passed)
|
|
574
|
+
****************************************************
|
|
575
|
+
`,
|
|
576
|
+
);
|
|
577
|
+
if (failed > 0) {
|
|
578
|
+
console.warn(
|
|
579
|
+
`The following tests failed:\n${testsFailed
|
|
580
|
+
.map((x) => `* ${x.error}`)
|
|
581
|
+
.join('\n')}\n\n`,
|
|
582
|
+
);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
});
|
|
589
|
+
});
|
|
590
|
+
});
|