Add debugging info in assert.response

This commit is contained in:
Raul Ochoa 2016-12-19 10:50:05 +01:00
parent ac400016e8
commit 6c8929079c

View File

@ -1,5 +1,6 @@
var assert = module.exports = exports = require('assert');
var request = require('request');
var debug = require('debug')('assert-response');
assert.response = function(server, req, res, callback) {
if (!callback) {
@ -14,7 +15,9 @@ assert.response = function(server, req, res, callback) {
var listener;
function listen() {
if (listeningAttempts > 25) {
return callback(new Error('Tried too many ports'));
var message = 'Tried too many ports';
debug(message);
return callback(new Error(message));
}
listener = server.listen(port, host);
listener.on('error', function() {
@ -27,8 +30,11 @@ assert.response = function(server, req, res, callback) {
listen();
debug('Request', req);
// jshint maxcomplexity:10
function onServerListening() {
debug('Server listening on port = %d', port);
var status = res.status || res.statusCode;
var requestParams = {
url: 'http://' + host + ':' + port + req.url,
@ -44,12 +50,14 @@ assert.response = function(server, req, res, callback) {
request(requestParams, function assert$response$requestHandler(error, response, body) {
listener.close(function() {
debug('Server closed', error);
if (error) {
return callback(error);
}
response = response || {};
response.body = response.body || body;
debug('Response status', response.statusCode)
// Assert response body
if (res.body) {
@ -89,7 +97,7 @@ assert.response = function(server, req, res, callback) {
}
// Callback
callback(null, response);
return callback(null, response);
});
});