netwerk/test/unit/test_multipart_streamconv.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 Cu.import("resource://testing-common/httpd.js");
     3 var httpserver = null;
     5 XPCOMUtils.defineLazyGetter(this, "uri", function() {
     6   return "http://localhost:" + httpserver.identity.primaryPort + "/multipart";
     7 });
     9 function make_channel(url) {
    10   var ios = Cc["@mozilla.org/network/io-service;1"].
    11             getService(Ci.nsIIOService);
    12   return ios.newChannel(url, "", null);
    13 }
    15 var multipartBody = "--boundary\r\n\r\nSome text\r\n--boundary\r\n\r\n<?xml version='1.0'?><root/>\r\n--boundary--";
    17 function make_channel(url) {
    18   var ios = Cc["@mozilla.org/network/io-service;1"].
    19             getService(Ci.nsIIOService);
    20   return ios.newChannel(url, "", null);
    21 }
    23 function contentHandler(metadata, response)
    24 {
    25   response.setHeader("Content-Type", 'multipart/mixed; boundary="boundary"');
    26   response.bodyOutputStream.write(multipartBody, multipartBody.length);
    27 }
    29 var numTests = 2;
    30 var testNum = 0;
    32 var testData =
    33   [
    34    { data: "Some text", type: "text/plain" },
    35    { data: "<?xml version='1.0'?><root/>", type: "text/xml" }
    36   ];
    38 function responseHandler(request, buffer)
    39 {
    40     do_check_eq(buffer, testData[testNum].data);
    41     do_check_eq(request.QueryInterface(Ci.nsIChannel).contentType,
    42 		testData[testNum].type);
    43     if (++testNum == numTests)
    44 	httpserver.stop(do_test_finished);
    45 }
    47 var multipartListener = {
    48   _buffer: "",
    50   QueryInterface: function(iid) {
    51     if (iid.equals(Components.interfaces.nsIStreamListener) ||
    52         iid.equals(Components.interfaces.nsIRequestObserver) ||
    53         iid.equals(Components.interfaces.nsISupports))
    54       return this;
    55     throw Components.results.NS_ERROR_NO_INTERFACE;
    56   },
    58   onStartRequest: function(request, context) {
    59     this._buffer = "";
    60   },
    62   onDataAvailable: function(request, context, stream, offset, count) {
    63     try {
    64       this._buffer = this._buffer.concat(read_stream(stream, count));
    65       dump("BUFFEEE: " + this._buffer + "\n\n");
    66     } catch (ex) {
    67       do_throw("Error in onDataAvailable: " + ex);
    68     }
    69   },
    71   onStopRequest: function(request, context, status) {
    72     try {
    73       responseHandler(request, this._buffer);
    74     } catch (ex) {
    75       do_throw("Error in closure function: " + ex);
    76     }
    77   }
    78 };
    80 function run_test()
    81 {
    82   httpserver = new HttpServer();
    83   httpserver.registerPathHandler("/multipart", contentHandler);
    84   httpserver.start(-1);
    86   var streamConv = Cc["@mozilla.org/streamConverters;1"]
    87                      .getService(Ci.nsIStreamConverterService);
    88   var conv = streamConv.asyncConvertData("multipart/mixed",
    89 					 "*/*",
    90 					 multipartListener,
    91 					 null);
    93   var chan = make_channel(uri);
    94   chan.asyncOpen(conv, null);
    95   do_test_pending();
    96 }

mercurial