1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/test/unit/test_multipart_streamconv.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,96 @@ 1.4 +Cu.import("resource://testing-common/httpd.js"); 1.5 + 1.6 +var httpserver = null; 1.7 + 1.8 +XPCOMUtils.defineLazyGetter(this, "uri", function() { 1.9 + return "http://localhost:" + httpserver.identity.primaryPort + "/multipart"; 1.10 +}); 1.11 + 1.12 +function make_channel(url) { 1.13 + var ios = Cc["@mozilla.org/network/io-service;1"]. 1.14 + getService(Ci.nsIIOService); 1.15 + return ios.newChannel(url, "", null); 1.16 +} 1.17 + 1.18 +var multipartBody = "--boundary\r\n\r\nSome text\r\n--boundary\r\n\r\n<?xml version='1.0'?><root/>\r\n--boundary--"; 1.19 + 1.20 +function make_channel(url) { 1.21 + var ios = Cc["@mozilla.org/network/io-service;1"]. 1.22 + getService(Ci.nsIIOService); 1.23 + return ios.newChannel(url, "", null); 1.24 +} 1.25 + 1.26 +function contentHandler(metadata, response) 1.27 +{ 1.28 + response.setHeader("Content-Type", 'multipart/mixed; boundary="boundary"'); 1.29 + response.bodyOutputStream.write(multipartBody, multipartBody.length); 1.30 +} 1.31 + 1.32 +var numTests = 2; 1.33 +var testNum = 0; 1.34 + 1.35 +var testData = 1.36 + [ 1.37 + { data: "Some text", type: "text/plain" }, 1.38 + { data: "<?xml version='1.0'?><root/>", type: "text/xml" } 1.39 + ]; 1.40 + 1.41 +function responseHandler(request, buffer) 1.42 +{ 1.43 + do_check_eq(buffer, testData[testNum].data); 1.44 + do_check_eq(request.QueryInterface(Ci.nsIChannel).contentType, 1.45 + testData[testNum].type); 1.46 + if (++testNum == numTests) 1.47 + httpserver.stop(do_test_finished); 1.48 +} 1.49 + 1.50 +var multipartListener = { 1.51 + _buffer: "", 1.52 + 1.53 + QueryInterface: function(iid) { 1.54 + if (iid.equals(Components.interfaces.nsIStreamListener) || 1.55 + iid.equals(Components.interfaces.nsIRequestObserver) || 1.56 + iid.equals(Components.interfaces.nsISupports)) 1.57 + return this; 1.58 + throw Components.results.NS_ERROR_NO_INTERFACE; 1.59 + }, 1.60 + 1.61 + onStartRequest: function(request, context) { 1.62 + this._buffer = ""; 1.63 + }, 1.64 + 1.65 + onDataAvailable: function(request, context, stream, offset, count) { 1.66 + try { 1.67 + this._buffer = this._buffer.concat(read_stream(stream, count)); 1.68 + dump("BUFFEEE: " + this._buffer + "\n\n"); 1.69 + } catch (ex) { 1.70 + do_throw("Error in onDataAvailable: " + ex); 1.71 + } 1.72 + }, 1.73 + 1.74 + onStopRequest: function(request, context, status) { 1.75 + try { 1.76 + responseHandler(request, this._buffer); 1.77 + } catch (ex) { 1.78 + do_throw("Error in closure function: " + ex); 1.79 + } 1.80 + } 1.81 +}; 1.82 + 1.83 +function run_test() 1.84 +{ 1.85 + httpserver = new HttpServer(); 1.86 + httpserver.registerPathHandler("/multipart", contentHandler); 1.87 + httpserver.start(-1); 1.88 + 1.89 + var streamConv = Cc["@mozilla.org/streamConverters;1"] 1.90 + .getService(Ci.nsIStreamConverterService); 1.91 + var conv = streamConv.asyncConvertData("multipart/mixed", 1.92 + "*/*", 1.93 + multipartListener, 1.94 + null); 1.95 + 1.96 + var chan = make_channel(uri); 1.97 + chan.asyncOpen(conv, null); 1.98 + do_test_pending(); 1.99 +}