1
_("Make sure Collection can correctly incrementally parse GET requests");
2
Cu.import("resource://services-sync/record.js");
5
let base = "http://fake/";
6
let coll = new Collection("http://fake/uri/", WBORecord);
7
let stream = { _data: "" };
8
let called, recCount, sum;
10
_("Not-JSON, string payloads are strings");
12
stream._data = '{"id":"hello","payload":"world"}\n';
13
coll.recordHandler = function(rec) {
15
_("Got record:", JSON.stringify(rec));
16
rec.collection = "uri"; // This would be done by an engine, so do it here.
17
do_check_eq(rec.collection, "uri");
18
do_check_eq(rec.id, "hello");
19
do_check_eq(rec.uri(base).spec, "http://fake/uri/hello");
20
do_check_eq(rec.payload, "world");
22
coll._onProgress.call(stream);
23
do_check_eq(stream._data, '');
24
do_check_true(called);
28
_("Parse record with payload");
30
stream._data = '{"payload":"{\\"value\\":123}"}\n';
31
coll.recordHandler = function(rec) {
33
_("Got record:", JSON.stringify(rec));
34
do_check_eq(rec.payload.value, 123);
36
coll._onProgress.call(stream);
37
do_check_eq(stream._data, '');
38
do_check_true(called);
42
_("Parse multiple records in one go");
46
stream._data = '{"id":"hundred","payload":"{\\"value\\":100}"}\n{"id":"ten","payload":"{\\"value\\":10}"}\n{"id":"one","payload":"{\\"value\\":1}"}\n';
47
coll.recordHandler = function(rec) {
49
_("Got record:", JSON.stringify(rec));
51
sum += rec.payload.value;
52
_("Incremental status: count", recCount, "sum", sum);
53
rec.collection = "uri";
56
do_check_eq(rec.id, "hundred");
57
do_check_eq(rec.uri(base).spec, "http://fake/uri/hundred");
58
do_check_eq(rec.payload.value, 100);
59
do_check_eq(sum, 100);
62
do_check_eq(rec.id, "ten");
63
do_check_eq(rec.uri(base).spec, "http://fake/uri/ten");
64
do_check_eq(rec.payload.value, 10);
65
do_check_eq(sum, 110);
68
do_check_eq(rec.id, "one");
69
do_check_eq(rec.uri(base).spec, "http://fake/uri/one");
70
do_check_eq(rec.payload.value, 1);
71
do_check_eq(sum, 111);
74
do_throw("unexpected number of record counts", recCount);
78
coll._onProgress.call(stream);
79
do_check_eq(recCount, 3);
80
do_check_eq(sum, 111);
81
do_check_eq(stream._data, '');
82
do_check_true(called);
86
_("Handle incremental data incoming");
90
stream._data = '{"payl';
91
coll.recordHandler = function(rec) {
93
do_throw("shouldn't have gotten a record..");
95
coll._onProgress.call(stream);
96
_("shouldn't have gotten anything yet");
97
do_check_eq(recCount, 0);
99
_("leading array bracket should have been trimmed");
100
do_check_eq(stream._data, '{"payl');
101
do_check_false(called);
104
_("adding more data enough for one record..");
106
stream._data += 'oad":"{\\"value\\":100}"}\n';
107
coll.recordHandler = function(rec) {
109
_("Got record:", JSON.stringify(rec));
111
sum += rec.payload.value;
113
coll._onProgress.call(stream);
114
_("should have 1 record with sum 100");
115
do_check_eq(recCount, 1);
116
do_check_eq(sum, 100);
117
_("all data should have been consumed including trailing comma");
118
do_check_eq(stream._data, '');
119
do_check_true(called);
122
_("adding more data..");
124
stream._data += '{"payload":"{\\"value\\":10}"';
125
coll.recordHandler = function(rec) {
127
do_throw("shouldn't have gotten a record..");
129
coll._onProgress.call(stream);
130
_("should still have 1 record with sum 100");
131
do_check_eq(recCount, 1);
132
do_check_eq(sum, 100);
133
_("should almost have a record");
134
do_check_eq(stream._data, '{"payload":"{\\"value\\":10}"');
135
do_check_false(called);
138
_("add data for two records..");
140
stream._data += '}\n{"payload":"{\\"value\\":1}"}\n';
141
coll.recordHandler = function(rec) {
143
_("Got record:", JSON.stringify(rec));
145
sum += rec.payload.value;
148
do_check_eq(rec.payload.value, 10);
149
do_check_eq(sum, 110);
152
do_check_eq(rec.payload.value, 1);
153
do_check_eq(sum, 111);
156
do_throw("unexpected number of record counts", recCount);
160
coll._onProgress.call(stream);
161
_("should have gotten all 3 records with sum 111");
162
do_check_eq(recCount, 3);
163
do_check_eq(sum, 111);
164
_("should have consumed all data");
165
do_check_eq(stream._data, '');
166
do_check_true(called);
169
_("add no extra data");
172
coll.recordHandler = function(rec) {
174
do_throw("shouldn't have gotten a record..");
176
coll._onProgress.call(stream);
177
_("should still have 3 records with sum 111");
178
do_check_eq(recCount, 3);
179
do_check_eq(sum, 111);
180
_("should have consumed nothing but still have nothing");
181
do_check_eq(stream._data, "");
182
do_check_false(called);