Bug 1931425 - Limit how often moz-label's #setStyles runs r=reusable-components-revie...
[gecko.git] / netwerk / test / unit / test_multipart_byteranges.js
blob014e040395a3af4ae2f8fd87f26fabf17cfcb3ac
1 "use strict";
3 const { HttpServer } = ChromeUtils.importESModule(
4 "resource://testing-common/httpd.sys.mjs"
5 );
7 var httpserver = null;
9 ChromeUtils.defineLazyGetter(this, "uri", function () {
10 return "http://localhost:" + httpserver.identity.primaryPort + "/multipart";
11 });
13 function make_channel(url) {
14 return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
17 var multipartBody =
18 "--boundary\r\n" +
19 "Content-type: text/plain\r\n" +
20 "Content-range: bytes 0-2/10\r\n" +
21 "\r\n" +
22 "aaa\r\n" +
23 "--boundary\r\n" +
24 "Content-type: text/plain\r\n" +
25 "Content-range: bytes 3-7/10\r\n" +
26 "\r\n" +
27 "bbbbb" +
28 "\r\n" +
29 "--boundary\r\n" +
30 "Content-type: text/plain\r\n" +
31 "Content-range: bytes 8-9/10\r\n" +
32 "\r\n" +
33 "cc" +
34 "\r\n" +
35 "--boundary--";
37 function contentHandler(metadata, response) {
38 response.setHeader(
39 "Content-Type",
40 'multipart/byteranges; boundary="boundary"'
42 response.bodyOutputStream.write(multipartBody, multipartBody.length);
45 var numTests = 2;
46 var testNum = 0;
48 var testData = [
50 data: "aaa",
51 type: "text/plain",
52 isByteRangeRequest: true,
53 startRange: 0,
54 endRange: 2,
57 data: "bbbbb",
58 type: "text/plain",
59 isByteRangeRequest: true,
60 startRange: 3,
61 endRange: 7,
64 data: "cc",
65 type: "text/plain",
66 isByteRangeRequest: true,
67 startRange: 8,
68 endRange: 9,
72 function responseHandler(request, buffer) {
73 Assert.equal(buffer, testData[testNum].data);
74 Assert.equal(
75 request.QueryInterface(Ci.nsIChannel).contentType,
76 testData[testNum].type
78 Assert.equal(
79 request.QueryInterface(Ci.nsIByteRangeRequest).isByteRangeRequest,
80 testData[testNum].isByteRangeRequest
82 Assert.equal(
83 request.QueryInterface(Ci.nsIByteRangeRequest).startRange,
84 testData[testNum].startRange
86 Assert.equal(
87 request.QueryInterface(Ci.nsIByteRangeRequest).endRange,
88 testData[testNum].endRange
90 if (++testNum == numTests) {
91 httpserver.stop(do_test_finished);
95 var multipartListener = {
96 _buffer: "",
98 QueryInterface: ChromeUtils.generateQI([
99 "nsIStreamListener",
100 "nsIRequestObserver",
103 onStartRequest() {
104 this._buffer = "";
107 onDataAvailable(request, stream, offset, count) {
108 try {
109 this._buffer = this._buffer.concat(read_stream(stream, count));
110 dump("BUFFEEE: " + this._buffer + "\n\n");
111 } catch (ex) {
112 do_throw("Error in onDataAvailable: " + ex);
116 onStopRequest(request) {
117 try {
118 responseHandler(request, this._buffer);
119 } catch (ex) {
120 do_throw("Error in closure function: " + ex);
125 function run_test() {
126 httpserver = new HttpServer();
127 httpserver.registerPathHandler("/multipart", contentHandler);
128 httpserver.start(-1);
130 var streamConv = Cc["@mozilla.org/streamConverters;1"].getService(
131 Ci.nsIStreamConverterService
133 var conv = streamConv.asyncConvertData(
134 "multipart/byteranges",
135 "*/*",
136 multipartListener,
137 null
140 var chan = make_channel(uri);
141 chan.asyncOpen(conv, null);
142 do_test_pending();