summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/npm-registry-client/test/send-anon-metrics.js
blob: 7afa0542c434def8cc517f7ea5b4a95953127fc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
var test = require('tap').test

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

function nop () {}

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

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

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

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

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

  t.throws(function () {
    client.sendAnonymousCLIMetrics(common.registry, metricInfo, undefined)
  }, 'requires callback')

  t.throws(function () {
    client.sendAnonymousCLIMetrics(common.registry, metricInfo, 'callback')
  }, 'callback must be function')

  t.end()
})

test('sendAnonymousCLIMetrics', function (t) {
  server.expect('PUT', '/-/npm/anon-metrics/v1/' + metricId, function (req, res) {
    t.is(req.method, 'PUT')
    var b = ''
    req.setEncoding('utf8')
    req.on('data', function (d) {
      b += d
    })

    req.on('end', function () {
      var d = JSON.parse(b)
      t.isDeeply(d, metricInfo.metrics, 'PUT received metrics')

      res.statusCode = 200
      res.json({})
    })
  })

  client.sendAnonymousCLIMetrics(common.registry, metricInfo, function (err, res) {
    t.ifError(err, 'no errors')
    t.isDeeply(res, {}, 'result ok')
    t.end()
  })
})

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