npm-registry-mock
Advanced tools
Comparing version
48
index.js
@@ -14,30 +14,34 @@ var path = require('path') | ||
if (typeof port == "object") { | ||
mocks = port.mocks | ||
mocks = port.mocks || mocks | ||
port = port.port || 1331 | ||
} | ||
hock.createHock(port, function(err, hockServer) { | ||
for (var method in mocks) { | ||
for (var route in mocks[method]) { | ||
var status = mocks[method][route][0] | ||
var customTarget = mocks[method][route][1] | ||
var isTarball = /.tgz$/.test(route) | ||
if (isTarball) { | ||
var target = __dirname + path.sep + "fixtures" + route.replace(/\//g, path.sep); | ||
if (customTarget && typeof customTarget == 'string') | ||
target = customTarget | ||
if (typeof mocks == "function") { | ||
mocks(hockServer) | ||
} else { | ||
for (var method in mocks) { | ||
for (var route in mocks[method]) { | ||
var status = mocks[method][route][0] | ||
var customTarget = mocks[method][route][1] | ||
var isTarball = /.tgz$/.test(route) | ||
if (isTarball) { | ||
var target = __dirname + path.sep + "fixtures" + route.replace(/\//g, path.sep); | ||
if (customTarget && typeof customTarget == 'string') | ||
target = customTarget | ||
hockServer[method](route).replyWithFile(status, target); | ||
} else { | ||
if (!customTarget) { | ||
var res = require(__dirname + path.sep + "fixtures" + route.replace(/\//g, path.sep)) | ||
res = JSON.stringify(res).replace(/http:\/\/registry\.npmjs\.org/ig, 'http://localhost:' + port) | ||
} | ||
else { | ||
try { | ||
var res = require(customTarget) | ||
} catch (e) { | ||
var res = customTarget | ||
hockServer[method](route).replyWithFile(status, target); | ||
} else { | ||
if (!customTarget) { | ||
var res = require(__dirname + path.sep + "fixtures" + route.replace(/\//g, path.sep)) | ||
res = JSON.stringify(res).replace(/http:\/\/registry\.npmjs\.org/ig, 'http://localhost:' + port) | ||
} | ||
else { | ||
try { | ||
var res = require(customTarget) | ||
} catch (e) { | ||
var res = customTarget | ||
} | ||
} | ||
hockServer[method](route).reply(status, res) | ||
} | ||
hockServer[method](route).reply(status, res) | ||
} | ||
@@ -44,0 +48,0 @@ } |
{ | ||
"name": "npm-registry-mock", | ||
"version": "0.4.1", | ||
"version": "0.5.0", | ||
"description": "mock the npm registry", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -120,2 +120,41 @@ var assert = require("assert") | ||
}) | ||
}) | ||
}) | ||
describe("injecting functions", function () { | ||
it("handles plugins", function (done) { | ||
function plugin (s) { | ||
s.get("/test").reply(500, {"foo": "true"}) | ||
s.get("/test").reply(500, {"foo": "true"}) | ||
s.get("/test").reply(200, {"lala": "true"}) | ||
} | ||
mr({port: 1331, mocks: plugin}, function (s) { | ||
request(address + "/test", function (er, res) { | ||
assert.deepEqual(res.body, JSON.stringify({foo: "true"})) | ||
assert.equal(res.statusCode, 500) | ||
request(address + "/test", function (er, res) { | ||
assert.deepEqual(res.body, JSON.stringify({foo: "true"})) | ||
assert.equal(res.statusCode, 500) | ||
request(address + "/test", function (er, res) { | ||
assert.deepEqual(res.body, JSON.stringify({lala: "true"})) | ||
assert.equal(res.statusCode, 200) | ||
s.close() | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
describe("api", function () { | ||
it("allow options object with port but no mocks", function (done) { | ||
mr({port: 1331}, function (s) { | ||
var client = new RC(conf) | ||
client.get("/underscore/latest", function (er, data, raw, res) { | ||
assert.equal(data._id, "underscore@1.5.1") | ||
s.close() | ||
done(er) | ||
}) | ||
}) | ||
}) | ||
}) |
748638
0.19%209
25.15%