c16b8c916a8676ed17b29ad01ec5766ba3fa5d53
[matches/MCTX3420.git] / testing / qunit / unit-tests.js
1 /**
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
6  * @date 28/8/13
7  * @author Jeremy Tan
8  */
9
10 var api = location.protocol + "//" +  location.host + "/api/";
11
12 /**
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
19  * @returns JSON data
20  */
21 function query(module, opts, callback, username, password) {
22   function buildQuery(opts) {
23     var result = "?";
24     var first = true;
25     
26     for (key in opts) {
27       if (!first) 
28         result += "&";
29       else 
30         first = false;
31       result += encodeURIComponent(key) + 
32                 (opts.key ? "=" + encodeURIComponent(opts.key) : "");
33     }
34     return result;
35   }
36   
37   var queryurl = api + module;
38   if (opts)
39     queryurl += buildQuery(opts);
40   
41   $.ajax({
42     url: queryurl,
43     type: 'GET',
44     dataType: 'json',
45     beforeSend: !username ? undefined : function (xhr) { 
46         xhr.setRequestHeader("Authorization", 
47             "Basic " + btoa(username + ":" + password)); 
48     }
49   }).done(callback)
50     .fail(function(jqXHR) {
51       if (jqXHR.status === 400) {
52         callback($.parseJSON(jqXHR.responseText));
53       } else {
54         callback({status:-999, 
55           description: jqXHR.status.toString() + " " + jqXHR.responseText});
56       }
57     });
58 }
59
60 QUnit.test("API Existence", function () {
61   stop(); //?????
62   query("test", undefined, function(data) {
63    equal(parseInt(data.status, 10), -1, "Nonexistent module"); //Magic numbers!
64    start();
65   });   
66
67   /*query("version", undefined, function (data) {
68     assert.equal(data.status, 0);
69   });*/
70   
71 });

UCC git Repository :: git.ucc.asn.au