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

128 lines
3.3 KiB
JavaScript
Raw Normal View History

2013-12-19 10:11:54 -05:00
var request = require('request')
, assert = require('assert')
2013-09-25 05:01:55 -05:00
function Server(url) {
2013-12-19 10:11:54 -05:00
if (!(this instanceof Server)) return new Server(url)
this.url = url.replace(/\/$/, '')
this.userAgent = 'node/v0.10.8 linux x64'
this.authstr = 'Basic '+(new Buffer('test:test')).toString('base64')
2013-09-25 05:01:55 -05:00
}
2013-09-26 21:27:11 -05:00
function prep(cb) {
return function(err, res, body) {
2013-12-19 10:11:54 -05:00
if (err) throw err
cb(res, body)
}
2013-09-26 21:27:11 -05:00
}
2013-09-25 05:01:55 -05:00
Server.prototype.request = function(options, cb) {
assert(options.uri)
2013-12-19 10:11:54 -05:00
var headers = options.headers || {}
headers.accept = headers.accept || 'application/json'
headers['user-agent'] = headers['user-agent'] || this.userAgent
headers.authorization = headers.authorization || this.authstr
2013-09-25 05:01:55 -05:00
return request({
url: this.url + options.uri,
method: options.method || 'GET',
2013-09-27 06:31:28 -05:00
headers: headers,
2013-09-26 21:27:11 -05:00
json: options.json || true,
2013-12-19 10:11:54 -05:00
}, cb)
2013-09-25 05:01:55 -05:00
}
Server.prototype.auth = function(user, pass, cb) {
2013-12-19 10:11:54 -05:00
this.authstr = 'Basic '+(new Buffer(user+':'+pass)).toString('base64')
2013-09-25 05:01:55 -05:00
this.request({
2013-12-19 10:11:54 -05:00
uri: '/-/user/org.couchdb.user:'+encodeURIComponent(user)+'/-rev/undefined',
2013-09-25 05:01:55 -05:00
method: 'PUT',
json: {
content: "doesn't matter, 'cause sinopia uses info from Authorization header anywayz",
}
2013-12-19 10:11:54 -05:00
}, prep(cb))
2013-09-25 05:01:55 -05:00
}
Server.prototype.get_package = function(name, cb) {
2013-09-26 21:27:11 -05:00
this.request({
2013-09-25 05:01:55 -05:00
uri: '/'+name,
method: 'GET',
2013-12-19 10:11:54 -05:00
}, prep(cb))
2013-09-25 05:01:55 -05:00
}
Server.prototype.put_package = function(name, data, cb) {
2013-12-19 10:11:54 -05:00
if (typeof(data) === 'object' && !Buffer.isBuffer(data)) data = JSON.stringify(data)
2013-09-26 21:27:11 -05:00
this.request({
2013-12-19 10:11:54 -05:00
uri: '/'+encodeURIComponent(name),
2013-09-27 03:56:44 -05:00
method: 'PUT',
headers: {
'content-type': 'application/json'
},
2013-12-19 10:11:54 -05:00
}, prep(cb)).end(data)
2013-09-27 03:56:44 -05:00
}
Server.prototype.put_version = function(name, version, data, cb) {
2013-12-19 10:11:54 -05:00
if (typeof(data) === 'object' && !Buffer.isBuffer(data)) data = JSON.stringify(data)
2013-09-27 03:56:44 -05:00
this.request({
2013-12-19 10:11:54 -05:00
uri: '/'+encodeURIComponent(name)+'/'+encodeURIComponent(version)+'/-tag/latest',
2013-09-25 05:01:55 -05:00
method: 'PUT',
2013-09-26 21:27:11 -05:00
headers: {
'content-type': 'application/json'
},
2013-12-19 10:11:54 -05:00
}, prep(cb)).end(data)
2013-09-26 21:27:11 -05:00
}
Server.prototype.get_tarball = function(name, filename, cb) {
this.request({
2013-12-19 10:11:54 -05:00
uri: '/'+encodeURIComponent(name)+'/-/'+encodeURIComponent(filename),
2013-09-26 21:27:11 -05:00
method: 'GET',
2013-12-19 10:11:54 -05:00
}, prep(cb))
2013-09-26 21:27:11 -05:00
}
Server.prototype.put_tarball = function(name, filename, data, cb) {
this.request({
2013-12-19 10:11:54 -05:00
uri: '/'+encodeURIComponent(name)+'/-/'+encodeURIComponent(filename)+'/whatever',
2013-09-26 21:27:11 -05:00
method: 'PUT',
headers: {
'content-type': 'application/octet-stream'
},
2013-12-19 10:11:54 -05:00
}, prep(cb)).end(data)
2013-09-25 05:01:55 -05:00
}
2013-12-29 01:40:47 -05:00
Server.prototype.add_tag = function(name, tag, version, cb) {
this.request({
uri: '/'+encodeURIComponent(name)+'/'+encodeURIComponent(tag),
method: 'PUT',
headers: {
'content-type': 'application/json'
},
}, prep(cb)).end(JSON.stringify(version))
}
2013-09-27 06:31:28 -05:00
Server.prototype.put_tarball_incomplete = function(name, filename, data, size, cb) {
var req = this.request({
2013-12-19 10:11:54 -05:00
uri: '/'+encodeURIComponent(name)+'/-/'+encodeURIComponent(filename)+'/whatever',
2013-09-27 06:31:28 -05:00
method: 'PUT',
headers: {
'content-type': 'application/octet-stream',
'content-length': size,
},
timeout: 1000,
}, function(err) {
2013-12-19 10:11:54 -05:00
assert(err)
cb()
})
req.write(data)
2013-09-27 06:31:28 -05:00
setTimeout(function() {
2013-12-19 10:11:54 -05:00
req.req.abort()
}, 20)
2013-09-27 06:31:28 -05:00
}
2013-12-29 01:40:47 -05:00
Server.prototype.add_package = function(name, cb) {
this.put_package(name, require('./package')(name), function(res, body) {
assert.equal(res.statusCode, 201)
assert(~body.ok.indexOf('created new package'))
cb()
})
}
2013-12-19 10:11:54 -05:00
module.exports = Server
2013-09-25 05:01:55 -05:00