services/sync/tests/unit/test_collection_inc_get.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 _("Make sure Collection can correctly incrementally parse GET requests");
michael@0 5 Cu.import("resource://services-sync/record.js");
michael@0 6 Cu.import("resource://services-sync/service.js");
michael@0 7
michael@0 8 function run_test() {
michael@0 9 let base = "http://fake/";
michael@0 10 let coll = new Collection("http://fake/uri/", WBORecord, Service);
michael@0 11 let stream = { _data: "" };
michael@0 12 let called, recCount, sum;
michael@0 13
michael@0 14 _("Not-JSON, string payloads are strings");
michael@0 15 called = false;
michael@0 16 stream._data = '{"id":"hello","payload":"world"}\n';
michael@0 17 coll.recordHandler = function(rec) {
michael@0 18 called = true;
michael@0 19 _("Got record:", JSON.stringify(rec));
michael@0 20 rec.collection = "uri"; // This would be done by an engine, so do it here.
michael@0 21 do_check_eq(rec.collection, "uri");
michael@0 22 do_check_eq(rec.id, "hello");
michael@0 23 do_check_eq(rec.uri(base).spec, "http://fake/uri/hello");
michael@0 24 do_check_eq(rec.payload, "world");
michael@0 25 };
michael@0 26 coll._onProgress.call(stream);
michael@0 27 do_check_eq(stream._data, '');
michael@0 28 do_check_true(called);
michael@0 29 _("\n");
michael@0 30
michael@0 31
michael@0 32 _("Parse record with payload");
michael@0 33 called = false;
michael@0 34 stream._data = '{"payload":"{\\"value\\":123}"}\n';
michael@0 35 coll.recordHandler = function(rec) {
michael@0 36 called = true;
michael@0 37 _("Got record:", JSON.stringify(rec));
michael@0 38 do_check_eq(rec.payload.value, 123);
michael@0 39 };
michael@0 40 coll._onProgress.call(stream);
michael@0 41 do_check_eq(stream._data, '');
michael@0 42 do_check_true(called);
michael@0 43 _("\n");
michael@0 44
michael@0 45
michael@0 46 _("Parse multiple records in one go");
michael@0 47 called = false;
michael@0 48 recCount = 0;
michael@0 49 sum = 0;
michael@0 50 stream._data = '{"id":"hundred","payload":"{\\"value\\":100}"}\n{"id":"ten","payload":"{\\"value\\":10}"}\n{"id":"one","payload":"{\\"value\\":1}"}\n';
michael@0 51 coll.recordHandler = function(rec) {
michael@0 52 called = true;
michael@0 53 _("Got record:", JSON.stringify(rec));
michael@0 54 recCount++;
michael@0 55 sum += rec.payload.value;
michael@0 56 _("Incremental status: count", recCount, "sum", sum);
michael@0 57 rec.collection = "uri";
michael@0 58 switch (recCount) {
michael@0 59 case 1:
michael@0 60 do_check_eq(rec.id, "hundred");
michael@0 61 do_check_eq(rec.uri(base).spec, "http://fake/uri/hundred");
michael@0 62 do_check_eq(rec.payload.value, 100);
michael@0 63 do_check_eq(sum, 100);
michael@0 64 break;
michael@0 65 case 2:
michael@0 66 do_check_eq(rec.id, "ten");
michael@0 67 do_check_eq(rec.uri(base).spec, "http://fake/uri/ten");
michael@0 68 do_check_eq(rec.payload.value, 10);
michael@0 69 do_check_eq(sum, 110);
michael@0 70 break;
michael@0 71 case 3:
michael@0 72 do_check_eq(rec.id, "one");
michael@0 73 do_check_eq(rec.uri(base).spec, "http://fake/uri/one");
michael@0 74 do_check_eq(rec.payload.value, 1);
michael@0 75 do_check_eq(sum, 111);
michael@0 76 break;
michael@0 77 default:
michael@0 78 do_throw("unexpected number of record counts", recCount);
michael@0 79 break;
michael@0 80 }
michael@0 81 };
michael@0 82 coll._onProgress.call(stream);
michael@0 83 do_check_eq(recCount, 3);
michael@0 84 do_check_eq(sum, 111);
michael@0 85 do_check_eq(stream._data, '');
michael@0 86 do_check_true(called);
michael@0 87 _("\n");
michael@0 88
michael@0 89
michael@0 90 _("Handle incremental data incoming");
michael@0 91 called = false;
michael@0 92 recCount = 0;
michael@0 93 sum = 0;
michael@0 94 stream._data = '{"payl';
michael@0 95 coll.recordHandler = function(rec) {
michael@0 96 called = true;
michael@0 97 do_throw("shouldn't have gotten a record..");
michael@0 98 };
michael@0 99 coll._onProgress.call(stream);
michael@0 100 _("shouldn't have gotten anything yet");
michael@0 101 do_check_eq(recCount, 0);
michael@0 102 do_check_eq(sum, 0);
michael@0 103 _("leading array bracket should have been trimmed");
michael@0 104 do_check_eq(stream._data, '{"payl');
michael@0 105 do_check_false(called);
michael@0 106 _();
michael@0 107
michael@0 108 _("adding more data enough for one record..");
michael@0 109 called = false;
michael@0 110 stream._data += 'oad":"{\\"value\\":100}"}\n';
michael@0 111 coll.recordHandler = function(rec) {
michael@0 112 called = true;
michael@0 113 _("Got record:", JSON.stringify(rec));
michael@0 114 recCount++;
michael@0 115 sum += rec.payload.value;
michael@0 116 };
michael@0 117 coll._onProgress.call(stream);
michael@0 118 _("should have 1 record with sum 100");
michael@0 119 do_check_eq(recCount, 1);
michael@0 120 do_check_eq(sum, 100);
michael@0 121 _("all data should have been consumed including trailing comma");
michael@0 122 do_check_eq(stream._data, '');
michael@0 123 do_check_true(called);
michael@0 124 _();
michael@0 125
michael@0 126 _("adding more data..");
michael@0 127 called = false;
michael@0 128 stream._data += '{"payload":"{\\"value\\":10}"';
michael@0 129 coll.recordHandler = function(rec) {
michael@0 130 called = true;
michael@0 131 do_throw("shouldn't have gotten a record..");
michael@0 132 };
michael@0 133 coll._onProgress.call(stream);
michael@0 134 _("should still have 1 record with sum 100");
michael@0 135 do_check_eq(recCount, 1);
michael@0 136 do_check_eq(sum, 100);
michael@0 137 _("should almost have a record");
michael@0 138 do_check_eq(stream._data, '{"payload":"{\\"value\\":10}"');
michael@0 139 do_check_false(called);
michael@0 140 _();
michael@0 141
michael@0 142 _("add data for two records..");
michael@0 143 called = false;
michael@0 144 stream._data += '}\n{"payload":"{\\"value\\":1}"}\n';
michael@0 145 coll.recordHandler = function(rec) {
michael@0 146 called = true;
michael@0 147 _("Got record:", JSON.stringify(rec));
michael@0 148 recCount++;
michael@0 149 sum += rec.payload.value;
michael@0 150 switch (recCount) {
michael@0 151 case 2:
michael@0 152 do_check_eq(rec.payload.value, 10);
michael@0 153 do_check_eq(sum, 110);
michael@0 154 break;
michael@0 155 case 3:
michael@0 156 do_check_eq(rec.payload.value, 1);
michael@0 157 do_check_eq(sum, 111);
michael@0 158 break;
michael@0 159 default:
michael@0 160 do_throw("unexpected number of record counts", recCount);
michael@0 161 break;
michael@0 162 }
michael@0 163 };
michael@0 164 coll._onProgress.call(stream);
michael@0 165 _("should have gotten all 3 records with sum 111");
michael@0 166 do_check_eq(recCount, 3);
michael@0 167 do_check_eq(sum, 111);
michael@0 168 _("should have consumed all data");
michael@0 169 do_check_eq(stream._data, '');
michael@0 170 do_check_true(called);
michael@0 171 _();
michael@0 172
michael@0 173 _("add no extra data");
michael@0 174 called = false;
michael@0 175 stream._data += '';
michael@0 176 coll.recordHandler = function(rec) {
michael@0 177 called = true;
michael@0 178 do_throw("shouldn't have gotten a record..");
michael@0 179 };
michael@0 180 coll._onProgress.call(stream);
michael@0 181 _("should still have 3 records with sum 111");
michael@0 182 do_check_eq(recCount, 3);
michael@0 183 do_check_eq(sum, 111);
michael@0 184 _("should have consumed nothing but still have nothing");
michael@0 185 do_check_eq(stream._data, "");
michael@0 186 do_check_false(called);
michael@0 187 _("\n");
michael@0 188 }

mercurial