Unit test for Bug 893316 - Http.jsm doesn't listen to method option parameter. r=Mossop
authorMike Conley <mconley@mozilla.com>
Mon, 15 Jul 2013 09:26:55 +0200
changeset 138454 f8b9e399e92f62c474f40207512199c7df5e2a1e
parent 138453 a862c3557b27dccd5579dddf1c60678b0b68de72
child 138455 58fe760f66d8997077d0cde7274b935197f0f5c9
push id30983
push userbugzilla@standard8.plus.com
push dateMon, 15 Jul 2013 07:28:20 +0000
treeherdermozilla-inbound@f8b9e399e92f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMossop
bugs893316
milestone25.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Unit test for Bug 893316 - Http.jsm doesn't listen to method option parameter. r=Mossop
toolkit/modules/tests/xpcshell/test_Http.js
--- a/toolkit/modules/tests/xpcshell/test_Http.js
+++ b/toolkit/modules/tests/xpcshell/test_Http.js
@@ -6,43 +6,53 @@ Components.utils.import("resource://test
 
 const BinaryInputStream = Components.Constructor("@mozilla.org/binaryinputstream;1",
   "nsIBinaryInputStream", "setInputStream");
 
 var server;
 
 const kDefaultServerPort = 9000;
 const kSuccessPath = "/success";
-const kPostPath = "/post";
 const kBaseUrl = "http://localhost:" + kDefaultServerPort;
 const kSuccessUrl = kBaseUrl + kSuccessPath;
+
+const kPostPath = "/post";
 const kPostUrl = kBaseUrl + kPostPath;
+const kPostDataSent = [["foo", "bar"], ["complex", "!*()@"]];
+const kPostDataReceived = "foo=bar&complex=%21%2A%28%29%40";
+
+const kPutPath = "/put";
+const kPutUrl = kBaseUrl + kPutPath;
+const kPutDataSent = [["P", "NP"]];
+const kPutDataReceived = "P=NP";
 
 function successResult(aRequest, aResponse) {
   aResponse.setStatusLine(null, 200, "OK");
   aResponse.setHeader("Content-Type", "application/json");
   aResponse.write("Success!");
 }
 
-function checkData(aRequest, aResponse) {
-  let body = new BinaryInputStream(aRequest.bodyInputStream);
-  let bytes = [];
-  let avail;
-  while ((avail = body.available()) > 0)
-    Array.prototype.push.apply(bytes, body.readByteArray(avail));
+function getDataChecker(aExpectedMethod, aExpectedData) {
+  return function(aRequest, aResponse) {
+    let body = new BinaryInputStream(aRequest.bodyInputStream);
+    let bytes = [];
+    let avail;
+    while ((avail = body.available()) > 0)
+      Array.prototype.push.apply(bytes, body.readByteArray(avail));
 
-  do_check_eq(aRequest.method, "POST");
+    do_check_eq(aRequest.method, aExpectedMethod);
 
-  var data = String.fromCharCode.apply(null, bytes);
+    var data = String.fromCharCode.apply(null, bytes);
 
-  do_check_eq(data, "foo=bar&complex=%21%2A%28%29%40");
+    do_check_eq(data, aExpectedData);
 
-  aResponse.setStatusLine(null, 200, "OK");
-  aResponse.setHeader("Content-Type", "application/json");
-  aResponse.write("Success!");
+    aResponse.setStatusLine(null, 200, "OK");
+    aResponse.setHeader("Content-Type", "application/json");
+    aResponse.write("Success!");
+  }
 }
 
 add_test(function test_successCallback() {
   do_test_pending();
   let options = {
     onLoad: function(aResponse) {
       do_check_eq(aResponse, "Success!");
       do_test_finished();
@@ -82,26 +92,48 @@ add_test(function test_PostData() {
       do_test_finished();
       run_next_test();
     },
     onError: function(e) {
       do_check_true(false);
       do_test_finished();
       run_next_test();
     },
-    postData: [["foo", "bar"], ["complex", "!*()@"]]
+    postData: kPostDataSent
   }
   httpRequest(kPostUrl, options);
 });
 
+add_test(function test_PutData() {
+  do_test_pending();
+  let options = {
+    method: "PUT",
+    onLoad: function(aResponse) {
+      do_check_eq(aResponse, "Success!");
+      do_test_finished();
+      run_next_test();
+    },
+    onError: function(e) {
+      do_check_true(false);
+      do_test_finished();
+      run_next_test();
+    },
+    postData: kPutDataSent
+  }
+  httpRequest(kPutUrl, options);
+});
+
 function run_test() {
   // Set up a mock HTTP server to serve a success page.
   server = new HttpServer();
   server.registerPathHandler(kSuccessPath, successResult);
-  server.registerPathHandler(kPostPath, checkData);
+  server.registerPathHandler(kPostPath,
+                             getDataChecker("POST", kPostDataReceived));
+  server.registerPathHandler(kPutPath,
+                             getDataChecker("PUT", kPutDataReceived));
   server.start(kDefaultServerPort);
 
   run_next_test();
 
   // Teardown.
   do_register_cleanup(function() {
     server.stop(function() { });
   });