Skip to content

Commit

Permalink
Factor out runScripts option tests
Browse files Browse the repository at this point in the history
We will need to expand these substantially as part of fixing #1848 and #1828, so it'll be easier to keep them in another file.
  • Loading branch information
domenic committed May 21, 2017
1 parent ecf7449 commit 38c430f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 68 deletions.
75 changes: 75 additions & 0 deletions test/api/options-run-scripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"use strict";
const { assert } = require("chai");
const { describe, it } = require("mocha-sugar-free");

const { JSDOM } = require("../..");

describe("API: runScripts constructor option", () => {
describe("<script>s and eval()", () => {
it("should not execute any scripts by default", () => {
const dom = new JSDOM(`<body>
<script>document.body.appendChild(document.createElement("hr"));</script>
</body>`);

assert.strictEqual(dom.window.document.body.children.length, 1);
assert.strictEqual(dom.window.eval, undefined);
});

it("should not execute any scripts, even in iframes, by default (GH-1821)", () => {
const dom = new JSDOM(`<iframe></iframe>`);
const frameWindow = dom.window.document.querySelector("iframe").contentWindow;

frameWindow.document.open();
frameWindow.document.write(`<script>parent.prop = "i was executed";</script>`);
frameWindow.document.close();

assert.strictEqual(dom.window.prop, undefined);
assert.strictEqual(frameWindow.eval, undefined);
});

it("should execute <script>s and eval when set to \"dangerously\"", () => {
const dom = new JSDOM(`<body>
<script>document.body.appendChild(document.createElement("hr"));</script>
</body>`, { runScripts: "dangerously" });
dom.window.eval(`document.body.appendChild(document.createElement("p"));`);

assert.strictEqual(dom.window.document.body.children.length, 3);
});

it("should only run eval when set to \"outside-only\"", () => {
const dom = new JSDOM(`<body>
<script>document.body.appendChild(document.createElement("hr"));</script>
</body>`, { runScripts: "outside-only" });
dom.window.eval(`document.body.appendChild(document.createElement("p"));`);

assert.strictEqual(dom.window.document.body.children.length, 2);
});

it("should ensure eval exists on iframes when set to \"outside-only\"", () => {
const dom = new JSDOM(`<iframe></iframe>`, { runScripts: "outside-only" });
const frameWindow = dom.window.document.querySelector("iframe").contentWindow;

frameWindow.eval(`document.body.appendChild(document.createElement("p"));`);

assert.strictEqual(frameWindow.document.body.children.length, 1);
});

it("should execute <script>s in iframes when set to \"dangerously\"", () => {
const dom = new JSDOM(`<iframe></iframe>`, { runScripts: "dangerously" });
const frameWindow = dom.window.document.querySelector("iframe").contentWindow;

frameWindow.document.open();
frameWindow.document.write(`<script>parent.prop = "i was executed";</script>`);
frameWindow.document.close();

assert.strictEqual(dom.window.prop, "i was executed");
});
});

it("should disallow other values", () => {
assert.throws(() => new JSDOM(``, { runScripts: null }), RangeError);
assert.throws(() => new JSDOM(``, { runScripts: "asdf" }), RangeError);
assert.throws(() => new JSDOM(``, { runScripts: true }), RangeError);
assert.throws(() => new JSDOM(``, { runScripts: false }), RangeError);
});
});
68 changes: 0 additions & 68 deletions test/api/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,74 +179,6 @@ describe("API: constructor options", () => {
});
});

describe("runScripts", () => {
it("should not execute any scripts by default", () => {
const dom = new JSDOM(`<body>
<script>document.body.appendChild(document.createElement("hr"));</script>
</body>`);

assert.strictEqual(dom.window.document.body.children.length, 1);
assert.strictEqual(dom.window.eval, undefined);
});

it("should not execute any scripts, even in iframes, by default (GH-1821)", () => {
const dom = new JSDOM(`<iframe></iframe>`);
const frameWindow = dom.window.document.querySelector("iframe").contentWindow;

frameWindow.document.open();
frameWindow.document.write(`<script>parent.prop = "i was executed";</script>`);
frameWindow.document.close();

assert.strictEqual(dom.window.prop, undefined);
assert.strictEqual(frameWindow.eval, undefined);
});

it("should execute <script>s and eval when set to \"dangerously\"", () => {
const dom = new JSDOM(`<body>
<script>document.body.appendChild(document.createElement("hr"));</script>
</body>`, { runScripts: "dangerously" });
dom.window.eval(`document.body.appendChild(document.createElement("p"));`);

assert.strictEqual(dom.window.document.body.children.length, 3);
});

it("should only run eval when set to \"outside-only\"", () => {
const dom = new JSDOM(`<body>
<script>document.body.appendChild(document.createElement("hr"));</script>
</body>`, { runScripts: "outside-only" });
dom.window.eval(`document.body.appendChild(document.createElement("p"));`);

assert.strictEqual(dom.window.document.body.children.length, 2);
});

it("should ensure eval exists on iframes when set to \"outside-only\"", () => {
const dom = new JSDOM(`<iframe></iframe>`, { runScripts: "outside-only" });
const frameWindow = dom.window.document.querySelector("iframe").contentWindow;

frameWindow.eval(`document.body.appendChild(document.createElement("p"));`);

assert.strictEqual(frameWindow.document.body.children.length, 1);
});

it("should execute <script>s in iframes when set to \"dangerously\"", () => {
const dom = new JSDOM(`<iframe></iframe>`, { runScripts: "dangerously" });
const frameWindow = dom.window.document.querySelector("iframe").contentWindow;

frameWindow.document.open();
frameWindow.document.write(`<script>parent.prop = "i was executed";</script>`);
frameWindow.document.close();

assert.strictEqual(dom.window.prop, "i was executed");
});

it("should disallow other values", () => {
assert.throws(() => new JSDOM(``, { runScripts: null }), RangeError);
assert.throws(() => new JSDOM(``, { runScripts: "asdf" }), RangeError);
assert.throws(() => new JSDOM(``, { runScripts: true }), RangeError);
assert.throws(() => new JSDOM(``, { runScripts: false }), RangeError);
});
});

describe("beforeParse", () => {
it("should execute with a window and document but no nodes", () => {
let windowPassed;
Expand Down
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require("./api/from-file.js");
require("./api/from-url.js");
require("./api/methods.js");
require("./api/options.js");
require("./api/options-run-scripts.js");
require("./api/resources.js");
require("./api/virtual-console.js");

Expand Down

0 comments on commit 38c430f

Please sign in to comment.