diff --git a/netwerk/test/unit/test_alt-data_stream.js b/netwerk/test/unit/test_alt-data_stream.js index 55fa003b8d76..8a811c48af45 100644 --- a/netwerk/test/unit/test_alt-data_stream.js +++ b/netwerk/test/unit/test_alt-data_stream.js @@ -20,7 +20,7 @@ XPCOMUtils.defineLazyGetter(this, "URL", function() { var httpServer = null; -function make_channel(url, callback, ctx) { +function make_channel(url) { return NetUtil.newChannel({uri: url, loadUsingSystemPrincipal: true}); } @@ -90,10 +90,10 @@ function openAltChannel() var cc = chan.QueryInterface(Ci.nsICacheInfoChannel); cc.preferAlternativeDataType(altContentType, "", true); - chan.asyncOpen(listener); + chan.asyncOpen(altDataListener); } -var listener = { +var altDataListener = { buffer: "", onStartRequest: function(request, context) { }, onDataAvailable: function(request, context, stream, offset, count) { @@ -114,6 +114,54 @@ var listener = { Assert.equal(cc.alternativeDataType, altContentType); Assert.equal(this.buffer.length, altContent.length); Assert.equal(this.buffer, altContent); - httpServer.stop(do_test_finished); + openAltChannelWithOriginalContent(); }, }; + +function openAltChannelWithOriginalContent() +{ + var chan = make_channel(URL); + var cc = chan.QueryInterface(Ci.nsICacheInfoChannel); + cc.preferAlternativeDataType(altContentType, "", false); + + chan.asyncOpen(originalListener); +} + +var originalListener = { + buffer: "", + onStartRequest: function(request, context) { }, + onDataAvailable: function(request, context, stream, offset, count) { + let string = NetUtil.readInputStreamToString(stream, count); + this.buffer += string; + }, + onStopRequest: function(request, context, status) { + var cc = request.QueryInterface(Ci.nsICacheInfoChannel); + Assert.equal(cc.alternativeDataType, altContentType); + Assert.equal(this.buffer.length, responseContent.length); + Assert.equal(this.buffer, responseContent); + testAltDataStream(cc); + }, +}; + +function testAltDataStream(cc) +{ + cc.getAltDataInputStream(altContentType, { + onInputStreamReady: function(aInputStream) { + executeSoon(() => readAltDataInputStream(aInputStream)); + } + }); +} + +function readAltDataInputStream(aInputStream) +{ + // We expect the async stream length to match the expected content. + // If the test times out, it's probably because of this. + try { + let data = read_stream(aInputStream, altContent.length); + Assert.equal(data, altContent); + httpServer.stop(do_test_finished); + } catch (e) { + equal(e.result, Cr.NS_BASE_STREAM_WOULD_BLOCK); + executeSoon(() => readAltDataInputStream(aInputStream)); + } +}