From e8505bffdbe70195e2e37f26c0198c30803ae259 Mon Sep 17 00:00:00 2001 From: Mjumbe Wawatu Ukweli Date: Mon, 16 May 2011 23:11:56 -0700 Subject: [PATCH 1/3] Change the response assertion to kill the server as soon as it enters the response.on('end',...) callback --- bin/expresso | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/bin/expresso b/bin/expresso index a7edd2f..e2ad607 100755 --- a/bin/expresso +++ b/bin/expresso @@ -433,6 +433,25 @@ assert.response = function(server, req, res, msg){ response.on('end', function(){ if (timer) clearTimeout(timer); + // Shut down the server + check(); + + /** + * I feel like this is almost right. Instead, I think all these + * asserts should be wrapped in a try/catch block and that there + * should be an error callback that is passed in as a parameter. + * If an error is caught, it should be passed along to the error + * callback along with the response. Otherwise the normal + * callback should be called. + * + * For now, since I don't want to go through the logic of + * checking for an error callback (and should it come before or + * after the normal one), I'm going to leave this. + */ + + // Callback + callback(response); + // Assert response body if (res.body !== undefined) { var eql = res.body instanceof RegExp @@ -475,10 +494,6 @@ assert.response = function(server, req, res, msg){ ); } } - - // Callback - callback(response); - check(); }); }); request.end(); From e6a9d0fb5e09d99c71202b9aee7d50945d156e19 Mon Sep 17 00:00:00 2001 From: Mjumbe Wawatu Ukweli Date: Wed, 18 May 2011 14:20:38 -0700 Subject: [PATCH 2/3] Move 'response' callback back to the end of the function. It really should be the last thing done. The 'check' at the beginning is still the right thing to do though. --- bin/expresso | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/bin/expresso b/bin/expresso index e2ad607..1f64d09 100755 --- a/bin/expresso +++ b/bin/expresso @@ -436,22 +436,6 @@ assert.response = function(server, req, res, msg){ // Shut down the server check(); - /** - * I feel like this is almost right. Instead, I think all these - * asserts should be wrapped in a try/catch block and that there - * should be an error callback that is passed in as a parameter. - * If an error is caught, it should be passed along to the error - * callback along with the response. Otherwise the normal - * callback should be called. - * - * For now, since I don't want to go through the logic of - * checking for an error callback (and should it come before or - * after the normal one), I'm going to leave this. - */ - - // Callback - callback(response); - // Assert response body if (res.body !== undefined) { var eql = res.body instanceof RegExp @@ -494,6 +478,9 @@ assert.response = function(server, req, res, msg){ ); } } + + // Callback + callback(response); }); }); request.end(); From d70cb31c51c1622768a969f122af46d834cdcef4 Mon Sep 17 00:00:00 2001 From: Mjumbe Wawatu Ukweli Date: Fri, 20 May 2011 04:30:24 -0700 Subject: [PATCH 3/3] Add a test for sending a body into assert.response --- test/http.test.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/http.test.js b/test/http.test.js index 1f9d249..509048f 100644 --- a/test/http.test.js +++ b/test/http.test.js @@ -68,6 +68,26 @@ module.exports = { }) }, + 'test assert.response(req, res, fn) with POSTed body': function(beforeExit){ + var calls = 0; + + assert.response(server, { + url: '/echo', + method: 'POST', + body: 'Hello, Expresso!' + },{ + body: '/echo Hello, Expresso!', + status: 200 + }, function(res){ + ++calls; + assert.ok(res); + }); + + beforeExit(function(){ + assert.equal(1, calls); + }) + }, + 'test assert.response(req, fn)': function(beforeExit){ var calls = 0;