netwerk/test/unit/test_simple.js

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

     1 //
     2 //  Simple HTTP test: fetches page
     3 //
     5 // Note: sets Cc and Ci variables
     7 Cu.import("resource://testing-common/httpd.js");
     9 var httpserver = new HttpServer();
    10 var testpath = "/simple";
    11 var httpbody = "0123456789";
    12 var buffer = "";
    14 var dbg=0
    15 if (dbg) { print("============== START =========="); }
    17 function run_test() {
    18   setup_test();
    19   do_test_pending();
    20 }
    22 function setup_test() {
    23   if (dbg) { print("============== setup_test: in"); }
    24   httpserver.registerPathHandler(testpath, serverHandler);
    25   httpserver.start(-1);
    26   var channel = setupChannel(testpath);
    27   // ChannelListener defined in head_channels.js
    28   channel.asyncOpen(new ChannelListener(checkRequest, channel), null);
    29   if (dbg) { print("============== setup_test: out"); }
    30 }
    32 function setupChannel(path) {
    33   var ios = Cc["@mozilla.org/network/io-service;1"].
    34                        getService(Ci.nsIIOService);
    35   var chan = ios.newChannel("http://localhost:" +
    36                             httpserver.identity.primaryPort + path, "", null);
    37   chan.QueryInterface(Ci.nsIHttpChannel);
    38   chan.requestMethod = "GET";
    39   return chan;
    40 }
    42 function serverHandler(metadata, response) {
    43   if (dbg) { print("============== serverHandler: in"); }
    44   response.setHeader("Content-Type", "text/plain", false);
    45   response.bodyOutputStream.write(httpbody, httpbody.length);
    46   if (dbg) { print("============== serverHandler: out"); }
    47 }
    49 function checkRequest(request, data, context) {
    50   if (dbg) { print("============== checkRequest: in"); }
    51   do_check_eq(data, httpbody);
    52   httpserver.stop(do_test_finished);
    53   if (dbg) { print("============== checkRequest: out"); }
    54 }

mercurial