HTML and CSS Reference
In-Depth Information
/* ... */
transport.send(options.data);
};
Because the data handling might include modifying the URL to embed data
onto it, we added it to the
options
object and passed that to
setData
, as before.
Obviously, the above solution will break down if the URL already has query param-
eters on it. As an exercise, I urge you to test for such a URL and update
setData
as necessary.
12.6.3 Setting Request Headers
The last thing we need to do in order to pass data is setting request headers. Head-
ers can be set using the
setRequestHeader(name, value)
method. At this
point adding in header handling is pretty straightforward, so I will leave doing
that as an exercise. To test this you will need to augment the
fakeXMLHttp-
Request
object to record headers set on it so you can inspect them from your
tests. Listing 12.65 shows an updated version of the object you can use for this
purpose.
Listing 12.65
Adding a fake
setRequestHeader
method
var fakeXMLHttpRequest = {
open: stubFn(),
send: stubFn(),
setRequestHeader: function (header, value) {
if (!this.headers) {
this.headers = {};
}
this.headers[header] = value;
},
readyStateChange: function (readyState) {
this.readyState = readyState;
this.onreadystatechange();
}
};