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
@@ -10,9 +10,9 @@ var inherits = require('inherits')
var loadIPSet = require('load-ip-set') // browser exclude
var parallel = require('run-parallel')
var parseTorrent = require('parse-torrent')
var path = require('path')
var speedometer = require('speedometer')
var zeroFill = require('zero-fill')
var path = require('path')

var Torrent = require('./lib/torrent')

@@ -205,7 +205,8 @@ WebTorrent.prototype.seed = function (input, opts, onseed) {

// When seeding from filesystem, initialize store from that path (avoids a copy)
if (typeof input === 'string') opts.path = path.dirname(input)
if (!opts.createdBy) opts.createdBy = 'WebTorrent/' + VERSION
if (!opts.createdBy) opts.createdBy = 'WebTorrent/' + VERSION_STR
if (!self.tracker) opts.announce = []

var streams
var torrent = self.add(undefined, opts, function (torrent) {
@@ -3,6 +3,7 @@ module.exports = Server
var debug = require('debug')('webtorrent:server')
var http = require('http')
var mime = require('mime')
var prettyBytes = require('pretty-bytes')
var pump = require('pump')
var rangeParser = require('range-parser')
var url = require('url')
@@ -58,9 +59,12 @@ function Server (torrent, opts) {
if (pathname === '/') {
res.setHeader('Content-Type', 'text/html')
var listHtml = torrent.files.map(function (file, i) {
return '<li><a href="/' + i + '">' + file.name + '</a></li>'
return '<li><a download="' + file.name + '" href="/' + i + '">' + file.path + '</a> ' +
'(' + prettyBytes(file.length) + ')</li>'
}).join('<br>')
return res.end('<h1>WebTorrent</h1><ol>' + listHtml + '</ol>')

var html = '<h1>' + torrent.name + '</h1><ol>' + listHtml + '</ol>'
return res.end(html)
}

var index = Number(pathname.slice(1))
@@ -234,12 +234,12 @@ Torrent.prototype._processParsedTorrent = function (parsedTorrent) {
parsedTorrent.announce = parsedTorrent.announce.concat(this.announce)
}

if (global.WEBTORRENT_ANNOUNCE) {
if (this.client.tracker && global.WEBTORRENT_ANNOUNCE) {
// So `webtorrent-hybrid` can force specific trackers to be used
parsedTorrent.announce = parsedTorrent.announce.concat(global.WEBTORRENT_ANNOUNCE)
}

if (parsedTorrent.announce.length === 0) {
if (this.client.tracker && parsedTorrent.announce.length === 0) {
// When no trackers specified, use some reasonable defaults
parsedTorrent.announce = createTorrent.announceList.map(function (list) {
return list[0]
@@ -1185,7 +1185,7 @@ Torrent.prototype.load = function (streams, cb) {

Torrent.prototype.createServer = function (opts) {
var self = this
if (typeof Server !== 'function') return // browser exclude
if (typeof Server !== 'function') throw new Error('node.js-only method')
var server = new Server(self, opts)
self._servers.push(server)
return server
@@ -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)
t.plan(8)

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,104 +20,97 @@ 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)
client.destroy()
server.close()
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 destroyed') })
t.equal(client.torrents.length, 0)

server.close(function () { t.pass('http server closed') })
client.destroy(function (err) { t.error(err, 'client destroyed') })
})
})
})

test('client.add: filesystem path to a torrent file, string', function (t) {
t.plan(2)
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.add(leavesPath, function (torrent) {
t.equal(torrent.infoHash, leavesTorrent.infoHash)
t.equal(torrent.magnetURI, leavesMagnetURI)
client.destroy()
client.add(common.leaves.torrentPath, 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 destroyed') })
t.equal(client.torrents.length, 0)

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

test('client.seed: filesystem path to file, string', function (t) {
t.plan(2)

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(leavesBookPath, opts, function (torrent) {
t.equal(torrent.infoHash, leavesTorrent.infoHash)
t.equal(torrent.magnetURI, leavesMagnetURI)
client.destroy()
client.seed(common.leaves.contentPath, {
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 destroyed') })
t.equal(client.torrents.length, 0)

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

test('client.seed: filesystem path to folder with one file, string', function (t) {
t.plan(2)

var opts = {
pieceLength: 32768, // force piece length to 32KB so info-hash will
// match what transmission generated, since we use
// a different algo for picking piece length

private: false, // also force `private: false` to match transmission
announce: [
'udp://tracker.webtorrent.io:80'
]
}
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(folderPath, 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()
client.seed(common.folder.contentPath, function (torrent) {
t.equal(client.torrents.length, 1)
t.equal(torrent.infoHash, common.folder.parsedTorrent.infoHash)
t.equal(torrent.magnetURI, common.folder.magnetURI)

client.remove(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.seed: filesystem path to folder with multiple files, string', function (t) {
t.plan(2)

var opts = {
pieceLength: 32768, // force piece length to 32KB so info-hash will
// match what transmission generated, since we use
// a different algo for picking piece length

private: false, // also force `private: false` to match transmission
announce: [
'udp://tracker.webtorrent.io:80'
]
}
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(numbersPath, 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()
client.seed(common.numbers.contentPath, function (torrent) {
t.equal(client.torrents.length, 1)
t.equal(torrent.infoHash, common.numbers.parsedTorrent.infoHash)
t.equal(torrent.magnetURI, common.numbers.magnetURI)

client.remove(torrent, function (err) { t.error(err, 'torrent destroyed') })
t.equal(client.torrents.length, 0)

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