HTML and CSS Reference
In-Depth Information Filtering Messages with Access Tokens
Remember how the cometClient from Chapter 13, Streaming Data with Ajax
and Comet, informs the server of what data to retrieve? We set it up to use the
X-Access-Token header, which can contain any value and is controlled by the
server. Because we built waitForMessagesSince to use ids, it should not come
as a surprise that we are going to track progress using them.
When a client connects for the first time, it's going to send an empty
X-Access-Token , so handling that case seems like a good start. Listing 14.69
shows the test for the initial attempt. We expect the controller to simply return all
available messages on first attempt, meaning that empty access token should imply
waiting for messages since 0.
Listing 14.69 Expecting the client to grab all messages
testCase(exports, "chatRoomController.get", {
setUp: controllerSetUp,
tearDown: controllerTearDown,
"should wait for any message": function (test) {
this.req.headers = { "x-access-token": "" };
var chatRoom = this.controller.chatRoom;
chatRoom.waitForMessagesSince = stub();
test.equals(chatRoom.waitForMessagesSince.args[0], 0);
Notice that Node downcases the headers. Failing to recognize this may take
away some precious minutes from your life. Or so I've heard. To pass this test we
can cheat by passing the expected id directly to the method, as Listing 14.70 does.
Listing 14.70 Cheating to pass tests
var chatRoomController = {
/* ... */
get: function () {
Search WWH ::

Custom Search