~ubuntu-branches/ubuntu/trusty/enigmail/trusty-security

« back to all changes in this revision

Viewing changes to services/sync/tests/unit/test_collection_inc_get.js

  • Committer: Package Import Robot
  • Author(s): Chris Coulson
  • Date: 2011-08-12 12:25:06 UTC
  • mfrom: (0.12.4 upstream)
  • Revision ID: package-import@ubuntu.com-20110812122506-zko6c7zfexvyg71q
Tags: 2:1.2.1-0ubuntu1
* New upstream release
* Drop fix_install_rdf_xml_errors.diff - fixed upstream
* Refresh port_to_latest_thunderbird.diff
* Add a proper get-orig-source target which pulls the build system from
  lp:~mozillateam/mozilla-build-system/beta, now that we don't have the old
  build-system.tar.gz from xulrunner

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
_("Make sure Collection can correctly incrementally parse GET requests");
2
 
Cu.import("resource://services-sync/record.js");
3
 
 
4
 
function run_test() {
5
 
  let base = "http://fake/";
6
 
  let coll = new Collection("http://fake/uri/", WBORecord);
7
 
  let stream = { _data: "" };
8
 
  let called, recCount, sum;
9
 
 
10
 
  _("Not-JSON, string payloads are strings");
11
 
  called = false;
12
 
  stream._data = '{"id":"hello","payload":"world"}\n';
13
 
  coll.recordHandler = function(rec) {
14
 
    called = true;
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");
21
 
  };
22
 
  coll._onProgress.call(stream);
23
 
  do_check_eq(stream._data, '');
24
 
  do_check_true(called);
25
 
  _("\n");
26
 
 
27
 
 
28
 
  _("Parse record with payload");
29
 
  called = false;
30
 
  stream._data = '{"payload":"{\\"value\\":123}"}\n';
31
 
  coll.recordHandler = function(rec) {
32
 
    called = true;
33
 
    _("Got record:", JSON.stringify(rec));
34
 
    do_check_eq(rec.payload.value, 123);
35
 
  };
36
 
  coll._onProgress.call(stream);
37
 
  do_check_eq(stream._data, '');
38
 
  do_check_true(called);
39
 
  _("\n");
40
 
 
41
 
 
42
 
  _("Parse multiple records in one go");
43
 
  called = false;
44
 
  recCount = 0;
45
 
  sum = 0;
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) {
48
 
    called = true;
49
 
    _("Got record:", JSON.stringify(rec));
50
 
    recCount++;
51
 
    sum += rec.payload.value;
52
 
    _("Incremental status: count", recCount, "sum", sum);
53
 
    rec.collection = "uri";
54
 
    switch (recCount) {
55
 
      case 1:
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);
60
 
        break;
61
 
      case 2:
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);
66
 
        break;
67
 
      case 3:
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);
72
 
        break;
73
 
      default:
74
 
        do_throw("unexpected number of record counts", recCount);
75
 
        break;
76
 
    }
77
 
  };
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);
83
 
  _("\n");
84
 
 
85
 
 
86
 
  _("Handle incremental data incoming");
87
 
  called = false;
88
 
  recCount = 0;
89
 
  sum = 0;
90
 
  stream._data = '{"payl';
91
 
  coll.recordHandler = function(rec) {
92
 
    called = true;
93
 
    do_throw("shouldn't have gotten a record..");
94
 
  };
95
 
  coll._onProgress.call(stream);
96
 
  _("shouldn't have gotten anything yet");
97
 
  do_check_eq(recCount, 0);
98
 
  do_check_eq(sum, 0);
99
 
  _("leading array bracket should have been trimmed");
100
 
  do_check_eq(stream._data, '{"payl');
101
 
  do_check_false(called);
102
 
  _();
103
 
 
104
 
  _("adding more data enough for one record..");
105
 
  called = false;
106
 
  stream._data += 'oad":"{\\"value\\":100}"}\n';
107
 
  coll.recordHandler = function(rec) {
108
 
    called = true;
109
 
    _("Got record:", JSON.stringify(rec));
110
 
    recCount++;
111
 
    sum += rec.payload.value;
112
 
  };
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);
120
 
  _();
121
 
 
122
 
  _("adding more data..");
123
 
  called = false;
124
 
  stream._data += '{"payload":"{\\"value\\":10}"';
125
 
  coll.recordHandler = function(rec) {
126
 
    called = true;
127
 
    do_throw("shouldn't have gotten a record..");
128
 
  };
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);
136
 
  _();
137
 
 
138
 
  _("add data for two records..");
139
 
  called = false;
140
 
  stream._data += '}\n{"payload":"{\\"value\\":1}"}\n';
141
 
  coll.recordHandler = function(rec) {
142
 
    called = true;
143
 
    _("Got record:", JSON.stringify(rec));
144
 
    recCount++;
145
 
    sum += rec.payload.value;
146
 
    switch (recCount) {
147
 
      case 2:
148
 
        do_check_eq(rec.payload.value, 10);
149
 
        do_check_eq(sum, 110);
150
 
        break;
151
 
      case 3:
152
 
        do_check_eq(rec.payload.value, 1);
153
 
        do_check_eq(sum, 111);
154
 
        break;
155
 
      default:
156
 
        do_throw("unexpected number of record counts", recCount);
157
 
        break;
158
 
    }
159
 
  };
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);
167
 
  _();
168
 
 
169
 
  _("add no extra data");
170
 
  called = false;
171
 
  stream._data += '';
172
 
  coll.recordHandler = function(rec) {
173
 
    called = true;
174
 
    do_throw("shouldn't have gotten a record..");
175
 
  };
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);
183
 
  _("\n");
184
 
}