Security update to Drupal 8.4.6
[yaffs-website] / node_modules / phridge / test / spawn.test.js
1 "use strict";
2
3 var chai = require("chai");
4 var getport = require("getport");
5 var net = require("net");
6 var expect = chai.expect;
7 var spawn = require("../lib/spawn.js");
8 var phridge = require("../lib/main.js");
9 var Phantom = require("../lib/Phantom.js");
10 var slow = require("./helpers/slow.js");
11 var lift = require("../lib/lift.js");
12
13 require("./helpers/setup.js");
14
15 getport = lift(getport);
16
17 describe("spawn(config?)", function () {
18     var port;
19
20     function blockGhostDriverPort() {
21         return getport(10000)
22             .then(function (freePort) {
23                 var server = net.createServer();
24                 var listen = lift(server.listen);
25
26                 port = freePort;
27
28                 // We're blocking the GhostDriver port so phantomjs crashes on startup.
29                 // Otherwise the phantomjs processes can't be killed because it doesn't
30                 // listen on our commands in GhostDriver-mode.
31                 return listen.call(server, freePort);
32             });
33     }
34
35     after(slow(function () {
36         return phridge.disposeAll();
37     }));
38
39     it("should resolve to an instance of Phantom", slow(function () {
40         return expect(spawn()).to.eventually.be.an.instanceOf(Phantom);
41     }));
42
43     it("should pass the provided config to phantomjs", slow(function (done) {
44         // We're setting the webdriver option to test if the config is recognized
45         // Setting this option does not make any sense because phantomjs is
46         // unusable with phantomjs in GhostDriver-mode. But it prints a nice
47         // message to the console which causes the promise to be rejected
48         blockGhostDriverPort()
49             .then(function () {
50                 // Prevent PhantomJS from printing a disturbing error message to the console
51                 phridge.config.stdout = null;
52                 phridge.config.stderr = null;
53
54                 return expect(phridge.spawn({
55                     webdriver: "localhost:" + port
56                 })).to.be.rejectedWith(/GhostDriver - main\.fail/);
57             })
58             .then(function () {
59                 phridge.config.stdout = process.stdout;
60                 phridge.config.stderr = process.stderr;
61
62                 // Give phantomjs some time to exit
63                 setTimeout(done, 100);
64             });
65     }));
66
67     it("should also allow CLI style config options", slow(function (done) {
68         // We're setting the webdriver option to test if the config is recognized
69         // Setting this option does not make any sense because phantomjs is
70         // unusable with phantomjs in GhostDriver-mode. But it prints a nice
71         // message to the console which causes the promise to be rejected
72         blockGhostDriverPort()
73             .then(function () {
74                 // Prevent PhantomJS from printing a disturbing error message to the console
75                 phridge.config.stdout = null;
76                 phridge.config.stderr = null;
77
78                 return expect(phridge.spawn({
79                     "--load-images": "true",
80                     "--webdriver": "localhost:" + port
81                 })).to.be.rejectedWith("GhostDriver");
82             })
83             .then(function () {
84                 phridge.config.stdout = process.stdout;
85                 phridge.config.stderr = process.stderr;
86
87                 // Give phantomjs some time to exit
88                 setTimeout(done, 100);
89             })
90             .catch(done);
91     }));
92
93 });