diff options
author | Feross Aboukhadijeh <feross@feross.org> | 2016-01-14 01:19:44 +0300 |
---|---|---|
committer | Feross Aboukhadijeh <feross@feross.org> | 2016-01-14 01:19:44 +0300 |
commit | 218eea5420deb4a9d057cd33105631dffa2a867c (patch) | |
tree | 9635ba88f1c9139a2a09dc6ef4361f88d2fa29c8 /test/client-add.js | |
parent | 2413dc38fc5a502680324e8e468f22c6706ad204 (diff) |
split up tests
Diffstat (limited to 'test/client-add.js')
-rw-r--r-- | test/client-add.js | 208 |
1 files changed, 208 insertions, 0 deletions
diff --git a/test/client-add.js b/test/client-add.js new file mode 100644 index 0000000..c13f6a6 --- /dev/null +++ b/test/client-add.js @@ -0,0 +1,208 @@ +var common = require('./common') +var extend = require('xtend') +var test = require('tape') +var WebTorrent = require('../') + +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(common.leaves.magnetURI) + 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.magnetURI, function (err) { t.error(err, 'torrent destroyed') }) + t.equal(client.torrents.length, 0) + + client.destroy(function (err) { t.error(err, 'client destroyed') }) + }) +}) + +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) }) + client.on('warning', function (err) { t.fail(err) }) + + 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, function (err) { t.error(err, 'torrent destroyed') }) + t.equal(client.torrents.length, 0) + + client.destroy(function (err) { t.error(err, 'client destroyed') }) + }) +}) + +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) }) + client.on('warning', function (err) { t.fail(err) }) + + 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.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(function (err) { t.error(err, 'client destroyed') }) + }) +}) + +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) }) + client.on('warning', function (err) { t.fail(err) }) + + 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'), function (err) { t.error(err, 'torrent destroyed') }) + t.equal(client.torrents.length, 0) + + client.destroy(function (err) { t.error(err, 'client destroyed') }) + }) +}) + +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) }) + client.on('warning', function (err) { t.fail(err) }) + + 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, function (err) { t.error(err, 'torrent destroyed') }) + t.equal(client.torrents.length, 0) + + client.destroy(function (err) { t.error(err, 'client destroyed') }) + }) +}) + +test('client.add: parsed torrent, with string type announce property', function (t) { + t.plan(7) + + 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 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) + + var expectedMagnetURI = common.leaves.magnetURI + + '&tr=' + encodeURIComponent('http://tracker.local:80') + t.equal(torrent.magnetURI, expectedMagnetURI) + + // `torrent.announce` must always be an array + t.deepEqual(torrent.announce, [ 'http://tracker.local:80' ]) + + client.remove(common.leaves.parsedTorrent, function (err) { t.error(err, 'torrent destroyed') }) + t.equal(client.torrents.length, 0) + + client.destroy(function (err) { t.error(err, 'client destroyed') }) + }) +}) + +test('client.add: parsed torrent, with array type announce property', function (t) { + t.plan(7) + + 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 parsedTorrent = extend(common.leaves.parsedTorrent) + parsedTorrent.announce = [ 'http://tracker.local:80', 'http://tracker.local:81' ] + + var torrent = client.add(parsedTorrent) + t.equal(client.torrents.length, 1) + + torrent.on('infoHash', function () { + t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) + + var expectedMagnetURI = common.leaves.magnetURI + + '&tr=' + encodeURIComponent('http://tracker.local:80') + + '&tr=' + encodeURIComponent('http://tracker.local:81') + t.equal(torrent.magnetURI, expectedMagnetURI) + + t.deepEqual(torrent.announce, [ 'http://tracker.local:80', 'http://tracker.local:81' ]) + + client.remove(common.leaves.parsedTorrent, function (err) { t.error(err, 'torrent destroyed') }) + t.equal(client.torrents.length, 0) + + client.destroy(function (err) { t.error(err, 'client destroyed') }) + }) +}) + +test('client.add: invalid torrent id: empty string', function (t) { + t.plan(3) + + var client = new WebTorrent({ dht: false, tracker: false }) + + client.on('error', function (err) { + t.ok(err instanceof Error) + t.ok(err.message.indexOf('Invalid torrent identifier') >= 0) + + client.destroy(function (err) { t.error(err, 'client destroyed') }) + }) + client.on('warning', function (err) { t.fail(err) }) + + client.add('') +}) + +test('client.add: invalid torrent id: short buffer', function (t) { + t.plan(3) + + var client = new WebTorrent({ dht: false, tracker: false }) + + client.on('error', function (err) { + t.ok(err instanceof Error) + t.ok(err.message.indexOf('Invalid torrent identifier') >= 0) + + client.destroy(function (err) { t.error(err, 'client destroyed') }) + }) + client.on('warning', function (err) { t.fail(err) }) + + client.add(new Buffer('abc')) +}) |