0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-23 22:27:34 -05:00
verdaccio/test/functional/lib/server.js

170 lines
4.2 KiB
JavaScript
Raw Normal View History

2017-04-19 14:15:28 -05:00
'use strict';
const assert = require('assert');
const request = require('./smart_request');
2013-09-25 05:01:55 -05:00
function Server(url) {
2017-04-19 14:15:28 -05:00
let self = Object.create(Server.prototype);
self.url = url.replace(/\/$/, '');
self.userAgent = 'node/v0.10.8 linux x64';
self.authstr = 'Basic '+(new Buffer('test:test')).toString('base64');
return self;
2013-09-25 05:01:55 -05:00
}
Server.prototype.request = function(options) {
2017-04-19 14:15:28 -05:00
assert(options.uri);
let headers = options.headers || {};
headers.accept = headers.accept || 'application/json';
headers['user-agent'] = headers['user-agent'] || this.userAgent;
headers.authorization = headers.authorization || this.authstr;
return request({
url: this.url + options.uri,
method: options.method || 'GET',
headers: headers,
encoding: options.encoding,
json: options.json != null ? options.json : true,
2017-04-19 14:15:28 -05:00
});
};
2013-09-25 05:01:55 -05:00
Server.prototype.auth = function(user, pass) {
2017-04-19 14:15:28 -05:00
this.authstr = 'Basic '+(new Buffer(user+':'+pass)).toString('base64');
return this.request({
uri: '/-/user/org.couchdb.user:'+encodeURIComponent(user)+'/-rev/undefined',
method: 'PUT',
json: {
name: user,
password: pass,
email: 'test@example.com',
_id: 'org.couchdb.user:' + user,
type: 'user',
roles: [],
date: new Date(),
2017-04-19 14:15:28 -05:00
},
});
};
2013-09-25 05:01:55 -05:00
2016-08-10 05:14:08 -05:00
Server.prototype.logout = function(token) {
return this.request({
uri: '/-/user/token/'+encodeURIComponent(token),
2017-04-19 14:15:28 -05:00
method: 'DELETE',
});
};
2016-08-10 05:14:08 -05:00
Server.prototype.get_package = function(name) {
return this.request({
2014-12-14 15:29:56 -05:00
uri: '/'+encodeURIComponent(name),
method: 'GET',
2017-04-19 14:15:28 -05:00
});
};
2013-09-25 05:01:55 -05:00
Server.prototype.put_package = function(name, data) {
2017-04-19 14:15:28 -05:00
if (typeof(data) === 'object' && !Buffer.isBuffer(data)) data = JSON.stringify(data);
return this.request({
uri: '/'+encodeURIComponent(name),
method: 'PUT',
headers: {
2017-04-19 14:15:28 -05:00
'content-type': 'application/json',
},
2017-04-19 14:15:28 -05:00
}).send(data);
};
2013-09-27 03:56:44 -05:00
Server.prototype.put_version = function(name, version, data) {
2017-04-19 14:15:28 -05:00
if (typeof(data) === 'object' && !Buffer.isBuffer(data)) data = JSON.stringify(data);
return this.request({
uri: '/'+encodeURIComponent(name)+'/'+encodeURIComponent(version)+'/-tag/latest',
method: 'PUT',
headers: {
2017-04-19 14:15:28 -05:00
'content-type': 'application/json',
},
2017-04-19 14:15:28 -05:00
}).send(data);
};
2013-09-26 21:27:11 -05:00
Server.prototype.get_tarball = function(name, filename) {
return this.request({
uri: '/'+encodeURIComponent(name)+'/-/'+encodeURIComponent(filename),
method: 'GET',
2017-04-19 14:15:28 -05:00
encoding: null,
});
};
2013-09-26 21:27:11 -05:00
Server.prototype.put_tarball = function(name, filename, data) {
return this.request({
uri: '/'+encodeURIComponent(name)+'/-/'+encodeURIComponent(filename)+'/whatever',
method: 'PUT',
headers: {
2017-04-19 14:15:28 -05:00
'content-type': 'application/octet-stream',
},
2017-04-19 14:15:28 -05:00
}).send(data);
};
2013-09-25 05:01:55 -05:00
Server.prototype.add_tag = function(name, tag, version) {
return this.request({
uri: '/'+encodeURIComponent(name)+'/'+encodeURIComponent(tag),
method: 'PUT',
headers: {
2017-04-19 14:15:28 -05:00
'content-type': 'application/json',
},
2017-04-19 14:15:28 -05:00
}).send(JSON.stringify(version));
};
2013-12-29 01:40:47 -05:00
2013-09-27 06:31:28 -05:00
Server.prototype.put_tarball_incomplete = function(name, filename, data, size, cb) {
2017-04-19 14:15:28 -05:00
let promise = this.request({
uri: '/'+encodeURIComponent(name)+'/-/'+encodeURIComponent(filename)+'/whatever',
method: 'PUT',
headers: {
'content-type': 'application/octet-stream',
'content-length': size,
},
timeout: 1000,
2017-04-19 14:15:28 -05:00
});
2013-09-27 06:31:28 -05:00
2017-04-19 14:15:28 -05:00
promise.request(function(req) {
req.write(data);
setTimeout(function() {
2017-04-19 14:15:28 -05:00
req.req.abort();
}, 20);
});
2013-12-29 01:40:47 -05:00
2017-04-19 14:15:28 -05:00
return new Promise(function(resolve, reject) {
promise
.then(function() {
2017-04-19 14:15:28 -05:00
reject(Error('no error'));
})
.catch(function(err) {
if (err.code === 'ECONNRESET') {
2017-04-19 14:15:28 -05:00
resolve();
} else {
2017-04-19 14:15:28 -05:00
reject(err);
}
2017-04-19 14:15:28 -05:00
});
});
};
2015-04-11 08:09:19 -05:00
Server.prototype.add_package = function(name) {
return this.put_package(name, require('./package')(name))
.status(201)
2017-04-19 14:15:28 -05:00
.body_ok('created new package');
};
Server.prototype.whoami = function() {
2017-04-19 14:15:28 -05:00
return this.request({uri: '/-/whoami'})
.status(200)
2017-04-19 14:15:28 -05:00
.then(function(x) {
return x.username;
});
};
Server.prototype.debug = function() {
return this.request({
uri: '/-/_debug',
method: 'GET',
headers: {
2017-04-19 14:15:28 -05:00
'content-type': 'application/json',
},
2017-04-19 14:15:28 -05:00
});
};
2014-09-24 20:21:59 -05:00
2017-04-19 14:15:28 -05:00
module.exports = Server;
2013-09-25 05:01:55 -05:00