HTML and CSS Reference
In-Depth Information
Listing 14.67
Expecting
post
not to respond immediately
"should not respond immediately": function (test) {
this.controller.post();
this.sendRequest({ data: {} });
test.ok(!this.res.end.called);
test.done();
}
This
test does not run as smoothly as the previous two. Passing it is a matter
of deferring the closing calls until the promise returned by
addMessage
resolves.
Listing 14.68 has the lowdown.
Listing 14.68
post
responds when
addMessage
resolves
post: function () {
/* ... */
this.request.addListener("end", function () {
var data = JSON.parse(decodeURI(body)).data;
this.chatRoom.addMessage(
data.user, data.message
).then(function () {
this.response.writeHead(201);
this.response.end();
}.bind(this));
}.bind(this));
}
That's about it for the
post
method. Note that the method does not handle
errors in any way; in fact it will respond with a 201 status even if the message was
not added successfully. I'll leave fixing it as an exercise.
14.6.2 Streaming Messages with GET
GET requests should either be immediately responded to with messages, or held
open until messages are available. Luckily, we did most of the heavy lifting while
implementing
chatRoom.waitForMessagesSince
,sothe
get
method of the
controller will simply glue together the request and the data.