netwerk/test/unit/test_multipart_byteranges.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/test_multipart_byteranges.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,120 @@
     1.4 +Cu.import("resource://testing-common/httpd.js");
     1.5 +
     1.6 +var httpserver = null;
     1.7 +
     1.8 +XPCOMUtils.defineLazyGetter(this, "uri", function() {
     1.9 +  return "http://localhost:" + httpserver.identity.primaryPort + "/multipart";
    1.10 +});
    1.11 +
    1.12 +function make_channel(url) {
    1.13 +  var ios = Cc["@mozilla.org/network/io-service;1"].
    1.14 +            getService(Ci.nsIIOService);
    1.15 +  return ios.newChannel(url, "", null);
    1.16 +}
    1.17 +
    1.18 +var multipartBody = "--boundary\r\n"+
    1.19 +"Content-type: text/plain\r\n"+
    1.20 +"Content-range: bytes 0-2/10\r\n"+
    1.21 +"\r\n"+
    1.22 +"aaa\r\n"+
    1.23 +"--boundary\r\n"+
    1.24 +"Content-type: text/plain\r\n"+
    1.25 +"Content-range: bytes 3-7/10\r\n"+
    1.26 +"\r\n"+
    1.27 +"bbbbb"+
    1.28 +"\r\n"+
    1.29 +"--boundary\r\n"+
    1.30 +"Content-type: text/plain\r\n"+
    1.31 +"Content-range: bytes  8-9/10\r\n"+
    1.32 +"\r\n"+
    1.33 +"cc"+
    1.34 +"\r\n"+
    1.35 +"--boundary--";
    1.36 +
    1.37 +function make_channel(url) {
    1.38 +  var ios = Cc["@mozilla.org/network/io-service;1"].
    1.39 +            getService(Ci.nsIIOService);
    1.40 +  return ios.newChannel(url, "", null);
    1.41 +}
    1.42 +
    1.43 +function contentHandler(metadata, response)
    1.44 +{
    1.45 +  response.setHeader("Content-Type", 'multipart/byteranges; boundary="boundary"');
    1.46 +  response.bodyOutputStream.write(multipartBody, multipartBody.length);
    1.47 +}
    1.48 +
    1.49 +var numTests = 2;
    1.50 +var testNum = 0;
    1.51 +
    1.52 +var testData =
    1.53 +  [
    1.54 +   { data: "aaa", type: "text/plain", isByteRangeRequest: true, startRange: 0, endRange: 2 },
    1.55 +   { data: "bbbbb", type: "text/plain", isByteRangeRequest: true, startRange: 3, endRange: 7 },
    1.56 +   { data: "cc", type: "text/plain", isByteRangeRequest: true, startRange: 8, endRange: 9 }
    1.57 +  ];
    1.58 +
    1.59 +function responseHandler(request, buffer)
    1.60 +{
    1.61 +    do_check_eq(buffer, testData[testNum].data);
    1.62 +    do_check_eq(request.QueryInterface(Ci.nsIChannel).contentType,
    1.63 +		testData[testNum].type);
    1.64 +    do_check_eq(request.QueryInterface(Ci.nsIByteRangeRequest).isByteRangeRequest,
    1.65 +		testData[testNum].isByteRangeRequest);
    1.66 +    do_check_eq(request.QueryInterface(Ci.nsIByteRangeRequest).startRange,
    1.67 +		testData[testNum].startRange);
    1.68 +    do_check_eq(request.QueryInterface(Ci.nsIByteRangeRequest).endRange,
    1.69 +		testData[testNum].endRange);
    1.70 +    if (++testNum == numTests)
    1.71 +	httpserver.stop(do_test_finished);
    1.72 +}
    1.73 +
    1.74 +var multipartListener = {
    1.75 +  _buffer: "",
    1.76 +
    1.77 +  QueryInterface: function(iid) {
    1.78 +    if (iid.equals(Components.interfaces.nsIStreamListener) ||
    1.79 +        iid.equals(Components.interfaces.nsIRequestObserver) ||
    1.80 +        iid.equals(Components.interfaces.nsISupports))
    1.81 +      return this;
    1.82 +    throw Components.results.NS_ERROR_NO_INTERFACE;
    1.83 +  },
    1.84 +
    1.85 +  onStartRequest: function(request, context) {
    1.86 +    this._buffer = "";
    1.87 +  },
    1.88 +
    1.89 +  onDataAvailable: function(request, context, stream, offset, count) {
    1.90 +    try {
    1.91 +      this._buffer = this._buffer.concat(read_stream(stream, count));
    1.92 +      dump("BUFFEEE: " + this._buffer + "\n\n");
    1.93 +    } catch (ex) {
    1.94 +      do_throw("Error in onDataAvailable: " + ex);
    1.95 +    }
    1.96 +  },
    1.97 +
    1.98 +  onStopRequest: function(request, context, status) {
    1.99 +    try {
   1.100 +      responseHandler(request, this._buffer);
   1.101 +    } catch (ex) {
   1.102 +      do_throw("Error in closure function: " + ex);
   1.103 +    }
   1.104 +  }
   1.105 +};
   1.106 +
   1.107 +function run_test()
   1.108 +{
   1.109 +  httpserver = new HttpServer();
   1.110 +  httpserver.registerPathHandler("/multipart", contentHandler);
   1.111 +  httpserver.start(-1);
   1.112 +
   1.113 +  var streamConv = Cc["@mozilla.org/streamConverters;1"]
   1.114 +                     .getService(Ci.nsIStreamConverterService);
   1.115 +  var conv = streamConv.asyncConvertData("multipart/byteranges",
   1.116 +					 "*/*",
   1.117 +					 multipartListener,
   1.118 +					 null);
   1.119 +
   1.120 +  var chan = make_channel(uri);
   1.121 +  chan.asyncOpen(conv, null);
   1.122 +  do_test_pending();
   1.123 +}

mercurial