Repository navigation
Expand file tree
/
Copy pathenv.js
More file actions
676 lines (605 loc) · 21.3 KB
/
Copy pathenv.js
File metadata and controls
676 lines (605 loc) · 21.3 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
"use strict";
const assert = require("chai").assert;
const describe = require("mocha-sugar-free").describe;
const specify = require("mocha-sugar-free").specify;
const env = require("../..").env;
const createVirtualConsole = require("../..").createVirtualConsole;
const serializeDocument = require("../..").serializeDocument;
const path = require("path");
const http = require("http");
const toFileUrl = require("../util").toFileUrl(__dirname);
describe("jsdom/env", () => {
specify("with invalid arguments", () => {
assert.throws(() => env());
assert.throws(() => env({}));
assert.throws(() => env({ html: "abc123" }));
assert.throws(() => env({ done() { /* doesn't matter */ } }));
});
specify("explicit config.html, full document", { async: true }, t => {
env({
html: "<!DOCTYPE html><html><head><title>Hi</title></head><body>Hello</body></html>",
url: "http://example.com/",
done(err, window) {
assert.ifError(err);
assert.equal(serializeDocument(window.document),
"<!DOCTYPE html><html><head><title>Hi</title></head><body>Hello</body></html>");
assert.equal(window.location.href, "http://example.com/");
assert.equal(window.location.origin, "http://example.com");
t.done();
}
});
});
specify("explicit config.html, with overridden config.url", { async: true }, t => {
env({
html: "Hello",
url: "http://example.com/",
done(err, window) {
assert.ifError(err);
assert.equal(serializeDocument(window.document), "<html><head></head><body>Hello</body></html>");
assert.equal(window.location.href, "http://example.com/");
assert.equal(window.location.origin, "http://example.com");
assert.equal(window.location.search, "");
assert.equal(window.location.hash, "");
t.done();
}
});
});
specify("explicit config.html, with overridden config.url including hash", { async: true }, t => {
env({
html: "Hello",
url: "http://example.com/#foo",
done(err, window) {
assert.ifError(err);
assert.equal(window.location.hash, "#foo");
t.done();
}
});
});
specify("explicit config.html, with overridden config.url including search and hash", { async: true }, t => {
env({
html: "Hello",
url: "http://example.com/?foo=bar#baz",
done(err, window) {
assert.ifError(err);
assert.equal(window.location.search, "?foo=bar");
assert.equal(window.location.hash, "#baz");
t.done();
}
});
});
specify("explicit config.html, without a config.url", { async: true }, t => {
env({
html: "<html><head></head><body><p>hello world!</p></body></html>",
done(err, window) {
assert.ifError(err);
assert.notEqual(window.location.href, null);
t.done();
}
});
});
specify("explicit config.html, with poorly formatted HTML", { async: true }, t => {
env({
html: "some poorly<div>formed<b>html</div> fragment",
done(err, window) {
assert.ifError(err);
assert.notEqual(window.location.href, null);
t.done();
}
});
});
specify("explicit config.html, with a relative frame URL but no document URL (GH-1277)", { async: true }, t => {
env({
html: `<iframe src="/ads/728x90.html"></iframe>`,
done(err, window) {
assert.ifError(err);
// Unresolvable URLs (in this case a relative URL against about:blank) should create windows for about:blank.
assert.equal(window.document.querySelector("iframe").src, "/ads/728x90.html");
assert.equal(window.document.querySelector("iframe").contentWindow.document.URL, "about:blank");
t.done();
}
});
});
specify("explicit config.html, a string that is also a valid URL", { async: true }, t => {
env({
html: "http://example.com/",
url: "http://example.com/",
done(err, window) {
assert.ifError(err);
assert.equal(serializeDocument(window.document), "<html><head></head><body>http://example.com/</body></html>");
assert.equal(window.location.href, "http://example.com/");
t.done();
}
});
});
specify("explicit config.html, an empty string", { async: true }, t => {
env({
html: "",
created() {
t.done();
}
});
});
specify("string, full HTML document", { async: true }, t => {
env(
"
8000
;<!DOCTYPE html><html><head><title>Hi</title></head><body>Hello</body></html>",
(err, window) => {
assert.ifError(err);
assert.equal(serializeDocument(window.document),
"<!DOCTYPE html><html><head><title>Hi</title></head><body>Hello</body></html>");
t.done();
}
);
});
specify("string, HTML content with a null byte", { async: true }, t => {
env(
"<div>\0</div>",
(err, window) => {
assert.ifError(err);
assert.ok(window.document.querySelector("div") !== null, "div was parsed");
t.done();
}
);
});
specify("with src", { async: true }, t => {
env({
html: "<!DOCTYPE html><html><head></head><body><p>hello world!</p></body></html>",
src: "window.attachedHere = 123;",
done(err, window) {
assert.ifError(err);
assert.ok(window.location.href);
assert.equal(window.attachedHere, 123);
assert.equal(window.document.getElementsByTagName("p").item(0).innerHTML, "hello world!");
t.done();
}
});
});
specify("with document referrer", { async: true }, t => {
env({
html: "<!DOCTYPE html><html><head></head><body><p>hello world!</p></body></html>",
document: { referrer: "https://github.com/tmpvar/jsdom" },
done(err, window) {
assert.ifError(err);
assert.equal(window.document.referrer, "https://github.com/tmpvar/jsdom");
t.done();
}
});
});
specify("should call callbacks correctly", { async: true }, t => {
env({
html: "<!DOCTYPE html><html><head><script>window.isExecuted = true;" +
"window.wasCreatedSet = window.isCreated;</script></head><body></body></html>",
features: {
FetchExternalResources: ["script"],
ProcessExternalResources: ["script"],
SkipExternalResources: false
},
created(err, window) {
assert.ifError(err);
assert.notEqual(window.isExecuted, true);
assert.strictEqual(window.wasCreatedSet, undefined);
window.isCreated = true;
},
onload(window) {
assert.strictEqual(window.isCreated, true);
assert.strictEqual(window.isExecuted, true);
assert.strictEqual(window.wasCreatedSet, true);
},
done(err, window) {
assert.ifError(err);
assert.strictEqual(window.isCreated, true);
assert.strictEqual(window.isExecuted, true);
assert.strictEqual(window.wasCreatedSet, true);
t.done();
}
});
});
specify("with configurable resource loader", { async: true }, t => {
env({
html: "<!DOCTYPE html><html><head><script src='foo.js'></script></head><body></body></html>",
resourceLoader(resource, callback) {
callback(null, "window.resourceLoaderWasOverridden = true;");
},
features: {
FetchExternalResources: ["script"],
ProcessExternalResources: ["script"],
SkipExternalResources: false
},
done(err, window) {
assert.ifError(err);
assert.strictEqual(window.resourceLoaderWasOverridden, true);
t.done();
}
});
});
specify("with configurable resource loader and iframe", { async: true }, t => {
const routes = {
"/iframe.html": "<html><head><script src='foo.js'></script></head><body></body></html>",
"/foo.js": "window.resourceLoaderWasOverridden = true;"
};
env({
html: "<html><head></head><body><iframe id='bar' src='http://localhost/iframe.html'></iframe></body></html>",
resourceLoader(resource, callback) {
const response = routes[resource.url.path];
assert.ok(response, `Not found: ${resource.url.path}`);
callback(null, response);
},
features: {
FetchExternalResources: ["script", "iframe"],
ProcessExternalResources: ["script"],
SkipExternalResources: false
},
done(err, window) {
assert.ifError(err);
assert.strictEqual(window.frames.bar.resourceLoaderWasOverridden, true);
t.done();
}
});
});
specify("done should be called only once, after all src scripts
8000
have executed", { async: true }, t => {
const script = "window.a = (typeof window.a !== 'undefined') ? window.a + 1 : 0;";
let doneCounter = 0;
env({
html: "<div></div>",
src: [script, script, script],
ProcessExternalResources: ["script"],
done(err, window) {
assert.ifError(err);
++doneCounter;
assert.strictEqual(window.a, 2);
assert.strictEqual(doneCounter, 1);
t.done();
}
});
});
specify("window instances should be initialized when provided to callbacks", { async: true }, t => {
env({
html: "<div></div>",
features: {
ProcessExternalResources: ["script"]
},
created(err, window) {
assert.ifError(err);
assert.notEqual(window.Array, undefined);
},
onload(window) {
assert.notEqual(window.Array, undefined);
},
done(err, window) {
assert.ifError(err);
assert.notEqual(window.Array, undefined);
t.done();
}
});
});
specify("window instances provided to callbacks always refer to the same object", { async: true }, t => {
const instances = [];
function finish() {
for (let i = 0; i < instances.length; ++i) {
for (let j = 0; j < i; ++j) {
assert.strictEqual(instances[i], instances[j], `instances ${i} and ${j} should be equal`);
}
}
t.done();
}
env({
html: "<div></div>",
features: {
ProcessExternalResources: ["script"]
},
created(err, window) {
assert.ifError(err);
instances.push(window);
},
onload(window) {
instances.push(window);
},
done(err, window) {
assert.ifError(err);
instances.push(window);
finish();
}
});
});
specify("explicit config.url, invalid", { async: true }, t => {
env({
// Use 0.0.0.0 because it will always fail, and without a timeout
// (which would slow down the test suite)
url: "http://0.0.0.0:8976",
done(err, window) {
assert.ok(err, "an error should exist");
assert.strictEqual(window, undefined, "window should not exist");
t.done();
}
});
});
specify("string, parseable as a URL, invalid", { async: true }, t => {
env(
"http://0.0.0.0:8976",
(err, window) => {
assert.ok(err, "an error should exist");
assert.strictEqual(window, undefined, "window should not exist");
t.done();
}
);
});
specify("script loading errors show up as jsdomErrors in the virtual console", { async: true }, t => {
const virtualConsole = createVirtualConsole();
virtualConsole.on("jsdomError", error => {
assert.ok(error instanceof Error);
assert.equal(error.message, `Could not load script: "http://0.0.0.0:12345/script.js"`);
assert.ok(error.detail);
});
env({
html: "",
scripts: ["http://0.0.0.0:12345/script.js"],
virtualConsole,
done(err, window) {
assert.equal(err, null);
assert.ok(window);
t.done();
}
});
});
describe("node specific tests", { skipIfBrowser: true }, () => {
specify("explicit config.html, a string that is also a valid file", { async: true }, t => {
const body = path.resolve(__dirname, "files/env.html");
env({
html: body,
url: "http://example.com/",
done(err, window) {
assert.ifError(err);
assert.equal(serializeDocument(window.document), "<html><head></head><body>" + body + "</body></html>");
assert.equal(window.location.href, "http://example.com/");
t.done();
}
});
});
specify("explicit config.url, valid", { async: true }, t => {
const html = "<html><head><title>Example URL</title></head><body>Example!</body></html>";
const responseText = "<!DOCTYPE html>" + html;
const server = http.createServer((req, res) => {
res.writeHead(200, { "Content-Length": responseText.length });
res.end(responseText);
server.close();
}).listen(8976);
env({
url: "http://localhost:8976/",
done(err, window) {
assert.ifError(err);
assert.equal(serializeDocument(window.document), responseText);
assert.equal(window.location.href, "http://localhost:8976/");
assert.equal(window.location.origin, "http://localhost:8976");
t.done();
}
});
});
specify("explicit config.file, valid", { async: true }, t => {
const fileName = path.resolve(__dirname, "files/env.html");
env({
file: fileName,
done(err, window) {
assert.ifError(err);
assert.equal(serializeDocument(window.document), `<!DOCTYPE html><html><head>
<title>hello, Node.js!</title>
</head>
<body>\n \n\n</body></html>`);
assert.equal(window.location.href, toFileUrl(fileName));
t.done();
}
});
});
specify("explicit config.file, invalid", { async: true }, t => {
env({
file: "__DOES_NOT_EXIST__",
done(err, window) {
assert.ok(err, "an error should exist");
assert.strictEqual(window, undefined, "window should not exist");
t.done();
}
});
});
specify("explicit config.file, with a script", { async: true }, t => {
env({
file: path.resolve(__dirname, "files/env.html"),
scripts: [path.resolve(__dirname, "../jquery-fixtures/jquery-1.6.2.js")],
done(err, window) {
assert.ifError(err);
const $ = window.jQuery;
const text = "Let's Rock!";
$("body").text(text);
assert.equal($("body").text(), text);
t.done();
}
});
});
specify("explicit config.file, with spaces in the file name", { async: true }, t => {
const fileName = path.resolve(__dirname, "files/folder space/space.html");
env({
file: fileName,
done(err) {
assert.ifError(err);
t.done();
}
});
});
specify("string, parseable as a URL, valid", { async: true }, t => {
const html = "<html><head><title>Example URL</title></head><body>Example!</body></html>";
const responseText = "<!DOCTYPE html>" + html;
const server = http.createServer((req, res) => {
res.writeHead(200, { "Content-Length": responseText.length });
res.end(responseText);
server.close();
}).listen(8976);
env(
"http://localhost:8976/",
(err, window) => {
assert.ifError(err);
assert.equal(serializeDocument(window.document), responseText);
assert.equal(window.location.href, "http://localhost:8976/");
assert.equal(window.location.origin, "http://localhost:8976");
t.done();
}
);
});
specify("string, for an existing filename", { async: true }, t => {
const fileName = path.resolve(__dirname, "files/env.html");
env(
fileName,
(err, window) => {
assert.ifError(err);
assert.equal(serializeDocument(window.document), `<!DOCTYPE html><html><head>
<title>hello, Node.js!</title>
</head>
<body>\n \n\n</body></html>`);
assert.equal(window.location.href, toFileUrl(fileName));
t.done();
}
);
});
specify("string, does not exist as a file", { async: true }, t => {
const body = "__DOES_NOT_EXIST__";
env(
body,
(err, window) => {
assert.ifError(err);
assert.equal(serializeDocument(window.document), "<html><head></head><body>" + body + "</body></html>");
t.done();
}
);
});
specify("with a nonexistent script", { async: true }, t => {
env({
html: "<!DOCTYPE html><html><head></head><body><p>hello world!</p></body></html>",
scripts: ["path/to/invalid.js", "another/invalid.js"],
done(err, window) {
assert.equal(err, null);
assert.ok(window.location.href);
t.done();
}
});
});
specify("with document cookie", { async: true }, t => {
const cookie = "key=value";
const time = new Date(Date.now() + 24 * 60 * 60 * 1000);
const setcookie = cookie + "; expires=" + time.toGMTString() + "; path=/";
const routes = {
"/js": "",
"/html": "<!DOCTYPE html><html><head><script src=\"/js\"></script></head><body></body></html>"
};
const server = http.createServer((req, res) => {
if (req.url === "/js") {
assert.equal(req.headers.cookie, cookie);
}
res.writeHead(200, { "Content-Length": routes[req.url].length });
res.end(routes[req.url]);
});
server.listen(63999, "127.0.0.1", () => {
env({
url: "http://127.0.0.1:63999/html",
document: { cookie: setcookie },
done(err, window) {
server.close();
assert.ifError(err);
assert.equal(window.document.cookie, cookie);
t.done();
},
features: {
FetchExternalResources: ["script"]
}
});
});
});
specify("with scripts and content retrieved from URLs", { async: true }, t => {
const routes = {
"/js": "window.attachedHere = 123;",
"/html": "<a href='/path/to/hello'>World</a>"
};
const server = http.createServer((req, res) => {
res.writeHead(200, { "Content-Length": routes[req.url].length });
res.end(routes[req.url]);
});
server.listen(64000, "127.0.0.1", () => {
env({
url: "http://127.0.0.1:64000/html",
scripts: "http://127.0.0.1:64000/js",
done(err, window) {
server.close();
assert.ifError(err);
assert.equal(window.location.href, "http://127.0.0.1:64000/html");
assert.equal(window.location.origin, "http://127.0.0.1:64000");
assert.equal(window.attachedHere, 123);
assert.equal(window.document.getElementsByTagName("a").item(0).innerHTML, "World");
t.done();
}
});
});
});
specify("with configurable resource loader modifying routes and content",
{ async: true },
t => {
const routes = {
"/js/dir/test.js": "window.modifiedRoute = true;",
"/html": "<!DOCTYPE html><html><head><script src='./test.js'></script></head><body></body></html>"
};
const server = http.createServer((req, res) => {
res.writeHead(200, { "Content-Length": routes[req.url].length });
res.end(routes[req.url]);
});
const time = new Date(Date.now() + 24 * 60 * 60 * 1000);
const cookie = "key=value; expires=" + time.toGMTString() + "; path=/";
server.listen(64001, "127.0.0.1", () => {
env({
document: { cookie },
url: "http://127.0.0.1:64001/html",
resourceLoader(resource, callback) {
assert.ok(typeof resource === "object");
assert.ok(typeof resource.url === "object");
assert.equal(resource.cookie, "key=value");
assert.equal(resource.baseUrl, "http://127.0.0.1:64001/html");
assert.ok(typeof resource.defaultFetch === "function");
assert.ok(typeof callback === "function");
if (/\.js$/.test(resource.url.path)) {
resource.url.path = "/js/dir" + resource.url.path;
resource.defaultFetch((err, body) => {
if (err) {
callback(err);
} else {
callback(null, body + "\nwindow.modifiedContent = true;");
}
});
} else {
resource.defaultFetch(callback);
}
},
done(err, window) {
server.close();
assert.ifError(err);
assert.ok(window.modifiedRoute);
assert.ok(window.modifiedContent);
t.done();
},
features: {
FetchExternalResources: ["script"],
ProcessExternalResources: ["script"],
SkipExternalResources: false
}
});
41F3
});
});
});
describe("browser specific tests", { skipUnlessBrowser: true }, () => {
/* global location */
// location is a Location or WorkerLocation
const testServerLocation = typeof location !== "undefined" && location.origin;
specify("should be able to fetch a html document", { async: true }, t => {
env({
url: testServerLocation + "/base/test/jsdom/files/reddit.html",
done(err, window) {
assert.ifError(err);
assert.strictEqual(window.document.getElementById("header-bottom-left").nodeName, "DIV");
t.done();
}
});
});
});
});