selftest.js
2.42 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
var require = patchRequire(require);
var colorizer = require('colorizer').create('Colorizer');
var fs = require('fs');
var utils = require('utils');
var server = require('webserver').create();
var service;
var testServerPort = 54321;
var contentTypes = {
html: {type: 'text/html', binMode: false},
js: {type: 'application/javascript', binMode: false},
json: {type: 'application/json', binMode: false},
txt: {type: 'text/plain', binMode: false},
png: {type: 'image/png', binMode: true},
notype: {type: null, binMode: false},
_default: {type: 'application/octet-stream', binMode: true}
};
var extensionRE = /\.([a-zA-Z]*)$/;
function info(message) {
"use strict";
console.log(colorizer.colorize('INFO', 'INFO_BAR') + ' ' + message);
}
service = server.listen(testServerPort, function(request, response) {
"use strict";
/*eslint max-statements:0*/
var requestPath = request.url;
if (requestPath.indexOf('?') !== -1) {
requestPath = request.url.split('?')[0];
}
var pageFile = fs.pathJoin(phantom.casperPath, requestPath);
if (!fs.exists(pageFile) || !fs.isFile(pageFile)) {
response.statusCode = 404;
console.log(utils.format('Test server url not found: %s (file: %s)', request.url, pageFile), "warning");
response.write("404 - NOT FOUND");
} else {
var headers = {};
var binMode;
var extension = extensionRE.exec(pageFile);
extension = extension && extension[1];
var contentType = contentTypes[extension] ||
contentTypes._default;
if (contentType.type) {
headers['Content-Type'] = contentType.type;
}
binMode = contentType.binMode;
response.writeHead(200, headers);
if (binMode) {
response.setEncoding("binary");
response.write(fs.read(pageFile, 'b'));
}
else {
response.write(fs.read(pageFile));
}
}
response.close();
});
// overriding Casper.open to prefix all test urls
casper.setFilter('open.location', function(location) {
"use strict";
if (/^file/.test(location)) {
return location;
}
if (!/^http/.test(location)) {
return utils.format('http://localhost:%d/%s', testServerPort, location);
}
return location;
});
// test suites completion listener
casper.test.on('tests.complete', function() {
"use strict";
server.close();
});