tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_multipart_streamconv-byte-by-byte.js (2810B)


      1 "use strict";
      2 
      3 const { HttpServer } = ChromeUtils.importESModule(
      4  "resource://testing-common/httpd.sys.mjs"
      5 );
      6 
      7 var httpserver = null;
      8 
      9 ChromeUtils.defineLazyGetter(this, "uri", function () {
     10  return "http://localhost:" + httpserver.identity.primaryPort + "/multipart";
     11 });
     12 
     13 function make_channel(url) {
     14  return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
     15 }
     16 
     17 var multipartBody =
     18  "--boundary\r\n\r\nSome text\r\n--boundary\r\nContent-Type: text/x-test\r\n\r\n<?xml version='1.1'?>\r\n<root/>\r\n--boundary\r\n\r\n<?xml version='1.0'?><root/>\r\n--boundary--";
     19 
     20 function contentHandler(metadata, response) {
     21  response.setHeader("Content-Type", 'multipart/mixed; boundary="boundary"');
     22  response.processAsync();
     23 
     24  var body = multipartBody;
     25  function byteByByte() {
     26    if (!body.length) {
     27      response.finish();
     28      return;
     29    }
     30 
     31    var onebyte = body[0];
     32    response.bodyOutputStream.write(onebyte, 1);
     33    body = body.substring(1);
     34    do_timeout(1, byteByByte);
     35  }
     36 
     37  do_timeout(1, byteByByte);
     38 }
     39 
     40 var numTests = 2;
     41 var testNum = 0;
     42 
     43 var testData = [
     44  { data: "Some text", type: "text/plain" },
     45  { data: "<?xml version='1.1'?>\r\n<root/>", type: "text/x-test" },
     46  { data: "<?xml version='1.0'?><root/>", type: "text/xml" },
     47 ];
     48 
     49 function responseHandler(request, buffer) {
     50  Assert.equal(buffer, testData[testNum].data);
     51  Assert.equal(
     52    request.QueryInterface(Ci.nsIChannel).contentType,
     53    testData[testNum].type
     54  );
     55  if (++testNum == numTests) {
     56    httpserver.stop(do_test_finished);
     57  }
     58 }
     59 
     60 var multipartListener = {
     61  _buffer: "",
     62  _index: 0,
     63 
     64  QueryInterface: ChromeUtils.generateQI([
     65    "nsIStreamListener",
     66    "nsIRequestObserver",
     67  ]),
     68 
     69  onStartRequest() {
     70    this._buffer = "";
     71  },
     72 
     73  onDataAvailable(request, stream, offset, count) {
     74    try {
     75      this._buffer = this._buffer.concat(read_stream(stream, count));
     76      dump("BUFFEEE: " + this._buffer + "\n\n");
     77    } catch (ex) {
     78      do_throw("Error in onDataAvailable: " + ex);
     79    }
     80  },
     81 
     82  onStopRequest(request) {
     83    this._index++;
     84    // Second part should be last part
     85    Assert.equal(
     86      request.QueryInterface(Ci.nsIMultiPartChannel).isLastPart,
     87      this._index == testData.length
     88    );
     89    try {
     90      responseHandler(request, this._buffer);
     91    } catch (ex) {
     92      do_throw("Error in closure function: " + ex);
     93    }
     94  },
     95 };
     96 
     97 function run_test() {
     98  httpserver = new HttpServer();
     99  httpserver.registerPathHandler("/multipart", contentHandler);
    100  httpserver.start(-1);
    101 
    102  var streamConv = Cc["@mozilla.org/streamConverters;1"].getService(
    103    Ci.nsIStreamConverterService
    104  );
    105  var conv = streamConv.asyncConvertData(
    106    "multipart/mixed",
    107    "*/*",
    108    multipartListener,
    109    null
    110  );
    111 
    112  var chan = make_channel(uri);
    113  chan.asyncOpen(conv);
    114  do_test_pending();
    115 }