Permalink
Browse files

Moving response loading into the base attitude, do the server doesn't…

… need to know about the filesystem.
  • Loading branch information...
1 parent 3fa9a6e commit c3e43db4ddb37b9b68493b3641a1172d083a2b6b @markstory committed Feb 26, 2011
Showing with 19 additions and 18 deletions.
  1. +13 −0 lib/attitude/base.js
  2. +6 −7 lib/server.js
  3. +0 −11 test/server-test.js
View
@@ -2,6 +2,8 @@
The base attitude for jerkbot. Provides some base methods.
*/
+var fs = require('fs');
+
var Base = module.exports = function () {
};
@@ -30,5 +32,16 @@ Base.prototype = {
serverResponse.write(this._response);
serverResponse.end();
return true;
+ },
+
+ loadResponse: function (filename, complete) {
+ var _this = this;
+ fs.readFile(filename, 'binary', function (err, file) {
+ if (err) {
+ throw new Error('Could not read file for responder.');
+ }
+ _this.setResponse(file);
+ complete();
+ });
}
};
View
@@ -51,14 +51,13 @@ Server.prototype.handle = function (request, response) {
var attitude = getAttitude(responder.attitude);
attitude.config(responder);
- // This should probably be a method of the attitudes.
- fs.readFile(path.join(this.config.dirname, responder.response), 'binary', function (err, file) {
- if (err) {
- throw new Error('Could not read file for responder. ' + request.url);
- }
- attitude.setResponse(file);
+ if (responder.response) {
+ attitude.loadResponse(path.join(this.config.dirname, responder.response), function () {
+ attitude.run(response);
+ });
+ } else {
attitude.run(response);
- });
+ }
} catch (err) {
error(response, err.message);
}
View
@@ -97,17 +97,6 @@ vows.describe('jerkbot Server').addBatch({
topic.handle(req, res);
},
- 'fails when response file is missing': function (topic) {
- var res = Object.create(mocks.httpResponse);
- var req = Object.create(mocks.httpRequest, {url : {value: '/missing/response'}});
-
- console.error = function (message) {
- assert.match(message, /Could not read file/);
- }
-
- topic.handle(req, res);
- },
-
'fails when no responder can be found': function (topic) {
var res = Object.create(mocks.httpResponse);
var req = Object.create(mocks.httpRequest, {url : {value: '/no/path'}});

0 comments on commit c3e43db

Please sign in to comment.