2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2012-07-13 04:54:12 +08:00
|
|
|
var assert = module.exports = exports = require('assert');
|
2016-09-27 00:09:27 +08:00
|
|
|
var request = require('request');
|
2016-12-19 17:50:05 +08:00
|
|
|
var debug = require('debug')('assert-response');
|
2012-07-13 04:54:12 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
assert.response = function (server, req, res, callback) {
|
2016-09-27 00:09:27 +08:00
|
|
|
if (!callback) {
|
|
|
|
callback = res;
|
|
|
|
res = {};
|
|
|
|
}
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
var port = 5555;
|
|
|
|
var host = '127.0.0.1';
|
2016-09-27 00:09:27 +08:00
|
|
|
|
|
|
|
var listeningAttempts = 0;
|
|
|
|
var listener;
|
2019-12-24 01:19:08 +08:00
|
|
|
function listen () {
|
2016-09-27 00:09:27 +08:00
|
|
|
if (listeningAttempts > 25) {
|
2016-12-19 17:50:05 +08:00
|
|
|
var message = 'Tried too many ports';
|
|
|
|
debug(message);
|
|
|
|
return callback(new Error(message));
|
2016-09-27 00:09:27 +08:00
|
|
|
}
|
|
|
|
listener = server.listen(port, host);
|
2019-12-24 01:19:08 +08:00
|
|
|
listener.on('error', function () {
|
2016-09-27 00:09:27 +08:00
|
|
|
port++;
|
|
|
|
listeningAttempts++;
|
|
|
|
listen();
|
|
|
|
});
|
|
|
|
listener.on('listening', onServerListening);
|
|
|
|
}
|
|
|
|
|
|
|
|
listen();
|
|
|
|
|
2016-12-19 18:11:31 +08:00
|
|
|
debug('Request definition', req);
|
2016-12-19 17:50:05 +08:00
|
|
|
|
2016-09-27 00:09:27 +08:00
|
|
|
// jshint maxcomplexity:10
|
2019-12-24 01:19:08 +08:00
|
|
|
function onServerListening () {
|
2016-12-19 17:50:05 +08:00
|
|
|
debug('Server listening on port = %d', port);
|
2016-09-27 00:09:27 +08:00
|
|
|
var status = res.status || res.statusCode;
|
|
|
|
var requestParams = {
|
|
|
|
url: 'http://' + host + ':' + port + req.url,
|
|
|
|
method: req.method || 'GET',
|
|
|
|
headers: req.headers || {},
|
2016-11-24 07:15:17 +08:00
|
|
|
timeout: req.timeout || 5000,
|
2016-09-27 00:09:27 +08:00
|
|
|
encoding: req.encoding || 'utf8'
|
|
|
|
};
|
|
|
|
|
|
|
|
if (req.body || req.data) {
|
|
|
|
requestParams.body = req.body || req.data;
|
|
|
|
}
|
|
|
|
|
2018-05-04 00:32:15 +08:00
|
|
|
if (req.formData) {
|
|
|
|
requestParams.formData = req.formData;
|
|
|
|
}
|
|
|
|
|
2016-12-19 18:11:31 +08:00
|
|
|
debug('Request params', requestParams);
|
2019-12-24 01:19:08 +08:00
|
|
|
request(requestParams, function assert$response$requestHandler (error, response, body) {
|
2016-12-19 21:31:25 +08:00
|
|
|
debug('Request response', error);
|
2019-12-24 01:19:08 +08:00
|
|
|
listener.close(function () {
|
2016-12-19 18:11:31 +08:00
|
|
|
debug('Server closed');
|
2016-09-27 00:09:27 +08:00
|
|
|
if (error) {
|
|
|
|
return callback(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
response = response || {};
|
|
|
|
response.body = response.body || body;
|
2019-12-24 01:19:08 +08:00
|
|
|
debug('Response status', response.statusCode);
|
2016-09-27 00:09:27 +08:00
|
|
|
|
|
|
|
// Assert response body
|
|
|
|
if (res.body) {
|
|
|
|
var eql = res.body instanceof RegExp ? res.body.test(response.body) : res.body === response.body;
|
|
|
|
assert.ok(
|
|
|
|
eql,
|
|
|
|
colorize('[red]{Invalid response body.}\n' +
|
|
|
|
' Expected: [green]{' + res.body + '}\n' +
|
|
|
|
' Got: [red]{' + response.body + '}')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert response status
|
|
|
|
if (typeof status === 'number') {
|
|
|
|
assert.equal(response.statusCode, status,
|
|
|
|
colorize('[red]{Invalid response status code.}\n' +
|
|
|
|
' Expected: [green]{' + status + '}\n' +
|
|
|
|
' Got: [red]{' + response.statusCode + '}\n' +
|
|
|
|
' Body: ' + response.body)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert response headers
|
|
|
|
if (res.headers) {
|
|
|
|
var keys = Object.keys(res.headers);
|
|
|
|
for (var i = 0, len = keys.length; i < len; ++i) {
|
2019-12-24 01:19:08 +08:00
|
|
|
var name = keys[i];
|
|
|
|
var actual = response.headers[name.toLowerCase()];
|
|
|
|
var expected = res.headers[name];
|
|
|
|
var headerEql = expected instanceof RegExp ? expected.test(actual) : expected === actual;
|
2016-09-27 00:09:27 +08:00
|
|
|
assert.ok(headerEql,
|
|
|
|
colorize('Invalid response header [bold]{' + name + '}.\n' +
|
|
|
|
' Expected: [green]{' + expected + '}\n' +
|
|
|
|
' Got: [red]{' + actual + '}')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Callback
|
2016-12-19 17:50:05 +08:00
|
|
|
return callback(null, response);
|
2016-09-27 00:09:27 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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}
|
|
|
|
*/
|
2019-12-24 01:19:08 +08:00
|
|
|
function colorize (str) {
|
2014-08-27 23:09:31 +08:00
|
|
|
var colors = { bold: 1, red: 31, green: 32, yellow: 33 };
|
2019-12-24 01:19:08 +08:00
|
|
|
return str.replace(/\[(\w+)\]\{([^]*?)\}/g, function (_, color, str) {
|
2014-08-27 23:09:31 +08:00
|
|
|
return '\x1B[' + colors[color] + 'm' + str + '\x1B[0m';
|
|
|
|
});
|
2016-05-20 20:42:53 +08:00
|
|
|
}
|