2012-09-14 16:02:32 -07:00
|
|
|
/* Any copyright is dedicated to the Public Domain.
|
|
|
|
http://creativecommons.org/publicdomain/zero/1.0/ */
|
|
|
|
|
2009-07-22 16:38:34 -07:00
|
|
|
_("Make sure Collection can correctly incrementally parse GET requests");
|
2011-01-18 16:23:30 -08:00
|
|
|
Cu.import("resource://services-sync/record.js");
|
2012-09-14 16:02:32 -07:00
|
|
|
Cu.import("resource://services-sync/service.js");
|
2009-07-22 16:38:34 -07:00
|
|
|
|
|
|
|
function run_test() {
|
2010-11-29 16:41:17 -08:00
|
|
|
let base = "http://fake/";
|
2012-09-14 16:02:32 -07:00
|
|
|
let coll = new Collection("http://fake/uri/", WBORecord, Service);
|
2009-07-22 16:38:34 -07:00
|
|
|
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-08-12 13:19:41 -07:00
|
|
|
stream._data = '{"id":"hello","payload":"world"}\n';
|
2009-07-22 16:38:34 -07:00
|
|
|
coll.recordHandler = function(rec) {
|
|
|
|
called = true;
|
|
|
|
_("Got record:", JSON.stringify(rec));
|
2010-11-29 16:41:17 -08:00
|
|
|
rec.collection = "uri"; // This would be done by an engine, so do it here.
|
|
|
|
do_check_eq(rec.collection, "uri");
|
2010-08-12 13:19:41 -07:00
|
|
|
do_check_eq(rec.id, "hello");
|
2010-11-29 16:41:17 -08:00
|
|
|
do_check_eq(rec.uri(base).spec, "http://fake/uri/hello");
|
2010-08-12 13:19:41 -07:00
|
|
|
do_check_eq(rec.payload, "world");
|
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;
|
2010-08-12 13:19:41 -07:00
|
|
|
stream._data = '{"id":"hundred","payload":"{\\"value\\":100}"}\n{"id":"ten","payload":"{\\"value\\":10}"}\n{"id":"one","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);
|
2010-11-29 16:41:17 -08:00
|
|
|
rec.collection = "uri";
|
2009-07-22 16:38:34 -07:00
|
|
|
switch (recCount) {
|
|
|
|
case 1:
|
2010-08-12 13:19:41 -07:00
|
|
|
do_check_eq(rec.id, "hundred");
|
2010-11-29 16:41:17 -08:00
|
|
|
do_check_eq(rec.uri(base).spec, "http://fake/uri/hundred");
|
2009-07-22 16:38:34 -07:00
|
|
|
do_check_eq(rec.payload.value, 100);
|
|
|
|
do_check_eq(sum, 100);
|
|
|
|
break;
|
|
|
|
case 2:
|
2010-08-12 13:19:41 -07:00
|
|
|
do_check_eq(rec.id, "ten");
|
2010-11-29 16:41:17 -08:00
|
|
|
do_check_eq(rec.uri(base).spec, "http://fake/uri/ten");
|
2009-07-22 16:38:34 -07:00
|
|
|
do_check_eq(rec.payload.value, 10);
|
|
|
|
do_check_eq(sum, 110);
|
|
|
|
break;
|
|
|
|
case 3:
|
2010-08-12 13:19:41 -07:00
|
|
|
do_check_eq(rec.id, "one");
|
2010-11-29 16:41:17 -08:00
|
|
|
do_check_eq(rec.uri(base).spec, "http://fake/uri/one");
|
2009-07-22 16:38:34 -07:00
|
|
|
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");
|
|
|
|
}
|