HTML and CSS Reference
In-Depth Information
The test passes. Onward to the subsequent requests, which should be coming
in with an access token. Listing 14.71 stubs the access token with an actual value,
and expects this to be passed to
waitForMessagesSince
.
Listing 14.71
Expecting
get
to pass the access token
"should wait for messages since X-Access-Token":
function (test) {
this.req.headers={"x-access-token": "2" };
var chatRoom = this.controller.chatRoom;
chatRoom.waitForMessagesSince = stub();
this.controller.get();
test.ok(chatRoom.waitForMessagesSince.called);
test.equals(chatRoom.waitForMessagesSince.args[0], 2);
test.done();
}
This test looks a lot like the previous one, only it expects the passed id to be
the same as provided with the
X-Access-Token
header. These tests could need
some cleaning up, and I encourage you to give them a spin. Passing the test is simple,
as Listing 14.72 shows.
Listing 14.72
Passing the access token header
get: function () {
var id = this.request.headers["x-access-token"]
||
0;
this.chatRoom.waitForMessagesSince(id);
}
14.6.2.2 The
respond
Method
Along with the response body, which should be a JSON response of some kind, the
get
method should also send status code and possibly some response headers, and
finally close the connection. This sounds awfully similar to what
post
is currently
doing. We'll extract the response into a new method in order to reuse it with the
get
request. Listing 14.73 shows two test cases for it, copied from the
post
test case.
Listing 14.73
Initial tests for
respond
testCase(exports, "chatRoomController.respond", {
setUp: controllerSetUp,
"should write status code": function (test) {
this.controller.respond(201);