Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

Commit 177d37a

Browse files
committed
Updated ping command to stick to The New Way
PR-URL: #110
1 parent 9ab131e commit 177d37a

File tree

2 files changed

+70
-40
lines changed

2 files changed

+70
-40
lines changed

lib/ping.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
1-
21
module.exports = ping
32

4-
var url = require("url")
3+
var url = require('url')
4+
var assert = require('assert')
5+
6+
function ping (uri, params, cb) {
7+
assert(typeof uri === 'string', 'must pass registry URI to ping')
8+
assert(params && typeof params === 'object', 'must pass params to ping')
9+
assert(typeof cb === 'function', 'must pass callback to ping')
10+
11+
var auth = params.auth
12+
assert(auth && typeof auth === 'object', 'must pass auth to ping')
513

6-
function ping (uri, cb) {
7-
var c = this.conf.getCredentialsByURI(uri)
8-
if (!c || !c.auth) {
9-
var er = new Error("Must be logged in to ping the registry")
10-
er.code = "ENEEDAUTH"
11-
return cb(er)
12-
}
13-
var parsed = url.parse(uri)
14-
parsed.auth = c.username + ":" + escape(c.password)
15-
uri = url.resolve(parsed, "/-/ping?write=true")
16-
this.get(uri, null, function (er, fullData) {
14+
this.request(url.resolve(uri, '-/ping?write=true'), { auth: auth }, function (er, fullData) {
1715
if (er) {
1816
cb(er)
1917
} else if (fullData) {
20-
console.log(fullData)
2118
cb(null, fullData)
2219
} else {
23-
cb(new Error("No data received"))
20+
cb(new Error('No data received'))
2421
}
2522
})
2623
}

test/ping.js

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,73 @@
1-
var tap = require("tap")
1+
var test = require('tap').test
22

3-
var server = require("./lib/server.js")
4-
var common = require("./lib/common.js")
3+
var server = require('./lib/server.js')
4+
var common = require('./lib/common.js')
5+
var client = common.freshClient()
56

6-
var DEP_USER = "username"
7-
var HOST = "localhost"
7+
function nop () {}
88

9-
var nerfed = "//" + HOST + ":" + server.port + "/:"
9+
var TOKEN = 'not-bad-meaning-bad-but-bad-meaning-wombat'
10+
var AUTH = { token: TOKEN }
11+
var PARAMS = { auth: AUTH }
12+
var DEP_USER = 'username'
13+
var HOST = 'localhost'
1014

11-
var configuration = {}
12-
configuration[nerfed + "username"] = DEP_USER
13-
configuration[nerfed + "_password"] = new Buffer("%1234@asdf%").toString("base64")
14-
configuration[nerfed + "email"] = "[email protected]"
15+
test('ping call contract', function (t) {
16+
t.throws(function () {
17+
client.ping(undefined, AUTH, nop)
18+
}, 'requires a URI')
1519

16-
var client = common.freshClient(configuration)
20+
t.throws(function () {
21+
client.ping([], AUTH, nop)
22+
}, 'requires URI to be a string')
1723

18-
tap.test("ping registry", function (t) {
19-
t.plan(3)
20-
server.expect("GET", "/-/ping?write=true", function (req, res) {
21-
t.equal(req.method, "GET")
24+
t.throws(function () {
25+
client.ping(common.registry, undefined, nop)
26+
}, 'requires params object')
27+
28+
t.throws(function () {
29+
client.ping(common.registry, '', nop)
30+
}, 'params must be object')
31+
32+
t.throws(function () {
33+
client.ping(common.registry, AUTH, undefined)
34+
}, 'requires callback')
35+
36+
t.throws(function () {
37+
client.ping(common.registry, AUTH, 'callback')
38+
}, 'callback must be function')
39+
40+
t.throws(
41+
function () {
42+
var params = {}
43+
client.ping(common.registry, params, nop)
44+
},
45+
{ name: 'AssertionError', message: 'must pass auth to ping' },
46+
'must pass auth to ping'
47+
)
48+
49+
t.end()
50+
})
51+
52+
test('ping', function (t) {
53+
server.expect('GET', '/-/ping?write=true', function (req, res) {
54+
t.equal(req.method, 'GET')
2255
res.statusCode = 200
2356
res.json({
24-
ok: true
25-
, host: HOST
26-
, peer: HOST
27-
, username: DEP_USER
57+
ok: true,
58+
host: HOST,
59+
peer: HOST,
60+
username: DEP_USER
2861
})
2962
})
3063

31-
client.ping(common.registry, function (er, found) {
32-
t.ifError(er, "no errors")
64+
client.ping(common.registry, PARAMS, function (error, found) {
65+
t.ifError(error, 'no errors')
3366
var wanted = {
34-
ok: true
35-
, host: HOST
36-
, peer: HOST
37-
, username: DEP_USER
67+
ok: true,
68+
host: HOST,
69+
peer: HOST,
70+
username: DEP_USER
3871
}
3972
t.same(found, wanted)
4073
t.end()

0 commit comments

Comments
 (0)