HTML and CSS Reference
In-Depth Information
This test simulates a successful request with a JSON response that includes
only the token. After completing the request, the clock is ticked 1,000 milliseconds
ahead to trigger a new request, and for this request we expect the token header to
be sent with the received token. The test fails as expected; the token is still the blank
string.
Note that because we didn't make it possible to configure the polling interval
through the client, we cannot set the polling interval explicitly in the test. This
makes the Clock.tick(1000) something of a magical incantation, as it is not
obvious why it is ticked exactly 1,000 milliseconds ahead. The client should have
a way to set the poller interval, and when it does, this test should be updated for
clarity.
To pass this test we need a reference to the headers object so we can change
it after each request. Listing 13.69 shows the implementation.
Listing 13.69 Updating the request header upon request completion
function connect() {
/* ... */
var headers = {
"Content-Type": "application/json",
"X-Access-Token": ""
};
if (!this.poller) {
this.poller = ajax.poll(this.url, {
success: function (xhr) {
try {
var data = JSON.parse(xhr.responseText);
headers["X-Access-Token"] = data.token;
this.dispatch(data);
} catch (e) {}
}.bind(this),
headers: headers
});
}
}
With this implementation in place the test passes, yet we are not done. If, for
some reason, the server fails to deliver a token in response to a request, we should
not blatantly overwrite the token we already have with a blank one, losing track of
our progress. Also, we do not need to send the token to the dispatch method.
 
Search WWH ::




Custom Search