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

sendAnonymousCLIMetrics added #139

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/sendAnonymousCLIMetrics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = send

var assert = require('assert')
var url = require('url')

function send (uri, params, cb) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should compose its URL on its own (for example how lib/dist-tags/set.js works).

assert(typeof uri === 'string', 'must pass registry URI')
assert(params && typeof params === 'object', 'must pass params')
assert(typeof cb === 'function', 'must pass callback')

var parsed = url.parse(uri)
assert(
parsed.protocol === 'http:' || parsed.protocol === 'https:',
'must have a URL that starts with http: or https:'
)

params.method = 'PUT'
this.request(uri, params, cb)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Params here can't just be passed through to request. What you're passing in the test isn't valid for this.request. Again, see lib/dist-tags/set.js, they would need to be JSON encoded and put into a body property.

}
68 changes: 68 additions & 0 deletions test/send-anon-metrics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
var test = require('tap').test

var common = require('./lib/common.js')
var client = common.freshClient()
var server = require('./lib/server.js')

function nop () {}

var URI = 'https://npm.registry:8043/-/npm/anon-metrics/v1/:metricId'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:metricId shouldn't be passed in literally, it's supposed to be replaced with the metricId value from the document we're given.

var from = new Date()
var to = new Date()
var PARAMS = {
metricId: 'this-is-a-uuid',
metrics: [{
from: from,
to: to,
successfulInstalls: 0,
failedInstalls: 1
}]
}

test('sendAnonymousCLIMetrics call contract', function (t) {
t.throws(function () {
client.sendAnonymousCLIMetrics(undefined, PARAMS, nop)
}, 'requires a URI')

t.throws(function () {
client.sendAnonymousCLIMetrics([], PARAMS, nop)
}, 'requires URI to be a string')

t.throws(function () {
client.sendAnonymousCLIMetrics(URI, undefined, nop)
}, 'requires params object')

t.throws(function () {
client.sendAnonymousCLIMetrics(URI, '', nop)
}, 'params must be object')

t.throws(function () {
client.sendAnonymousCLIMetrics(URI, PARAMS, undefined)
}, 'requires callback')

t.throws(function () {
client.sendAnonymousCLIMetrics(URI, PARAMS, 'callback')
}, 'callback must be function')

t.end()
})

test('sendAnonymousCLIMetrics', function (t) {

server.expect('PUT', '/-/npm/anon-metrics/v1/:metricId', function (req, res) {
res.end("200")
})

var uri = common.registry + '/-/npm/anon-metrics/v1/:metricId'
client.sendAnonymousCLIMetrics(uri, PARAMS, function (error, res) {
t.ifError(error, 'no errors')
server.close()
t.end()
})

})

test('cleanup', function (t) {
server.close()
t.end()
})