1 // This testcase verifies that channels can't be reopened
2 // See https://bugzilla.mozilla.org/show_bug.cgi?id=372486
4 do_import_script("netwerk/test/httpserver/httpd.js");
6 const NS_ERROR_IN_PROGRESS
= 0x804b000f;
7 const NS_ERROR_ALREADY_OPENED
= 0x804b0049;
17 // Commented by default as it relies on external ressources
24 function makeChan(url
) {
25 var ios
= Cc
["@mozilla.org/network/io-service;1"]
26 .getService(Ci
.nsIIOService
);
27 return chan
= ios
.newChannel(url
, null, null)
28 .QueryInterface(Ci
.nsIChannel
);
31 function new_file_channel(file
) {
32 var ios
= Cc
["@mozilla.org/network/io-service;1"]
33 .getService(Ci
.nsIIOService
);
34 return ios
.newChannelFromURI(ios
.newFileURI(file
));
38 function check_throws(closure
, error
) {
43 if (error
instanceof Array
) {
44 do_check_neq(error
.indexOf(e
.result
), -1);
46 do_check_eq(e
.result
, error
);
50 do_check_true(thrown
);
53 function check_open_throws(error
) {
54 check_throws(function() {
55 chan
.open(listener
, null);
59 function check_async_open_throws(error
) {
60 check_throws(function() {
61 chan
.asyncOpen(listener
, null);
66 onStartRequest
: function test_onStartR(request
, ctx
) {
67 check_async_open_throws(NS_ERROR_IN_PROGRESS
);
70 onDataAvailable
: function test_ODA(request
, cx
, inputStream
,
72 new BinaryInputStream(inputStream
).readByteArray(count
); // required by API
73 check_async_open_throws(NS_ERROR_IN_PROGRESS
);
76 onStopRequest
: function test_onStopR(request
, ctx
, status
) {
77 // Once onStopRequest is reached, the channel is marked as having been
79 check_async_open_throws(NS_ERROR_ALREADY_OPENED
);
80 do_timeout(0, "after_channel_closed()");
84 function after_channel_closed() {
85 check_async_open_throws(NS_ERROR_ALREADY_OPENED
);
90 function run_next_test() {
91 test_array
[test_index
++]();
94 function test_channel(createChanClosure
) {
95 // First, synchronous reopening test
96 chan
= createChanClosure();
97 var inputStream
= chan
.open();
98 check_open_throws(NS_ERROR_IN_PROGRESS
);
99 check_async_open_throws([NS_ERROR_IN_PROGRESS
, NS_ERROR_ALREADY_OPENED
]);
101 // Then, asynchronous one
102 chan
= createChanClosure();
103 chan
.asyncOpen(listener
, null);
104 check_open_throws(NS_ERROR_IN_PROGRESS
);
105 check_async_open_throws(NS_ERROR_IN_PROGRESS
);
108 function test_data_channel() {
109 test_channel(function() {
110 return makeChan("data:text/plain,foo");
114 function test_http_channel() {
115 test_channel(function() {
116 return makeChan("http://localhost:4444/");
120 function test_file_channel() {
121 var file
= do_get_file("netwerk/test/Makefile.in");
122 test_channel(function() {
123 return new_file_channel(file
);
127 // Uncomment test_ftp_channel in test_array to test this
128 function test_ftp_channel() {
129 test_channel(function() {
130 return makeChan("ftp://ftp.mozilla.org/pub/mozilla.org/README");
139 function run_test() {
141 httpserv
= new nsHttpServer();
142 httpserv
.start(4444);