1
_("Make sure Collection can correctly incrementally parse GET requests");
2
Cu.import("resource://weave/base_records/collection.js");
3
Cu.import("resource://weave/base_records/wbo.js");
6
let coll = new Collection("", WBORecord);
7
let stream = { _data: "" };
8
let called, recCount, sum;
10
_("Not-JSON, string payloads are strings");
12
stream._data = '{"payload":"hello"}\n';
13
coll.recordHandler = function(rec) {
15
_("Got record:", JSON.stringify(rec));
16
do_check_eq(rec.payload, "hello");
18
coll._onProgress.call(stream);
19
do_check_eq(stream._data, '');
20
do_check_true(called);
24
_("Parse record with payload");
26
stream._data = '{"payload":"{\\"value\\":123}"}\n';
27
coll.recordHandler = function(rec) {
29
_("Got record:", JSON.stringify(rec));
30
do_check_eq(rec.payload.value, 123);
32
coll._onProgress.call(stream);
33
do_check_eq(stream._data, '');
34
do_check_true(called);
38
_("Parse multiple records in one go");
42
stream._data = '{"payload":"{\\"value\\":100}"}\n{"payload":"{\\"value\\":10}"}\n{"payload":"{\\"value\\":1}"}\n';
43
coll.recordHandler = function(rec) {
45
_("Got record:", JSON.stringify(rec));
47
sum += rec.payload.value;
48
_("Incremental status: count", recCount, "sum", sum);
51
do_check_eq(rec.payload.value, 100);
52
do_check_eq(sum, 100);
55
do_check_eq(rec.payload.value, 10);
56
do_check_eq(sum, 110);
59
do_check_eq(rec.payload.value, 1);
60
do_check_eq(sum, 111);
63
do_throw("unexpected number of record counts", recCount);
67
coll._onProgress.call(stream);
68
do_check_eq(recCount, 3);
69
do_check_eq(sum, 111);
70
do_check_eq(stream._data, '');
71
do_check_true(called);
75
_("Handle incremental data incoming");
79
stream._data = '{"payl';
80
coll.recordHandler = function(rec) {
82
do_throw("shouldn't have gotten a record..");
84
coll._onProgress.call(stream);
85
_("shouldn't have gotten anything yet");
86
do_check_eq(recCount, 0);
88
_("leading array bracket should have been trimmed");
89
do_check_eq(stream._data, '{"payl');
90
do_check_false(called);
93
_("adding more data enough for one record..");
95
stream._data += 'oad":"{\\"value\\":100}"}\n';
96
coll.recordHandler = function(rec) {
98
_("Got record:", JSON.stringify(rec));
100
sum += rec.payload.value;
102
coll._onProgress.call(stream);
103
_("should have 1 record with sum 100");
104
do_check_eq(recCount, 1);
105
do_check_eq(sum, 100);
106
_("all data should have been consumed including trailing comma");
107
do_check_eq(stream._data, '');
108
do_check_true(called);
111
_("adding more data..");
113
stream._data += '{"payload":"{\\"value\\":10}"';
114
coll.recordHandler = function(rec) {
116
do_throw("shouldn't have gotten a record..");
118
coll._onProgress.call(stream);
119
_("should still have 1 record with sum 100");
120
do_check_eq(recCount, 1);
121
do_check_eq(sum, 100);
122
_("should almost have a record");
123
do_check_eq(stream._data, '{"payload":"{\\"value\\":10}"');
124
do_check_false(called);
127
_("add data for two records..");
129
stream._data += '}\n{"payload":"{\\"value\\":1}"}\n';
130
coll.recordHandler = function(rec) {
132
_("Got record:", JSON.stringify(rec));
134
sum += rec.payload.value;
137
do_check_eq(rec.payload.value, 10);
138
do_check_eq(sum, 110);
141
do_check_eq(rec.payload.value, 1);
142
do_check_eq(sum, 111);
145
do_throw("unexpected number of record counts", recCount);
149
coll._onProgress.call(stream);
150
_("should have gotten all 3 records with sum 111");
151
do_check_eq(recCount, 3);
152
do_check_eq(sum, 111);
153
_("should have consumed all data");
154
do_check_eq(stream._data, '');
155
do_check_true(called);
158
_("add no extra data");
161
coll.recordHandler = function(rec) {
163
do_throw("shouldn't have gotten a record..");
165
coll._onProgress.call(stream);
166
_("should still have 3 records with sum 111");
167
do_check_eq(recCount, 3);
168
do_check_eq(sum, 111);
169
_("should have consumed nothing but still have nothing");
170
do_check_eq(stream._data, "");
171
do_check_false(called);