Welcome to mirror list, hosted at ThFree Co, Russian Federation.

fetch-basic.js « test « npm-registry-client « node_modules « npm « deps - github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 68e67f023dd1fadb83b2d19522d2b0e1be7527d1 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
var resolve = require("path").resolve
var createReadStream = require("graceful-fs").createReadStream
var readFileSync = require("graceful-fs").readFileSync

var test = require("tap").test
var concat = require("concat-stream")

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

var tgz = resolve(__dirname, "./fixtures/underscore/1.3.3/package.tgz")

function nop () {}

var URI      = "https://npm.registry:8043/rewrite"
var USERNAME = "username"
var PASSWORD = "hi"
var EMAIL    = "n@p.m"
var HEADERS  = {
  "npm-custom" : "lolbutts"
}
var AUTH     = {
  username : USERNAME,
  password : PASSWORD,
  email : EMAIL
}
var PARAMS   = {
  headers : HEADERS,
  auth    : AUTH
}

test("fetch call contract", function (t) {
  t.throws(function () {
    client.get(undefined, PARAMS, nop)
  }, "requires a URI")

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

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

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

  t.throws(function () {
    client.get(URI, PARAMS, undefined)
  }, "requires callback")

  t.throws(function () {
    client.get(URI, PARAMS, "callback")
  }, "callback must be function")

  t.end()
})

test("basic fetch", function (t) {
  server.expect("/underscore/-/underscore-1.3.3.tgz", function (req, res) {
    t.equal(req.method, "GET", "got expected method")

    res.writeHead(200, {
      "content-type"     : "application/x-tar",
      "content-encoding" : "gzip"
    })

    createReadStream(tgz).pipe(res)
  })

  var defaulted = {}
  client.fetch(
    "http://localhost:1337/underscore/-/underscore-1.3.3.tgz",
    defaulted,
    function (er, res) {
      t.ifError(er, "loaded successfully")

      var sink = concat(function (data) {
        t.deepEqual(data, readFileSync(tgz))
        t.end()
      })

      res.on("error", function (error) {
        t.ifError(error, "no errors on stream")
      })

      res.pipe(sink)
    }
  )
})