2012-07-13 04:54:12 +08:00
|
|
|
var http = require('http');
|
|
|
|
|
|
|
|
var assert = module.exports = exports = require('assert');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert response from `server` with
|
|
|
|
* the given `req` object and `res` assertions object.
|
|
|
|
*
|
|
|
|
* @param {Server} server
|
|
|
|
* @param {Object} req
|
|
|
|
* @param {Object|Function} res
|
2016-09-26 20:37:40 +08:00
|
|
|
* @param {String|Function|Object} msg
|
2012-07-13 04:54:12 +08:00
|
|
|
*/
|
|
|
|
assert.response = function(server, req, res, msg){
|
|
|
|
var port = 5555;
|
|
|
|
function check(){
|
|
|
|
try {
|
|
|
|
server.__port = server.address().port;
|
|
|
|
server.__listening = true;
|
|
|
|
} catch (err) {
|
|
|
|
process.nextTick(check);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (server.__deferred) {
|
|
|
|
server.__deferred.forEach(function(args){
|
|
|
|
assert.response.apply(assert, args);
|
|
|
|
});
|
|
|
|
server.__deferred = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the server is ready or defer
|
|
|
|
if (!server.fd) {
|
|
|
|
server.__deferred = server.__deferred || [];
|
|
|
|
server.listen(server.__port = port++, '127.0.0.1', check);
|
|
|
|
} else if (!server.__port) {
|
|
|
|
server.__deferred = server.__deferred || [];
|
|
|
|
process.nextTick(check);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The socket was created but is not yet listening, so keep deferring
|
|
|
|
if (!server.__listening) {
|
|
|
|
server.__deferred.push(arguments);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Callback as third or fourth arg
|
|
|
|
var callback = typeof res === 'function'
|
|
|
|
? res
|
|
|
|
: typeof msg === 'function'
|
|
|
|
? msg
|
|
|
|
: function(){};
|
|
|
|
|
|
|
|
// Default messate to test title
|
|
|
|
if (typeof msg === 'function') msg = null;
|
|
|
|
msg = msg || assert.testTitle;
|
|
|
|
msg += '. ';
|
|
|
|
|
|
|
|
// Pending responses
|
|
|
|
server.__pending = server.__pending || 0;
|
|
|
|
server.__pending++;
|
|
|
|
|
|
|
|
// Create client
|
|
|
|
if (!server.fd) {
|
|
|
|
server.listen(server.__port = port++, '127.0.0.1', issue);
|
|
|
|
} else {
|
|
|
|
issue();
|
|
|
|
}
|
|
|
|
|
|
|
|
function issue(){
|
|
|
|
|
|
|
|
// Issue request
|
|
|
|
var timer,
|
|
|
|
method = req.method || 'GET',
|
|
|
|
status = res.status || res.statusCode,
|
|
|
|
data = req.data || req.body,
|
|
|
|
requestTimeout = req.timeout || 0,
|
|
|
|
encoding = req.encoding || 'utf8';
|
|
|
|
|
|
|
|
var request = http.request({
|
|
|
|
host: '127.0.0.1',
|
|
|
|
port: server.__port,
|
|
|
|
path: req.url,
|
|
|
|
method: method,
|
2012-07-16 19:38:40 +08:00
|
|
|
headers: req.headers,
|
|
|
|
agent: false
|
2012-07-13 04:54:12 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
var check = function() {
|
|
|
|
if (--server.__pending === 0) {
|
|
|
|
server.close();
|
|
|
|
server.__listening = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Timeout
|
|
|
|
if (requestTimeout) {
|
|
|
|
timer = setTimeout(function(){
|
|
|
|
check();
|
|
|
|
delete req.timeout;
|
2014-03-19 20:30:29 +08:00
|
|
|
request.destroy(); // will trigger 'error' event
|
2012-07-13 04:54:12 +08:00
|
|
|
}, requestTimeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data) request.write(data);
|
|
|
|
|
2014-01-31 17:55:30 +08:00
|
|
|
request.on('error', function(err){
|
|
|
|
check();
|
2016-09-26 20:37:40 +08:00
|
|
|
callback(err);
|
2014-01-31 17:55:30 +08:00
|
|
|
});
|
|
|
|
|
2012-07-13 04:54:12 +08:00
|
|
|
request.on('response', function(response){
|
|
|
|
response.body = '';
|
|
|
|
response.setEncoding(encoding);
|
|
|
|
response.on('data', function(chunk){ response.body += chunk; });
|
|
|
|
response.on('end', function(){
|
|
|
|
if (timer) clearTimeout(timer);
|
|
|
|
|
2014-08-27 22:39:16 +08:00
|
|
|
check();
|
|
|
|
|
2012-07-13 04:54:12 +08:00
|
|
|
// Assert response body
|
|
|
|
if (res.body !== undefined) {
|
|
|
|
var eql = res.body instanceof RegExp
|
|
|
|
? res.body.test(response.body)
|
|
|
|
: res.body === response.body;
|
|
|
|
assert.ok(
|
|
|
|
eql,
|
|
|
|
msg + 'Invalid response body.\n'
|
|
|
|
+ ' Expected: ' + res.body + '\n'
|
|
|
|
+ ' Got: ' + response.body
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert response status
|
|
|
|
if (typeof status === 'number') {
|
|
|
|
assert.equal(
|
|
|
|
response.statusCode,
|
|
|
|
status,
|
2014-08-27 23:09:31 +08:00
|
|
|
msg + colorize('Invalid response status code.\n'
|
2012-07-13 04:54:12 +08:00
|
|
|
+ ' Expected: [green]{' + status + '}\n'
|
2014-08-27 23:09:31 +08:00
|
|
|
+ ' Got: [red]{' + response.statusCode + '}\n'
|
|
|
|
+ ' Response body: ' + response.body)
|
2012-07-13 04:54:12 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert response headers
|
|
|
|
if (res.headers) {
|
|
|
|
var keys = Object.keys(res.headers);
|
|
|
|
for (var i = 0, len = keys.length; i < len; ++i) {
|
|
|
|
var name = keys[i],
|
|
|
|
actual = response.headers[name.toLowerCase()],
|
|
|
|
expected = res.headers[name],
|
|
|
|
eql = expected instanceof RegExp
|
|
|
|
? expected.test(actual)
|
|
|
|
: expected == actual;
|
|
|
|
assert.ok(
|
|
|
|
eql,
|
2014-08-27 23:09:31 +08:00
|
|
|
msg + colorize('Invalid response header [bold]{' + name + '}.\n'
|
2012-07-13 04:54:12 +08:00
|
|
|
+ ' Expected: [green]{' + expected + '}\n'
|
2014-08-27 23:09:31 +08:00
|
|
|
+ ' Got: [red]{' + actual + '}\n'
|
|
|
|
+ ' Response body: ' + response.body)
|
2012-07-13 04:54:12 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-26 20:37:40 +08:00
|
|
|
callback(null, response);
|
2012-07-13 04:54:12 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
request.end();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-27 23:09:31 +08:00
|
|
|
/**
|
|
|
|
* Colorize the given string using ansi-escape sequences.
|
|
|
|
* Disabled when --boring is set.
|
|
|
|
*
|
|
|
|
* @param {String} str
|
|
|
|
* @return {String}
|
|
|
|
*/
|
|
|
|
function colorize(str) {
|
|
|
|
var colors = { bold: 1, red: 31, green: 32, yellow: 33 };
|
|
|
|
return str.replace(/\[(\w+)\]\{([^]*?)\}/g, function(_, color, str) {
|
|
|
|
return '\x1B[' + colors[color] + 'm' + str + '\x1B[0m';
|
|
|
|
});
|
2016-05-20 20:42:53 +08:00
|
|
|
}
|