2 * MCTX3420 2013 - Remote pressurised can experiment.
3 * Unit testing for the server API.
4 * These unit tests use the QUnit unit testing framework.
5 * @requires QUnit and jQuery
10 var api = location.protocol + "//" + location.host + "/api/";
13 * Sends an AJAX query to the API
14 * @param {string} module The name of the module to be queried
15 * @param {Object} opts Object containing parameters to pass to module
16 * @param {function} callback Function that receives JSON data
17 * @param {string} username Optional
18 * @param {string} password Required if username specified
21 function query(module, opts, callback, username, password) {
22 function buildQuery(opts) {
31 result += encodeURIComponent(key) +
32 (opts.key ? "=" + encodeURIComponent(opts.key) : "");
37 var queryurl = api + module;
39 queryurl += buildQuery(opts);
45 beforeSend: !username ? undefined : function (xhr) {
46 xhr.setRequestHeader("Authorization",
47 "Basic " + btoa(username + ":" + password));
50 .fail(function(jqXHR) {
51 if (jqXHR.status === 400) {
52 callback($.parseJSON(jqXHR.responseText));
54 callback({status:-999,
55 description: jqXHR.status.toString() + " " + jqXHR.responseText});
60 QUnit.test("API Existence", function () {
62 query("test", undefined, function(data) {
63 equal(parseInt(data.status, 10), -1, "Nonexistent module"); //Magic numbers!
67 /*query("version", undefined, function (data) {
68 assert.equal(data.status, 0);