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
|
var resolve = require("path").resolve
var createReadStream = require("graceful-fs").createReadStream
var readFileSync = require("graceful-fs").readFileSync
var tap = require("tap")
var cat = require("concat-stream")
var Negotiator = require("negotiator")
var server = require("./lib/server.js")
var common = require("./lib/common.js")
var tgz = resolve(__dirname, "./fixtures/underscore/1.3.3/package.tgz")
tap.test("fetch accepts github api's json", function (t) {
server.expect("/underscore/-/underscore-1.3.3", function (req, res) {
t.equal(req.method, "GET", "got expected method")
var negotiator = new Negotiator(req)
// fetching a tarball from `api.github.com` returns a 415 error if json is
// not accepted
if (negotiator.mediaTypes().indexOf("application/vnd.github+json") === -1) {
res.writeHead(415, {
"Content-Type" : "application/json"
})
}
else {
res.writeHead(302, {
"Content-Type" : "text/html",
"Location" : "/underscore/-/underscore-1.3.3.tgz"
})
}
res.end()
})
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 client = common.freshClient()
var defaulted = {}
client.fetch(
"http://localhost:1337/underscore/-/underscore-1.3.3",
defaulted,
function (er, res) {
t.ifError(er, "loaded successfully")
var sink = cat(function (data) {
t.deepEqual(data, readFileSync(tgz))
t.end()
})
res.on("error", function (error) {
t.ifError(error, "no errors on stream")
})
res.pipe(sink)
}
)
})
|