icingaweb2/test/js/testlib/asyncmock.js

34 lines
906 B
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Helper for mocking $.async's XHR requests
*
*/
var getCallback = function(empty, response, succeed, headers) {
if (empty)
return function() {};
return function(callback) {
callback(response, succeed, {
getAllResponseHeaders: function() {
return headers;
},
getResponseHeader: function(header) {
return headers[header] || null;
}
});
};
};
module.exports = {
setNextAsyncResult: function(async, response, fails, headers) {
headers = headers || {};
var succeed = fails ? "fail" : "success";
async.__internalXHRImplementation = function(config) {
return {
done: getCallback(fails, response, succeed, headers),
fail: getCallback(!fails, response, succeed, headers)
};
};
}
};