0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-16 21:56:25 -05:00

better tests

This commit is contained in:
Alex Kocharin 2013-09-27 06:27:11 +04:00
parent ff4778e3c6
commit 6dc6f31579
6 changed files with 151 additions and 24 deletions

1
test/dos/README.md Normal file
View file

@ -0,0 +1 @@
stuff used for stress-testing, using against real servers is unadvisable

41
test/dos/get-tarball.js Executable file
View file

@ -0,0 +1,41 @@
#!/usr/bin/env node
var async = require('async');
var assert = require('assert');
var Server = require('../lib/server');
var readfile = require('fs').readFileSync;
var binary = readfile('../fixtures/binary');
var count = 10000;
var server = new Server('http://localhost:55550/');
async.series([
function(cb) {
server.auth('test', 'test', function(res, body) {
cb();
});
},
function(cb) {
server.put_package('testpkg', readfile('../fixtures/test-package.json'), function(res, body) {
cb();
});
},
function(cb) {
server.put_tarball('testpkg', 'blahblah', binary, function(res, body) {
cb();
});
},
function dos(cb) {
server.get_tarball('testpkg', 'blahblah', function(res, body) {
assert(res.statusCode === 200);
assert.deepEqual(body, binary.toString('utf8'));
if (count-- > 0) {
dos(cb);
} else {
cb();
}
});
},
], function() {
process.exit();
});

BIN
test/fixtures/binary vendored Normal file

Binary file not shown.

4
test/fixtures/test-package.json vendored Normal file
View file

@ -0,0 +1,4 @@
{
"name": "testpkg",
"version": "0.0.0"
}

View file

@ -7,18 +7,25 @@ function Server(url) {
this.userAgent = 'node/v0.10.8 linux x64';
}
function prep(cb) {
return function(err, res, body) {
if (err) throw err;
cb(res, body);
};
}
Server.prototype.request = function(options, cb) {
options.headers = options.headers || {};
return request({
url: this.url + options.uri,
method: options.method || 'GET',
headers: {
accept: options.headers.accept || 'application/json',
'user-agent': options.headers['user-agent'] || this.userAgent,
'content-type': options.headers['content-type'],
authorization: this.auth,
},
json: options.json
json: options.json || true,
}, cb);
}
@ -30,29 +37,42 @@ Server.prototype.auth = function(user, pass, cb) {
json: {
content: "doesn't matter, 'cause sinopia uses info from Authorization header anywayz",
}
}, function(req, res, body) {
assert.notEqual(body.ok.indexOf('"'+user+'"'), -1);
cb();
});
}, prep(cb));
}
Server.prototype.get_package = function(name, cb) {
request({
this.request({
uri: '/'+name,
method: 'GET',
}, function(req, res, body) {
cb(body);
});
}, prep(cb));
}
Server.prototype.put_package = function(name, data, cb) {
request({
if (typeof(data) === 'object' && !Buffer.isBuffer(data)) data = JSON.stringify(data);
this.request({
uri: '/'+name,
method: 'PUT',
json: data,
}, function(req, res, body) {
cb(body);
});
headers: {
'content-type': 'application/json'
},
}, prep(cb)).end(data);
}
Server.prototype.get_tarball = function(name, filename, cb) {
this.request({
uri: '/'+name+'/-/'+filename,
method: 'GET',
}, prep(cb));
}
Server.prototype.put_tarball = function(name, filename, data, cb) {
this.request({
uri: '/'+name+'/-/'+filename+'/whatever',
method: 'PUT',
headers: {
'content-type': 'application/octet-stream'
},
}, prep(cb)).end(data);
}
module.exports = Server;

View file

@ -1,14 +1,75 @@
#!/usr/bin/env node
var async = require('async');
var assert = require('assert');
var rimraf = require('rimraf');
var Server = require('./lib/server');
var readfile = require('fs').readFileSync;
process.argv = ['node', 'sinopia', '-c', './config.yaml'];
require('../bin/sinopia');
setTimeout(function() {
var server = new Server('http://localhost:55550/');
server.auth('test', 'test', function() {
console.log('ok');
});
}, 1000);
var server = new Server('http://localhost:55550/');
async.series([
function(cb) {
rimraf('./test-storage', cb);
},
function(cb) {
process.argv = ['node', 'sinopia', '-c', './config.yaml'];
require('../bin/sinopia');
cb();
},
function(cb) {
setTimeout(cb, 1000);
},
function(cb) {
server.auth('test', 'test', function(res, body) {
assert(res.statusCode === 201);
assert.notEqual(body.ok.indexOf('"test"'), -1);
cb();
});
},
function(cb) {
server.get_package('testpkg', function(res, body) {
// shouldn't exist yet
assert(res.statusCode === 404);
assert(~body.error.indexOf('no such package'));
cb();
});
},
function(cb) {
server.put_package('testpkg', readfile('fixtures/test-package.json'), function(res, body) {
assert(res.statusCode === 201);
assert(~body.ok.indexOf('created new package'));
cb();
});
},
function(cb) {
server.get_package('testpkg', function(res, body) {
assert(res.statusCode === 200);
assert(body.name === 'testpkg');
cb();
});
},
function(cb) {
server.get_tarball('testpkg', 'blahblah', function(res, body) {
assert(res.statusCode === 404);
assert(~body.error.indexOf('no such file'));
cb();
});
},
function(cb) {
server.put_tarball('testpkg', 'blahblah', readfile('fixtures/binary'), function(res, body) {
assert(res.statusCode === 201);
assert(body.ok);
cb();
});
},
function(cb) {
server.get_tarball('testpkg', 'blahblah', function(res, body) {
assert(res.statusCode === 200);
assert.deepEqual(body, readfile('fixtures/binary').toString('utf8'));
cb();
});
},
], function() {
process.exit();
});