Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: chrome/content/tests/synchronizer.js

Issue 11193004: Use mock XMLHttpRequest object to speed up tests (Closed)
Patch Set: Created July 25, 2013, 1:58 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/content/tests/notification.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 (function() 1 (function()
2 { 2 {
3 let testRunner = null; 3 let testRunner = null;
4 let server = null; 4 let requestHandlers = null;
5 let randomResult = 0.5; 5 let randomResult = 0.5;
6 6
7 module("Synchronizer", { 7 module("Synchronizer", {
8 setup: function() 8 setup: function()
9 { 9 {
10 testRunner = this; 10 testRunner = this;
11 11
12 prepareFilterComponents.call(this); 12 prepareFilterComponents.call(this);
13 preparePrefs.call(this); 13 preparePrefs.call(this);
14 setupVirtualTime.call(this, function(wrapTimer) 14 setupVirtualTime.call(this, function(wrapTimer)
15 { 15 {
16 let SynchronizerModule = getModuleGlobal("synchronizer"); 16 let SynchronizerModule = getModuleGlobal("synchronizer");
17 SynchronizerModule.downloader._timer = wrapTimer(SynchronizerModule.down loader._timer); 17 SynchronizerModule.downloader._timer = wrapTimer(SynchronizerModule.down loader._timer);
18 }, "synchronizer", "downloader"); 18 }, "synchronizer", "downloader");
19 19 setupVirtualXMLHttp.call(this, "synchronizer", "downloader");
20 server = new nsHttpServer();
21 server.start(1234);
22 20
23 // Replace Math.random() function 21 // Replace Math.random() function
24 let DownloaderGlobal = Cu.getGlobalForObject(getModuleGlobal("downloader") ); 22 let DownloaderGlobal = Cu.getGlobalForObject(getModuleGlobal("downloader") );
25 this._origRandom = DownloaderGlobal.Math.random; 23 this._origRandom = DownloaderGlobal.Math.random;
26 DownloaderGlobal.Math.random = function() randomResult; 24 DownloaderGlobal.Math.random = function() randomResult;
27 randomResult = 0.5; 25 randomResult = 0.5;
28 }, 26 },
29 27
30 teardown: function() 28 teardown: function()
31 { 29 {
32 restoreFilterComponents.call(this); 30 restoreFilterComponents.call(this);
33 restorePrefs.call(this); 31 restorePrefs.call(this);
34 restoreVirtualTime.call(this); 32 restoreVirtualTime.call(this);
35 33 restoreVirtualXMLHttp.call(this);
36 stop();
37 server.stop(function()
38 {
39 server = null;
40 start();
41 });
42 34
43 if (this._origRandom) 35 if (this._origRandom)
44 { 36 {
45 let DownloaderGlobal = Cu.getGlobalForObject(getModuleGlobal("downloader ")); 37 let DownloaderGlobal = Cu.getGlobalForObject(getModuleGlobal("downloader "));
46 DownloaderGlobal.Math.random = this._origRandom; 38 DownloaderGlobal.Math.random = this._origRandom;
47 delete this._origRandom; 39 delete this._origRandom;
48 } 40 }
49 41
50 Synchronizer.init(); 42 Synchronizer.init();
51 } 43 }
52 }); 44 });
53 45
54 function resetSubscription(subscription) 46 function resetSubscription(subscription)
55 { 47 {
56 FilterStorage.updateSubscriptionFilters(subscription, []); 48 FilterStorage.updateSubscriptionFilters(subscription, []);
57 subscription.lastCheck = subscription.lastDownload = 49 subscription.lastCheck = subscription.lastDownload =
58 subscription.version = subscription.lastSuccess = 50 subscription.version = subscription.lastSuccess =
59 subscription.expires = subscription.softExpiration = 0; 51 subscription.expires = subscription.softExpiration = 0;
60 subscription.title = ""; 52 subscription.title = "";
61 subscription.homepage = null; 53 subscription.homepage = null;
62 subscription.errors = 0; 54 subscription.errors = 0;
63 subscription.downloadStatus = null; 55 subscription.downloadStatus = null;
64 subscription.requiredVersion = null; 56 subscription.requiredVersion = null;
65 } 57 }
66 58
67 test("Downloads of one subscription", function() 59 test("Downloads of one subscription", function()
68 { 60 {
69 let subscription = Subscription.fromURL("http://127.0.0.1:1234/subscription" ); 61 let subscription = Subscription.fromURL("http://example.com/subscription");
70 FilterStorage.addSubscription(subscription); 62 FilterStorage.addSubscription(subscription);
71 63
72 let requests = []; 64 let requests = [];
73 function handler(metadata, response) 65 testRunner.registerHandler("/subscription", function(metadata)
74 { 66 {
75 requests.push([testRunner.getTimeOffset(), metadata.method, metadata.path] ); 67 requests.push([testRunner.getTimeOffset(), metadata.method, metadata.path] );
76 68 return [Cr.NS_OK, 200, "[Adblock]\n! ExPiREs: 1day\nfoo\nbar"];
77 response.setStatusLine("1.1", "200", "OK"); 69 });
78 response.setHeader("Content-Type", "text/plain");
79
80 let result = "[Adblock]\n! ExPiREs: 1day\nfoo\nbar";
81 response.bodyOutputStream.write(result, result.length);
82 }
83
84 server.registerPathHandler("/subscription", handler);
85 70
86 testRunner.runScheduledTasks(50); 71 testRunner.runScheduledTasks(50);
87 deepEqual(requests, [ 72 deepEqual(requests, [
88 [0.1, "GET", "/subscription"], 73 [0.1, "GET", "/subscription"],
89 [24.1, "GET", "/subscription"], 74 [24.1, "GET", "/subscription"],
90 [48.1, "GET", "/subscription"], 75 [48.1, "GET", "/subscription"],
91 ], "Requests after 50 hours"); 76 ], "Requests after 50 hours");
92 }); 77 });
93 78
94 test("Downloads of two subscriptions", function() 79 test("Downloads of two subscriptions", function()
95 { 80 {
96 let subscription1 = Subscription.fromURL("http://127.0.0.1:1234/subscription 1"); 81 let subscription1 = Subscription.fromURL("http://example.com/subscription1") ;
97 FilterStorage.addSubscription(subscription1); 82 FilterStorage.addSubscription(subscription1);
98 83
99 let subscription2 = Subscription.fromURL("http://127.0.0.1:1234/subscription 2"); 84 let subscription2 = Subscription.fromURL("http://example.com/subscription2") ;
100 subscription2.expires = 85 subscription2.expires =
101 subscription2.softExpiration = 86 subscription2.softExpiration =
102 (testRunner.currentTime + 2 * MILLIS_IN_HOUR) / MILLIS_IN_SECOND; 87 (testRunner.currentTime + 2 * MILLIS_IN_HOUR) / MILLIS_IN_SECOND;
103 FilterStorage.addSubscription(subscription2); 88 FilterStorage.addSubscription(subscription2);
104 89
105 let requests = []; 90 let requests = [];
106 function handler(metadata, response) 91 function handler(metadata)
107 { 92 {
108 requests.push([testRunner.getTimeOffset(), metadata.method, metadata.path] ); 93 requests.push([testRunner.getTimeOffset(), metadata.method, metadata.path] );
109 94 return [Cr.NS_OK, 200, "[Adblock]\n! ExPiREs: 1day\nfoo\nbar"];
110 response.setStatusLine("1.1", "200", "OK");
111 response.setHeader("Content-Type", "text/plain");
112
113 let result = "[Adblock]\n! ExPiREs: 1day\nfoo\nbar";
114 response.bodyOutputStream.write(result, result.length);
115 } 95 }
116 96
117 server.registerPathHandler("/subscription1", handler); 97 testRunner.registerHandler("/subscription1", handler);
118 server.registerPathHandler("/subscription2", handler); 98 testRunner.registerHandler("/subscription2", handler);
119 99
120 testRunner.runScheduledTasks(55); 100 testRunner.runScheduledTasks(55);
121 deepEqual(requests, [ 101 deepEqual(requests, [
122 [0.1, "GET", "/subscription1"], 102 [0.1, "GET", "/subscription1"],
123 [2.1, "GET", "/subscription2"], 103 [2.1, "GET", "/subscription2"],
124 [24.1, "GET", "/subscription1"], 104 [24.1, "GET", "/subscription1"],
125 [26.1, "GET", "/subscription2"], 105 [26.1, "GET", "/subscription2"],
126 [48.1, "GET", "/subscription1"], 106 [48.1, "GET", "/subscription1"],
127 [50.1, "GET", "/subscription2"], 107 [50.1, "GET", "/subscription2"],
128 ], "Requests after 55 hours"); 108 ], "Requests after 55 hours");
129 }); 109 });
130 110
131 test("Download result, various subscription headers", function() 111 test("Download result, various subscription headers", function()
132 { 112 {
133 let test; 113 let test;
134 let subscription = Subscription.fromURL("http://127.0.0.1:1234/subscription" ); 114 let subscription = Subscription.fromURL("http://example.com/subscription");
135 FilterStorage.addSubscription(subscription); 115 FilterStorage.addSubscription(subscription);
136 116
137 function handler(metadata, response) 117 testRunner.registerHandler("/subscription", function(metadata)
138 { 118 {
139 response.setStatusLine("1.1", "200", "OK"); 119 return [Cr.NS_OK, 200, test.header + "\n!Expires: 8 hours\nfoo\n!bar\n\n@@ bas\n#bam"];
140 120 });
141 // Wrong content type shouldn't matter
142 response.setHeader("Content-Type", "text/xml");
143
144 let result = test.header + "\n!Expires: 8 hours\nfoo\n!bar\n\n@@bas\n#bam" ;
145 response.bodyOutputStream.write(result, result.length);
146 }
147 server.registerPathHandler("/subscription", handler);
148 121
149 let tests = [ 122 let tests = [
150 {header: "[Adblock]", downloadStatus: "synchronize_ok", requiredVersion: n ull}, 123 {header: "[Adblock]", downloadStatus: "synchronize_ok", requiredVersion: n ull},
151 {header: "[Adblock Plus]", downloadStatus: "synchronize_ok", requiredVersi on: null}, 124 {header: "[Adblock Plus]", downloadStatus: "synchronize_ok", requiredVersi on: null},
152 {header: "(something)[Adblock]", downloadStatus: "synchronize_ok", require dVersion: null}, 125 {header: "(something)[Adblock]", downloadStatus: "synchronize_ok", require dVersion: null},
153 {header: "[Adblock Plus 0.0.1]", downloadStatus: "synchronize_ok", require dVersion: "0.0.1"}, 126 {header: "[Adblock Plus 0.0.1]", downloadStatus: "synchronize_ok", require dVersion: "0.0.1"},
154 {header: "[Adblock Plus 99.9]", downloadStatus: "synchronize_ok", required Version: "99.9"}, 127 {header: "[Adblock Plus 99.9]", downloadStatus: "synchronize_ok", required Version: "99.9"},
155 {header: "[Foo]", downloadStatus: "synchronize_invalid_data", requiredVers ion: null} 128 {header: "[Foo]", downloadStatus: "synchronize_invalid_data", requiredVers ion: null}
156 ]; 129 ];
157 for each (test in tests) 130 for each (test in tests)
(...skipping 18 matching lines...) Expand all
176 deepEqual(subscription.filters, [ 149 deepEqual(subscription.filters, [
177 ], "Resulting subscription filters for " + test.header); 150 ], "Resulting subscription filters for " + test.header);
178 } 151 }
179 } 152 }
180 }) 153 })
181 154
182 test("Automatic updates disabled", function() 155 test("Automatic updates disabled", function()
183 { 156 {
184 Prefs.subscriptions_autoupdate = false; 157 Prefs.subscriptions_autoupdate = false;
185 158
186 let subscription = Subscription.fromURL("http://127.0.0.1:1234/subscription" ); 159 let subscription = Subscription.fromURL("http://example.com/subscription");
187 FilterStorage.addSubscription(subscription); 160 FilterStorage.addSubscription(subscription);
188 161
189 let requests = 0; 162 let requests = 0;
190 function handler(metadata, response) 163 testRunner.registerHandler("/subscription", function(metadata)
191 { 164 {
192 requests++; 165 requests++;
193 throw new Error("Unexpected request"); 166 throw new Error("Unexpected request");
194 } 167 });
195
196 server.registerPathHandler("/subscription", handler);
197 168
198 testRunner.runScheduledTasks(50); 169 testRunner.runScheduledTasks(50);
199 equal(requests, 0, "Request count"); 170 equal(requests, 0, "Request count");
200 }); 171 });
201 172
202 test("Expiration time", function() 173 test("Expiration time", function()
203 { 174 {
204 let subscription = Subscription.fromURL("http://127.0.0.1:1234/subscription" ); 175 let subscription = Subscription.fromURL("http://example.com/subscription");
205 FilterStorage.addSubscription(subscription); 176 FilterStorage.addSubscription(subscription);
206 177
207 let test; 178 let test;
208 let requests = []; 179 let requests = [];
209 function handler(metadata, response) 180 testRunner.registerHandler("/subscription", function(metadata)
210 { 181 {
211 requests.push(testRunner.getTimeOffset()); 182 requests.push(testRunner.getTimeOffset());
212 183 return [Cr.NS_OK, 200, "[Adblock]\nfoo\n!Expires: " + test.expiration + "\ nbar"];
213 response.setStatusLine("1.1", "200", "OK"); 184 });
214 response.setHeader("Content-Type", "text/plain");
215
216 let result = "[Adblock]\nfoo\n!Expires: " + test.expiration + "\nbar";
217 response.bodyOutputStream.write(result, result.length);
218 }
219 server.registerPathHandler("/subscription", handler);
220 185
221 let tests = [ 186 let tests = [
222 { 187 {
223 expiration: "default", 188 expiration: "default",
224 randomResult: 0.5, 189 randomResult: 0.5,
225 requests: [0.1, 5 * 24 + 0.1] 190 requests: [0.1, 5 * 24 + 0.1]
226 }, 191 },
227 { 192 {
228 expiration: "1 hours", // Minimal expiration interval 193 expiration: "1 hours", // Minimal expiration interval
229 randomResult: 0.5, 194 randomResult: 0.5,
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 testRunner.runScheduledTasks(maxHours, test.skipAfter, test.skip); 257 testRunner.runScheduledTasks(maxHours, test.skipAfter, test.skip);
293 258
294 let randomAddendum = (randomResult == 0.5 ? "" : " with Math.random() retu rning " + randomResult); 259 let randomAddendum = (randomResult == 0.5 ? "" : " with Math.random() retu rning " + randomResult);
295 let skipAddendum = (typeof test.skip != "number" ? "" : " skipping " + tes t.skip + " hours after " + test.skipAfter + " hours"); 260 let skipAddendum = (typeof test.skip != "number" ? "" : " skipping " + tes t.skip + " hours after " + test.skipAfter + " hours");
296 deepEqual(requests, test.requests, "Requests for \"" + test.expiration + " \"" + randomAddendum + skipAddendum); 261 deepEqual(requests, test.requests, "Requests for \"" + test.expiration + " \"" + randomAddendum + skipAddendum);
297 } 262 }
298 }); 263 });
299 264
300 test("Checksum verification", function() 265 test("Checksum verification", function()
301 { 266 {
302 let subscription = Subscription.fromURL("http://127.0.0.1:1234/subscription" ); 267 let subscription = Subscription.fromURL("http://example.com/subscription");
303 FilterStorage.addSubscription(subscription); 268 FilterStorage.addSubscription(subscription);
304 269
305 let testName, subscriptionBody, expectedResult; 270 let testName, subscriptionBody, expectedResult;
306 let tests = [ 271 let tests = [
307 ["Correct checksum", "[Adblock]\n! Checksum: e/JCmqXny6Fn24b7JHsq/A\nfoo\n bar\n", true], 272 ["Correct checksum", "[Adblock]\n! Checksum: e/JCmqXny6Fn24b7JHsq/A\nfoo\n bar\n", true],
308 ["Wrong checksum", "[Adblock]\n! Checksum: wrongggny6Fn24b7JHsq/A\nfoo\nba r\n", false], 273 ["Wrong checksum", "[Adblock]\n! Checksum: wrongggny6Fn24b7JHsq/A\nfoo\nba r\n", false],
309 ["Empty lines ignored", "[Adblock]\n! Checksum: e/JCmqXny6Fn24b7JHsq/A\n\n foo\n\nbar\n\n", true], 274 ["Empty lines ignored", "[Adblock]\n! Checksum: e/JCmqXny6Fn24b7JHsq/A\n\n foo\n\nbar\n\n", true],
310 ["CR LF line breaks treated like LR", "[Adblock]\n! Checksum: e/JCmqXny6Fn 24b7JHsq/A\nfoo\r\nbar\r\n", true], 275 ["CR LF line breaks treated like LR", "[Adblock]\n! Checksum: e/JCmqXny6Fn 24b7JHsq/A\nfoo\r\nbar\r\n", true],
311 ["CR line breaks treated like LR", "[Adblock]\n! Checksum: e/JCmqXny6Fn24b 7JHsq/A\nfoo\rbar\r", true], 276 ["CR line breaks treated like LR", "[Adblock]\n! Checksum: e/JCmqXny6Fn24b 7JHsq/A\nfoo\rbar\r", true],
312 ["Trailing line break not ignored", "[Adblock]\n! Checksum: e/JCmqXny6Fn24 b7JHsq/A\nfoo\nbar", false], 277 ["Trailing line break not ignored", "[Adblock]\n! Checksum: e/JCmqXny6Fn24 b7JHsq/A\nfoo\nbar", false],
313 ["Line breaks between lines not ignored", "[Adblock]\n! Checksum: e/JCmqXn y6Fn24b7JHsq/A\nfoobar", false], 278 ["Line breaks between lines not ignored", "[Adblock]\n! Checksum: e/JCmqXn y6Fn24b7JHsq/A\nfoobar", false],
314 ["Lines with spaces not ignored", "[Adblock]\n! Checksum: e/JCmqXny6Fn24b7 JHsq/A\n \nfoo\n\nbar\n", false], 279 ["Lines with spaces not ignored", "[Adblock]\n! Checksum: e/JCmqXny6Fn24b7 JHsq/A\n \nfoo\n\nbar\n", false],
315 ["Extra content in checksum line is part of the checksum", "[Adblock]\n! C hecksum: e/JCmqXny6Fn24b7JHsq/A foobar\nfoo\nbar\n", false], 280 ["Extra content in checksum line is part of the checksum", "[Adblock]\n! C hecksum: e/JCmqXny6Fn24b7JHsq/A foobar\nfoo\nbar\n", false],
316 ["= symbols after checksum are ignored", "[Adblock]\n! Checksum: e/JCmqXny 6Fn24b7JHsq/A===\nfoo\nbar\n", true], 281 ["= symbols after checksum are ignored", "[Adblock]\n! Checksum: e/JCmqXny 6Fn24b7JHsq/A===\nfoo\nbar\n", true],
317 ["Header line is part of the checksum", "[Adblock Plus]\n! Checksum: e/JCm qXny6Fn24b7JHsq/A\nfoo\nbar\n", false], 282 ["Header line is part of the checksum", "[Adblock Plus]\n! Checksum: e/JCm qXny6Fn24b7JHsq/A\nfoo\nbar\n", false],
318 ["Special comments are part of the checksum", "[Adblock]\n! Checksum: e/JC mqXny6Fn24b7JHsq/A\n! Expires: 1\nfoo\nbar\n", false], 283 ["Special comments are part of the checksum", "[Adblock]\n! Checksum: e/JC mqXny6Fn24b7JHsq/A\n! Expires: 1\nfoo\nbar\n", false],
319 ]; 284 ];
320 285
321 function handler(metadata, response) 286 testRunner.registerHandler("/subscription", function(metadata)
322 { 287 {
323 response.setStatusLine("1.1", "200", "OK"); 288 return [Cr.NS_OK, 200, subscriptionBody];
324 response.setHeader("Content-Type", "text/plain"); 289 });
325
326 response.bodyOutputStream.write(subscriptionBody, subscriptionBody.length) ;
327 }
328 server.registerPathHandler("/subscription", handler);
329 290
330 for each ([testName, subscriptionBody, expectedResult] in tests) 291 for each ([testName, subscriptionBody, expectedResult] in tests)
331 { 292 {
332 resetSubscription(subscription); 293 resetSubscription(subscription);
333 testRunner.runScheduledTasks(2); 294 testRunner.runScheduledTasks(2);
334 equal(subscription.downloadStatus, expectedResult ? "synchronize_ok" : "sy nchronize_checksum_mismatch", testName); 295 equal(subscription.downloadStatus, expectedResult ? "synchronize_ok" : "sy nchronize_checksum_mismatch", testName);
335 } 296 }
336 }); 297 });
337 298
338 test("Special comments", function() 299 test("Special comments", function()
339 { 300 {
340 let subscription = Subscription.fromURL("http://127.0.0.1:1234/subscription" ); 301 let subscription = Subscription.fromURL("http://example.com/subscription");
341 FilterStorage.addSubscription(subscription); 302 FilterStorage.addSubscription(subscription);
342 303
343 let comment, check; 304 let comment, check;
344 let tests = [ 305 let tests = [
345 ["! Homepage: http://example.com/", function() equal(subscription.homepage , "http://example.com/", "Valid homepage comment")], 306 ["! Homepage: http://example.com/", function() equal(subscription.homepage , "http://example.com/", "Valid homepage comment")],
346 ["! Homepage: ssh://example.com/", function() equal(subscription.homepage, null, "Invalid homepage comment")], 307 ["! Homepage: ssh://example.com/", function() equal(subscription.homepage, null, "Invalid homepage comment")],
347 ["! Title: foo", function() 308 ["! Title: foo", function()
348 { 309 {
349 equal(subscription.title, "foo", "Title comment"); 310 equal(subscription.title, "foo", "Title comment");
350 equal(subscription.fixedTitle, true, "Fixed title"); 311 equal(subscription.fixedTitle, true, "Fixed title");
351 }], 312 }],
352 ["! Version: 1234", function() equal(subscription.version, 1234, "Version comment")] 313 ["! Version: 1234", function() equal(subscription.version, 1234, "Version comment")]
353 ]; 314 ];
354 315
355 function handler(metadata, response) 316 testRunner.registerHandler("/subscription", function(metadata)
356 { 317 {
357 response.setStatusLine("1.1", "200", "OK"); 318 return [Cr.NS_OK, 200, "[Adblock]\n" + comment + "\nfoo\nbar"];
358 response.setHeader("Content-Type", "text/plain"); 319 });
359
360 let result = "[Adblock]\n" + comment + "\nfoo\nbar";
361 response.bodyOutputStream.write(result, result.length);
362 }
363 server.registerPathHandler("/subscription", handler);
364 320
365 for each([comment, check] in tests) 321 for each([comment, check] in tests)
366 { 322 {
367 resetSubscription(subscription); 323 resetSubscription(subscription);
368 testRunner.runScheduledTasks(2); 324 testRunner.runScheduledTasks(2);
369 check(); 325 check();
370 deepEqual(subscription.filters, [Filter.fromText("foo"), Filter.fromText(" bar")], "Special comment not added to filters"); 326 deepEqual(subscription.filters, [Filter.fromText("foo"), Filter.fromText(" bar")], "Special comment not added to filters");
371 } 327 }
372 }); 328 });
373 329
374 test("Redirects", function() 330 test("Redirects", function()
375 { 331 {
376 let subscription = Subscription.fromURL("http://127.0.0.1:1234/subscription" ); 332 let subscription = Subscription.fromURL("http://example.com/subscription");
377 FilterStorage.addSubscription(subscription); 333 FilterStorage.addSubscription(subscription);
378 334
379 function redirect_handler(metadata, response) 335 testRunner.registerHandler("/subscription", function(metadata)
380 { 336 {
381 response.setStatusLine("1.1", "200", "OK"); 337 return [Cr.NS_OK, 200, "[Adblock]\nfoo\n!Redirect: http://example.com/redi rected\nbar"];
382 response.setHeader("Content-Type", "text/plain"); 338 });
383
384 let result = "[Adblock]\nfoo\n!Redirect: http://127.0.0.1:1234/redirected\ nbar";
385 response.bodyOutputStream.write(result, result.length);
386 }
387 server.registerPathHandler("/subscription", redirect_handler);
388 339
389 testRunner.runScheduledTasks(30); 340 testRunner.runScheduledTasks(30);
390 equal(FilterStorage.subscriptions[0], subscription, "Invalid redirect ignore d"); 341 equal(FilterStorage.subscriptions[0], subscription, "Invalid redirect ignore d");
391 equal(subscription.downloadStatus, "synchronize_connection_error", "Connecti on error recorded"); 342 equal(subscription.downloadStatus, "synchronize_connection_error", "Connecti on error recorded");
392 equal(subscription.errors, 2, "Number of download errors"); 343 equal(subscription.errors, 2, "Number of download errors");
393 344
394 let requests = []; 345 let requests = [];
395 function handler(metadata, response) 346 testRunner.registerHandler("/redirected", function(metadata)
396 { 347 {
397 requests.push(testRunner.getTimeOffset()); 348 requests.push(testRunner.getTimeOffset());
398 349 return [Cr.NS_OK, 200, "[Adblock]\nfoo\n! Expires: 8 hours\nbar"];
399 response.setStatusLine("1.1", "200", "OK"); 350 });
400 response.setHeader("Content-Type", "text/plain");
401
402 let result = "[Adblock]\nfoo\n! Expires: 8 hours\nbar";
403 response.bodyOutputStream.write(result, result.length);
404 }
405 server.registerPathHandler("/redirected", handler);
406 351
407 resetSubscription(subscription); 352 resetSubscription(subscription);
408 testRunner.runScheduledTasks(15); 353 testRunner.runScheduledTasks(15);
409 equal(FilterStorage.subscriptions[0].url, "http://127.0.0.1:1234/redirected" , "Redirect followed"); 354 equal(FilterStorage.subscriptions[0].url, "http://example.com/redirected", " Redirect followed");
410 deepEqual(requests, [0.1, 8.1], "Resulting requests"); 355 deepEqual(requests, [0.1, 8.1], "Resulting requests");
411 356
412 server.registerPathHandler("/redirected", function(metadata, response) 357 testRunner.registerHandler("/redirected", function(metadata)
413 { 358 {
414 response.setStatusLine("1.1", "200", "OK"); 359 return [Cr.NS_OK, 200, "[Adblock]\nfoo\n!Redirect: http://example.com/subs cription\nbar"];
415 response.setHeader("Content-Type", "text/plain");
416
417 let result = "[Adblock]\nfoo\n!Redirect: http://127.0.0.1:1234/subscriptio n\nbar";
418 response.bodyOutputStream.write(result, result.length);
419 }) 360 })
420 361
421 let subscription = Subscription.fromURL("http://127.0.0.1:1234/subscription" ); 362 let subscription = Subscription.fromURL("http://example.com/subscription");
422 resetSubscription(subscription); 363 resetSubscription(subscription);
423 FilterStorage.removeSubscription(FilterStorage.subscriptions[0]); 364 FilterStorage.removeSubscription(FilterStorage.subscriptions[0]);
424 FilterStorage.addSubscription(subscription); 365 FilterStorage.addSubscription(subscription);
425 366
426 testRunner.runScheduledTasks(2); 367 testRunner.runScheduledTasks(2);
427 equal(FilterStorage.subscriptions[0], subscription, "Redirect not followed o n redirect loop"); 368 equal(FilterStorage.subscriptions[0], subscription, "Redirect not followed o n redirect loop");
428 equal(subscription.downloadStatus, "synchronize_connection_error", "Download status after redirect loop"); 369 equal(subscription.downloadStatus, "synchronize_connection_error", "Download status after redirect loop");
429 }); 370 });
430 371
431 test("Fallback", function() 372 test("Fallback", function()
432 { 373 {
433 Prefs.subscriptions_fallbackerrors = 3; 374 Prefs.subscriptions_fallbackerrors = 3;
434 Prefs.subscriptions_fallbackurl = "http://127.0.0.1:1234/fallback?%SUBSCRIPT ION%&%CHANNELSTATUS%&%RESPONSESTATUS%"; 375 Prefs.subscriptions_fallbackurl = "http://example.com/fallback?%SUBSCRIPTION %&%CHANNELSTATUS%&%RESPONSESTATUS%";
435 376
436 let subscription = Subscription.fromURL("http://127.0.0.1:1234/subscription" ); 377 let subscription = Subscription.fromURL("http://example.com/subscription");
437 FilterStorage.addSubscription(subscription); 378 FilterStorage.addSubscription(subscription);
438 379
439 // No valid response from fallback 380 // No valid response from fallback
440 381
441 let requests = []; 382 let requests = [];
442 function handler(metadata, response) 383 testRunner.registerHandler("/subscription", function(metadata)
443 { 384 {
444 requests.push(testRunner.getTimeOffset()); 385 requests.push(testRunner.getTimeOffset());
445 386 return [Cr.NS_OK, 404, ""];
446 response.setStatusLine("1.1", "404", "Not found"); 387 });
447 }
448 server.registerPathHandler("/subscription", handler);
449 388
450 testRunner.runScheduledTasks(100); 389 testRunner.runScheduledTasks(100);
451 deepEqual(requests, [0.1, 24.1, 48.1, 72.1, 96.1], "Continue trying if the f allback doesn't respond"); 390 deepEqual(requests, [0.1, 24.1, 48.1, 72.1, 96.1], "Continue trying if the f allback doesn't respond");
452 391
453 // Fallback giving "Gone" response 392 // Fallback giving "Gone" response
454 393
455 resetSubscription(subscription); 394 resetSubscription(subscription);
456 requests = []; 395 requests = [];
457 fallbackParams = null; 396 fallbackParams = null;
458 server.registerPathHandler("/fallback", function(metadata, response) 397 testRunner.registerHandler("/fallback", function(metadata)
459 { 398 {
460 response.setStatusLine("1.1", "200", "OK");
461 fallbackParams = decodeURIComponent(metadata.queryString); 399 fallbackParams = decodeURIComponent(metadata.queryString);
462 400 return [Cr.NS_OK, 200, "410 Gone"];
463 let result = "410 Gone";
464 response.bodyOutputStream.write(result, result.length);
465 }); 401 });
466 402
467 testRunner.runScheduledTasks(100); 403 testRunner.runScheduledTasks(100);
468 deepEqual(requests, [0.1, 24.1, 48.1], "Stop trying if the fallback responds with Gone"); 404 deepEqual(requests, [0.1, 24.1, 48.1], "Stop trying if the fallback responds with Gone");
469 equal(fallbackParams, "http://127.0.0.1:1234/subscription&0&404", "Fallback arguments"); 405 equal(fallbackParams, "http://example.com/subscription&0&404", "Fallback arg uments");
470 406
471 // Fallback redirecting to a missing file 407 // Fallback redirecting to a missing file
472 408
473 subscription = Subscription.fromURL("http://127.0.0.1:1234/subscription"); 409 subscription = Subscription.fromURL("http://example.com/subscription");
474 resetSubscription(subscription); 410 resetSubscription(subscription);
475 FilterStorage.removeSubscription(FilterStorage.subscriptions[0]); 411 FilterStorage.removeSubscription(FilterStorage.subscriptions[0]);
476 FilterStorage.addSubscription(subscription); 412 FilterStorage.addSubscription(subscription);
477 requests = []; 413 requests = [];
478 414
479 server.registerPathHandler("/fallback", function(metadata, response) 415 testRunner.registerHandler("/fallback", function(metadata)
480 { 416 {
481 response.setStatusLine("1.1", "200", "OK"); 417 return [Cr.NS_OK, 200, "301 http://example.com/redirected"];
482 418 });
483 let result = "301 http://127.0.0.1:1234/redirected"; 419 testRunner.runScheduledTasks(100);
484 response.bodyOutputStream.write(result, result.length); 420 equal(FilterStorage.subscriptions[0].url, "http://example.com/subscription", "Ignore invalid redirect from fallback");
485 });
486 testRunner.runScheduledTasks(100);
487 equal(FilterStorage.subscriptions[0].url, "http://127.0.0.1:1234/subscriptio n", "Ignore invalid redirect from fallback");
488 deepEqual(requests, [0.1, 24.1, 48.1, 72.1, 96.1], "Requests not affected by invalid redirect"); 421 deepEqual(requests, [0.1, 24.1, 48.1, 72.1, 96.1], "Requests not affected by invalid redirect");
489 422
490 // Fallback redirecting to an existing file 423 // Fallback redirecting to an existing file
491 424
492 resetSubscription(subscription); 425 resetSubscription(subscription);
493 requests = []; 426 requests = [];
494 let redirectedRequests = []; 427 let redirectedRequests = [];
495 server.registerPathHandler("/redirected", function(metadata, response) 428 testRunner.registerHandler("/redirected", function(metadata)
496 { 429 {
497 redirectedRequests.push(testRunner.getTimeOffset()); 430 redirectedRequests.push(testRunner.getTimeOffset());
498 431 return [Cr.NS_OK, 200, "[Adblock]\n!Expires: 1day\nfoo\nbar"];
499 response.setStatusLine("1.1", "200", "OK"); 432 });
500 response.setHeader("Content-Type", "text/plain"); 433
501 434 testRunner.runScheduledTasks(100);
502 let result = "[Adblock]\n!Expires: 1day\nfoo\nbar"; 435 equal(FilterStorage.subscriptions[0].url, "http://example.com/redirected", " Valid redirect from fallback is followed");
503 response.bodyOutputStream.write(result, result.length);
504 });
505
506 testRunner.runScheduledTasks(100);
507 equal(FilterStorage.subscriptions[0].url, "http://127.0.0.1:1234/redirected" , "Valid redirect from fallback is followed");
508 deepEqual(requests, [0.1, 24.1, 48.1], "Stop polling original URL after a va lid redirect from fallback"); 436 deepEqual(requests, [0.1, 24.1, 48.1], "Stop polling original URL after a va lid redirect from fallback");
509 deepEqual(redirectedRequests, [48.1, 72.1, 96.1], "Request new URL after a v alid redirect from fallback"); 437 deepEqual(redirectedRequests, [48.1, 72.1, 96.1], "Request new URL after a v alid redirect from fallback");
510 438
511 // Checksum mismatch 439 // Checksum mismatch
512 440
513 function handler2(metadata, response) 441 testRunner.registerHandler("/subscription", function(metadata)
514 { 442 {
515 response.setStatusLine("1.1", "200", "OK"); 443 return [Cr.NS_OK, 200, "[Adblock]\n! Checksum: wrong\nfoo\nbar"];
516 response.setHeader("Content-Type", "text/plain"); 444 });
517 445
518 let result = "[Adblock]\n! Checksum: wrong\nfoo\nbar"; 446 subscription = Subscription.fromURL("http://example.com/subscription");
519 response.bodyOutputStream.write(result, result.length); 447 resetSubscription(subscription);
520 } 448 FilterStorage.removeSubscription(FilterStorage.subscriptions[0]);
521 server.registerPathHandler("/subscription", handler2); 449 FilterStorage.addSubscription(subscription);
522 450
523 subscription = Subscription.fromURL("http://127.0.0.1:1234/subscription"); 451 testRunner.runScheduledTasks(100);
524 resetSubscription(subscription); 452 equal(FilterStorage.subscriptions[0].url, "http://example.com/redirected", " Wrong checksum produces fallback request");
525 FilterStorage.removeSubscription(FilterStorage.subscriptions[0]);
526 FilterStorage.addSubscription(subscription);
527
528 testRunner.runScheduledTasks(100);
529 equal(FilterStorage.subscriptions[0].url, "http://127.0.0.1:1234/redirected" , "Wrong checksum produces fallback request");
530 453
531 // Redirect loop 454 // Redirect loop
532 455
533 server.registerPathHandler("/subscription", function(metadata, response) 456 testRunner.registerHandler("/subscription", function(metadata)
534 { 457 {
535 response.setStatusLine("1.1", "200", "OK"); 458 return [Cr.NS_OK, 200, "[Adblock]\n! Redirect: http://example.com/subscrip tion2"];
536 response.setHeader("Content-Type", "text/plain"); 459 });
537 460 testRunner.registerHandler("/subscription2", function(metadata, response)
538 let result = "[Adblock]\n! Redirect: http://127.0.0.1:1234/subscription2"; 461 {
539 response.bodyOutputStream.write(result, result.length); 462 return [Cr.NS_OK, 200, "[Adblock]\n! Redirect: http://example.com/subscrip tion"];
540 }); 463 });
541 server.registerPathHandler("/subscription2", function(metadata, response) 464
542 { 465 subscription = Subscription.fromURL("http://example.com/subscription");
543 response.setStatusLine("1.1", "200", "OK"); 466 resetSubscription(subscription);
544 response.setHeader("Content-Type", "text/plain"); 467 FilterStorage.removeSubscription(FilterStorage.subscriptions[0]);
545 468 FilterStorage.addSubscription(subscription);
546 let result = "[Adblock]\n! Redirect: http://127.0.0.1:1234/subscription"; 469
547 response.bodyOutputStream.write(result, result.length); 470 testRunner.runScheduledTasks(100);
548 }); 471 equal(FilterStorage.subscriptions[0].url, "http://example.com/redirected", " Fallback can still redirect even after a redirect loop");
549
550 subscription = Subscription.fromURL("http://127.0.0.1:1234/subscription");
551 resetSubscription(subscription);
552 FilterStorage.removeSubscription(FilterStorage.subscriptions[0]);
553 FilterStorage.addSubscription(subscription);
554
555 testRunner.runScheduledTasks(100);
556 equal(FilterStorage.subscriptions[0].url, "http://127.0.0.1:1234/redirected" , "Fallback can still redirect even after a redirect loop");
557 }); 472 });
558 473
559 test("State fields", function() 474 test("State fields", function()
560 { 475 {
561 let subscription = Subscription.fromURL("http://127.0.0.1:1234/subscription" ); 476 let subscription = Subscription.fromURL("http://example.com/subscription");
562 FilterStorage.addSubscription(subscription); 477 FilterStorage.addSubscription(subscription);
563 478
564 server.registerPathHandler("/subscription", function successHandler(metadata , response) 479 testRunner.registerHandler("/subscription", function(metadata)
565 { 480 {
566 response.setStatusLine("1.1", "200", "OK"); 481 return [Cr.NS_OK, 200, "[Adblock]\n! Expires: 2 hours\nfoo\nbar"];
567 response.setHeader("Content-Type", "text/plain");
568
569 let result = "[Adblock]\n! Expires: 2 hours\nfoo\nbar";
570 response.bodyOutputStream.write(result, result.length);
571 }); 482 });
572 483
573 let startTime = testRunner.currentTime; 484 let startTime = testRunner.currentTime;
574 testRunner.runScheduledTasks(2); 485 testRunner.runScheduledTasks(2);
575 486
576 equal(subscription.downloadStatus, "synchronize_ok", "downloadStatus after s uccessful download"); 487 equal(subscription.downloadStatus, "synchronize_ok", "downloadStatus after s uccessful download");
577 equal(subscription.lastDownload * MILLIS_IN_SECOND, startTime + 0.1 * MILLIS _IN_HOUR, "lastDownload after successful download"); 488 equal(subscription.lastDownload * MILLIS_IN_SECOND, startTime + 0.1 * MILLIS _IN_HOUR, "lastDownload after successful download");
578 equal(subscription.lastSuccess * MILLIS_IN_SECOND, startTime + 0.1 * MILLIS_ IN_HOUR, "lastSuccess after successful download"); 489 equal(subscription.lastSuccess * MILLIS_IN_SECOND, startTime + 0.1 * MILLIS_ IN_HOUR, "lastSuccess after successful download");
579 equal(subscription.lastCheck * MILLIS_IN_SECOND, startTime + 1.1 * MILLIS_IN _HOUR, "lastCheck after successful download"); 490 equal(subscription.lastCheck * MILLIS_IN_SECOND, startTime + 1.1 * MILLIS_IN _HOUR, "lastCheck after successful download");
580 equal(subscription.errors, 0, "errors after successful download"); 491 equal(subscription.errors, 0, "errors after successful download");
581 492
582 server.registerPathHandler("/subscription", function errorHandler(metadata, response) 493 testRunner.registerHandler("/subscription", function(metadata)
583 { 494 {
584 response.setStatusLine("1.1", "404", "Not Found"); 495 return [Cr.NS_ERROR_FAILURE, 0, ""];
585 }); 496 });
586 497
587 testRunner.runScheduledTasks(2); 498 testRunner.runScheduledTasks(2);
588 499
500 equal(subscription.downloadStatus, "synchronize_connection_error", "download Status after connection error");
501 equal(subscription.lastDownload * MILLIS_IN_SECOND, startTime + 2.1 * MILLIS _IN_HOUR, "lastDownload after connection error");
502 equal(subscription.lastSuccess * MILLIS_IN_SECOND, startTime + 0.1 * MILLIS_ IN_HOUR, "lastSuccess after connection error");
503 equal(subscription.lastCheck * MILLIS_IN_SECOND, startTime + 3.1 * MILLIS_IN _HOUR, "lastCheck after connection error");
504 equal(subscription.errors, 1, "errors after connection error");
505
506 testRunner.registerHandler("/subscription", function(metadata)
507 {
508 return [Cr.NS_OK, 404, ""];
509 });
510
511 testRunner.runScheduledTasks(24);
512
589 equal(subscription.downloadStatus, "synchronize_connection_error", "download Status after download error"); 513 equal(subscription.downloadStatus, "synchronize_connection_error", "download Status after download error");
590 equal(subscription.lastDownload * MILLIS_IN_SECOND, startTime + 2.1 * MILLIS _IN_HOUR, "lastDownload after download error"); 514 equal(subscription.lastDownload * MILLIS_IN_SECOND, startTime + 26.1 * MILLI S_IN_HOUR, "lastDownload after download error");
591 equal(subscription.lastSuccess * MILLIS_IN_SECOND, startTime + 0.1 * MILLIS_ IN_HOUR, "lastSuccess after download error"); 515 equal(subscription.lastSuccess * MILLIS_IN_SECOND, startTime + 0.1 * MILLIS_ IN_HOUR, "lastSuccess after download error");
592 equal(subscription.lastCheck * MILLIS_IN_SECOND, startTime + 3.1 * MILLIS_IN _HOUR, "lastCheck after download error"); 516 equal(subscription.lastCheck * MILLIS_IN_SECOND, startTime + 27.1 * MILLIS_I N_HOUR, "lastCheck after download error");
593 equal(subscription.errors, 1, "errors after download error"); 517 equal(subscription.errors, 2, "errors after download error");
594 }); 518 });
595 })(); 519 })();
OLDNEW
« no previous file with comments | « chrome/content/tests/notification.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld