michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: _("Make sure Collection can correctly incrementally parse GET requests"); michael@0: Cu.import("resource://services-sync/record.js"); michael@0: Cu.import("resource://services-sync/service.js"); michael@0: michael@0: function run_test() { michael@0: let base = "http://fake/"; michael@0: let coll = new Collection("http://fake/uri/", WBORecord, Service); michael@0: let stream = { _data: "" }; michael@0: let called, recCount, sum; michael@0: michael@0: _("Not-JSON, string payloads are strings"); michael@0: called = false; michael@0: stream._data = '{"id":"hello","payload":"world"}\n'; michael@0: coll.recordHandler = function(rec) { michael@0: called = true; michael@0: _("Got record:", JSON.stringify(rec)); michael@0: rec.collection = "uri"; // This would be done by an engine, so do it here. michael@0: do_check_eq(rec.collection, "uri"); michael@0: do_check_eq(rec.id, "hello"); michael@0: do_check_eq(rec.uri(base).spec, "http://fake/uri/hello"); michael@0: do_check_eq(rec.payload, "world"); michael@0: }; michael@0: coll._onProgress.call(stream); michael@0: do_check_eq(stream._data, ''); michael@0: do_check_true(called); michael@0: _("\n"); michael@0: michael@0: michael@0: _("Parse record with payload"); michael@0: called = false; michael@0: stream._data = '{"payload":"{\\"value\\":123}"}\n'; michael@0: coll.recordHandler = function(rec) { michael@0: called = true; michael@0: _("Got record:", JSON.stringify(rec)); michael@0: do_check_eq(rec.payload.value, 123); michael@0: }; michael@0: coll._onProgress.call(stream); michael@0: do_check_eq(stream._data, ''); michael@0: do_check_true(called); michael@0: _("\n"); michael@0: michael@0: michael@0: _("Parse multiple records in one go"); michael@0: called = false; michael@0: recCount = 0; michael@0: sum = 0; michael@0: stream._data = '{"id":"hundred","payload":"{\\"value\\":100}"}\n{"id":"ten","payload":"{\\"value\\":10}"}\n{"id":"one","payload":"{\\"value\\":1}"}\n'; michael@0: coll.recordHandler = function(rec) { michael@0: called = true; michael@0: _("Got record:", JSON.stringify(rec)); michael@0: recCount++; michael@0: sum += rec.payload.value; michael@0: _("Incremental status: count", recCount, "sum", sum); michael@0: rec.collection = "uri"; michael@0: switch (recCount) { michael@0: case 1: michael@0: do_check_eq(rec.id, "hundred"); michael@0: do_check_eq(rec.uri(base).spec, "http://fake/uri/hundred"); michael@0: do_check_eq(rec.payload.value, 100); michael@0: do_check_eq(sum, 100); michael@0: break; michael@0: case 2: michael@0: do_check_eq(rec.id, "ten"); michael@0: do_check_eq(rec.uri(base).spec, "http://fake/uri/ten"); michael@0: do_check_eq(rec.payload.value, 10); michael@0: do_check_eq(sum, 110); michael@0: break; michael@0: case 3: michael@0: do_check_eq(rec.id, "one"); michael@0: do_check_eq(rec.uri(base).spec, "http://fake/uri/one"); michael@0: do_check_eq(rec.payload.value, 1); michael@0: do_check_eq(sum, 111); michael@0: break; michael@0: default: michael@0: do_throw("unexpected number of record counts", recCount); michael@0: break; michael@0: } michael@0: }; michael@0: coll._onProgress.call(stream); michael@0: do_check_eq(recCount, 3); michael@0: do_check_eq(sum, 111); michael@0: do_check_eq(stream._data, ''); michael@0: do_check_true(called); michael@0: _("\n"); michael@0: michael@0: michael@0: _("Handle incremental data incoming"); michael@0: called = false; michael@0: recCount = 0; michael@0: sum = 0; michael@0: stream._data = '{"payl'; michael@0: coll.recordHandler = function(rec) { michael@0: called = true; michael@0: do_throw("shouldn't have gotten a record.."); michael@0: }; michael@0: coll._onProgress.call(stream); michael@0: _("shouldn't have gotten anything yet"); michael@0: do_check_eq(recCount, 0); michael@0: do_check_eq(sum, 0); michael@0: _("leading array bracket should have been trimmed"); michael@0: do_check_eq(stream._data, '{"payl'); michael@0: do_check_false(called); michael@0: _(); michael@0: michael@0: _("adding more data enough for one record.."); michael@0: called = false; michael@0: stream._data += 'oad":"{\\"value\\":100}"}\n'; michael@0: coll.recordHandler = function(rec) { michael@0: called = true; michael@0: _("Got record:", JSON.stringify(rec)); michael@0: recCount++; michael@0: sum += rec.payload.value; michael@0: }; michael@0: coll._onProgress.call(stream); michael@0: _("should have 1 record with sum 100"); michael@0: do_check_eq(recCount, 1); michael@0: do_check_eq(sum, 100); michael@0: _("all data should have been consumed including trailing comma"); michael@0: do_check_eq(stream._data, ''); michael@0: do_check_true(called); michael@0: _(); michael@0: michael@0: _("adding more data.."); michael@0: called = false; michael@0: stream._data += '{"payload":"{\\"value\\":10}"'; michael@0: coll.recordHandler = function(rec) { michael@0: called = true; michael@0: do_throw("shouldn't have gotten a record.."); michael@0: }; michael@0: coll._onProgress.call(stream); michael@0: _("should still have 1 record with sum 100"); michael@0: do_check_eq(recCount, 1); michael@0: do_check_eq(sum, 100); michael@0: _("should almost have a record"); michael@0: do_check_eq(stream._data, '{"payload":"{\\"value\\":10}"'); michael@0: do_check_false(called); michael@0: _(); michael@0: michael@0: _("add data for two records.."); michael@0: called = false; michael@0: stream._data += '}\n{"payload":"{\\"value\\":1}"}\n'; michael@0: coll.recordHandler = function(rec) { michael@0: called = true; michael@0: _("Got record:", JSON.stringify(rec)); michael@0: recCount++; michael@0: sum += rec.payload.value; michael@0: switch (recCount) { michael@0: case 2: michael@0: do_check_eq(rec.payload.value, 10); michael@0: do_check_eq(sum, 110); michael@0: break; michael@0: case 3: michael@0: do_check_eq(rec.payload.value, 1); michael@0: do_check_eq(sum, 111); michael@0: break; michael@0: default: michael@0: do_throw("unexpected number of record counts", recCount); michael@0: break; michael@0: } michael@0: }; michael@0: coll._onProgress.call(stream); michael@0: _("should have gotten all 3 records with sum 111"); michael@0: do_check_eq(recCount, 3); michael@0: do_check_eq(sum, 111); michael@0: _("should have consumed all data"); michael@0: do_check_eq(stream._data, ''); michael@0: do_check_true(called); michael@0: _(); michael@0: michael@0: _("add no extra data"); michael@0: called = false; michael@0: stream._data += ''; michael@0: coll.recordHandler = function(rec) { michael@0: called = true; michael@0: do_throw("shouldn't have gotten a record.."); michael@0: }; michael@0: coll._onProgress.call(stream); michael@0: _("should still have 3 records with sum 111"); michael@0: do_check_eq(recCount, 3); michael@0: do_check_eq(sum, 111); michael@0: _("should have consumed nothing but still have nothing"); michael@0: do_check_eq(stream._data, ""); michael@0: do_check_false(called); michael@0: _("\n"); michael@0: }