Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / netwerk / test / unit / test_reopen.js
blob271a7f3e9a36b561d98f1ac7e873bcb8451bf65c
1 // This testcase verifies that channels can't be reopened
2 // See https://bugzilla.mozilla.org/show_bug.cgi?id=372486
4 "use strict";
6 const { HttpServer } = ChromeUtils.importESModule(
7 "resource://testing-common/httpd.sys.mjs"
8 );
10 const BinaryInputStream = Components.Constructor(
11 "@mozilla.org/binaryinputstream;1",
12 "nsIBinaryInputStream",
13 "setInputStream"
16 const NS_ERROR_IN_PROGRESS = 0x804b000f;
17 const NS_ERROR_ALREADY_OPENED = 0x804b0049;
19 var chan = null;
20 var httpserv = null;
22 [test_data_channel, test_http_channel, test_file_channel, end].forEach(f =>
23 add_test(f)
26 // Utility functions
28 function makeChan(url) {
29 return (chan = NetUtil.newChannel({
30 uri: url,
31 loadUsingSystemPrincipal: true,
32 }).QueryInterface(Ci.nsIChannel));
35 function new_file_channel(file) {
36 return NetUtil.newChannel({
37 uri: Services.io.newFileURI(file),
38 loadUsingSystemPrincipal: true,
39 });
42 function check_throws(closure, error) {
43 var thrown = false;
44 try {
45 closure();
46 } catch (e) {
47 if (error instanceof Array) {
48 Assert.notEqual(error.indexOf(e.result), -1);
49 } else {
50 Assert.equal(e.result, error);
52 thrown = true;
54 Assert.ok(thrown);
57 function check_open_throws(error) {
58 check_throws(function () {
59 chan.open(listener);
60 }, error);
63 function check_async_open_throws(error) {
64 check_throws(function () {
65 chan.asyncOpen(listener);
66 }, error);
69 var listener = {
70 onStartRequest: function test_onStartR() {
71 check_async_open_throws(NS_ERROR_IN_PROGRESS);
74 onDataAvailable: function test_ODA(request, inputStream, offset, count) {
75 new BinaryInputStream(inputStream).readByteArray(count); // required by API
76 check_async_open_throws(NS_ERROR_IN_PROGRESS);
79 onStopRequest: function test_onStopR() {
80 // Once onStopRequest is reached, the channel is marked as having been
81 // opened
82 check_async_open_throws(NS_ERROR_ALREADY_OPENED);
83 do_timeout(0, after_channel_closed);
87 function after_channel_closed() {
88 check_async_open_throws(NS_ERROR_ALREADY_OPENED);
90 run_next_test();
93 function test_channel(createChanClosure) {
94 // First, synchronous reopening test
95 chan = createChanClosure();
96 chan.open();
97 check_open_throws(NS_ERROR_IN_PROGRESS);
98 check_async_open_throws([NS_ERROR_IN_PROGRESS, NS_ERROR_ALREADY_OPENED]);
100 // Then, asynchronous one
101 chan = createChanClosure();
102 chan.asyncOpen(listener);
103 check_open_throws(NS_ERROR_IN_PROGRESS);
104 check_async_open_throws(NS_ERROR_IN_PROGRESS);
107 function test_data_channel() {
108 test_channel(function () {
109 return makeChan("data:text/plain,foo");
113 function test_http_channel() {
114 test_channel(function () {
115 return makeChan("http://localhost:" + httpserv.identity.primaryPort + "/");
119 function test_file_channel() {
120 var file = do_get_file("data/test_readline1.txt");
121 test_channel(function () {
122 return new_file_channel(file);
126 function end() {
127 httpserv.stop(do_test_finished);
130 function run_test() {
131 // start server
132 httpserv = new HttpServer();
133 httpserv.start(-1);
135 run_next_test();