HTML and CSS Reference
In-Depth Information
returned from
waitForMessagesSince
is resolved, the listener added to the
“message” event needs to be cleared. Otherwise, the original call to
waitForMes-
sagesSince
will have its callback called every time a message is added, even after
the current request has ended.
To do this you will need a reference to the function added as a handler,
and use
this.removeListener
. To test it, it will be helpful to know that
room.listeners()
returns the array of listeners, for your inspection pleasure.
With a functional data layer we can get back to finishing the controller. We're going
to give
post
the final polish and implement
get
.
14.6.1 Finishing the
post
Method
The
post
method currently responds with the 201 status code, regardless of
whether the message was added or not, which is in violation with the seman-
tics of a 201 response; the HTTP spec states that “The origin server MUST cre-
ate the resource before returning the 201 status code.” Having implemented the
addMessage
method we know that this is not necessarily the case in our current
implementation. Let's get right on fixing that.
The test that expects
post
to call
writeHead
needs updating. We now expect
the headers to be written once the
addMessage
method resolves. Listing 14.65
shows the updated test.
Listing 14.65
Expecting
post
to respond immediately when
addMessage
resolves
/* ... */
var Promise = require("node-promise/promise").Promise;
/* ... */
function controllerSetUp() {
/* ... */
var promise = this.addMessagePromise = new Promise();
this.controller.chatRoom = { addMessage: stub(promise) };
/* ... */
}
/* ... */
testCase(exports, "chatRoomController.post", {