|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 Cu.import("resource://services-sync/record.js"); |
|
5 Cu.import("resource://services-sync/identity.js"); |
|
6 Cu.import("resource://services-sync/resource.js"); |
|
7 Cu.import("resource://services-sync/service.js"); |
|
8 Cu.import("resource://services-sync/util.js"); |
|
9 Cu.import("resource://testing-common/services/sync/utils.js"); |
|
10 |
|
11 |
|
12 function test_toJSON() { |
|
13 _("Create a record, for now without a TTL."); |
|
14 let wbo = new WBORecord("coll", "a_record"); |
|
15 wbo.modified = 12345; |
|
16 wbo.sortindex = 42; |
|
17 wbo.payload = {}; |
|
18 |
|
19 _("Verify that the JSON representation contains the WBO properties, but not TTL."); |
|
20 let json = JSON.parse(JSON.stringify(wbo)); |
|
21 do_check_eq(json.modified, 12345); |
|
22 do_check_eq(json.sortindex, 42); |
|
23 do_check_eq(json.payload, "{}"); |
|
24 do_check_false("ttl" in json); |
|
25 |
|
26 _("Set a TTL, make sure it's present in the JSON representation."); |
|
27 wbo.ttl = 30*60; |
|
28 json = JSON.parse(JSON.stringify(wbo)); |
|
29 do_check_eq(json.ttl, 30*60); |
|
30 } |
|
31 |
|
32 |
|
33 function test_fetch() { |
|
34 let record = {id: "asdf-1234-asdf-1234", |
|
35 modified: 2454725.98283, |
|
36 payload: JSON.stringify({cheese: "roquefort"})}; |
|
37 let record2 = {id: "record2", |
|
38 modified: 2454725.98284, |
|
39 payload: JSON.stringify({cheese: "gruyere"})}; |
|
40 let coll = [{id: "record2", |
|
41 modified: 2454725.98284, |
|
42 payload: JSON.stringify({cheese: "gruyere"})}]; |
|
43 |
|
44 _("Setting up server."); |
|
45 let server = httpd_setup({ |
|
46 "/record": httpd_handler(200, "OK", JSON.stringify(record)), |
|
47 "/record2": httpd_handler(200, "OK", JSON.stringify(record2)), |
|
48 "/coll": httpd_handler(200, "OK", JSON.stringify(coll)) |
|
49 }); |
|
50 do_test_pending(); |
|
51 |
|
52 try { |
|
53 _("Fetching a WBO record"); |
|
54 let rec = new WBORecord("coll", "record"); |
|
55 rec.fetch(Service.resource(server.baseURI + "/record")); |
|
56 do_check_eq(rec.id, "asdf-1234-asdf-1234"); // NOT "record"! |
|
57 |
|
58 do_check_eq(rec.modified, 2454725.98283); |
|
59 do_check_eq(typeof(rec.payload), "object"); |
|
60 do_check_eq(rec.payload.cheese, "roquefort"); |
|
61 |
|
62 _("Fetching a WBO record using the record manager"); |
|
63 let rec2 = Service.recordManager.get(server.baseURI + "/record2"); |
|
64 do_check_eq(rec2.id, "record2"); |
|
65 do_check_eq(rec2.modified, 2454725.98284); |
|
66 do_check_eq(typeof(rec2.payload), "object"); |
|
67 do_check_eq(rec2.payload.cheese, "gruyere"); |
|
68 do_check_eq(Service.recordManager.response.status, 200); |
|
69 |
|
70 // Testing collection extraction. |
|
71 _("Extracting collection."); |
|
72 let rec3 = new WBORecord("tabs", "foo"); // Create through constructor. |
|
73 do_check_eq(rec3.collection, "tabs"); |
|
74 |
|
75 } finally { |
|
76 server.stop(do_test_finished); |
|
77 } |
|
78 } |
|
79 |
|
80 function run_test() { |
|
81 initTestLogging("Trace"); |
|
82 ensureLegacyIdentityManager(); |
|
83 |
|
84 test_toJSON(); |
|
85 test_fetch(); |
|
86 } |