summaryrefslogtreecommitdiffstats
path: root/services/sync/tests/unit/test_collection_inc_get.js
blob: 7747c0ef3e605176f2d608f7a7b8646b1a2ce31a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

_("Make sure Collection can correctly incrementally parse GET requests");
Cu.import("resource://services-sync/record.js");
Cu.import("resource://services-sync/service.js");

function run_test() {
  let base = "http://fake/";
  let coll = new Collection("http://fake/uri/", WBORecord, Service);
  let stream = { _data: "" };
  let called, recCount, sum;

  _("Not-JSON, string payloads are strings");
  called = false;
  stream._data = '{"id":"hello","payload":"world"}\n';
  coll.recordHandler = function(rec) {
    called = true;
    _("Got record:", JSON.stringify(rec));
    rec.collection = "uri";           // This would be done by an engine, so do it here.
    do_check_eq(rec.collection, "uri");
    do_check_eq(rec.id, "hello");
    do_check_eq(rec.uri(base).spec, "http://fake/uri/hello");
    do_check_eq(rec.payload, "world");
  };
  coll._onProgress.call(stream);
  do_check_eq(stream._data, '');
  do_check_true(called);
  _("\n");


  _("Parse record with payload");
  called = false;
  stream._data = '{"payload":"{\\"value\\":123}"}\n';
  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;
  stream._data = '{"id":"hundred","payload":"{\\"value\\":100}"}\n{"id":"ten","payload":"{\\"value\\":10}"}\n{"id":"one","payload":"{\\"value\\":1}"}\n';
  coll.recordHandler = function(rec) {
    called = true;
    _("Got record:", JSON.stringify(rec));
    recCount++;
    sum += rec.payload.value;
    _("Incremental status: count", recCount, "sum", sum);
    rec.collection = "uri";
    switch (recCount) {
      case 1:
        do_check_eq(rec.id, "hundred");
        do_check_eq(rec.uri(base).spec, "http://fake/uri/hundred");
        do_check_eq(rec.payload.value, 100);
        do_check_eq(sum, 100);
        break;
      case 2:
        do_check_eq(rec.id, "ten");
        do_check_eq(rec.uri(base).spec, "http://fake/uri/ten");
        do_check_eq(rec.payload.value, 10);
        do_check_eq(sum, 110);
        break;
      case 3:
        do_check_eq(rec.id, "one");
        do_check_eq(rec.uri(base).spec, "http://fake/uri/one");
        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;
  stream._data = '{"payl';
  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;
  stream._data += 'oad":"{\\"value\\":100}"}\n';
  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;
  stream._data += '}\n{"payload":"{\\"value\\":1}"}\n';
  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);
  _();

  _("add no extra data");
  called = false;
  stream._data += '';
  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);
  _("should have consumed nothing but still have nothing");
  do_check_eq(stream._data, "");
  do_check_false(called);
  _("\n");
}