Rewrite assert.response using request module
This commit is contained in:
parent
967ef99277
commit
7fd870cfd2
@ -1,11 +1,12 @@
|
||||
// Cribbed from the ever prolific Konstantin Kaefer
|
||||
// https://github.com/mapbox/tilelive-mapnik/blob/master/test/support/assert.js
|
||||
|
||||
var exec = require('child_process').exec,
|
||||
fs = require('fs'),
|
||||
http = require('http'),
|
||||
path = require('path'),
|
||||
util = require('util');
|
||||
var exec = require('child_process').exec;
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
var util = require('util');
|
||||
|
||||
var request = require('request');
|
||||
|
||||
var assert = module.exports = exports = require('assert');
|
||||
|
||||
@ -82,140 +83,70 @@ function imageFilesAreEqual(testImageFilePath, referenceImageFilePath, tolerance
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert response from `server` with
|
||||
* the given `req` object and `res` assertions object.
|
||||
*
|
||||
* @param {Server|Object} server
|
||||
* @param {Object} req
|
||||
* @param {Object|Function} res
|
||||
* @param {String|Function} msg
|
||||
*/
|
||||
// jshint maxcomplexity:12
|
||||
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;
|
||||
// jshint maxcomplexity:9
|
||||
assert.response = function(server, req, res, callback) {
|
||||
if (!callback) {
|
||||
callback = res;
|
||||
res = {};
|
||||
}
|
||||
|
||||
var port = 5555,
|
||||
host = '127.0.0.1';
|
||||
|
||||
var listeningAttempts = 0;
|
||||
var listener;
|
||||
function listen() {
|
||||
if (listeningAttempts > 25) {
|
||||
return callback(null, new Error('Tried too many ports'));
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
// jshint maxcomplexity:8
|
||||
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,
|
||||
headers: req.headers
|
||||
listener = server.listen(port, host);
|
||||
listener.on('error', function() {
|
||||
port++;
|
||||
listeningAttempts++;
|
||||
listen();
|
||||
});
|
||||
listener.on('listening', onServerListening);
|
||||
}
|
||||
|
||||
var check = function() {
|
||||
if (--server.__pending === 0) {
|
||||
server.close();
|
||||
server.__listening = false;
|
||||
}
|
||||
listen();
|
||||
|
||||
// jshint maxcomplexity:9
|
||||
function onServerListening() {
|
||||
var status = res.status || res.statusCode;
|
||||
var requestParams = {
|
||||
url: 'http://' + host + ':' + port + req.url,
|
||||
method: req.method || 'GET',
|
||||
headers: req.headers || {},
|
||||
timeout: req.timeout || 0,
|
||||
encoding: req.encoding || 'utf8'
|
||||
};
|
||||
|
||||
// Timeout
|
||||
if (requestTimeout) {
|
||||
timer = setTimeout(function(){
|
||||
check();
|
||||
delete req.timeout;
|
||||
assert.fail(msg + 'Request timed out after ' + requestTimeout + 'ms.');
|
||||
}, requestTimeout);
|
||||
if (req.body || req.data) {
|
||||
requestParams.body = req.body || req.data;
|
||||
}
|
||||
|
||||
if (data) {
|
||||
request.write(data);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
check();
|
||||
request(requestParams, function assert$response$requestHandler(error, response, body) {
|
||||
listener.close(function() {
|
||||
response.body = response.body || body;
|
||||
|
||||
// Assert response body
|
||||
if (res.body !== undefined) {
|
||||
if (res.body) {
|
||||
var eql = res.body instanceof RegExp ? res.body.test(response.body) : res.body === response.body;
|
||||
assert.ok(
|
||||
eql,
|
||||
msg + colorize('[red]{Invalid response body.}\n' +
|
||||
' Expected: [green]{' + res.body + '}\n' +
|
||||
' Got: [red]{' + response.body + '}')
|
||||
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,
|
||||
msg + colorize('[red]{Invalid response status code.}\n' +
|
||||
' Expected: [green]{' + status + '}\n' +
|
||||
' Got: [red]{' + response.statusCode + '}\n' +
|
||||
' Body: ' + response.body)
|
||||
assert.equal(response.statusCode, status,
|
||||
colorize('[red]{Invalid response status code.}\n' +
|
||||
' Expected: [green]{' + status + '}\n' +
|
||||
' Got: [red]{' + response.statusCode + '}\n' +
|
||||
' Body: ' + response.body)
|
||||
);
|
||||
}
|
||||
|
||||
@ -227,11 +158,10 @@ assert.response = function(server, req, res, msg){
|
||||
actual = response.headers[name.toLowerCase()],
|
||||
expected = res.headers[name],
|
||||
headerEql = expected instanceof RegExp ? expected.test(actual) : expected === actual;
|
||||
assert.ok(
|
||||
headerEql,
|
||||
msg + colorize('Invalid response header [bold]{' + name + '}.\n' +
|
||||
' Expected: [green]{' + expected + '}\n' +
|
||||
' Got: [red]{' + actual + '}')
|
||||
assert.ok(headerEql,
|
||||
colorize('Invalid response header [bold]{' + name + '}.\n' +
|
||||
' Expected: [green]{' + expected + '}\n' +
|
||||
' Got: [red]{' + actual + '}')
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -241,12 +171,12 @@ assert.response = function(server, req, res, msg){
|
||||
});
|
||||
});
|
||||
|
||||
request.end();
|
||||
}
|
||||
};
|
||||
// jshint maxcomplexity:6
|
||||
|
||||
// @param tolerance number of tolerated grid cell differences
|
||||
// jshint maxcomplexity:8
|
||||
// jshint maxcomplexity:9
|
||||
assert.utfgridEqualsFile = function(buffer, file_b, tolerance, callback) {
|
||||
fs.writeFileSync('/tmp/grid.json', buffer, 'binary'); // <-- to debug/update
|
||||
var expected_json = JSON.parse(fs.readFileSync(file_b, 'utf8'));
|
||||
@ -265,7 +195,7 @@ assert.utfgridEqualsFile = function(buffer, file_b, tolerance, callback) {
|
||||
};
|
||||
|
||||
try {
|
||||
var obtained_json = JSON.parse(buffer);
|
||||
var obtained_json = Object.prototype.toString() === buffer.toString() ? buffer : JSON.parse(buffer);
|
||||
|
||||
// compare grid
|
||||
var obtained_grid = obtained_json.grid;
|
||||
|
Loading…
Reference in New Issue
Block a user