HTML and CSS Reference
In-Depth Information
test.ok(this.res.writeHead.called);
test.equals(this.res.writeHead.args[0], 201);
test.done();
},
"should close connection": function (test) {
this.controller.respond(201);
test.ok(this.res.end.called);
test.done();
}
});
We can pass these tests by copying the two lines we last added to
post
into
the new
respond
method, as Listing 14.74 shows.
Listing 14.74
A dedicated
respond
method
var chatRoomController = {
/* ... */
respond: function (status) {
this.response.writeHead(status);
this.response.end();
}
};
Now we can simplify the
post
method by calling this method instead. Doing
so also allows us to merge the original tests for status code and connection closing,
by stubbing
respond
and asserting that it was called.
14.6.2.3 Formatting Messages
Next up for the
get
method is properly formatting messages. Again we'll need to
lean on the
cometClient
, which defines the data format. The method should
respond with a JSON object whose properties name the topic and values are arrays
of objects. Additionally, the JSON object should include a
token
property. The
JSON string should be written to the response body.
We can formulate this as a test by stubbing
respond
as we did before, this
time expecting an object passed as the second argument. Thus, we will need to
embellish
respond
later, having it write its second argument to the response body
as a JSON string. Listing 14.75 shows the test.