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

Improve tests; misc fixes #532

Merged
merged 9 commits into from Dec 18, 2015

test: put fixtures into single location

  • Loading branch information
feross committed Dec 10, 2015
commit dfdf52b747201cdb9f616a2b1f0831e96b250ab6
@@ -1,24 +1,14 @@
var WebTorrent = require('../')
var fs = require('fs')
var common = require('./common')
var http = require('http')
var path = require('path')
var parseTorrent = require('parse-torrent')
var test = require('tape')

var leavesPath = path.resolve(__dirname, 'torrents', 'leaves.torrent')
var leaves = fs.readFileSync(leavesPath)
var leavesTorrent = parseTorrent(leaves)
var leavesBookPath = path.resolve(__dirname, 'content', 'Leaves of Grass by Walt Whitman.epub')
var leavesMagnetURI = 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves+of+Grass+by+Walt+Whitman.epub&tr=http%3A%2F%2Ftracker.bittorrent.am%2Fannounce&tr=http%3A%2F%2Ftracker.thepiratebay.org%2Fannounce&tr=udp%3A%2F%2Ffr33domtracker.h33t.com%3A3310%2Fannounce&tr=udp%3A%2F%2Ftracker.ccc.de%3A80&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80'
var numbersPath = path.resolve(__dirname, 'content', 'numbers')
var folderPath = path.resolve(__dirname, 'content', 'folder')
var WebTorrent = require('../')

