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

improve torrent.createServer() test

- check that index page works
- wait for destroy
  • Loading branch information
feross committed Dec 10, 2015
commit 992caf9650a1f6821006387dfaf58f4004b17005
@@ -1,41 +1,49 @@
var path = require('path')
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('start http server programmatically', function (t) {
t.plan(4)
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) })

var torrent = client.add(leavesTorrent, { dht: false, tracker: false }, function (torrent) {
client.add(leavesTorrent, function (torrent) {
t.pass('got "torrent" event')

var server = torrent.createServer()

server.listen(0, function () {
t.pass('server is listening')
var port = server.address().port
get.concat('http://localhost:' + port + '/0', function (err, data) {
if (err) throw err
// Verify data for first (and only file)
t.deepEqual(data, fs.readFileSync(leavesPath))
t.pass('server is listening on ' + port)

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

var host = 'http://localhost:' + port

// Index page should list files in the torrent
get.concat(host + '/', function (err, data) {
t.error(err)
data = data.toString()
t.ok(data.indexOf('Leaves of Grass by Walt Whitman.epub') !== -1)

// Verify file content for first (and only) file
get.concat(host + '/0', function (err, data) {
t.error(err)
t.deepEqual(data, fs.readFileSync(leavesPath))

server.close(function () { t.pass('server closed') })
client.destroy(function () { t.pass('client destroyed') })
})
})
})
})
torrent.on('ready', function () {
torrent.load(fs.createReadStream(leavesPath), function (err) {
if (err) throw err
t.pass('loaded seed content into torrent')
})
})
})
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.