Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test improvements: use fixtures #540

Merged
merged 8 commits into from Dec 27, 2015

test: All remaining tests use new fixtures

- Tests use new fixtures in common.js
- Tests ensure that destroy callbacks don’t fire an error
  • Loading branch information
feross committed Dec 21, 2015
commit 748347861fc024dd6451be76d646a22ed7d1369b
@@ -73,7 +73,6 @@
"brfs": "^1.2.0",
"browserify": "^12.0.1",
"finalhandler": "^0.4.0",
"run-auto": "^1.0.0",
"run-series": "^1.0.2",
"serve-static": "^1.9.3",
"simple-get": "^1.0.0",
@@ -6,7 +6,7 @@ var test = require('tape')
var WebTorrent = require('../')

test('blocklist blocks peers discovered via DHT', function (t) {
t.plan(8)
t.plan(9)

var dhtServer, client1, client2

@@ -80,9 +80,8 @@ test('blocklist blocks peers discovered via DHT', function (t) {
})
}

], function (err, r) {
if (err) throw err

], function (err) {
t.error(err)
dhtServer.destroy(function (err) {
t.error(err, 'dht server destroyed')
})
@@ -6,7 +6,7 @@ var TrackerServer = require('bittorrent-tracker/server')
var WebTorrent = require('../')

test('blocklist blocks peers discovered via tracker', function (t) {
t.plan(8)
t.plan(9)

var parsedTorrent = extend(common.leaves.parsedTorrent)
var tracker, client1, client2
@@ -75,11 +75,10 @@ test('blocklist blocks peers discovered via tracker', function (t) {
})
}

], function (err, r) {
if (err) throw err

tracker.close(function (err) {
t.error(err, 'tracker closed')
], function (err) {
t.error(err)
tracker.close(function () {
t.pass('tracker closed')
})
client1.destroy(function (err) {
t.error(err, 'client1 destroyed')
@@ -30,29 +30,33 @@ function assertReachable (t, torrent, addr) {
}

test('blocklist (single IP)', function (t) {
t.plan(8)
t.plan(9)

var client = new WebTorrent({
dht: false,
tracker: false,
blocklist: [ '1.2.3.4' ]
})
.on('error', function (err) { t.fail(err) })
.on('warning', function (err) { t.fail(err) })
.on('ready', function () {
client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })

// blocklist isn't fully loaded until `ready` event
client.on('ready', function () {
client.add(leavesParsed, function (torrent) {
assertBlocked(t, torrent, '1.2.3.4:1234')
assertBlocked(t, torrent, '1.2.3.4:6969')
assertReachable(t, torrent, '1.1.1.1:1234')
assertReachable(t, torrent, '1.1.1.1:6969')

client.destroy()
client.destroy(function (err) {
t.error(err, 'client destroyed')
})
})
})
})

test('blocklist (array of IPs)', function (t) {
t.plan(12)
t.plan(13)

var client = new WebTorrent({
dht: false,
@@ -70,7 +74,9 @@ test('blocklist (array of IPs)', function (t) {
assertReachable(t, torrent, '1.1.1.1:1234')
assertReachable(t, torrent, '1.1.1.1:6969')

client.destroy()
client.destroy(function (err) {
t.error(err, 'client destroyed')
})
})
})
})
@@ -115,7 +121,7 @@ function assertList (t, torrent) {
}

test('blocklist (array of IP ranges)', function (t) {
t.plan(48)
t.plan(49)
var client = new WebTorrent({
dht: false,
tracker: false,
@@ -129,13 +135,15 @@ test('blocklist (array of IP ranges)', function (t) {
.on('ready', function () {
client.add(leavesParsed, function (torrent) {
assertList(t, torrent)
client.destroy()
client.destroy(function (err) {
t.error(err, 'client destroyed')
})
})
})
})

test('blocklist (http url)', function (t) {
t.plan(49)
t.plan(51)
var server = http.createServer(function (req, res) {
// Check that WebTorrent declares a user agent
t.ok(req.headers['user-agent'].indexOf('WebTorrent') !== -1)
@@ -156,15 +164,19 @@ test('blocklist (http url)', function (t) {
.on('ready', function () {
client.add(leavesParsed, function (torrent) {
assertList(t, torrent)
client.destroy()
server.close()
client.destroy(function (err) {
t.error(err, 'client destroyed')
})
server.close(function () {
t.pass('server closed')
})
})
})
})
})

test('blocklist (http url with gzip encoding)', function (t) {
t.plan(49)
t.plan(51)
var server = http.createServer(function (req, res) {
// Check that WebTorrent declares a user agent
t.ok(req.headers['user-agent'].indexOf('WebTorrent') !== -1)
@@ -188,15 +200,19 @@ test('blocklist (http url with gzip encoding)', function (t) {
.on('ready', function () {
client.add(leavesParsed, function (torrent) {
assertList(t, torrent)
client.destroy()
server.close()
client.destroy(function (err) {
t.error(err, 'client destroyed')
})
server.close(function () {
t.pass('server closed')
})
})
})
})
})

test('blocklist (http url with deflate encoding)', function (t) {
t.plan(49)
t.plan(51)
var server = http.createServer(function (req, res) {
// Check that WebTorrent declares a user agent
t.ok(req.headers['user-agent'].indexOf('WebTorrent') !== -1)
@@ -220,15 +236,19 @@ test('blocklist (http url with deflate encoding)', function (t) {
.on('ready', function () {
client.add(leavesParsed, function (torrent) {
assertList(t, torrent)
client.destroy()
server.close()
client.destroy(function (err) {
t.error(err, 'client destroyed')
})
server.close(function () {
t.pass('server closed')
})
})
})
})
})

test('blocklist (fs path)', function (t) {
t.plan(48)
t.plan(49)
var client = new WebTorrent({
dht: false,
tracker: false,
@@ -239,13 +259,15 @@ test('blocklist (fs path)', function (t) {
.on('ready', function () {
client.add(leavesParsed, function (torrent) {
assertList(t, torrent)
client.destroy()
client.destroy(function (err) {
t.error(err, 'client destroyed')
})
})
})
})

test('blocklist (fs path with gzip)', function (t) {
t.plan(48)
t.plan(49)
var client = new WebTorrent({
dht: false,
tracker: false,
@@ -256,7 +278,9 @@ test('blocklist (fs path with gzip)', function (t) {
.on('ready', function () {
client.add(leavesParsed, function (torrent) {
assertList(t, torrent)
client.destroy()
client.destroy(function (err) {
t.error(err, 'client destroyed')
})
})
})
})
@@ -1,3 +1,5 @@
// Torrent and content test files. Content is Public Domain or Creative Commons.

var fs = require('fs')
var path = require('path')
var parseTorrent = require('parse-torrent')
@@ -9,37 +11,32 @@ module.exports = {
torrentPath: path.join(__dirname, 'torrents', 'leaves.torrent'),
content: fs.readFileSync(path.join(__dirname, 'content', 'Leaves of Grass by Walt Whitman.epub')),
torrent: fs.readFileSync(path.join(__dirname, 'torrents', 'leaves.torrent')),
parsedTorrent: parseTorrent(
fs.readFileSync(path.join(__dirname, 'torrents', 'leaves.torrent'))
),
magnetURI: parseTorrent.toMagnetURI(parseTorrent(
fs.readFileSync(path.join(__dirname, 'torrents', 'leaves.torrent'))
))
parsedTorrent: parseTorrent(fs.readFileSync(path.join(__dirname, 'torrents', 'leaves.torrent'))),
magnetURI: parseTorrent.toMagnetURI(parseTorrent(fs.readFileSync(path.join(__dirname, 'torrents', 'leaves.torrent'))))
},

// Folder which contains single file
folder: {
contentPath: path.join(__dirname, 'content', 'folder'),
torrentPath: path.join(__dirname, 'torrents', 'folder.torrent'),
torrent: fs.readFileSync(path.join(__dirname, 'torrents', 'folder.torrent')),
parsedTorrent: parseTorrent(
fs.readFileSync(path.join(__dirname, 'torrents', 'folder.torrent'))
),
magnetURI: parseTorrent.toMagnetURI(parseTorrent(
fs.readFileSync(path.join(__dirname, 'torrents', 'folder.torrent'))
))
parsedTorrent: parseTorrent(fs.readFileSync(path.join(__dirname, 'torrents', 'folder.torrent'))),
magnetURI: parseTorrent.toMagnetURI(parseTorrent(fs.readFileSync(path.join(__dirname, 'torrents', 'folder.torrent'))))
},

// Folder which contains multiple files
numbers: {
contentPath: path.join(__dirname, 'content', 'numbers'),
torrentPath: path.join(__dirname, 'torrents', 'numbers.torrent'),
torrent: fs.readFileSync(path.join(__dirname, 'torrents', 'numbers.torrent')),
parsedTorrent: parseTorrent(
fs.readFileSync(path.join(__dirname, 'torrents', 'numbers.torrent'))
),
magnetURI: parseTorrent.toMagnetURI(parseTorrent(
fs.readFileSync(path.join(__dirname, 'torrents', 'numbers.torrent'))
))
parsedTorrent: parseTorrent(fs.readFileSync(path.join(__dirname, 'torrents', 'numbers.torrent'))),
magnetURI: parseTorrent.toMagnetURI(parseTorrent(fs.readFileSync(path.join(__dirname, 'torrents', 'numbers.torrent'))))
},

// Torrent file with "private" flag
bunny: {
torrentPath: path.join(__dirname, 'torrents', 'big-buck-bunny-private.torrent'),
torrent: fs.readFileSync(path.join(__dirname, 'torrents', 'big-buck-bunny-private.torrent')),
parsedTorrent: parseTorrent(fs.readFileSync(path.join(__dirname, 'torrents', 'big-buck-bunny-private.torrent')))
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.