1
0
Fork 0
mirror of https://github.com/SunRed/haste-server.git synced 2024-11-01 01:30:21 +01:00

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) { DocumentHandler.prototype.chooseKey = function(callback) {
var key = this.keyGenerator.createKey(this.keyLength); var key = this.acceptableKey();
var _this = this; var _this = this;
this.store.get(key, function(ret) { this.store.get(key, function(ret) {
if (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; module.exports = DocumentHandler;

View file

@ -1,7 +1,7 @@
var fs = require('fs'); var fs = require('fs');
var crypto = require('crypto');
var winston = require('winston'); var winston = require('winston');
var hashlib = require('hashlib');
// For storing in files // For storing in files
// options[type] = file // options[type] = file
@ -12,12 +12,19 @@ var FileDocumentStore = function(options) {
this.expire = options.expire; 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 // 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) { FileDocumentStore.prototype.set = function(key, data, callback, skipExpire) {
try { try {
var _this = this; var _this = this;
fs.mkdir(this.basePath, '700', function() { 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) { if (err) {
callback(false); callback(false);
} }
@ -37,7 +44,7 @@ FileDocumentStore.prototype.set = function(key, data, callback, skipExpire) {
// Get data from a file from key // Get data from a file from key
FileDocumentStore.prototype.get = function(key, callback, skipExpire) { FileDocumentStore.prototype.get = function(key, callback, skipExpire) {
var _this = this; 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) { if (err) {
callback(false); callback(false);
} }

View file

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

View file

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

View file

@ -19,13 +19,13 @@
"dependencies": { "dependencies": {
"winston": "*", "winston": "*",
"hashlib": "*",
"connect": "*", "connect": "*",
"uglify-js": "*" "uglify-js": "*"
}, },
"devDependencies": { "devDependencies": {
"jasmine-node": "*" "mocha": "*",
"should": "*"
}, },
"bundledDependencies": [], "bundledDependencies": [],
@ -46,7 +46,7 @@
"scripts": { "scripts": {
"start": "node server.js", "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 DocumentHandler = require('../lib/document_handler');
var Generator = require('../lib/key_generators/random');
describe('document_handler', function() { describe('document_handler', function() {
describe('randomKey', function() { describe('randomKey', function() {
it('should choose a key of the proper length', function() { it('should choose a key of the proper length', function() {
var dh = new DocumentHandler({ keyLength: 6 }); var gen = new Generator();
expect(dh.randomKey().length).toBe(6); var dh = new DocumentHandler({ keyLength: 6, keyGenerator: gen });
dh.acceptableKey().length.should.equal(6);
}); });
it('should choose a default key length', function() { it('should choose a default key length', function() {
var dh = new DocumentHandler(); var gen = new Generator();
expect(dh.keyLength).toBe(DocumentHandler.defaultKeyLength); 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() { 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 }); var store = new RedisDocumentStore({ expire: 10 });
runs(function() { store.set('hello1', 'world', 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;
RedisDocumentStore.client.ttl('hello1', function(err, res) { RedisDocumentStore.client.ttl('hello1', function(err, res) {
expect(res).toBeGreaterThan(1); res.should.be.above(1);
_this.done = true; 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 }); var store = new RedisDocumentStore({ expire: 10 });
runs(function() { store.set('hello2', 'world', 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;
RedisDocumentStore.client.ttl('hello2', function(err, res) { RedisDocumentStore.client.ttl('hello2', function(err, res) {
expect(res).toBe(-1); res.should.equal(-1);
_this.done = true; 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 }); var store = new RedisDocumentStore({ expire: false });
runs(function() { store.set('hello3', 'world', function(worked) {
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) { RedisDocumentStore.client.ttl('hello3', function(err, res) {
expect(res).toBe(-1); res.should.equal(-1);
_this.done = true; done();
}); });
}); });
waitsFor(function() {
return this.done;
});
}); });
}); });