HTML and CSS Reference
In-Depth Information
12.4 Making Get Requests
We will start working on the request API by describing our ultimate goal: a simple
interface to make requests to the server using a URL, an HTTP verb, and possi-
bly success and failure callbacks. We'll start with the GET request, as shown in
Listing 12.9; save it in test/request _ test.js .
Listing 12.9 Test for tddjs.ajax.get
TestCase("GetRequestTest", {
"test should define get method": function () {
assertFunction(tddjs.ajax.get);
}
});
Taking baby steps, we start by checking for the existence of the get method.
As expected, it fails because the method does not exist. Listing 12.10 defines the
method. Save it in src/request.js
Listing 12.10 Defining tddjs.ajax.get
tddjs.namespace("ajax").get = function () {};
12.4.1 Requiring a URL
The get method needs to accept a URL. In fact, it needs to require a URL.
Listing 12.11 has the scoop.
Listing 12.11 Testing for a required URL
"test should throw error without url": function () {
assertException(function () {
tddjs.ajax.get();
}, "TypeError");
}
Our code does not yet throw any exceptions at all, so we expect this method to
fail because of it. Luckily it does, so we move on to Listing 12.12.
Listing 12.12 Throwing exception if URL is not a string
tddjs.namespace("ajax").get = function (url) {
if (typeof url != "string") {
throw new TypeError("URL should be string");
}
};
 
 
Search WWH ::




Custom Search