test('client.add: http url to a torrent file, string', function (t) {
t.plan(3)

var server = http.createServer(function (req, res) {
t.ok(req.headers['user-agent'].indexOf('WebTorrent') !== -1)
res.end(leaves)
res.end(common.leaves.torrent)
})

server.listen(0, function () {
@@ -30,8 +20,8 @@ test('client.add: http url to a torrent file, string', function (t) {
client.on('warning', function (err) { t.fail(err) })

client.add(url, function (torrent) {
t.equal(torrent.infoHash, leavesTorrent.infoHash)
t.equal(torrent.magnetURI, leavesMagnetURI)
t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash)
t.equal(torrent.magnetURI, common.leaves.magnetURI)
client.destroy()
server.close()
})
@@ -46,9 +36,9 @@ test('client.add: filesystem path to a torrent file, string', function (t) {
client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })

client.add(leavesPath, function (torrent) {
t.equal(torrent.infoHash, leavesTorrent.infoHash)
t.equal(torrent.magnetURI, leavesMagnetURI)
client.add(common.leaves.torrentPath, function (torrent) {
t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash)
t.equal(torrent.magnetURI, common.leaves.magnetURI)
client.destroy()
})
})
@@ -73,9 +63,9 @@ test('client.seed: filesystem path to file, string', function (t) {
client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })

client.seed(leavesBookPath, opts, function (torrent) {
t.equal(torrent.infoHash, leavesTorrent.infoHash)
t.equal(torrent.magnetURI, leavesMagnetURI)
client.seed(common.leaves.contentPath, opts, function (torrent) {
t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash)
t.equal(torrent.magnetURI, common.leaves.magnetURI)
client.destroy()
})
})
@@ -99,7 +89,7 @@ test('client.seed: filesystem path to folder with one file, string', function (t
client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })

client.seed(folderPath, opts, function (torrent) {
client.seed(common.folder.contentPath, opts, function (torrent) {
t.equal(torrent.infoHash, '3a686c32404af0a66913dd5f8d2b40673f8d4490')
t.equal(torrent.magnetURI, 'magnet:?xt=urn:btih:3a686c32404af0a66913dd5f8d2b40673f8d4490&dn=folder&tr=udp%3A%2F%2Ftracker.webtorrent.io%3A80')
client.destroy()
@@ -125,7 +115,7 @@ test('client.seed: filesystem path to folder with multiple files, string', funct
client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })

client.seed(numbersPath, opts, function (torrent) {
client.seed(common.numbers.contentPath, opts, function (torrent) {
t.equal(torrent.infoHash, '80562f38656b385ea78959010e51a2cc9db41ea0')
t.equal(torrent.magnetURI, 'magnet:?xt=urn:btih:80562f38656b385ea78959010e51a2cc9db41ea0&dn=numbers&tr=udp%3A%2F%2Ftracker.webtorrent.io%3A80')
client.destroy()
@@ -1,28 +1,20 @@
var path = require('path')
var fs = require('fs')
var common = require('./common')
var extend = require('xtend')
var parseTorrent = require('parse-torrent')
var test = require('tape')
var WebTorrent = require('../')

var leaves = fs.readFileSync(path.resolve(__dirname, 'torrents', 'leaves.torrent'))
var leavesTorrent = parseTorrent(leaves)
var leavesBook = fs.readFileSync(path.resolve(__dirname, 'content', 'Leaves of Grass by Walt Whitman.epub'))

var leavesMagnetURI = 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves+of+Grass+by+Walt+Whitman.epub&tr=http%3A%2F%2Ftracker.bittorrent.am%2Fannounce&tr=http%3A%2F%2Ftracker.thepiratebay.org%2Fannounce&tr=udp%3A%2F%2Ffr33domtracker.h33t.com%3A3310%2Fannounce&tr=udp%3A%2F%2Ftracker.ccc.de%3A80&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80'

test('client.add/remove: magnet uri, utf-8 string', function (t) {
var client = new WebTorrent({ dht: false, tracker: false })

client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })

var torrent = client.add('magnet:?xt=urn:btih:' + leavesTorrent.infoHash)
var torrent = client.add('magnet:?xt=urn:btih:' + common.leaves.parsedTorrent.infoHash)
t.equal(client.torrents.length, 1)
torrent.on('infoHash', function () {
t.equal(torrent.infoHash, leavesTorrent.infoHash)
t.ok(torrent.magnetURI.indexOf('magnet:?xt=urn:btih:' + leavesTorrent.infoHash) === 0)
client.remove('magnet:?xt=urn:btih:' + leavesTorrent.infoHash)
t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash)
t.ok(torrent.magnetURI.indexOf('magnet:?xt=urn:btih:' + common.leaves.parsedTorrent.infoHash) === 0)
client.remove('magnet:?xt=urn:btih:' + common.leaves.parsedTorrent.infoHash)
t.equal(client.torrents.length, 0)
client.destroy()
t.end()
@@ -35,12 +27,12 @@ test('client.add/remove: torrent file, buffer', function (t) {
client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })

var torrent = client.add(leaves)
var torrent = client.add(common.leaves.torrent)
t.equal(client.torrents.length, 1)
torrent.on('infoHash', function () {
t.equal(torrent.infoHash, leavesTorrent.infoHash)
t.equal(torrent.magnetURI, leavesMagnetURI)
client.remove(leaves)
t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash)
t.equal(torrent.magnetURI, common.leaves.magnetURI)
client.remove(common.leaves.torrent)
t.equal(client.torrents.length, 0)
client.destroy()
t.end()
@@ -53,12 +45,12 @@ test('client.add/remove: info hash, hex string', function (t) {
client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })

var torrent = client.add(leavesTorrent.infoHash)
var torrent = client.add(common.leaves.parsedTorrent.infoHash)
t.equal(client.torrents.length, 1)
torrent.on('infoHash', function () {
t.equal(torrent.infoHash, leavesTorrent.infoHash)
t.ok(torrent.magnetURI.indexOf('magnet:?xt=urn:btih:' + leavesTorrent.infoHash) === 0)
client.remove(leavesTorrent.infoHash)
t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash)
t.ok(torrent.magnetURI.indexOf('magnet:?xt=urn:btih:' + common.leaves.parsedTorrent.infoHash) === 0)
client.remove(common.leaves.parsedTorrent.infoHash)
t.equal(client.torrents.length, 0)
client.destroy()
t.end()
@@ -71,12 +63,12 @@ test('client.add/remove: info hash, buffer', function (t) {
client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })

var torrent = client.add(new Buffer(leavesTorrent.infoHash, 'hex'))
var torrent = client.add(common.leaves.parsedTorrent.infoHashBuffer)
t.equal(client.torrents.length, 1)
torrent.on('infoHash', function () {
t.equal(torrent.infoHash, leavesTorrent.infoHash)
t.ok(torrent.magnetURI.indexOf('magnet:?xt=urn:btih:' + leavesTorrent.infoHash) === 0)
client.remove(new Buffer(leavesTorrent.infoHash, 'hex'))
t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash)
t.ok(torrent.magnetURI.indexOf('magnet:?xt=urn:btih:' + common.leaves.parsedTorrent.infoHash) === 0)
client.remove(new Buffer(common.leaves.parsedTorrent.infoHash, 'hex'))
t.equal(client.torrents.length, 0)
client.destroy()
t.end()
@@ -89,12 +81,12 @@ test('client.add/remove: parsed torrent, from `parse-torrent`', function (t) {
client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })

var torrent = client.add(leavesTorrent)
var torrent = client.add(common.leaves.parsedTorrent)
t.equal(client.torrents.length, 1)
torrent.on('infoHash', function () {
t.equal(torrent.infoHash, leavesTorrent.infoHash)
t.equal(torrent.magnetURI, leavesMagnetURI)
client.remove(leavesTorrent)
t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash)
t.equal(torrent.magnetURI, common.leaves.magnetURI)
client.remove(common.leaves.parsedTorrent)
t.equal(client.torrents.length, 0)
client.destroy()
t.end()
@@ -107,14 +99,14 @@ test('client.add/remove: parsed torrent, with string type announce property', fu
client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })

var modifiedParsedTorrent = extend(leavesTorrent, {
announce: leavesTorrent.announce[0]
var modifiedParsedTorrent = extend(common.leaves.parsedTorrent, {
announce: common.leaves.parsedTorrent.announce[0]
})
var torrent = client.add(modifiedParsedTorrent)
t.equal(client.torrents.length, 1)
torrent.on('infoHash', function () {
t.equal(torrent.infoHash, leavesTorrent.infoHash)
client.remove(leavesTorrent)
t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash)
client.remove(common.leaves.parsedTorrent)
t.equal(client.torrents.length, 0)
client.destroy()
t.end()
@@ -127,10 +119,10 @@ test('client.remove: remove by Torrent object', function (t) {
client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })

var torrent = client.add(leavesTorrent.infoHash)
var torrent = client.add(common.leaves.parsedTorrent.infoHash)
t.equal(client.torrents.length, 1)
torrent.on('infoHash', function () {
t.equal(torrent.infoHash, leavesTorrent.infoHash)
t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash)
client.remove(torrent)
t.equal(client.torrents.length, 0)
client.destroy()
@@ -144,10 +136,10 @@ test('torrent.destroy: destroy and remove torrent', function (t) {
client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })

var torrent = client.add(leavesTorrent.infoHash)
var torrent = client.add(common.leaves.parsedTorrent.infoHash)
t.equal(client.torrents.length, 1)
torrent.on('infoHash', function () {
t.equal(torrent.infoHash, leavesTorrent.infoHash)
t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash)
torrent.destroy()
t.equal(client.torrents.length, 0)
client.destroy()
@@ -175,10 +167,10 @@ test('client.seed: torrent file (Buffer)', function (t) {
client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })

client.seed(leavesBook, opts, function (torrent) {
client.seed(common.leaves.content, opts, function (torrent) {
t.equal(client.torrents.length, 1)
t.equal(torrent.infoHash, leavesTorrent.infoHash)
t.equal(torrent.magnetURI, leavesMagnetURI)
t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash)
t.equal(torrent.magnetURI, common.leaves.magnetURI)
client.remove(torrent)
t.equal(client.torrents.length, 0)
client.destroy()
@@ -205,10 +197,10 @@ test('client.seed: torrent file (Blob)', function (t) {
client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })

client.seed(new global.Blob([ leavesBook ]), opts, function (torrent) {
client.seed(new global.Blob([ common.leaves.content ]), opts, function (torrent) {
t.equal(client.torrents.length, 1)
t.equal(torrent.infoHash, leavesTorrent.infoHash)
t.equal(torrent.magnetURI, leavesMagnetURI)
t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash)
t.equal(torrent.magnetURI, common.leaves.magnetURI)
client.remove(torrent)
t.equal(client.torrents.length, 0)
client.destroy()
@@ -231,7 +223,7 @@ test('after client.destroy(), throw on client.add() or client.seed()', function
t.pass('client destroyed')
})
t.throws(function () {
client.add('magnet:?xt=urn:btih:' + leavesTorrent.infoHash)
client.add('magnet:?xt=urn:btih:' + common.leaves.parsedTorrent.infoHash)
})
t.throws(function () {
client.seed(new Buffer('sup'))
@@ -246,10 +238,10 @@ test('after client.destroy(), no "torrent" or "ready" events emitted', function
client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })

client.add(leaves, { name: 'leaves' }, function () {
client.add(common.leaves.torrent, { name: 'leaves' }, function () {
t.fail('unexpected "torrent" event (from add)')
})
client.seed(leavesBook, { name: 'leavesBook' }, function () {
client.seed(common.leaves.content, { name: 'leaves' }, function () {
t.fail('unexpected "torrent" event (from seed)')
})
client.on('ready', function () {
@@ -0,0 +1,34 @@
var fs = require('fs')
var path = require('path')
var parseTorrent = require('parse-torrent')

var content = path.join(__dirname, 'content')
var torrents = path.join(__dirname, 'torrents')

module.exports = {
// Leaves of Grass by Walt Whitman.epub
leaves: {
contentPath: path.join(content, 'Leaves of Grass by Walt Whitman.epub'),
torrentPath: path.join(torrents, 'leaves.torrent'),
content: fs.readFileSync(
path.join(content, 'Leaves of Grass by Walt Whitman.epub')
),
torrent: fs.readFileSync(path.join(torrents, 'leaves.torrent')),
parsedTorrent: parseTorrent(
fs.readFileSync(path.join(torrents, 'leaves.torrent'))
),
magnetURI: parseTorrent.toMagnetURI(
parseTorrent(fs.readFileSync(path.join(torrents, 'leaves.torrent')))
)
},

// Folder which contains single file
folder: {
contentPath: path.join(content, 'folder')
},

// Folder which contains multiple files
numbers: {
contentPath: path.join(content, 'numbers')
}
}
@@ -1,20 +1,17 @@
var common = require('./common')
var fs = require('fs')
var get = require('simple-get')
var path = require('path')
var test = require('tape')
var WebTorrent = require('../')

var leavesPath = path.resolve(__dirname, 'content', 'Leaves of Grass by Walt Whitman.epub')
var leavesTorrent = fs.readFileSync(path.resolve(__dirname, 'torrents', 'leaves.torrent'))

test('torrent.createServer(): programmatic http server', function (t) {
t.plan(9)

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

client.add(leavesTorrent, function (torrent) {
client.add(common.leaves.torrent, function (torrent) {
t.pass('got "torrent" event')
var server = torrent.createServer()

@@ -23,7 +20,7 @@ test('torrent.createServer(): programmatic http server', function (t) {
t.pass('server is listening on ' + port)

// Seeding after server is created should work
torrent.load(fs.createReadStream(leavesPath), function (err) {
torrent.load(fs.createReadStream(common.leaves.contentPath), function (err) {
t.error(err, 'loaded seed content into torrent')
})

@@ -38,7 +35,7 @@ test('torrent.createServer(): programmatic http server', function (t) {
// Verify file content for first (and only) file
get.concat(host + '/0', function (err, data) {
t.error(err)
t.deepEqual(data, fs.readFileSync(leavesPath))
t.deepEqual(data, common.leaves.content)

server.close(function () { t.pass('server closed') })
client.destroy(function () { t.pass('client destroyed') })
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.