2009-07-22 16:38:34 -07:00
|
|
|
_("Make sure Collection can correctly incrementally parse GET requests");
|
|
|
|
Cu.import("resource://weave/base_records/collection.js");
|
|
|
|
Cu.import("resource://weave/base_records/wbo.js");
|
|
|
|
|
|
|
|
function run_test() {
|
|
|
|
let coll = new Collection("", WBORecord);
|
|
|
|
let stream = { _data: "" };
|
|
|
|
let called, recCount, sum;
|
|
|
|
|
2010-03-16 16:31:56 -07:00
|
|
|
_("Not-JSON, string payloads are strings");
|
2009-07-22 16:38:34 -07:00
|
|
|
called = false;
|
2010-03-16 16:31:56 -07:00
|
|
|
stream._data = '{"payload":"hello"}\n';
|
2009-07-22 16:38:34 -07:00
|
|
|
coll.recordHandler = function(rec) {
|
|
|
|
called = true;
|
|
|
|
_("Got record:", JSON.stringify(rec));
|
2010-03-16 16:31:56 -07:00
|
|
|
do_check_eq(rec.payload, "hello");
|
2009-07-22 16:38:34 -07:00
|
|
|
};
|
|
|
|
coll._onProgress.call(stream);
|
|
|
|
do_check_eq(stream._data, '');
|
|
|
|
do_check_true(called);
|
|
|
|
_("\n");
|
|
|
|
|
|
|
|
|
|
|
|
_("Parse record with payload");
|
|
|
|
called = false;
|
2009-08-26 18:09:41 -07:00
|
|
|
stream._data = '{"payload":"{\\"value\\":123}"}\n';
|
2009-07-22 16:38:34 -07:00
|
|
|
coll.recordHandler = function(rec) {
|
|
|
|
called = true;
|
|
|
|
_("Got record:", JSON.stringify(rec));
|
|
|
|
do_check_eq(rec.payload.value, 123);
|
|
|
|
};
|
|
|
|
coll._onProgress.call(stream);
|
|
|
|
do_check_eq(stream._data, '');
|
|
|
|
do_check_true(called);
|
|
|
|
_("\n");
|
|
|
|
|
|
|
|
|
|
|
|
_("Parse multiple records in one go");
|
|
|
|
called = false;
|
|
|
|
recCount = 0;
|
|
|
|
sum = 0;
|
2009-08-26 18:09:41 -07:00
|
|
|
stream._data = '{"payload":"{\\"value\\":100}"}\n{"payload":"{\\"value\\":10}"}\n{"payload":"{\\"value\\":1}"}\n';
|
2009-07-22 16:38:34 -07:00
|
|
|
coll.recordHandler = function(rec) {
|
|
|
|
called = true;
|
|
|
|
_("Got record:", JSON.stringify(rec));
|
|
|
|
recCount++;
|
|
|
|
sum += rec.payload.value;
|
|
|
|
_("Incremental status: count", recCount, "sum", sum);
|
|
|
|
switch (recCount) {
|
|
|
|
case 1:
|
|
|
|
do_check_eq(rec.payload.value, 100);
|
|
|
|
do_check_eq(sum, 100);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
do_check_eq(rec.payload.value, 10);
|
|
|
|
do_check_eq(sum, 110);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
do_check_eq(rec.payload.value, 1);
|
|
|
|
do_check_eq(sum, 111);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
do_throw("unexpected number of record counts", recCount);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
coll._onProgress.call(stream);
|
|
|
|
do_check_eq(recCount, 3);
|
|
|
|
do_check_eq(sum, 111);
|
|
|
|
do_check_eq(stream._data, '');
|
|
|
|
do_check_true(called);
|
|
|
|
_("\n");
|
|
|
|
|
|
|
|
|
|
|
|
_("Handle incremental data incoming");
|
|
|
|
called = false;
|
|
|
|
recCount = 0;
|
|
|
|
sum = 0;
|
2009-08-26 18:09:41 -07:00
|
|
|
stream._data = '{"payl';
|
2009-07-22 16:38:34 -07:00
|
|
|
coll.recordHandler = function(rec) {
|
|
|
|
called = true;
|
|
|
|
do_throw("shouldn't have gotten a record..");
|
|
|
|
};
|
|
|
|
coll._onProgress.call(stream);
|
|
|
|
_("shouldn't have gotten anything yet");
|
|
|
|
do_check_eq(recCount, 0);
|
|
|
|
do_check_eq(sum, 0);
|
|
|
|
_("leading array bracket should have been trimmed");
|
|
|
|
do_check_eq(stream._data, '{"payl');
|
|
|
|
do_check_false(called);
|
|
|
|
_();
|
|
|
|
|
|
|
|
_("adding more data enough for one record..");
|
|
|
|
called = false;
|
2009-08-26 18:09:41 -07:00
|
|
|
stream._data += 'oad":"{\\"value\\":100}"}\n';
|
2009-07-22 16:38:34 -07:00
|
|
|
coll.recordHandler = function(rec) {
|
|
|
|
called = true;
|
|
|
|
_("Got record:", JSON.stringify(rec));
|
|
|
|
recCount++;
|
|
|
|
sum += rec.payload.value;
|
|
|
|
};
|
|
|
|
coll._onProgress.call(stream);
|
|
|
|
_("should have 1 record with sum 100");
|
|
|
|
do_check_eq(recCount, 1);
|
|
|
|
do_check_eq(sum, 100);
|
|
|
|
_("all data should have been consumed including trailing comma");
|
|
|
|
do_check_eq(stream._data, '');
|
|
|
|
do_check_true(called);
|
|
|
|
_();
|
|
|
|
|
|
|
|
_("adding more data..");
|
|
|
|
called = false;
|
|
|
|
stream._data += '{"payload":"{\\"value\\":10}"';
|
|
|
|
coll.recordHandler = function(rec) {
|
|
|
|
called = true;
|
|
|
|
do_throw("shouldn't have gotten a record..");
|
|
|
|
};
|
|
|
|
coll._onProgress.call(stream);
|
|
|
|
_("should still have 1 record with sum 100");
|
|
|
|
do_check_eq(recCount, 1);
|
|
|
|
do_check_eq(sum, 100);
|
|
|
|
_("should almost have a record");
|
|
|
|
do_check_eq(stream._data, '{"payload":"{\\"value\\":10}"');
|
|
|
|
do_check_false(called);
|
|
|
|
_();
|
|
|
|
|
|
|
|
_("add data for two records..");
|
|
|
|
called = false;
|
2009-08-26 18:09:41 -07:00
|
|
|
stream._data += '}\n{"payload":"{\\"value\\":1}"}\n';
|
2009-07-22 16:38:34 -07:00
|
|
|
coll.recordHandler = function(rec) {
|
|
|
|
called = true;
|
|
|
|
_("Got record:", JSON.stringify(rec));
|
|
|
|
recCount++;
|
|
|
|
sum += rec.payload.value;
|
|
|
|
switch (recCount) {
|
|
|
|
case 2:
|
|
|
|
do_check_eq(rec.payload.value, 10);
|
|
|
|
do_check_eq(sum, 110);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
do_check_eq(rec.payload.value, 1);
|
|
|
|
do_check_eq(sum, 111);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
do_throw("unexpected number of record counts", recCount);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
coll._onProgress.call(stream);
|
|
|
|
_("should have gotten all 3 records with sum 111");
|
|
|
|
do_check_eq(recCount, 3);
|
|
|
|
do_check_eq(sum, 111);
|
|
|
|
_("should have consumed all data");
|
|
|
|
do_check_eq(stream._data, '');
|
|
|
|
do_check_true(called);
|
|
|
|
_();
|
|
|
|
|
2009-08-26 18:09:41 -07:00
|
|
|
_("add no extra data");
|
2009-07-22 16:38:34 -07:00
|
|
|
called = false;
|
2009-08-26 18:09:41 -07:00
|
|
|
stream._data += '';
|
2009-07-22 16:38:34 -07:00
|
|
|
coll.recordHandler = function(rec) {
|
|
|
|
called = true;
|
|
|
|
do_throw("shouldn't have gotten a record..");
|
|
|
|
};
|
|
|
|
coll._onProgress.call(stream);
|
|
|
|
_("should still have 3 records with sum 111");
|
|
|
|
do_check_eq(recCount, 3);
|
|
|
|
do_check_eq(sum, 111);
|
2009-08-26 18:09:41 -07:00
|
|
|
_("should have consumed nothing but still have nothing");
|
2009-07-22 16:38:34 -07:00
|
|
|
do_check_eq(stream._data, "");
|
|
|
|
do_check_false(called);
|
|
|
|
_("\n");
|
|
|
|
}
|