datax
Version:
Wrapper fo Buffer for nodejs.
575 lines (499 loc) • 15.9 kB
JavaScript
var Data = require(__dirname+'/../data');
var Buffer = require('buffer').Buffer;
var http = require('http');
var https = require('https');
var rmdir = require('rmdir');
var tmp = require('tmp');
var util = require('util');
var path = require('path');
var fs = require('fs');
var assert = require('chai').assert;
describe('data.js', () => {
var httpPort = 9000;
var httpsPort = 9001;
var tmpDir = require('tmp').dirSync().name;
before((done) => {
var handle = function (req, res) {
var file = req.url;
var encoding = null;
if (req.url.indexOf('?') > 0) {
file = req.url.substring(0, req.url.indexOf('?'));
encoding = req.url.substring(req.url.indexOf('?') + 1);
}
Data.from(__dirname+'/test-data'+file)
.then((data) => {
if (encoding) {
res.setHeader('content-encoding', encoding);
return data.compress(encoding);
} else {
return data;
}
})
.then((data) => {
res.end(data.buffer);
})
.catch((error) => {
res.writeHeader(404);
res.end();
});
};
var privateKey = fs.readFileSync(__dirname + '/test-data/key/key.pem').toString();
var certificate = fs.readFileSync(__dirname + '/test-data/key/cert.pem').toString();
var httpServer = http.createServer(handle);
var httpsServer = https.createServer({ key: privateKey, cert: certificate }, handle);
httpServer.listen(httpPort, function () {
httpsServer.listen(httpsPort, function() {
done();
})
});
});
after((done) => {
rmdir(tmpDir, () => {
done();
});
});
describe('#constructor', () => {
it('shoul create with empty', () => {
var data = new Data();
assert.equal(data.buffer.length, 0);
});
it('shoul create with string', () => {
var data = new Data('string');
assert.equal(data.toString(), 'string');
});
it('shoul reuse buffer', () => {
var buffer = new Buffer('buffer');
var data = new Data(buffer);
assert.strictEqual(data.buffer, buffer);
});
});
describe('#compress', () => {
it('should compress zlib', (done) => {
(new Data('aaa')).compress('zlib')
.then((data) => {
assert.equal(data.buffer.toString('base64'), 'eJxLTEwEAAJJASQ=');
})
.then(done)
.catch(done);
});
it('should compress gzip', (done) => {
(new Data('aaa')).compress('gzip')
.then((data) => {
assert.equal(data.buffer.toString('base64'), 'H4sIAAAAAAAAA0tMTAQALXMH8AMAAAA=');
})
.then(done)
.catch(done);
});
it('should not compress with unsupported encoding', (done) => {
(new Data('aaa')).compress('gzip2')
.then(() => {
done('Should failed');
})
.catch((error) => {
assert.equal(error.message, 'Unsupported encoding gzip2')
})
.then(done)
.catch(done);
});
});
describe('#decompress', () => {
it('should decompress zlib', (done) => {
(new Data(new Buffer('eJxLTEwEAAJJASQ=', 'base64'))).decompress('zlib')
.then((data) => {
assert.equal(data.toString(), 'aaa');
})
.then(done)
.catch(done);
});
it('should decompress gzip', (done) => {
(new Data(new Buffer('H4sIAAAAAAAAA0tMTAQALXMH8AMAAAA=', 'base64'))).decompress('gzip')
.then((data) => {
assert.equal(data.toString(), 'aaa');
})
.then(done)
.catch(done);
});
it('should not decompress with unsupported encoding', (done) => {
(new Data('aaa')).decompress('gzip2')
.then(() => {
done('Should failed');
})
.catch((error) => {
assert.equal(error.message, 'Unsupported encoding gzip2')
})
.then(done)
.catch(done);
});
it('should not decompress zlib with corrupted', (done) => {
(new Data('aaa')).decompress('zlib')
.then(() => {
done('Should failed');
})
.catch((error) => {
})
.then(done)
.catch(done);
});
it('should not decompress gzip with corrupted', (done) => {
(new Data('aaa')).decompress('gzip')
.then(() => {
done('Should failed');
})
.catch((error) => {
})
.then(done)
.catch(done);
});
});
describe('#crc32', () => {
it('should return correct crc32', () => {
var data = new Data('onetwothree');
assert.equal(data.crc32.toString(16), '9e1c092');
});
});
describe('#md5', () => {
it('should return correct md5', () => {
var data = new Data('onetwothree');
assert.equal(data.md5, '7b0391feb2e0cd271f1cf39aafb4376f');
});
});
describe('#sha256', () => {
it('should return correct sha256', () => {
var data = new Data('onetwothree');
assert.equal(data.sha256, '4592092e1061c7ea85af2aed194621cc17a2762bae33a79bf8ce33fd0168b801');
});
});
describe('#writeToTemporaryFile', () => {
it('should write to temporary directory', (done) => {
Data.from(__filename)
.then((data) => {
assert.isAbove(data.buffer.length, 0, 'Incorrect file size');
return Data.writeToTemporaryFile(data);
})
.then((filename) => {
assert.equal(util.inspect(fs.stat(filename)).size, util.inspect(fs.stat(__filename)).size, 'Path does not exist');
fs.unlinkSync(filename);
})
.then(done)
.catch(done);
}).timeout(5000);
});
describe('#writeToFile', () => {
it('should write to a location', (done) => {
var data = new Data('data');
var filename = tmpDir + '/x/temp.x'
Data.from(__filename)
.then((data) => {
assert.isAbove(data.buffer.length, 0, 'Incorrect file size');
return data.writeToFile(filename);
})
.then(() => {
assert.equal(util.inspect(fs.stat(filename)).size, util.inspect(fs.stat(__filename)).size, 'Path does not exist');
fs.unlinkSync(filename);
})
.then(done)
.catch(done);
}).timeout(5000);
it('should fail to write to a invalid location', (done) => {
var data = new Data('data');
var invalid = '/!@#~^%?'
Data.from(__filename)
.then((data) => {
assert.isAbove(data.buffer.length, 0, 'Incorrect file size');
return data.writeToFile(invalid);
})
.then(() => {
done('should failed');
})
.catch(() => {
})
.then(done)
.catch(done);
}).timeout(5000);
it('should fail to write to a location not granted', (done) => {
var data = new Data('data');
var invalid = '/etc/va/gz'
Data.from(__filename)
.then((data) => {
assert.isAbove(data.buffer.length, 0, 'Incorrect file size');
return data.writeToFile(invalid);
})
.then(() => {
done('should failed');
})
.catch(() => {
})
.then(done)
.catch(done);
}).timeout(5000);
});
describe('#unpack', () => {
it('should get unpack zip', (done) => {
Data.from(__dirname+'/test-data/pack.zip')
.then(Data.unpackZip)
.then((items) => {
assert.equal(items.length, 2, 'Incorrect entries count');
items.forEach((item) => {
assert(item.name, 'Missing name in item');
assert(item.data, 'Missing data in item');
assert.isAbove(item.data.buffer.length, 0, 'Incorrect entry data size');
});
})
.then(done)
.catch(done);
}).timeout(10000);
it('should throw rejection for corrupted zip', (done) => {
Data.from(__dirname+'/test-data/nopack')
.then((data) => {
return data.unpack('zip')
.then(() => {
assert(false, 'Should throw rejection');
})
.catch(() => {
});
})
.then(done)
.catch(done);
}).timeout(5000);
it('should unpack rar', (done) => {
Data.from(__dirname+'/test-data/pack.rar')
.then(Data.unpackRar)
.then((items) => {
assert.isAbove(items.length, 0, 'Incorrect entries count');
items.forEach((item) => {
assert(item.name, 'Missing name in item');
assert(item.data, 'Missing data in item');
assert.isAbove(item.data.buffer.length, 0, 'Incorrect entry data size');
});
})
.then(done)
.catch(done);
}).timeout(5000);
it('should throw rejection for corrupted rar', (done) => {
Data.from(__dirname+'/test-data/nopack')
.then((data) => {
return data.unpack('rar')
.then(() => {
assert(false, 'Should throw rejection');
})
.catch((error) => {
assert.equal(error.message, 'Unable to unpack rar');
});
})
.then(done)
.catch(done);
}).timeout(10000);
it('should throw rejection for unsupported format', (done) => {
Data.from(__dirname+'/test-data/nopack')
.then((data) => {
return data.unpack('rarx')
.then(() => {
assert(false, 'Should throw rejection');
})
.catch((error) => {
assert.equal(error.message, 'Unsupported format rarx');
});
})
.then(done)
.catch(done);
}).timeout(10000);
});
describe('#from', () => {
it('should get data from file system', (done) => {
Data.from(__filename)
.then((data) => {
assert.isAbove(data.crc32, 0);
assert.isAbove(data.buffer.length, 0, 'Incorrect file size');
})
.then(done)
.catch(done);
}).timeout(5000);
it('should get data from http', (done) => {
Data.from('http://localhost:'+httpPort+'/nopack')
.then((data) => {
assert.equal(data.httpStatusCode, 200, 'Invalid status code');
assert.equal(data.buffer.length, 2003, 'Incorrect data size');
})
.then(done)
.catch(done);
}).timeout(5000);
it('should get data from https', (done) => {
Data.from('https://localhost:'+httpsPort+'/nopack', { rejectUnauthorized: false })
.then((data) => {
assert.equal(data.httpStatusCode, 200, 'Invalid status code');
assert.equal(data.buffer.length, 2003, 'Incorrect data size');
})
.then(done)
.catch(done);
}).timeout(5000);
it('should get data from http with gzip encoding', (done) => {
Data.from('http://localhost:'+httpPort+'/nopack?gzip')
.then((data) => {
assert.equal(data.httpStatusCode, 200, 'Invalid status code');
assert.equal(data.buffer.length, 2003, 'Incorrect data size');
})
.then(done)
.catch(done);
}).timeout(5000);
it('should get data from http with deflate encoding', (done) => {
Data.from('http://localhost:'+httpPort+'/nopack?deflate')
.then((data) => {
assert.equal(data.httpStatusCode, 200, 'Invalid status code');
assert.equal(data.buffer.length, 2003, 'Incorrect data size');
})
.then(done)
.catch(done);
}).timeout(5000);
it('should get http status code 200', (done) => {
Data.from('http://localhost:9000/notexist')
.then((data) => {
assert.notEqual(data.httpStatusCode, 200, 'Thet path should not be exist');
})
.then(done)
.catch(done);
}).timeout(5000);
it('should not get from invalid path', (done) => {
Data.from('#5sfsfdsd')
.then(() => {
done('Should failed');
})
.catch((error) => {
})
.then(done)
.catch(done);
}).timeout(5000);
it('should not get data from directory', (done) => {
Data.from(path.dirname(__filename))
.then(() => {
done('Should failed');
})
.catch((error) => {
})
.then(done)
.catch(done);
}).timeout(5000);
it('should not get data from unsupported protocol', (done) => {
Data.from('ftp://localhost/test')
.then(() => {
done('Should failed');
})
.catch((error) => {
assert.equal(error.message, 'Invalid or unsupported URI ftp://localhost/test');
})
.then(done)
.catch(done);
}).timeout(5000);
it('should not get data from invalid domain', (done) => {
Data.from('http://xygg.123/')
.then(() => {
done('Should failed');
})
.catch((error) => {
})
.then(done)
.catch(done);
}).timeout(5000);
});
describe('*performance', () => {
it('5 connections', (done) => {
var count = 5;
var context = { };
var server = http.createServer(function (req, res) {
res.write(req.url.slice(1));
setTimeout(res.end.bind(res), 150 * (count - parseInt(req.url.slice(1))));
});
server.listen(5000, function () {
var ps = [];
for (let i = 0; i < count; i++) {
ps.push(Data.from('http://localhost:5000/' + i));
}
Promise.all(ps).then(() => {
server.close();
})
.catch((error) => {
context.error = error;
server.close();
});
});
server.on('close', () => {
done(context.error);
});
}).timeout(100000);
});
describe('#setCachePolicy', () => {
it('should remove cache policy', () => {
Data.setCachePolicy();
Data.setCachePolicy(null);
});
it('should throw exception with invalid policy', () => {
try {
Data.setCachePolicy('invalid');
assert(false, 'Should fail');
} catch (error) {
assert.equal(error.message, 'Invalid policy');
}
});
it('should throw exception with missing location', () => {
try {
Data.setCachePolicy({});
assert(false, 'Should fail');
} catch (error) {
assert.equal(error.message, 'Missing cache location');
}
});
it('should throw exception with invalid location', () => {
try {
Data.setCachePolicy({ location: 23 });
assert(false, 'Should fail');
} catch (error) {
assert.equal(error.message, 'Invalid cache location');
}
});
it('should throw exception with invalid duration', () => {
try {
Data.setCachePolicy({ location: 'abc', duration: 'xyz' });
assert(false, 'Should fail');
} catch (error) {
assert.equal(error.message, 'Invalid cache duration');
}
});
it('should successfully set policy', () => {
Data.setCachePolicy({ location: tmpDir+'/cache', duration: 5000 });
});
});
describe('#from [cache]', () => {
it('should get data from server with setting cache = false', (done) => {
Data.from('http://localhost:'+httpPort+'/nopack')
.then((data) => {
assert.notProperty(data, 'stats');
assert.equal(data.httpStatusCode, 200, 'Invalid status code');
assert.equal(data.buffer.length, 2003, 'Incorrect data size');
})
.then(done)
.catch(done);
}).timeout(5000);
it('should get data from server by disabling cache while executing', (done) => {
var policy = Data.cachePolicy;
Data.from('http://localhost:'+httpPort+'/nopack')
.then((data) => {
Data.setCachePolicy(policy);
assert.notProperty(data, 'stats');
assert.equal(data.httpStatusCode, 200, 'Invalid status code');
assert.equal(data.buffer.length, 2003, 'Incorrect data size');
})
.then(done)
.catch(done);
Data.setCachePolicy();
}).timeout(5000);
it('should get data from cache', (done) => {
Data.from('http://localhost:'+httpPort+'/nopack')
.then((data) => {
assert.property(data, 'stats');
assert.equal(data.httpStatusCode, 200, 'Invalid status code');
assert.equal(data.buffer.length, 2003, 'Incorrect data size');
})
.then(done)
.catch(done);
}).timeout(5000);
});
});