1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/test/unit/test_streamcopier.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,53 @@ 1.4 +var testStr = "This is a test. "; 1.5 +for (var i = 0; i < 10; ++i) { 1.6 + testStr += testStr; 1.7 +} 1.8 + 1.9 +function run_test() { 1.10 + // Set up our stream to copy 1.11 + var inStr = Cc["@mozilla.org/io/string-input-stream;1"] 1.12 + .createInstance(Ci.nsIStringInputStream); 1.13 + inStr.setData(testStr, testStr.length); 1.14 + 1.15 + // Set up our destination stream. Make sure to use segments a good 1.16 + // bit smaller than our data length. 1.17 + do_check_true(testStr.length > 1024*10); 1.18 + var pipe = Cc["@mozilla.org/pipe;1"].createInstance(Ci.nsIPipe); 1.19 + pipe.init(true, true, 1024, 0xffffffff, null); 1.20 + 1.21 + var streamCopier = Cc["@mozilla.org/network/async-stream-copier;1"] 1.22 + .createInstance(Ci.nsIAsyncStreamCopier); 1.23 + streamCopier.init(inStr, pipe.outputStream, null, true, true, 1024, true, true); 1.24 + 1.25 + var ctx = { 1.26 + }; 1.27 + ctx.wrappedJSObject = ctx; 1.28 + 1.29 + var observer = { 1.30 + onStartRequest: function(aRequest, aContext) { 1.31 + do_check_eq(aContext.wrappedJSObject, ctx); 1.32 + }, 1.33 + onStopRequest: function(aRequest, aContext, aStatusCode) { 1.34 + do_check_eq(aStatusCode, 0); 1.35 + do_check_eq(aContext.wrappedJSObject, ctx); 1.36 + var sis = 1.37 + Cc["@mozilla.org/scriptableinputstream;1"] 1.38 + .createInstance(Ci.nsIScriptableInputStream); 1.39 + sis.init(pipe.inputStream); 1.40 + var result = ""; 1.41 + var temp; 1.42 + try { // Need this because read() can throw at EOF 1.43 + while ((temp = sis.read(1024))) { 1.44 + result += temp; 1.45 + } 1.46 + } catch(e) { 1.47 + do_check_eq(e.result, Components.results.NS_BASE_STREAM_CLOSED); 1.48 + } 1.49 + do_check_eq(result, testStr); 1.50 + do_test_finished(); 1.51 + } 1.52 + }; 1.53 + 1.54 + streamCopier.asyncCopy(observer, ctx); 1.55 + do_test_pending(); 1.56 +}