Bug 786419 - Part 7 - xpcshell test for HTTP per-app offline r=jduell
authorValentin Gosu <valentin.gosu@gmail.com>
Sat, 23 Aug 2014 05:06:44 +0300
changeset 209174 9653d9285cca794827cd6fa55b8ac8dbe1a3354b
parent 209173 1ca5026b6e8af8dc782a668cf843df476ef88457
child 209175 c748712cd1ee62af9ea485449b278af08288b29a
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersjduell
bugs786419
milestone35.0a1
Bug 786419 - Part 7 - xpcshell test for HTTP per-app offline r=jduell
netwerk/test/unit_ipc/child_app_offline.js
netwerk/test/unit_ipc/test_app_offline_http.js
netwerk/test/unit_ipc/xpcshell.ini
new file mode 100644
--- /dev/null
+++ b/netwerk/test/unit_ipc/child_app_offline.js
@@ -0,0 +1,55 @@
+
+function inChildProcess() {
+  return Cc["@mozilla.org/xre/app-info;1"]
+           .getService(Ci.nsIXULRuntime)
+           .processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
+}
+
+function makeChan(url, appId, inBrowser) {
+  var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
+  var chan = ios.newChannel(url, null, null).QueryInterface(Ci.nsIHttpChannel);
+  chan.notificationCallbacks = {
+    appId: appId,
+    isInBrowserElement: inBrowser,
+    QueryInterface: function(iid) {
+      if (iid.equals(Ci.nsILoadContext))
+        return this;
+      throw Cr.NS_ERROR_NO_INTERFACE;
+    },
+    getInterface: function(iid) { return this.QueryInterface(iid); }
+  };
+  return chan;
+}
+
+// Simple online load
+function run_test() {
+  do_test_pending();
+  var chan = makeChan("http://localhost:12345/first", 14, false);
+  chan.asyncOpen(new ChannelListener(checkResponse, "response0"), null);
+}
+
+// Should return cached result
+function test1() {
+  do_test_pending();
+  var chan = makeChan("http://localhost:12345/first", 14, false);
+  chan.asyncOpen(new ChannelListener(checkResponse, "response0"), null);
+}
+
+// This request should fail
+function test2() {
+  do_test_pending();
+  var chan = makeChan("http://localhost:12345/second", 14, false);
+  chan.asyncOpen(new ChannelListener(checkResponse, "", CL_EXPECT_FAILURE), null);
+}
+
+// This request should succeed
+function test3() {
+  do_test_pending();
+  var chan = makeChan("http://localhost:12345/second", 14, false);
+  chan.asyncOpen(new ChannelListener(checkResponse, "response3"), null);
+}
+
+function checkResponse(req, buffer, expected) {
+  do_check_eq(buffer, expected);
+  do_test_finished();
+}
new file mode 100644
--- /dev/null
+++ b/netwerk/test/unit_ipc/test_app_offline_http.js
@@ -0,0 +1,74 @@
+
+Cu.import("resource://testing-common/httpd.js");
+Cu.import("resource://gre/modules/Services.jsm");
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+
+var test_index = 0;
+
+var responses = [
+  "response0", // This should be the first returned value
+  "response1", // This response should not be recevied. Load response0 from cache
+  "response2", // This request should fail
+  "response3", // This request should succeed
+  ];
+
+function http_handler(metadata, response) {
+  response.setHeader("Content-Type", "text/plain", false);
+  response.setHeader("Cache-Control", "no-cache", false);
+  response.setStatusLine(metadata.httpVersion, 200, "OK");
+  var body = responses[test_index];
+  response.bodyOutputStream.write(body, body.length);
+}
+
+
+function set_app_offline(appId, offline) {
+  let ioservice = Cc['@mozilla.org/network/io-service;1'].
+    getService(Ci.nsIIOService);
+
+  ioservice.setAppOffline(appId, offline);
+}
+
+var httpserv;
+
+function setup() {
+  httpserv = new HttpServer();
+  httpserv.registerPathHandler("/first", http_handler);
+  httpserv.registerPathHandler("/second", http_handler);
+  httpserv.start(12345);
+}
+
+function run_test() {
+  setup();
+  test0();
+}
+
+// Test that app 14 can open channel
+function test0() {
+  test_index = 0;
+  run_test_in_child("child_app_offline.js", test1);
+}
+
+// Set app 14 offline and check that it still gets a cached response
+function test1() {
+  test_index = 1;
+  set_app_offline(14, Ci.nsIAppOfflineInfo.OFFLINE);
+  sendCommand('test1();\n', test2);
+}
+
+// Check that app 14 can't open a channel to a new location
+function test2() {
+  test_index = 2;
+  sendCommand('test2();\n', test3);
+}
+
+
+// Set app online and check that it now works
+function test3() {
+  test_index = 3;
+  set_app_offline(14, Ci.nsIAppOfflineInfo.ONLINE);
+  sendCommand('test3();\n', ending);
+}
+
+function ending(val) {
+  do_test_finished();
+}
--- a/netwerk/test/unit_ipc/xpcshell.ini
+++ b/netwerk/test/unit_ipc/xpcshell.ini
@@ -1,12 +1,13 @@
 [DEFAULT]
 head = head_channels_clone.js head_cc.js
 tail =
 support-files = disabled_test_bug528292_wrap.js
+	child_app_offline.js
 
 [test_bug248970_cookie_wrap.js]
 [test_cacheflags_wrap.js]
 [test_cache_jar_wrap.js]
 [test_channel_close_wrap.js]
 [test_cookie_header_wrap.js]
 [test_cookiejars_wrap.js]
 [test_dns_cancel_wrap.js]
@@ -30,8 +31,9 @@ skip-if = true
 [test_redirect_different-protocol_wrap.js]
 [test_reentrancy_wrap.js]
 [test_resumable_channel_wrap.js]
 [test_simple_wrap.js]
 [test_xmlhttprequest_wrap.js]
 [test_XHR_redirects.js]
 [test_redirect_history_wrap.js]
 [test_reply_without_content_type_wrap.js]
+[test_app_offline_http.js]