Skip to content

Commit a96380e

Browse files
update live_server branch to latest master
1 parent fb7780d commit a96380e

File tree

3 files changed

+146
-81
lines changed

3 files changed

+146
-81
lines changed

lib/config.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ if (commit_id) {
5353
config.build = 'Local run, ' + new Date().toISOString();
5454
}
5555

56-
['username', 'key', 'test_path', 'browsers'].forEach(function(param) {
56+
['username', 'key', 'browsers', 'test_path'].forEach(function(param) {
5757
if (typeof config[param] === 'undefined') {
5858
console.error('Configuration parameter `%s` is required.', param);
5959
process.exit(1);
@@ -73,13 +73,16 @@ var formatPath = function(path) {
7373

7474
config.tunnelIdentifier = process.env.TUNNEL_ID || process.env.TRAVIS_JOB_ID || process.env.TRAVIS_BUILD_ID;
7575

76-
if (Object.prototype.toString.call(config.test_path) === '[object Array]') {
77-
config.test_path.forEach(function(path) {
78-
path = formatPath(path);
79-
});
80-
} else {
81-
//Backward Compatibility, if test_path is not array of path
82-
config.test_path = formatPath(config.test_path);
76+
if (typeof(config['test_server']) === 'undefined') {
77+
if (Object.prototype.toString.call(config.test_path) === '[object Array]') {
78+
config.test_path.forEach(function(path) {
79+
path = formatPath(path);
80+
});
81+
82+
} else {
83+
//Backward Compatibility, if test_path is not array of path
84+
config.test_path = formatPath(config.test_path);
85+
}
8386
}
8487

8588
config.status = 0;

lib/proxy.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var http = require('http'),
2+
url = require('url');
3+
4+
var ProxyServer = {
5+
onRequest: function(client_req, client_res, host, callback) {
6+
var proxyUrl = url.parse(host);
7+
var path = url.parse(host);
8+
var options = {
9+
path: client_req.url,
10+
hostname: proxyUrl.hostname,
11+
port: proxyUrl.port,
12+
method: client_req.method,
13+
headers: client_req.headers
14+
};
15+
16+
var proxy = http.request(options, function (res) {
17+
data = "";
18+
res.on('data', function(chunk) {
19+
data += chunk;
20+
});
21+
res.on('end', function() {
22+
//Replace
23+
callback(res, data);
24+
});
25+
}).on('error', function(e) {
26+
client_res.writeHead(500);
27+
client_res.write('error: ' + e.toString());
28+
client_res.end();
29+
});
30+
proxy.end();
31+
}
32+
};
33+
34+
exports.proxyServer = ProxyServer;

lib/server.js

Lines changed: 101 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var Log = require('./logger'),
77
qs = require('querystring'),
88
utils = require('./utils'),
99
config = require('../lib/config'),
10+
proxyServer = require('./proxy').proxyServer,
1011
chalk = require('chalk');
1112

1213
var mimeTypes = {
@@ -30,7 +31,7 @@ function getTestBrowserInfo(worker) {
3031

3132
exports.Server = function Server(bsClient, workers) {
3233

33-
function handleFile(filename, request, response) {
34+
function handleFile(filename, request, response, doNotUseProxy) {
3435
var url_parts = url.parse(request.url, true);
3536
var query = url_parts.query;
3637

@@ -40,95 +41,122 @@ exports.Server = function Server(bsClient, workers) {
4041
logger.debug('[%s] Acknowledged', getTestBrowserInfo(worker));
4142
}
4243

43-
fs.exists(filename, function(exists) {
44-
if (!exists) {
45-
response.writeHead(404, {
46-
'Content-Type': 'text/plain'
47-
});
48-
response.write('404 Not Found\n');
49-
response.end();
50-
return;
51-
}
44+
var getReporterPatch = function (mimeType) {
45+
var scripts = [
46+
'json2.js',
47+
'browserstack.js',
48+
];
49+
50+
var framework_scripts = {
51+
'qunit': ['qunit-plugin.js'],
52+
'jasmine': ['jasmine-jsreporter.js', 'jasmine-plugin.js'],
53+
'jasmine2': ['jasmine2-plugin.js'],
54+
'mocha': ['mocha-plugin.js']
55+
};
5256

53-
if (fs.lstatSync(filename).isDirectory()) {
54-
filename = filename + (filename.lastIndexOf('/') === filename.length - 1 ? '' : '/') + 'index.html';
57+
var filePath = path.relative(process.cwd(), filename);
58+
var pathMatches;
59+
60+
if (typeof config.test_path === 'object') {
61+
pathMatches = (config.test_path.indexOf(filePath) !== -1);
62+
} else {
63+
pathMatches = (filePath === config.test_path);
5564
}
5665

57-
fs.readFile(filename, {encoding: 'utf8'}, function(err, file) {
66+
if (pathMatches && mimeType === 'text/html') {
67+
var framework = config['test_framework'];
68+
var tag_name = (framework === 'mocha') ? 'head' : 'body';
69+
var patch = '$1';
5870

59-
if (err) {
60-
response.writeHead(500, {
61-
'Content-Type': 'text/plain'
71+
scripts.forEach(function(script) {
72+
patch += '<script type="text/javascript" src="/_patch/' + script + '"></script>\n';
73+
});
74+
75+
// adding framework scripts
76+
if (framework === 'jasmine') {
77+
framework_scripts['jasmine'].forEach(function(script) {
78+
patch += '<script type="text/javascript" src="/_patch/' + script + '"></script>\n';
79+
});
80+
patch += '<script type="text/javascript">jasmine.getEnv().addReporter(new jasmine.JSReporter());</script>\n';
81+
} else if (framework === 'jasmine2') {
82+
framework_scripts['jasmine2'].forEach(function(script) {
83+
patch += '<script type="text/javascript" src="/_patch/' + script + '"></script>\n';
84+
});
85+
} else if (framework === 'mocha') {
86+
framework_scripts['mocha'].forEach(function(script) {
87+
patch += '<script type="text/javascript" src="/_patch/' + script + '"></script>\n';
88+
});
89+
patch += '<script type="text/javascript">mocha.reporter(Mocha.BrowserStack);</script>\n';
90+
} else {
91+
framework_scripts['qunit'].forEach(function(script) {
92+
patch += '<script type="text/javascript" src="/_patch/' + script + '"></script>\n';
6293
});
63-
response.write(err + '\n');
64-
response.end();
65-
return;
6694
}
95+
patch += '</' + tag_name + '>';
96+
return patch;
97+
}
98+
};
6799

68-
var mimeType = mimeTypes[path.extname(filename).split('.')[1]];
69-
response.writeHead(200, {
70-
'Content-Type': mimeType + '; charset=utf-8',
71-
});
100+
var writeResponse = function(err, data) {
72101

73-
var scripts = [
74-
'json2.js',
75-
'browserstack.js',
76-
];
102+
if (err) {
103+
response.writeHead(500, {
104+
'Content-Type': 'text/plain'
105+
});
106+
response.write(err + '\n');
107+
response.end();
108+
return;
109+
}
77110

78-
var framework_scripts = {
79-
'qunit': ['qunit-plugin.js'],
80-
'jasmine': ['jasmine-jsreporter.js', 'jasmine-plugin.js'],
81-
'jasmine2': ['jasmine2-plugin.js'],
82-
'mocha': ['mocha-plugin.js']
83-
};
111+
var mimeType = mimeTypes[path.extname(filename).split('.')[1]];
112+
response.writeHead(200, {
113+
'Content-Type': mimeType + '; charset=utf-8',
114+
});
115+
var tag_name = (config['test_framework'] === 'mocha') ? 'head' : 'body';
116+
var matcher = new RegExp('(.*)<\/' + tag_name + '>'); ///(.*)<\/body>/;
117+
var patch = getReporterPatch(mimeType);
118+
data = data.replace(matcher, patch);
84119

85-
var filePath = path.relative(process.cwd(), filename);
86-
var pathMatches;
120+
response.write(data);
121+
response.end();
122+
};
87123

88-
if (typeof config.test_path === 'object') {
89-
pathMatches = (config.test_path.indexOf(filePath) !== -1);
90-
} else {
91-
pathMatches = (filePath === config.test_path);
124+
if (!doNotUseProxy && config.test_server) {
125+
proxyServer.onRequest(request, response, config.test_server, function(remote_response, response_data) {
126+
var mimeType = mimeTypes[path.extname(filename).split('.')[1]];
127+
var final_data = response_data;
128+
var headers = remote_response.headers;
129+
if (mimeType === 'text/html') {
130+
var matcher = /(.*)<\/head>/;
131+
var patch = getReporterPatch(mimeType);
132+
final_data = response_data.replace(matcher, patch);
133+
headers['content-length'] = final_data.length;
92134
}
93-
if (pathMatches && mimeType === 'text/html') {
94-
var framework = config['test_framework'];
95-
var tag_name = (framework === 'mocha') ? 'head' : 'body';
96-
var matcher = new RegExp('(.*)<\/' + tag_name + '>'); ///(.*)<\/body>/;
97-
var patch = '$1';
98-
scripts.forEach(function(script) {
99-
patch += '<script type="text/javascript" src="/_patch/' + script + '"></script>\n';
100-
});
135+
response.writeHead(remote_response.statusCode, headers);
136+
response.write(final_data);
137+
response.end();
138+
return;
139+
});
101140

102-
// adding framework scripts
103-
if (framework === 'jasmine') {
104-
framework_scripts['jasmine'].forEach(function(script) {
105-
patch += '<script type="text/javascript" src="/_patch/' + script + '"></script>\n';
106-
});
107-
patch += '<script type="text/javascript">jasmine.getEnv().addReporter(new jasmine.JSReporter());</script>\n';
108-
} else if (framework === 'jasmine2') {
109-
framework_scripts['jasmine2'].forEach(function(script) {
110-
patch += '<script type="text/javascript" src="/_patch/' + script + '"></script>\n';
111-
});
112-
} else if (framework === 'mocha') {
113-
framework_scripts['mocha'].forEach(function(script) {
114-
patch += '<script type="text/javascript" src="/_patch/' + script + '"></script>\n';
115-
});
116-
patch += '<script type="text/javascript">mocha.reporter(Mocha.BrowserStack);</script>\n';
117-
} else {
118-
framework_scripts['qunit'].forEach(function(script) {
119-
patch += '<script type="text/javascript" src="/_patch/' + script + '"></script>\n';
120-
});
121-
}
122-
patch += '</' + tag_name + '>';
141+
} else {
123142

124-
file = file.replace(matcher, patch);
143+
fs.exists(filename, function(exists) {
144+
if (!exists) {
145+
response.writeHead(404, {
146+
'Content-Type': 'text/plain'
147+
});
148+
response.write('404 Not Found\n');
149+
response.end();
150+
return;
125151
}
126152

153+
if (fs.lstatSync(filename).isDirectory()) {
154+
filename = filename + (filename.lastIndexOf('/') === filename.length - 1 ? '' : '/') + 'index.html';
155+
}
127156

128-
response.write(file);
129-
response.end();
157+
fs.readFile(filename, {encoding: 'utf8'}, writeResponse);
130158
});
131-
});
159+
}
132160
}
133161

134162
function parseBody(body) {
@@ -267,7 +295,7 @@ exports.Server = function Server(bsClient, workers) {
267295
response.end();
268296
},
269297
'_patch': function patchHandler(uri, body, request, response) {
270-
handleFile(path.join(__dirname, uri), request, response);
298+
handleFile(path.join(__dirname, uri), request, response, true);
271299
},
272300
'_default': function defaultHandler(uri, body, request, response) {
273301
handleFile(path.join(process.cwd(), uri), request, response);

0 commit comments

Comments
 (0)