Simple jQuery (1.5+) AJAX Mocking (requires JSON, tested in jQuery 1.7))

  • Download Gist
jquery.ba-simple-ajax-mocking.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
/*!
* Simple jQuery (1.5+) AJAX Mocking - v0.1.0 - 11/16/2011
* http://benalman.com/
*
* Copyright (c) 2011 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
 
(function($) {
 
// Process all rules for a given AJAX request.
function processRules(options) {
// The dataType (eg. "json").
var dataType = options.dataType;
// If a rule is matched, override the built-in transport.
var transport;
// Iterate over all specified rules for this dataType
$.each($.mockAjax.rules[dataType], function(_, rule) {
// Test the AJAX request URL against this rule's regexp.
var matches = options.url.match(rule.re);
// If there was a match, override the default transport.
if (matches) {
transport = {
// Override the transport's send to immediately return a result.
send: function(_, done) {
// Get the response value.
var response = rule.response;
// If the response is a function, invoke it, passing in the matches
// array and the AJAX request options, and get its result.
if ($.isFunction(response)) {
response = response(matches, options);
}
// If the dataType is "json" or "jsonp" and not a string, serialize
// it into a valid JSON string. Note: requires JSON!
if (/^json/.test(dataType) && typeof response !== "string") {
response = window.JSON ? JSON.stringify(response) : String(response);
}
// Respond successfully!
done("200", "success", {status: response});
},
// Don't do anything on abort. Don't abort. Should this do anything?
abort: $.noop
};
// Don't process any other rules for this AJAX request.
return false;
}
});
return transport;
}
 
// Mock AJAX requests for a given dataType and map of rules.
$.mockAjax = function(dataType, userRules) {
var rules = $.mockAjax.rules[dataType];
 
// If no rules exist for this datatype, create a place to store them and
// register an ajax transport handler for that datatype.
if (!rules) {
rules = $.mockAjax.rules[dataType] = {};
$.ajaxTransport(dataType, processRules);
}
 
// For each user rule specified, add an entry into this dataType's rules
// object, overwriting any already-existing rule with the same pattern.
$.each(userRules, function(pattern, response) {
rules[pattern] = {
// Compile a matching regexp up-front to save processing later.
re: new RegExp("^" + pattern + "$"),
// Store the response value / function.
response: response
};
});
};
 
// Initialize an empty rules object.
$.mockAjax.rules = {};
 
}(jQuery));
usage.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// Simulate your API.
 
$.mockAjax("json", {
"/user": {status: -1},
"/user/(\\d+)": function(matches) {
return {status: 1, user: "sample user " + matches[1]};
}
});
 
// Unit tests.
 
test("user tests", function() {
expect(5);
 
stop();
$.getJSON("/user", function(data) {
ok(data, "data is returned from the server");
equal(data.status, "-1", "no user specified, status should be -1");
start();
});
 
stop();
$.getJSON("/user/123", function(data) {
ok(data, "data is returned from the server");
equal(data.status, "1", "user found, status should be 1");
equal(data.user, "sample user 123", "user found, id should be 123");
start();
});
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.