summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/npm-registry-client/test
diff options
context:
space:
mode:
Diffstat (limited to 'deps/npm/node_modules/npm-registry-client/test')
-rw-r--r--deps/npm/node_modules/npm-registry-client/test/econnreset.js95
-rw-r--r--deps/npm/node_modules/npm-registry-client/test/lib/server.js1
-rw-r--r--deps/npm/node_modules/npm-registry-client/test/send-anon-metrics.js78
3 files changed, 173 insertions, 1 deletions
diff --git a/deps/npm/node_modules/npm-registry-client/test/econnreset.js b/deps/npm/node_modules/npm-registry-client/test/econnreset.js
new file mode 100644
index 0000000000..ee0657112b
--- /dev/null
+++ b/deps/npm/node_modules/npm-registry-client/test/econnreset.js
@@ -0,0 +1,95 @@
+'use strict'
+var requireInject = require('require-inject')
+var test = require('tap').test
+var EventEmitter = require('events').EventEmitter
+var PassThrough = require('readable-stream').PassThrough
+
+var content = [
+ 'first chunk',
+ 'second chunk'
+]
+var requests = 0
+var fetch = requireInject('../lib/fetch.js', {
+ request: function (opts) {
+ var req = new EventEmitter()
+ ++requests
+ setTimeout(function () {
+ var res = new PassThrough()
+ res.statusCode = 200
+
+ setTimeout(function () {
+ res.write(content[0])
+ }, 50)
+
+ if (requests === 1) {
+ setTimeout(function () {
+ var err = new Error('read ECONNRESET')
+ err.code = 'ECONNRESET'
+ req.emit('error', err)
+ }, 100)
+ } else {
+ // we allow success on retries, though in practice we won't be
+ // retrying.
+ setTimeout(function () {
+ res.end(content[1])
+ }, 100)
+ }
+ req.emit('response', res)
+ }, 50)
+ return req
+ }
+})
+
+function clientMock (t) {
+ return {
+ log: {
+ info: function (section, message) {
+ t.comment('[info] ' + section + ': ' + [].slice.call(arguments, 1).join(' '))
+ },
+ http: function (section, message) {
+ t.comment('[http] ' + section + ': ' + [].slice.call(arguments, 1).join(' '))
+ }
+ },
+ authify: function (alwaysAuth, parsed, headers, auth) {
+ return
+ },
+ initialize: function (parsed, method, accept, headers) {
+ return {}
+ },
+ attempt: require('../lib/attempt.js'),
+ config: {
+ retry: {
+ retries: 2,
+ factor: 10,
+ minTimeout: 10000,
+ maxTimeout: 60000
+ }
+ }
+ }
+}
+
+/*
+This tests that errors that occur in the REQUEST object AFTER a `response`
+event has been emitted will be passed through to the `response` stream.
+This means that we won't try to retry these sorts of errors ourselves.
+*/
+
+test('econnreset', function (t) {
+ var client = clientMock(t)
+ fetch.call(client, 'http://example.com/', {}, function (err, res) {
+ t.ifError(err, 'initial fetch ok')
+ var data = ''
+ res.on('data', function (chunk) {
+ data += chunk
+ })
+ res.on('error', function (err) {
+ t.comment('ERROR:', err.stack)
+ t.pass("errored and that's ok")
+ t.done()
+ })
+ res.on('end', function () {
+ t.is(data, content.join(''), 'succeeded and got the right data')
+ t.done()
+ })
+ })
+})
diff --git a/deps/npm/node_modules/npm-registry-client/test/lib/server.js b/deps/npm/node_modules/npm-registry-client/test/lib/server.js
index 06bebdc139..3a702e6796 100644
--- a/deps/npm/node_modules/npm-registry-client/test/lib/server.js
+++ b/deps/npm/node_modules/npm-registry-client/test/lib/server.js
@@ -12,7 +12,6 @@ server._expect = {}
function handler (req, res) {
req.connection.setTimeout(1000)
-
// If we got authorization, make sure it's the right password.
if (req.headers.authorization && req.headers.authorization.match(/^Basic/)) {
var auth = req.headers.authorization.replace(/^Basic /, '')
diff --git a/deps/npm/node_modules/npm-registry-client/test/send-anon-metrics.js b/deps/npm/node_modules/npm-registry-client/test/send-anon-metrics.js
new file mode 100644
index 0000000000..7afa0542c4
--- /dev/null
+++ b/deps/npm/node_modules/npm-registry-client/test/send-anon-metrics.js
@@ -0,0 +1,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()
+})