Remove hashlib dependency and switch to mocha for testing

This commit is contained in:
John Crepezzi 2012-01-13 11:16:42 -05:00
parent b251978422
commit 6e4c087319
7 changed files with 42 additions and 65 deletions

View file

@ -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;

View file

@ -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);
}

View file

@ -1,4 +1,7 @@
var RandomKeyGenerator = function(options) {
if (!options) {
options = {};
}
this.keyspace = options.keyspace || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
};

View file

@ -1,6 +1,5 @@
var redis = require('redis');
var winston = require('winston');
var hashlib = require('hashlib');
// For storing in redis
// options[type] = redis

View file

@ -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/*"
}
}

View file

@ -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);
});
});

View file

@ -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;
store.set('hello2', 'world', function() {
RedisDocumentStore.client.ttl('hello2', function(err, res) {
res.should.equal(-1);
done();
});
}, true);
});
waitsFor(function() {
return typeof(this.result) === 'boolean';
});
runs(function() {
var _this = this;
RedisDocumentStore.client.ttl('hello2', function(err, res) {
expect(res).toBe(-1);
_this.done = true;
});
});
waitsFor(function() {
return this.done;
});
});
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;
RedisDocumentStore.client.ttl('hello3', function(err, res) {
expect(res).toBe(-1);
_this.done = true;
res.should.equal(-1);
done();
});
});
waitsFor(function() {
return this.done;
});
});
});