HTML and CSS Reference
In-Depth Information
Listing 12.63 Testing that GET requests can send data
"test should send data on URL for GET": function () {
var url = "/url";
var object = { field1: " $ 13", field2: "Lots of data!" };
var expected = url + "?" + tddjs.util.urlParams(object);
ajax.request(url, { data: object, method: "GET" });
assertEquals(expected, this.xhr.open.args[1]);
}
With this test in place we need to modify the data processing. For both GET
and POST requests we need to encode data, but for GET requests the data goes on
the URL, and we must remember to still pass null to the send method.
At this point we have enough requirements to make keeping them all in
our heads a confusing affair. Tests are slowly becoming a fantastic asset; because
we don't need to worry about requirements we have already met, we can code
along without being weighed down by ever-growing amounts of requirements. The
implementation can be seen in Listing 12.64.
Listing 12.64 Adding data to get requests
function setData(options) {
if (options.data) {
options.data = tddjs.util.urlParams(options.data);
if (options.method == "GET") {
options.url += "?" + options.data;
options.data = null;
}
} else {
options.data = null;
}
}
function request(url, options) {
/* ... */
options = tddjs.extend({}, options);
options.url = url;
setData(options);
/* ... */
transport.open(options.method
||
"GET", options.url, true);
 
Search WWH ::




Custom Search