diff options
author | Feross Aboukhadijeh <feross@feross.org> | 2015-12-10 10:59:52 +0300 |
---|---|---|
committer | Feross Aboukhadijeh <feross@feross.org> | 2015-12-17 14:39:56 +0300 |
commit | e31197d5cd9cd1b923cd56bc59d45a51b671ae1c (patch) | |
tree | 77d1de6caaeaa446cec3b21157c496a9c1a66e8e /test/basic.js | |
parent | 4a759a59e2f127e0cded044dcd647084d1b8c26f (diff) |
tests: lots of cleanup
Diffstat (limited to 'test/basic.js')
-rw-r--r-- | test/basic.js | 218 |
1 files changed, 128 insertions, 90 deletions
diff --git a/test/basic.js b/test/basic.js index 3a19e4c..64472b4 100644 --- a/test/basic.js +++ b/test/basic.js @@ -1,27 +1,35 @@ +/* global Blob */ + var common = require('./common') var extend = require('xtend') var test = require('tape') var WebTorrent = require('../') -test('client.add/remove: magnet uri, utf-8 string', function (t) { +test('client.add: magnet uri, utf-8 string', function (t) { + t.plan(6) + 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:' + common.leaves.parsedTorrent.infoHash) + var torrent = client.add(common.leaves.magnetURI) t.equal(client.torrents.length, 1) + torrent.on('infoHash', function () { 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(torrent.magnetURI, common.leaves.magnetURI) + + client.remove(common.leaves.magnetURI, function (err) { t.error(err, 'torrent destroyed') }) t.equal(client.torrents.length, 0) - client.destroy() - t.end() + + client.destroy(function (err) { t.error(err, 'client destroyed') }) }) }) -test('client.add/remove: torrent file, buffer', function (t) { +test('client.add: torrent file, buffer', function (t) { + t.plan(6) + var client = new WebTorrent({ dht: false, tracker: false }) client.on('error', function (err) { t.fail(err) }) @@ -29,17 +37,21 @@ test('client.add/remove: torrent file, buffer', function (t) { var torrent = client.add(common.leaves.torrent) t.equal(client.torrents.length, 1) + torrent.on('infoHash', function () { t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) t.equal(torrent.magnetURI, common.leaves.magnetURI) - client.remove(common.leaves.torrent) + + client.remove(common.leaves.torrent, function (err) { t.error(err, 'torrent destroyed') }) t.equal(client.torrents.length, 0) - client.destroy() - t.end() + + client.destroy(function (err) { t.error(err, 'client destroyed') }) }) }) -test('client.add/remove: info hash, hex string', function (t) { +test('client.add: info hash, hex string', function (t) { + t.plan(6) + var client = new WebTorrent({ dht: false, tracker: false }) client.on('error', function (err) { t.fail(err) }) @@ -47,17 +59,21 @@ test('client.add/remove: info hash, hex string', function (t) { var torrent = client.add(common.leaves.parsedTorrent.infoHash) t.equal(client.torrents.length, 1) + torrent.on('infoHash', function () { 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(torrent.magnetURI, 'magnet:?xt=urn:btih:' + common.leaves.parsedTorrent.infoHash) + + client.remove(common.leaves.parsedTorrent.infoHash, function (err) { t.error(err, 'torrent destroyed') }) t.equal(client.torrents.length, 0) - client.destroy() - t.end() + + client.destroy(function (err) { t.error(err, 'client destroyed') }) }) }) -test('client.add/remove: info hash, buffer', function (t) { +test('client.add: info hash, buffer', function (t) { + t.plan(6) + var client = new WebTorrent({ dht: false, tracker: false }) client.on('error', function (err) { t.fail(err) }) @@ -65,17 +81,21 @@ test('client.add/remove: info hash, buffer', function (t) { var torrent = client.add(common.leaves.parsedTorrent.infoHashBuffer) t.equal(client.torrents.length, 1) + torrent.on('infoHash', function () { 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')) + + client.remove(new Buffer(common.leaves.parsedTorrent.infoHash, 'hex'), function (err) { t.error(err, 'torrent destroyed') }) t.equal(client.torrents.length, 0) - client.destroy() - t.end() + + client.destroy(function (err) { t.error(err, 'client destroyed') }) }) }) -test('client.add/remove: parsed torrent, from `parse-torrent`', function (t) { +test('client.add: parsed torrent, from `parse-torrent`', function (t) { + t.plan(6) + var client = new WebTorrent({ dht: false, tracker: false }) client.on('error', function (err) { t.fail(err) }) @@ -83,37 +103,46 @@ test('client.add/remove: parsed torrent, from `parse-torrent`', function (t) { var torrent = client.add(common.leaves.parsedTorrent) t.equal(client.torrents.length, 1) + torrent.on('infoHash', function () { t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) t.equal(torrent.magnetURI, common.leaves.magnetURI) - client.remove(common.leaves.parsedTorrent) + + client.remove(common.leaves.parsedTorrent, function (err) { t.error(err, 'torrent destroyed') }) t.equal(client.torrents.length, 0) - client.destroy() - t.end() + + client.destroy(function (err) { t.error(err, 'client destroyed') }) }) }) -test('client.add/remove: parsed torrent, with string type announce property', function (t) { +test('client.add: parsed torrent, with string type announce property', function (t) { + t.plan(6) + 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 modifiedParsedTorrent = extend(common.leaves.parsedTorrent, { - announce: common.leaves.parsedTorrent.announce[0] - }) - var torrent = client.add(modifiedParsedTorrent) + var parsedTorrent = extend(common.leaves.parsedTorrent) + parsedTorrent.announce = 'http://tracker.local:80' + + var torrent = client.add(parsedTorrent) t.equal(client.torrents.length, 1) + torrent.on('infoHash', function () { t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) - client.remove(common.leaves.parsedTorrent) + t.equal(torrent.magnetURI, common.leaves.magnetURI + '&tr=' + encodeURIComponent('http://tracker.local:80')) + + client.remove(common.leaves.parsedTorrent, function (err) { t.error(err, 'torrent destroyed') }) t.equal(client.torrents.length, 0) - client.destroy() - t.end() + + client.destroy(function (err) { t.error(err, 'client destroyed') }) }) }) test('client.remove: remove by Torrent object', function (t) { + t.plan(5) + var client = new WebTorrent({ dht: false, tracker: false }) client.on('error', function (err) { t.fail(err) }) @@ -121,16 +150,20 @@ test('client.remove: remove by Torrent object', function (t) { var torrent = client.add(common.leaves.parsedTorrent.infoHash) t.equal(client.torrents.length, 1) + torrent.on('infoHash', function () { t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) - client.remove(torrent) + + client.remove(torrent, function (err) { t.error(err, 'torrent destroyed') }) t.equal(client.torrents.length, 0) - client.destroy() - t.end() + + client.destroy(function (err) { t.error(err, 'client destroyed') }) }) }) test('torrent.destroy: destroy and remove torrent', function (t) { + t.plan(5) + var client = new WebTorrent({ dht: false, tracker: false }) client.on('error', function (err) { t.fail(err) }) @@ -138,77 +171,84 @@ test('torrent.destroy: destroy and remove torrent', function (t) { var torrent = client.add(common.leaves.parsedTorrent.infoHash) t.equal(client.torrents.length, 1) + torrent.on('infoHash', function () { t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) - torrent.destroy() + + torrent.destroy(function (err) { t.error(err, 'torrent destroyed') }) t.equal(client.torrents.length, 0) - client.destroy() - t.end() + + client.destroy(function (err) { t.error(err, 'client destroyed') }) }) }) test('client.seed: torrent file (Buffer)', function (t) { - t.plan(4) - - var opts = { - name: 'Leaves of Grass by Walt Whitman.epub', - announce: [ - 'http://tracker.thepiratebay.org/announce', - 'udp://tracker.openbittorrent.com:80', - 'udp://tracker.ccc.de:80', - 'udp://tracker.publicbt.com:80', - 'udp://fr33domtracker.h33t.com:3310/announce', - 'http://tracker.bittorrent.am/announce' - ] - } + t.plan(6) var client = new WebTorrent({ dht: false, tracker: false }) client.on('error', function (err) { t.fail(err) }) client.on('warning', function (err) { t.fail(err) }) - client.seed(common.leaves.content, opts, function (torrent) { + client.seed(common.leaves.content, { + name: 'Leaves of Grass by Walt Whitman.epub' + }, function (torrent) { t.equal(client.torrents.length, 1) t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) t.equal(torrent.magnetURI, common.leaves.magnetURI) - client.remove(torrent) + + client.remove(torrent, function (err) { t.error(err, 'torrent removed') }) t.equal(client.torrents.length, 0) - client.destroy() + + client.destroy(function (err) { t.error(err, 'client destroyed') }) + }) +}) + +test('client.seed: torrent file (Buffer), set name on buffer', function (t) { + t.plan(6) + + 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 buf = new Buffer(common.leaves.content) + buf.name = 'Leaves of Grass by Walt Whitman.epub' + + client.seed(common.leaves.content, function (torrent) { + t.equal(client.torrents.length, 1) + t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) + t.equal(torrent.magnetURI, common.leaves.magnetURI) + + client.remove(torrent, function (err) { t.error(err, 'torrent removed') }) + t.equal(client.torrents.length, 0) + + client.destroy(function (err) { t.error(err, 'client destroyed') }) }) }) test('client.seed: torrent file (Blob)', function (t) { - var opts = { - name: 'Leaves of Grass by Walt Whitman.epub', - announce: [ - 'http://tracker.thepiratebay.org/announce', - 'udp://tracker.openbittorrent.com:80', - 'udp://tracker.ccc.de:80', - 'udp://tracker.publicbt.com:80', - 'udp://fr33domtracker.h33t.com:3310/announce', - 'http://tracker.bittorrent.am/announce' - ] - } - - if (global.Blob !== undefined) { - t.plan(4) - var client = new WebTorrent({ dht: false, tracker: false }) - - client.on('error', function (err) { t.fail(err) }) - client.on('warning', function (err) { t.fail(err) }) - - client.seed(new global.Blob([ common.leaves.content ]), opts, function (torrent) { - t.equal(client.torrents.length, 1) - 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() - }) - } else { - t.pass('Skipping Blob test because missing `Blob` constructor') - t.end() - } + if (typeof Blob === 'undefined') return t.end() + + t.plan(6) + + var client = new WebTorrent({ dht: false, tracker: false }) + + client.on('error', function (err) { t.fail(err) }) + client.on('warning', function (err) { t.fail(err) }) + + client.seed(new global.Blob([ common.leaves.content ]), { + name: 'Leaves of Grass by Walt Whitman.epub' + }, function (torrent) { + t.equal(client.torrents.length, 1) + t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) + t.equal(torrent.magnetURI, common.leaves.magnetURI) + + client.remove(torrent, function (err) { t.error(err, 'torrent removed') }) + t.equal(client.torrents.length, 0) + + client.destroy(function (err) { t.error(err, 'client destroyed') }) + }) }) test('after client.destroy(), throw on client.add() or client.seed()', function (t) { @@ -219,9 +259,8 @@ test('after client.destroy(), throw on client.add() or client.seed()', function client.on('error', function (err) { t.fail(err) }) client.on('warning', function (err) { t.fail(err) }) - client.destroy(function () { - t.pass('client destroyed') - }) + client.destroy(function (err) { t.error(err, 'client destroyed') }) + t.throws(function () { client.add('magnet:?xt=urn:btih:' + common.leaves.parsedTorrent.infoHash) }) @@ -247,7 +286,6 @@ test('after client.destroy(), no "torrent" or "ready" events emitted', function client.on('ready', function () { t.fail('unexpected "ready" event') }) - client.destroy(function () { - t.pass('client destroyed') - }) + + client.destroy(function (err) { t.error(err, 'client destroyed') }) }) |