HTML and CSS Reference
In-Depth Information
12.6.1.4 Introducing ajax.post
With ajax.request in place, implementing POST requests should be a breeze.
Feeling brave, we skip the simple test to prove the method's existence this time
around. Instead, the test in Listing 12.55 shows how we expect the method to
behave.
Listing 12.55 Expecting ajax.post to delegate to ajax.request
TestCase("PostRequestTest", {
setUp: function () {
this.ajaxRequest = ajax.request;
},
tearDown: function () {
ajax.request = this.ajaxRequest;
},
"test should call request with POST method": function () {
ajax.request = stubFn();
ajax.post("/url");
assertEquals("POST", ajax.request.args[1].method);
}
});
Implementation is trivial, as seen in Listing 12.56.
Listing 12.56 Delegating ajax.post to ajax.request with POST as method
function post(url, options) {
options = tddjs.extend({}, options);
options.method = "POST";
ajax.request(url, options);
}
ajax.post = post;
Running the tests confirms that this implementation solves the newly added
requirement. As always, we look for duplication before moving on. Obviously, the
get and post methods are very similar. We could extract a helper method, but
saving only two lines in two methods at the expense of another function call and
another level of indirection doesn't seem worthwhile at this point. You may feel
differently.
 
Search WWH ::




Custom Search