Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | * This file is a modified version of netwerk/test/unit/head_channels.js |
michael@0 | 3 | * The changes consist of making it work in mochitest files and removing |
michael@0 | 4 | * unused code. |
michael@0 | 5 | */ |
michael@0 | 6 | |
michael@0 | 7 | /** |
michael@0 | 8 | * Read count bytes from stream and return as a String object |
michael@0 | 9 | */ |
michael@0 | 10 | function read_stream(stream, count) { |
michael@0 | 11 | /* assume stream has non-ASCII data */ |
michael@0 | 12 | var wrapper = |
michael@0 | 13 | Components.classes["@mozilla.org/binaryinputstream;1"] |
michael@0 | 14 | .createInstance(Components.interfaces.nsIBinaryInputStream); |
michael@0 | 15 | wrapper.setInputStream(stream); |
michael@0 | 16 | /* JS methods can be called with a maximum of 65535 arguments, and input |
michael@0 | 17 | streams don't have to return all the data they make .available() when |
michael@0 | 18 | asked to .read() that number of bytes. */ |
michael@0 | 19 | var data = []; |
michael@0 | 20 | while (count > 0) { |
michael@0 | 21 | var bytes = wrapper.readByteArray(Math.min(65535, count)); |
michael@0 | 22 | data.push(String.fromCharCode.apply(null, bytes)); |
michael@0 | 23 | count -= bytes.length; |
michael@0 | 24 | if (bytes.length == 0) |
michael@0 | 25 | throw("Nothing read from input stream!"); |
michael@0 | 26 | } |
michael@0 | 27 | return data.join(''); |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | const CL_EXPECT_FAILURE = 0x1; |
michael@0 | 31 | const CL_EXPECT_GZIP = 0x2; |
michael@0 | 32 | const CL_EXPECT_3S_DELAY = 0x4; |
michael@0 | 33 | const CL_SUSPEND = 0x8; |
michael@0 | 34 | const CL_ALLOW_UNKNOWN_CL = 0x10; |
michael@0 | 35 | const CL_EXPECT_LATE_FAILURE = 0x20; |
michael@0 | 36 | |
michael@0 | 37 | const SUSPEND_DELAY = 3000; |
michael@0 | 38 | |
michael@0 | 39 | /** |
michael@0 | 40 | * A stream listener that calls a callback function with a specified |
michael@0 | 41 | * context and the received data when the channel is loaded. |
michael@0 | 42 | * |
michael@0 | 43 | * Signature of the closure: |
michael@0 | 44 | * void closure(in nsIRequest request, in ACString data, in JSObject context); |
michael@0 | 45 | * |
michael@0 | 46 | * This listener makes sure that various parts of the channel API are |
michael@0 | 47 | * implemented correctly and that the channel's status is a success code |
michael@0 | 48 | * (you can pass CL_EXPECT_FAILURE or CL_EXPECT_LATE_FAILURE as flags |
michael@0 | 49 | * to allow a failure code) |
michael@0 | 50 | * |
michael@0 | 51 | * Note that it also requires a valid content length on the channel and |
michael@0 | 52 | * is thus not fully generic. |
michael@0 | 53 | */ |
michael@0 | 54 | function ChannelListener(closure, ctx, flags) { |
michael@0 | 55 | this._closure = closure; |
michael@0 | 56 | this._closurectx = ctx; |
michael@0 | 57 | this._flags = flags; |
michael@0 | 58 | } |
michael@0 | 59 | ChannelListener.prototype = { |
michael@0 | 60 | _closure: null, |
michael@0 | 61 | _closurectx: null, |
michael@0 | 62 | _buffer: "", |
michael@0 | 63 | _got_onstartrequest: false, |
michael@0 | 64 | _got_onstoprequest: false, |
michael@0 | 65 | _contentLen: -1, |
michael@0 | 66 | _lastEvent: 0, |
michael@0 | 67 | |
michael@0 | 68 | QueryInterface: function(iid) { |
michael@0 | 69 | if (iid.equals(Components.interfaces.nsIStreamListener) || |
michael@0 | 70 | iid.equals(Components.interfaces.nsIRequestObserver) || |
michael@0 | 71 | iid.equals(Components.interfaces.nsISupports)) |
michael@0 | 72 | return this; |
michael@0 | 73 | throw Components.results.NS_ERROR_NO_INTERFACE; |
michael@0 | 74 | }, |
michael@0 | 75 | |
michael@0 | 76 | onStartRequest: function(request, context) { |
michael@0 | 77 | try { |
michael@0 | 78 | if (this._got_onstartrequest) |
michael@0 | 79 | throw("Got second onStartRequest event!"); |
michael@0 | 80 | this._got_onstartrequest = true; |
michael@0 | 81 | this._lastEvent = Date.now(); |
michael@0 | 82 | |
michael@0 | 83 | request.QueryInterface(Components.interfaces.nsIChannel); |
michael@0 | 84 | try { |
michael@0 | 85 | this._contentLen = request.contentLength; |
michael@0 | 86 | } |
michael@0 | 87 | catch (ex) { |
michael@0 | 88 | if (!(this._flags & (CL_EXPECT_FAILURE | CL_ALLOW_UNKNOWN_CL))) |
michael@0 | 89 | throw("Could not get contentLength"); |
michael@0 | 90 | } |
michael@0 | 91 | if (this._contentLen == -1 && !(this._flags & (CL_EXPECT_FAILURE | CL_ALLOW_UNKNOWN_CL))) |
michael@0 | 92 | throw("Content length is unknown in onStartRequest!"); |
michael@0 | 93 | |
michael@0 | 94 | if (this._flags & CL_SUSPEND) { |
michael@0 | 95 | request.suspend(); |
michael@0 | 96 | do_timeout(SUSPEND_DELAY, function() { request.resume(); }); |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | } catch (ex) { |
michael@0 | 100 | throw("Error in onStartRequest: " + ex); |
michael@0 | 101 | } |
michael@0 | 102 | }, |
michael@0 | 103 | |
michael@0 | 104 | onDataAvailable: function(request, context, stream, offset, count) { |
michael@0 | 105 | try { |
michael@0 | 106 | var current = Date.now(); |
michael@0 | 107 | |
michael@0 | 108 | if (!this._got_onstartrequest) |
michael@0 | 109 | throw("onDataAvailable without onStartRequest event!"); |
michael@0 | 110 | if (this._got_onstoprequest) |
michael@0 | 111 | throw("onDataAvailable after onStopRequest event!"); |
michael@0 | 112 | if (!request.isPending()) |
michael@0 | 113 | throw("request reports itself as not pending from onDataAvailable!"); |
michael@0 | 114 | if (this._flags & CL_EXPECT_FAILURE) |
michael@0 | 115 | throw("Got data despite expecting a failure"); |
michael@0 | 116 | |
michael@0 | 117 | if (current - this._lastEvent >= SUSPEND_DELAY && |
michael@0 | 118 | !(this._flags & CL_EXPECT_3S_DELAY)) |
michael@0 | 119 | throw("Data received after significant unexpected delay"); |
michael@0 | 120 | else if (current - this._lastEvent < SUSPEND_DELAY && |
michael@0 | 121 | this._flags & CL_EXPECT_3S_DELAY) |
michael@0 | 122 | throw("Data received sooner than expected"); |
michael@0 | 123 | else if (current - this._lastEvent >= SUSPEND_DELAY && |
michael@0 | 124 | this._flags & CL_EXPECT_3S_DELAY) |
michael@0 | 125 | this._flags &= ~CL_EXPECT_3S_DELAY; // No more delays expected |
michael@0 | 126 | |
michael@0 | 127 | this._buffer = this._buffer.concat(read_stream(stream, count)); |
michael@0 | 128 | this._lastEvent = current; |
michael@0 | 129 | } catch (ex) { |
michael@0 | 130 | throw("Error in onDataAvailable: " + ex); |
michael@0 | 131 | } |
michael@0 | 132 | }, |
michael@0 | 133 | |
michael@0 | 134 | onStopRequest: function(request, context, status) { |
michael@0 | 135 | try { |
michael@0 | 136 | var success = Components.isSuccessCode(status); |
michael@0 | 137 | if (!this._got_onstartrequest) |
michael@0 | 138 | throw("onStopRequest without onStartRequest event!"); |
michael@0 | 139 | if (this._got_onstoprequest) |
michael@0 | 140 | throw("Got second onStopRequest event!"); |
michael@0 | 141 | this._got_onstoprequest = true; |
michael@0 | 142 | if ((this._flags & (CL_EXPECT_FAILURE | CL_EXPECT_LATE_FAILURE)) && success) |
michael@0 | 143 | throw("Should have failed to load URL (status is " + status.toString(16) + ")"); |
michael@0 | 144 | else if (!(this._flags & (CL_EXPECT_FAILURE | CL_EXPECT_LATE_FAILURE)) && !success) |
michael@0 | 145 | throw("Failed to load URL: " + status.toString(16)); |
michael@0 | 146 | if (status != request.status) |
michael@0 | 147 | throw("request.status does not match status arg to onStopRequest!"); |
michael@0 | 148 | if (request.isPending()) |
michael@0 | 149 | throw("request reports itself as pending from onStopRequest!"); |
michael@0 | 150 | if (!(this._flags & (CL_EXPECT_FAILURE | CL_EXPECT_LATE_FAILURE)) && |
michael@0 | 151 | !(this._flags & CL_EXPECT_GZIP) && |
michael@0 | 152 | this._contentLen != -1) |
michael@0 | 153 | is(this._buffer.length, this._contentLen); |
michael@0 | 154 | } catch (ex) { |
michael@0 | 155 | throw("Error in onStopRequest: " + ex); |
michael@0 | 156 | } |
michael@0 | 157 | try { |
michael@0 | 158 | this._closure(request, this._buffer, this._closurectx); |
michael@0 | 159 | } catch (ex) { |
michael@0 | 160 | throw("Error in closure function: " + ex); |
michael@0 | 161 | } |
michael@0 | 162 | } |
michael@0 | 163 | }; |
michael@0 | 164 | |
michael@0 | 165 | /** |
michael@0 | 166 | * Class that implements nsILoadContext. Use it as callbacks for channel when |
michael@0 | 167 | * test needs it. |
michael@0 | 168 | */ |
michael@0 | 169 | function LoadContextCallback(appId, inBrowserElement, isPrivate, isContent) { |
michael@0 | 170 | this.appId = appId; |
michael@0 | 171 | this.isInBrowserElement = inBrowserElement; |
michael@0 | 172 | this.usePrivateBrowsing = isPrivate; |
michael@0 | 173 | this.isContent = isContent; |
michael@0 | 174 | } |
michael@0 | 175 | |
michael@0 | 176 | LoadContextCallback.prototype = { |
michael@0 | 177 | associatedWindow: null, |
michael@0 | 178 | topWindow : null, |
michael@0 | 179 | isAppOfType: function(appType) { |
michael@0 | 180 | throw Components.results.NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 181 | }, |
michael@0 | 182 | QueryInterface: function(iid) { |
michael@0 | 183 | if (iid == Ci.nsILoadContext || |
michael@0 | 184 | Ci.nsIInterfaceRequestor || |
michael@0 | 185 | Ci.nsISupports) { |
michael@0 | 186 | return this; |
michael@0 | 187 | } |
michael@0 | 188 | throw Components.results.NS_ERROR_NO_INTERFACE; |
michael@0 | 189 | }, |
michael@0 | 190 | getInterface: function(iid) { |
michael@0 | 191 | if (iid.equals(Ci.nsILoadContext)) |
michael@0 | 192 | return this; |
michael@0 | 193 | throw Components.results.NS_ERROR_NO_INTERFACE; |
michael@0 | 194 | }, |
michael@0 | 195 | } |