world.js
2.2 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
var util = require('util');
var Spooky = require('../../../../lib/spooky');
var World = function World(callback) {
var spooky;
var world = this;
this.baseUrl = 'http://localhost:' + process.env.TEST_PORT;
try {
spooky = world.spooky = new Spooky({
casper: {
verbose: true,
logLevel: 'debug'
},
child: {
port: 8081,
spooky_lib: './',
script: './lib/bootstrap.js'
}
}, onCreated);
} catch (e) {
console.dir(e);
console.trace('Spooky.listen failed');
}
//spooky.debug = true;
// track errors
spooky.errors = [];
spooky.on('error', function (error) {
error = error.data ? error.data : error;
spooky.errors.push(error);
if (spooky.debug) {
console.error('spooky error', util.inspect(error));
}
});
spooky.console = [];
spooky.on('console', function (line) {
spooky.console.push(line);
if (spooky.debug) {
console.log(line);
}
});
spooky.on('log', function (entry) {
if (!spooky.debug) { return; }
var message = entry.message;
var event = (message.event || '').toLowerCase();
if (event === 'request') {
console.log('%s: %s %s',
spooky.options.port, message.method, message.request.url);
console.log(' Headers: %s',
util.inspect(message.request.headers));
console.log(' Payload: %s',
util.inspect(JSON.parse(message.request.post)));
} else if (event === 'response') {
console.log('%s: %s %s',
spooky.options.port, message.code,
util.inspect(JSON.parse(message.body)));
} else {
console.log(spooky.options.port + ':');
console.dir(entry);
}
});
function onCreated(error, response) {
if (error) {
console.dir(error);
throw new Error('Failed to initialize context.spooky: ' +
error.code + ' - ' + error.message);
}
callback(world);
}
};
module.exports.World = World;