diff options
Diffstat (limited to 'node_modules/npm-registry-client/test/lib')
-rw-r--r-- | node_modules/npm-registry-client/test/lib/common.js | 17 | ||||
-rw-r--r-- | node_modules/npm-registry-client/test/lib/server.js | 58 |
2 files changed, 75 insertions, 0 deletions
diff --git a/node_modules/npm-registry-client/test/lib/common.js b/node_modules/npm-registry-client/test/lib/common.js new file mode 100644 index 000000000..f9048c094 --- /dev/null +++ b/node_modules/npm-registry-client/test/lib/common.js @@ -0,0 +1,17 @@ +var resolve = require("path").resolve +var server = require('./server.js') +var RC = require('../../') + +module.exports = { + freshClient : function freshClient(config) { + config = config || {} + config.cache = resolve(__dirname, '../fixtures/cache') + config.registry = 'http://localhost:' + server.port + + var client = new RC(config) + server.log = client.log + client.log.level = 'silent' + + return client + } +} diff --git a/node_modules/npm-registry-client/test/lib/server.js b/node_modules/npm-registry-client/test/lib/server.js new file mode 100644 index 000000000..03d3cd1fc --- /dev/null +++ b/node_modules/npm-registry-client/test/lib/server.js @@ -0,0 +1,58 @@ +// a fake registry server. + +var http = require('http') +var server = http.createServer(handler) +var port = server.port = process.env.PORT || 1337 +server.listen(port) + +module.exports = server + +server._expect = {} + +function handler (req, res) { + req.connection.setTimeout(1000) + + var u = '* ' + req.url + , mu = req.method + ' ' + req.url + + var k = server._expect[mu] ? mu : server._expect[u] ? u : null + if (!k) throw Error('unexpected request: ' + req.method + ' ' + req.url) + + var fn = server._expect[k].shift() + if (!fn) throw Error('unexpected request' + req.method + ' ' + req.url) + + + var remain = (Object.keys(server._expect).reduce(function (s, k) { + return s + server._expect[k].length + }, 0)) + if (remain === 0) server.close() + else this.log.info("fake-registry", "TEST SERVER: %d reqs remain", remain) + this.log.info("fake-registry", Object.keys(server._expect).map(function(k) { + return [k, server._expect[k].length] + }).reduce(function (acc, kv) { + acc[kv[0]] = kv[1] + return acc + }, {})) + + res.json = json + fn(req, res) +} + +function json (o) { + this.setHeader('content-type', 'application/json') + this.end(JSON.stringify(o)) +} + +// this log is meanto to be overridden +server.log = require("npmlog") + +server.expect = function (method, u, fn) { + if (typeof u === 'function') { + fn = u + u = method + method = '*' + } + u = method + ' ' + u + server._expect[u] = server._expect[u] || [] + server._expect[u].push(fn) +} |