forked from evanlucas/remote-file-size
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
64 lines (56 loc) · 1.57 KB
/
test.js
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
'use strict'
const test = require('tap').test
, remote = require('./')
, http = require('http')
test('should fail on invalid url', function(t) {
remote('blah', function(err, out) {
t.ok(err, 'err should exist')
t.equal(err.message, 'Invalid URI "blah"')
t.end()
})
})
test('should fail on 404', function(t) {
remote('http://evanlucas.com/biscuits', function(err, out) {
t.ok(err, 'err should exist')
t.equal(err.message, 'Received invalid status code: 404')
t.end()
})
})
test('should return null, size on success', function(t) {
const u = 'http://registry.npmjs.org/argsplit/-/argsplit-1.0.2.tgz'
remote(u, function(err, out) {
t.ifError(err, 'err should not exist')
t.ok(out, 'out should exist')
t.equal(out, 1548)
t.end()
})
})
test('should work passing an object', function(t) {
const opts = {
uri: 'http://registry.npmjs.org/argsplit/-/argsplit-1.0.2.tgz'
}
remote(opts, function(err, out) {
t.ifError(err, 'err should not exist')
t.ok(out, 'out should exist')
t.equal(out, 1548)
t.end()
})
})
test('should return error if invalid content-length', function(t) {
t.plan(2)
const server = http.createServer(function(req, res) {
res.writeHead(200, {
'Content-Type': 'application/json'
})
res.end()
}).listen(0, function() {
const port = server.address().port
remote(`http://localhost:${port}/`, function(err, out) {
t.ok(err, 'err should exist')
t.equal(err.message, 'Unable to determine file size')
})
})
t.on('end', function() {
server.close()
})
})