From 6e4c08731907fe2468f36f6c4483b81fadff3c57 Mon Sep 17 00:00:00 2001 From: John Crepezzi Date: Fri, 13 Jan 2012 11:16:42 -0500 Subject: [PATCH] Remove hashlib dependency and switch to mocha for testing --- lib/document_handler.js | 8 +++- lib/file_document_store.js | 13 +++++-- lib/key_generators/random.js | 3 ++ lib/redis_document_store.js | 1 - package.json | 6 +-- spec/document_handler_spec.js | 11 ++++-- spec/redis_document_store_spec.js | 65 +++++++------------------------ 7 files changed, 42 insertions(+), 65 deletions(-) diff --git a/lib/document_handler.js b/lib/document_handler.js index c8aec71..07291e7 100644 --- a/lib/document_handler.js +++ b/lib/document_handler.js @@ -86,9 +86,9 @@ DocumentHandler.prototype.handlePost = function(request, response) { }); }; -// Get a random key that hasn't been already used +// Keep choosing keys until one isn't taken DocumentHandler.prototype.chooseKey = function(callback) { - var key = this.keyGenerator.createKey(this.keyLength); + var key = this.acceptableKey(); var _this = this; this.store.get(key, function(ret) { if (ret) { @@ -99,4 +99,8 @@ DocumentHandler.prototype.chooseKey = function(callback) { }); }; +DocumentHandler.prototype.acceptableKey = function() { + return this.keyGenerator.createKey(this.keyLength); +}; + module.exports = DocumentHandler; diff --git a/lib/file_document_store.js b/lib/file_document_store.js index 76492f8..a0666fd 100644 --- a/lib/file_document_store.js +++ b/lib/file_document_store.js @@ -1,7 +1,7 @@ var fs = require('fs'); +var crypto = require('crypto'); var winston = require('winston'); -var hashlib = require('hashlib'); // For storing in files // options[type] = file @@ -12,12 +12,19 @@ var FileDocumentStore = function(options) { this.expire = options.expire; }; +// Generate md5 of a string +FileDocumentStore.md5 = function(str) { + var md5sum = crypto.createHash('md5'); + md5sum.update(str); + return md5sum.digest('hex'); +}; + // Save data in a file, key as md5 - since we don't know what we could be passed here FileDocumentStore.prototype.set = function(key, data, callback, skipExpire) { try { var _this = this; fs.mkdir(this.basePath, '700', function() { - fs.writeFile(_this.basePath + '/' + hashlib.md5(key), data, 'utf8', function(err) { + fs.writeFile(_this.basePath + '/' + _this.md5(key), data, 'utf8', function(err) { if (err) { callback(false); } @@ -37,7 +44,7 @@ FileDocumentStore.prototype.set = function(key, data, callback, skipExpire) { // Get data from a file from key FileDocumentStore.prototype.get = function(key, callback, skipExpire) { var _this = this; - fs.readFile(this.basePath + '/' + hashlib.md5(key), 'utf8', function(err, data) { + fs.readFile(this.basePath + '/' + _this..md5(key), 'utf8', function(err, data) { if (err) { callback(false); } diff --git a/lib/key_generators/random.js b/lib/key_generators/random.js index 4b48071..517fb7c 100644 --- a/lib/key_generators/random.js +++ b/lib/key_generators/random.js @@ -1,4 +1,7 @@ var RandomKeyGenerator = function(options) { + if (!options) { + options = {}; + } this.keyspace = options.keyspace || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; }; diff --git a/lib/redis_document_store.js b/lib/redis_document_store.js index fc44f07..346436f 100644 --- a/lib/redis_document_store.js +++ b/lib/redis_document_store.js @@ -1,6 +1,5 @@ var redis = require('redis'); var winston = require('winston'); -var hashlib = require('hashlib'); // For storing in redis // options[type] = redis diff --git a/package.json b/package.json index 9c69eaa..7c2187e 100644 --- a/package.json +++ b/package.json @@ -19,13 +19,13 @@ "dependencies": { "winston": "*", - "hashlib": "*", "connect": "*", "uglify-js": "*" }, "devDependencies": { - "jasmine-node": "*" + "mocha": "*", + "should": "*" }, "bundledDependencies": [], @@ -46,7 +46,7 @@ "scripts": { "start": "node server.js", - "test": "jasmine-node spec" + "test": "mocha -r should spec/*" } } diff --git a/spec/document_handler_spec.js b/spec/document_handler_spec.js index 2e1fab6..e31eae8 100644 --- a/spec/document_handler_spec.js +++ b/spec/document_handler_spec.js @@ -1,17 +1,20 @@ var DocumentHandler = require('../lib/document_handler'); +var Generator = require('../lib/key_generators/random'); describe('document_handler', function() { describe('randomKey', function() { it('should choose a key of the proper length', function() { - var dh = new DocumentHandler({ keyLength: 6 }); - expect(dh.randomKey().length).toBe(6); + var gen = new Generator(); + var dh = new DocumentHandler({ keyLength: 6, keyGenerator: gen }); + dh.acceptableKey().length.should.equal(6); }); it('should choose a default key length', function() { - var dh = new DocumentHandler(); - expect(dh.keyLength).toBe(DocumentHandler.defaultKeyLength); + var gen = new Generator(); + var dh = new DocumentHandler({ keyGenerator: gen }); + dh.keyLength.should.equal(DocumentHandler.defaultKeyLength); }); }); diff --git a/spec/redis_document_store_spec.js b/spec/redis_document_store_spec.js index da94712..1b06d6b 100644 --- a/spec/redis_document_store_spec.js +++ b/spec/redis_document_store_spec.js @@ -15,73 +15,34 @@ describe('redis_document_store', function() { describe('set', function() { - it('should be able to set a key and have an expiration set', function() { + it('should be able to set a key and have an expiration set', function(done) { var store = new RedisDocumentStore({ expire: 10 }); - runs(function() { - var _this = this; - store.set('hello1', 'world', function(worked) { - _this.result = worked; - }); - }); - waitsFor(function() { - return typeof(this.result) === 'boolean'; - }); - runs(function() { - var _this = this; + store.set('hello1', 'world', function() { RedisDocumentStore.client.ttl('hello1', function(err, res) { - expect(res).toBeGreaterThan(1); - _this.done = true; + res.should.be.above(1); + done(); }); }); - waitsFor(function() { - return this.done; - }); }); - it('should not set an expiration when told not to', function() { + it('should not set an expiration when told not to', function(done) { var store = new RedisDocumentStore({ expire: 10 }); - runs(function() { - var _this = this; - store.set('hello2', 'world', function(worked) { - _this.result = worked; - }, true); - }); - waitsFor(function() { - return typeof(this.result) === 'boolean'; - }); - runs(function() { - var _this = this; + store.set('hello2', 'world', function() { RedisDocumentStore.client.ttl('hello2', function(err, res) { - expect(res).toBe(-1); - _this.done = true; + res.should.equal(-1); + done(); }); - }); - waitsFor(function() { - return this.done; - }); + }, true); }); - it('should not set an expiration when expiration is off', function() { + it('should not set an expiration when expiration is off', function(done) { var store = new RedisDocumentStore({ expire: false }); - runs(function() { - var _this = this; - store.set('hello3', 'world', function(worked) { - _this.result = worked; - }); - }); - waitsFor(function() { - return typeof(this.result) === 'boolean'; - }); - runs(function() { - var _this = this; + store.set('hello3', 'world', function(worked) { RedisDocumentStore.client.ttl('hello3', function(err, res) { - expect(res).toBe(-1); - _this.done = true; + res.should.equal(-1); + done(); }); }); - waitsFor(function() { - return this.done; - }); }); });