michael@0: Cu.import("resource://testing-common/httpd.js"); michael@0: michael@0: var httpserver = null; michael@0: michael@0: XPCOMUtils.defineLazyGetter(this, "uri", function() { michael@0: return "http://localhost:" + httpserver.identity.primaryPort + "/multipart"; michael@0: }); michael@0: michael@0: function make_channel(url) { michael@0: var ios = Cc["@mozilla.org/network/io-service;1"]. michael@0: getService(Ci.nsIIOService); michael@0: return ios.newChannel(url, "", null); michael@0: } michael@0: michael@0: var multipartBody = "--boundary\r\n\r\nSome text\r\n--boundary\r\n\r\n\r\n--boundary--"; michael@0: michael@0: function make_channel(url) { michael@0: var ios = Cc["@mozilla.org/network/io-service;1"]. michael@0: getService(Ci.nsIIOService); michael@0: return ios.newChannel(url, "", null); michael@0: } michael@0: michael@0: function contentHandler(metadata, response) michael@0: { michael@0: response.setHeader("Content-Type", 'multipart/mixed; boundary="boundary"'); michael@0: response.bodyOutputStream.write(multipartBody, multipartBody.length); michael@0: } michael@0: michael@0: var numTests = 2; michael@0: var testNum = 0; michael@0: michael@0: var testData = michael@0: [ michael@0: { data: "Some text", type: "text/plain" }, michael@0: { data: "", type: "text/xml" } michael@0: ]; michael@0: michael@0: function responseHandler(request, buffer) michael@0: { michael@0: do_check_eq(buffer, testData[testNum].data); michael@0: do_check_eq(request.QueryInterface(Ci.nsIChannel).contentType, michael@0: testData[testNum].type); michael@0: if (++testNum == numTests) michael@0: httpserver.stop(do_test_finished); michael@0: } michael@0: michael@0: var multipartListener = { michael@0: _buffer: "", michael@0: michael@0: QueryInterface: function(iid) { michael@0: if (iid.equals(Components.interfaces.nsIStreamListener) || michael@0: iid.equals(Components.interfaces.nsIRequestObserver) || michael@0: iid.equals(Components.interfaces.nsISupports)) michael@0: return this; michael@0: throw Components.results.NS_ERROR_NO_INTERFACE; michael@0: }, michael@0: michael@0: onStartRequest: function(request, context) { michael@0: this._buffer = ""; michael@0: }, michael@0: michael@0: onDataAvailable: function(request, context, stream, offset, count) { michael@0: try { michael@0: this._buffer = this._buffer.concat(read_stream(stream, count)); michael@0: dump("BUFFEEE: " + this._buffer + "\n\n"); michael@0: } catch (ex) { michael@0: do_throw("Error in onDataAvailable: " + ex); michael@0: } michael@0: }, michael@0: michael@0: onStopRequest: function(request, context, status) { michael@0: try { michael@0: responseHandler(request, this._buffer); michael@0: } catch (ex) { michael@0: do_throw("Error in closure function: " + ex); michael@0: } michael@0: } michael@0: }; michael@0: michael@0: function run_test() michael@0: { michael@0: httpserver = new HttpServer(); michael@0: httpserver.registerPathHandler("/multipart", contentHandler); michael@0: httpserver.start(-1); michael@0: michael@0: var streamConv = Cc["@mozilla.org/streamConverters;1"] michael@0: .getService(Ci.nsIStreamConverterService); michael@0: var conv = streamConv.asyncConvertData("multipart/mixed", michael@0: "*/*", michael@0: multipartListener, michael@0: null); michael@0: michael@0: var chan = make_channel(uri); michael@0: chan.asyncOpen(conv, null); michael@0: do_test_pending(); michael@0: }