summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_multipart_streamconv_empty.js
blob: 82b7f7000cb792b5ca700b9c26d1ec0da7bc761f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"use strict";

function make_channel(url) {
  return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
}

add_task(async function test_empty() {
  let uri = "http://localhost";
  let httpChan = make_channel(uri);

  let channel = Cc["@mozilla.org/network/input-stream-channel;1"]
    .createInstance(Ci.nsIInputStreamChannel)
    .QueryInterface(Ci.nsIChannel);

  channel.setURI(httpChan.URI);
  channel.loadInfo = httpChan.loadInfo;

  let inputStream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(
    Ci.nsIStringInputStream
  );
  inputStream.setUTF8Data(""); // Pass an empty string

  channel.contentStream = inputStream;

  let [status, buffer] = await new Promise(resolve => {
    let streamConv = Cc["@mozilla.org/streamConverters;1"].getService(
      Ci.nsIStreamConverterService
    );
    let multipartListener = {
      _buffer: "",

      QueryInterface: ChromeUtils.generateQI([
        "nsIStreamListener",
        "nsIRequestObserver",
      ]),

      onStartRequest() {},
      onDataAvailable(request, stream, offset, count) {
        try {
          this._buffer = this._buffer.concat(read_stream(stream, count));
          dump("BUFFEEE: " + this._buffer + "\n\n");
        } catch (ex) {
          do_throw("Error in onDataAvailable: " + ex);
        }
      },

      onStopRequest(request, status1) {
        resolve([status1, this._buffer]);
      },
    };
    let conv = streamConv.asyncConvertData(
      "multipart/mixed",
      "*/*",
      multipartListener,
      null
    );

    let chan = make_channel(uri);
    chan.asyncOpen(conv);
  });

  Assert.notEqual(
    status,
    Cr.NS_OK,
    "Should be an error code because content has no boundary"
  );
  Assert.equal(buffer, "", "Should have received no content");
});