michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: Cu.import("resource://services-sync/record.js"); michael@0: Cu.import("resource://services-sync/identity.js"); michael@0: Cu.import("resource://services-sync/resource.js"); michael@0: Cu.import("resource://services-sync/service.js"); michael@0: Cu.import("resource://services-sync/util.js"); michael@0: Cu.import("resource://testing-common/services/sync/utils.js"); michael@0: michael@0: michael@0: function test_toJSON() { michael@0: _("Create a record, for now without a TTL."); michael@0: let wbo = new WBORecord("coll", "a_record"); michael@0: wbo.modified = 12345; michael@0: wbo.sortindex = 42; michael@0: wbo.payload = {}; michael@0: michael@0: _("Verify that the JSON representation contains the WBO properties, but not TTL."); michael@0: let json = JSON.parse(JSON.stringify(wbo)); michael@0: do_check_eq(json.modified, 12345); michael@0: do_check_eq(json.sortindex, 42); michael@0: do_check_eq(json.payload, "{}"); michael@0: do_check_false("ttl" in json); michael@0: michael@0: _("Set a TTL, make sure it's present in the JSON representation."); michael@0: wbo.ttl = 30*60; michael@0: json = JSON.parse(JSON.stringify(wbo)); michael@0: do_check_eq(json.ttl, 30*60); michael@0: } michael@0: michael@0: michael@0: function test_fetch() { michael@0: let record = {id: "asdf-1234-asdf-1234", michael@0: modified: 2454725.98283, michael@0: payload: JSON.stringify({cheese: "roquefort"})}; michael@0: let record2 = {id: "record2", michael@0: modified: 2454725.98284, michael@0: payload: JSON.stringify({cheese: "gruyere"})}; michael@0: let coll = [{id: "record2", michael@0: modified: 2454725.98284, michael@0: payload: JSON.stringify({cheese: "gruyere"})}]; michael@0: michael@0: _("Setting up server."); michael@0: let server = httpd_setup({ michael@0: "/record": httpd_handler(200, "OK", JSON.stringify(record)), michael@0: "/record2": httpd_handler(200, "OK", JSON.stringify(record2)), michael@0: "/coll": httpd_handler(200, "OK", JSON.stringify(coll)) michael@0: }); michael@0: do_test_pending(); michael@0: michael@0: try { michael@0: _("Fetching a WBO record"); michael@0: let rec = new WBORecord("coll", "record"); michael@0: rec.fetch(Service.resource(server.baseURI + "/record")); michael@0: do_check_eq(rec.id, "asdf-1234-asdf-1234"); // NOT "record"! michael@0: michael@0: do_check_eq(rec.modified, 2454725.98283); michael@0: do_check_eq(typeof(rec.payload), "object"); michael@0: do_check_eq(rec.payload.cheese, "roquefort"); michael@0: michael@0: _("Fetching a WBO record using the record manager"); michael@0: let rec2 = Service.recordManager.get(server.baseURI + "/record2"); michael@0: do_check_eq(rec2.id, "record2"); michael@0: do_check_eq(rec2.modified, 2454725.98284); michael@0: do_check_eq(typeof(rec2.payload), "object"); michael@0: do_check_eq(rec2.payload.cheese, "gruyere"); michael@0: do_check_eq(Service.recordManager.response.status, 200); michael@0: michael@0: // Testing collection extraction. michael@0: _("Extracting collection."); michael@0: let rec3 = new WBORecord("tabs", "foo"); // Create through constructor. michael@0: do_check_eq(rec3.collection, "tabs"); michael@0: michael@0: } finally { michael@0: server.stop(do_test_finished); michael@0: } michael@0: } michael@0: michael@0: function run_test() { michael@0: initTestLogging("Trace"); michael@0: ensureLegacyIdentityManager(); michael@0: michael@0: test_toJSON(); michael@0: test_fetch(); michael@0: }